Esempio n. 1
0
def CreateProcessWithLogonW(lpUsername=None,
                            lpDomain=None,
                            lpPassword=None,
                            dwLogonFlags=0,
                            lpApplicationName=None,
                            lpCommandLine=None,
                            dwCreationFlags=0,
                            lpEnvironment=None,
                            lpCurrentDirectory=None,
                            lpStartupInfo=None):
    if not lpUsername:
        lpUsername = NULL
    else:
        lpUsername = ctypes.c_wchar_p(lpUsername)
    if not lpDomain:
        lpDomain = NULL
    else:
        lpDomain = ctypes.c_wchar_p(lpDomain)
    if not lpPassword:
        lpPassword = NULL
    else:
        lpPassword = ctypes.c_wchar_p(lpPassword)
    if not lpApplicationName:
        lpApplicationName = NULL
    else:
        lpApplicationName = ctypes.c_wchar_p(lpApplicationName)
    if not lpCommandLine:
        lpCommandLine = NULL
    else:
        lpCommandLine = ctypes.create_unicode_buffer(lpCommandLine)
    if not lpEnvironment:
        lpEnvironment = NULL
    else:
        lpEnvironment = ctypes.c_wchar_p(lpEnvironment)
    if not lpCurrentDirectory:
        lpCurrentDirectory = NULL
    else:
        lpCurrentDirectory = ctypes.c_wchar_p(lpCurrentDirectory)

    if not lpStartupInfo:
        lpStartupInfo = STARTUPINFO()
        lpStartupInfo.cb = ctypes.sizeof(STARTUPINFO)
        lpStartupInfo.lpReserved = 0
        lpStartupInfo.lpDesktop = 0
        lpStartupInfo.lpTitle = 0
        lpStartupInfo.dwFlags = 0
        lpStartupInfo.cbReserved2 = 0
        lpStartupInfo.lpReserved2 = 0
        lpStartupInfo.hStdInput = win32api.GetStdHandle(
            win32api.STD_INPUT_HANDLE)
        lpStartupInfo.hStdOutput = win32api.GetStdHandle(
            win32api.STD_OUTPUT_HANDLE)
        lpStartupInfo.hStdError = win32api.GetStdHandle(
            win32api.STD_ERROR_HANDLE)

    lpProcessInformation = PROCESS_INFORMATION()
    lpProcessInformation.hProcess = INVALID_HANDLE_VALUE
    lpProcessInformation.hThread = INVALID_HANDLE_VALUE
    lpProcessInformation.dwProcessId = 0
    lpProcessInformation.dwThreadId = 0

    dwCreationFlags |= win32process.CREATE_NEW_CONSOLE

    success = ctypes.windll.advapi32.CreateProcessWithLogonW(
        lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName,
        ctypes.byref(lpCommandLine), dwCreationFlags, lpEnvironment,
        lpCurrentDirectory, ctypes.byref(lpStartupInfo),
        ctypes.byref(lpProcessInformation))

    if success == FALSE:
        raise ctypes.WinError()

    win32event.WaitForSingleObject(lpProcessInformation.hProcess,
                                   win32event.INFINITE)

    ctypes.windll.kernel32.CloseHandle(lpProcessInformation.hProcess)
    ctypes.windll.kernel32.CloseHandle(lpProcessInformation.hThread)

    return lpProcessInformation
Esempio n. 2
0
 def assertSignaled(self, event):
     self.assertEqual(win32event.WaitForSingleObject(event, 0),
                      win32event.WAIT_OBJECT_0)
Esempio n. 3
0
 def testWaitableFire(self):
     h = win32event.CreateWaitableTimer(None, 0, None)
     dt = -160  # 160 ns.
     win32event.SetWaitableTimer(h, dt, 0, None, None, 0)
     rc = win32event.WaitForSingleObject(h, 1000)
     self.assertEqual(rc, win32event.WAIT_OBJECT_0)
Esempio n. 4
0
                            start_url1)
                        crawl.inserttable_logging(cont_soure,
                                                  str(datetime.datetime.now()),
                                                  get_status, cont, time_send)
                        get_status = 0
                        if cont != 0:
                            crawl.inserttable(table, cont,
                                              '@'.join(picture_name),
                                              cont_soure, time_int)
                            time.sleep(5)
                        else:
                            time.sleep(10)
                    except Exception, e:
                        a, b, c, d = 'error', 0, 'error', 'error',
                        crawl.inserttable_logging(a,
                                                  str(datetime.datetime.now()),
                                                  b, c, d)
                        print e, '调用函数'
                        #logging.debug(e)
                        continue
                    finally:
                        if Stop_py == 0: break
                if Stop_py == 0: break
                time.sleep(120)

        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
Esempio n. 5
0
def _WaitForShutdown(h):
    win32event.WaitForSingleObject(h, win32event.INFINITE)
    print "Shutdown requested"

    from twisted.internet import reactor
    reactor.callLater(0, reactor.stop)
Esempio n. 6
0
    def main(self):
        
        try:
            workdir = os.path.dirname(os.path.abspath(__file__))
            monyze_config = "monyze_config.ini"
            config = os.path.join(workdir, monyze_config)

            #Проверяем версию системы. Копируем в системную папку конфигфайл.
            windir = os.environ['WINDIR']
            is_64bits = platform.architecture()[0].find('64') != -1
            if is_64bits:
                sys_directory = windir + r'\system32'
            else:
                sys_directory = windir + r'\sysWOW64'
            service_config = os.path.join(sys_directory, "monyze_config.ini" )
            if not os.path.isfile(service_config):
                shutil.copy(config, sys_directory )
            config = service_config

            url = 'https://monyze.ru/api_dev'


            with open(config) as f:
                id_user = f.readline().rstrip()
                id_computer = f.readline().rstrip()
                dll_path = f.readline().rstrip()


            #logging
            logConfig = {
                    "version":1,
                    "handlers":{
                        "fileHandler":{
                            "class":"logging.FileHandler",
                            "formatter":"myFormatter",
                            "filename":os.path.join(dll_path,"monyze-agent.log")
                        }
                    },
                    "loggers":{
                        "monyze":{
                            "handlers":["fileHandler"],
                            "level":"INFO",
                            "level":"DEBUG",
                        }
                    },
                    "formatters":{
                        "myFormatter":{
                            "format":"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
                        }
                    }
                }

            logging.config.dictConfig(logConfig)
            logger = logging.getLogger("monyze.main")


            ohwm_dll = os.path.join(dll_path, 'OpenHardwareMonitorLib.dll')

            logger.info ('------------------------------------- ')
            logger.info ('Agent v0.0api-py starting')
            try:
                logger.info ("OpenHardwareMonitorLib.dll path: " + ohwm_dll)
            except:
                pass

            #pp = pprint.PrettyPrinter(indent=4)
            
            #system = platform.system()
            #win32_ver = platform.win32_ver()
            
            key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,r"Hardware\Description\System\CentralProcessor\0")
            winreg.CloseKey(key)
            adapters = ifaddr.get_adapters()   

            logger.info ('Variables defined')

            driveinfo = []
            cpu_load = {}


            openhardwaremonitor_hwtypes = ['Mainboard', 'SuperIO', 'CPU','RAM', 'GpuNvidia', 'GpuAti', 'TBalancer', 'Heatmaster', 'HDD']
            openhardwaremonitor_sensortypes = ['Voltage', 'Clock', 'Temperature', 'Load','Fan', 'Flow', 'Control', 'Level', 'Factor', 'Power', 'Data', 'SmallData']
            cpu_load_sensornames = ['CPU Total', 'CPU Core', 'CPU Core #1', 'CPU Core #2','CPU Core #3', 'CPU Core #4', 'CPU Core #5', 'CPU Core #6', 'CPU Core #7', 'CPU Core #8']
            cpu_temperature_sensornames = ['CPU Package', 'CPU Core #1', 'CPU Core #2', 'CPU Core #3','CPU Core #4', 'CPU Core #5', 'CPU Core #6', 'CPU Core #7', 'CPU Core #8']
            mb_temperature_sensornames = ['Temperature #1', 'Temperature #2', 'Temperature #3', 'Temperature #4','Temperature #5', 'Temperature #6', 'Temperature #7', 'Temperature #8']
            mb_fan_sensornames = ['Fan #1', 'Fan #2', 'Fan #3', 'Fan #4','Fan #5', 'Fan #6', 'Fan #7', 'Fan #8','Fan #9', 'Fan #10', 'Fan #11', 'Fan #12']

            logger.info ('OHWM initialization ')
        except:
            logger.warning(traceback.format_exc())


        def initialize_openhardwaremonitor():
            clr.AddReference(ohwm_dll)
            from OpenHardwareMonitor import Hardware
            handle = Hardware.Computer()
            handle.MainboardEnabled = True
            handle.CPUEnabled = True
            handle.RAMEnabled = True
            handle.GPUEnabled = True
            handle.HDDEnabled = True
            handle.Open()
            return handle
        try:
            HardwareHandle = initialize_openhardwaremonitor()
            logger.info ('OHWM initialized')
        except:
            logger.warning(traceback.format_exc())

        def get_config_data(handle):
            try:
                logger.info ('Initialize device configuration')
                cpuinfo = {}
                c_count = 0
                for i in handle.Hardware:
                    i.Update()
                    for sensor in i.Sensors:
                        if sensor.Value:
                            sensortypes = openhardwaremonitor_sensortypes
                            hardwaretypes = openhardwaremonitor_hwtypes
                            ###CPU'S###
                            if sensor.Hardware.HardwareType == hardwaretypes.index('CPU'):
                                if sensor.Index == 0 and sensor.SensorType == 3:
                                    c_count += 1
                                    cpuinfo['cpu_'+str(c_count)] = sensor.Hardware.Name
                                    break
                                if sensor.Index >= 0:
                                    c_count += 1
                                    cpuinfo['cpu_'+str(c_count)] = sensor.Hardware.Name
                                    break

                net = {}
                net_stats = psutil.net_if_stats()
                for i, adapter in enumerate(adapters):
                    try:
                        model = adapter.nice_name
                        nm = adapter.name.decode("utf-8")
                        ip = "%s/%s" % (adapter.ips[0].ip, adapter.ips[0].network_prefix)
                        reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
                        reg_key = winreg.OpenKey(
                            reg, r"SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}")
                        try:
                            reg_subkey = winreg.OpenKey(reg_key, nm + r"\Connection")
                            name = winreg.QueryValueEx(reg_subkey, "Name")[0]
                        except FileNotFoundError:
                            pass
                        else:
                            speed = net_stats[name][2]
                            counters = psutil.net_io_counters(pernic=True)
                            btx = counters[name][0]
                            if btx > 0:
                                net['net_'+str((i)+1)] = {'model': model,'name': name, 'speed': speed, 'addr': ip}
                    except KeyError:
                        continue
                
                c = wmi.WMI()
                logger.info ('Get disks data')
                hddpos = 0
                for pdisk in c.query("SELECT * FROM Win32_DiskDrive"): 
                    lnames = []
                    if pdisk.Size:
                        hddpos += 1
                        Size = round((int(pdisk.Size)/1024 ** 2), 2)
                        for colpartition in c.query('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + pdisk.deviceID + '"} WHERE AssocClass = Win32_DiskDriveToDiskPartition'):
                            for logical_disk in c.query('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="' + colpartition.DeviceID + '"} WHERE AssocClass = Win32_LogicalDiskToPartition'):
                                lname = logical_disk.DeviceID
                                if lname:
                                    lnames.append(lname)
                        l = {'hdd_'+str(hddpos)+'': {'name': pdisk.Model,'size': Size, 'LOGICAL': lnames}}
                        driveinfo.append(l)
                nodename = platform.node()
                oss = platform.platform()
                bits = platform.architecture()[0]
                mem = psutil.virtual_memory()
                swap = psutil.swap_memory()

                logger.info('Disks data recieved')
                logger.info('Device configuration initialized')
                config_data = {
                    "id": {
                        "user_id": id_user,
                        "device_id": id_computer
                    },
                    "state": "config",
                    "device_config": {
                        "device_name": nodename,
                        "system": oss,
                        "icon": "f17a",
                        "cpu": cpuinfo,
                        "ram": {
                            "TotalPh": mem.total,
                            "TotalVrt": 0,
                            "TotalPF": swap.total,
                            "bits": bits
                        },
                        "hdd": driveinfo,
                        "net": net
                    }
                }


                #print(json.dumps(config_data, indent=4)) 
                #logger.info(json.dumps(config_data, indent=4))   
                requests.post(url, json.dumps(config_data))
                logger.info('Device configuration sent')
            except:
                logger.warning(traceback.format_exc())

        def get_load_data(handle):
            try:
                memory = psutil.virtual_memory()
                cpu_count = 0

                for i in handle.Hardware:
                    sensortypes = openhardwaremonitor_sensortypes
                    hardwaretypes = openhardwaremonitor_hwtypes
                    load_sensornames = cpu_load_sensornames
                    temp_sensornames = cpu_temperature_sensornames
                    i.Update()

                    ###CPU and Fans###
                    if i.HardwareType == hardwaretypes.index('CPU'):
                        if i.processorIndex == cpu_count:
                            load_arr = {}
                            temp_arr = {}
                            cpu_load_widg_arr = []
                            cpu_temp_widg_arr = []
                            cpu_count += 1
                            cpu_load_data = {}
                            for sensor in i.Sensors:
                                if sensor.Value:
                                    #CPU Load Sensors
                                    if sensor.SensorType == sensortypes.index('Load'):
                                        if sensor.Name == load_sensornames[0] or sensor.Name == load_sensornames[1]:
                                            load_arr['total'] = round(sensor.Value)
                                            cpu_load_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == load_sensornames[2]:
                                            load_arr['core_1'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[3]:
                                            load_arr['core_2'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[4]:
                                            load_arr['core_3'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[5]:
                                            load_arr['core_4'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[6]:
                                            load_arr['core_5'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[7]:
                                            load_arr['core_6'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[8]:
                                            load_arr['core_7'] = round(sensor.Value)
                                        elif sensor.Name == load_sensornames[9]:
                                            load_arr['core_8'] = round(sensor.Value)
                                    #CPU Temperature sensors
                                    elif sensor.SensorType == sensortypes.index('Temperature'):
                                        if sensor.Name == temp_sensornames[0]:
                                            temp_arr['total'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[1]:
                                            temp_arr['core_1'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[2]:
                                            temp_arr['core_2'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[3]:
                                            temp_arr['core_3'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[4]:
                                            temp_arr['core_4'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[5]:
                                            temp_arr['core_5'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[6]:
                                            temp_arr['core_6'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[7]:
                                            temp_arr['core_7'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                        elif sensor.Name == temp_sensornames[8]:
                                            temp_arr['core_8'] = round(sensor.Value)
                                            cpu_temp_widg_arr.append(round(sensor.Value))
                                    elif sensor.SensorType == sensortypes.index('Clock'):
                                        continue
                                cpu_load_data['load'] = load_arr
                                cpu_load_data['temp'] = temp_arr
                            cpu_pos = 'cpu_'+str(cpu_count)+''
                            cpu_load[cpu_pos] = cpu_load_data
                            cpu_total_load_widg = round(
                                sum(cpu_load_widg_arr)/len(cpu_load_widg_arr))
                            if len(cpu_temp_widg_arr) == 0:
                                cpu_total_temp_widg = 'n/a'
                            else:
                                cpu_total_temp_widg = round(
                                    sum(cpu_temp_widg_arr)/len(cpu_temp_widg_arr))
                    
                ##Fans & MB Temperature
                    if i.HardwareType == hardwaretypes.index('Mainboard'):
                        mb_data = {}
                        mb_temp_arr = {}
                        mb_fan_arr = {}
                        for SHardware in i.SubHardware:
                            SHardware.Update()
                            for sensor in SHardware.Sensors:
                                if sensor.Value:
                                    if sensor.SensorType == sensortypes.index('Temperature'):
                                        if sensor.Name == mb_temperature_sensornames[0]:
                                                mb_temp_arr['temp_1'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[1]:
                                                mb_temp_arr['temp_2'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[2]:
                                                mb_temp_arr['temp_3'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[3]:
                                                mb_temp_arr['temp_4'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[4]:
                                                mb_temp_arr['temp_5'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[5]:
                                                mb_temp_arr['temp_6'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[6]:
                                                mb_temp_arr['temp_7'] = round(sensor.Value)
                                        elif sensor.Name == mb_temperature_sensornames[7]:
                                                mb_temp_arr['temp_8'] = round(sensor.Value)
                                    elif sensor.SensorType == sensortypes.index('Fan'):
                                        if sensor.Name == mb_fan_sensornames[0]:
                                                mb_fan_arr['fan_1'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[1]:
                                                mb_fan_arr['fan_2'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[2]:
                                                mb_fan_arr['fan_3'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[3]:
                                                mb_fan_arr['fan_4'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[4]:
                                                mb_fan_arr['fan_5'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[5]:
                                                mb_fan_arr['fan_6'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[6]:
                                                mb_fan_arr['fan_7'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[7]:
                                                mb_fan_arr['fan_8'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[8]:
                                                mb_fan_arr['fan_9'] = round(sensor.Value)
                                        elif sensor.Name == mb_fan_sensornames[9]:
                                                mb_fan_arr['fan_10'] = round(sensor.Value)

                            mb_data['temp'] = mb_temp_arr
                            mb_data['fans'] = mb_fan_arr 




                ram = {"load": round(memory.percent), "AvailPh": memory.available}

                ##HDD###
                hdd = {}
                hdd_widgets = {}
                hddpos = 0
                for disk in driveinfo:
                    hddpos += 1
                    ldisks = []
                    ldisks_widgets = []
                    hdd_count = 'hdd_'+str(hddpos)+''
                    for ld in disk[hdd_count]['LOGICAL']:
                        if ld:
                            usage = psutil.disk_usage(ld)
                            perc = usage.percent
                            free = round((int(usage.free)/1024 ** 2), 2)
                            used = round((int(usage.used)/1024 ** 2), 2)
                            l = {'ldisk': ld, 'load': round(perc), 'free': free, 'used': used}
                            lw = {'ldisk': ld, 'load': round(perc)}
                            ldisks.append(l)
                            ldisks_widgets.append(lw)
                    x = {'ldisks': ldisks}
                    xw = {'ldisks': ldisks_widgets}

                    hdd[hdd_count] = x
                    hdd_widgets[hdd_count] = xw
                ###NET###
                net = {}
                for i, adapter in enumerate(adapters):
                    try:
                        nm = adapter.name.decode("utf-8")
                        reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
                        reg_key = winreg.OpenKey(
                            reg, r"SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}")
                        try:
                            reg_subkey = winreg.OpenKey(reg_key, nm + r"\Connection")
                            name = winreg.QueryValueEx(reg_subkey, "Name")[0]
                        except FileNotFoundError:
                            pass
                        else:
                            counters = psutil.net_io_counters(pernic=True)
                            btx = counters[name][0]
                            brx = counters[name][1]
                            ptx = counters[name][2]
                            prx = counters[name][3]
                            if btx > 0 and brx > 0:
                                netpos = 0
                                netpos = netpos+1
                                net_count = 'net_'+str(netpos)
                                netname = name
                                net_io = psutil.net_io_counters(pernic=True)
                                time.sleep(1)
                                net_io_1 = psutil.net_io_counters(pernic=True)
                                rx = round((net_io_1[netname][1] -net_io[netname][1]) / 1024 ** 2., 4)
                                tx = round((net_io_1[netname][0] -net_io[netname][0]) / 1024 ** 2., 4)
                                n = {'btx': btx, 'brx': brx, 'ptx': ptx,'prx': prx, 'tx': tx, 'rx': rx}
                                net[net_count] = (n)
                    except KeyError:
                        continue
                load_data = {
                    "id": {
                        "user_id": id_user,
                        "device_id": id_computer
                    },
                    "state": "load",
                    "load": {
                        "cpu": cpu_load,
                        "ram": ram,
                        "hdd": hdd,
                        "net": net,
                        "mb": mb_data
                    },
                    "widgets": {
                        "cpu_load": cpu_total_load_widg,
                        "cpu_temp": cpu_total_temp_widg,
                        "ram_load": round(memory.percent),
                        "hdd_load": hdd_widgets,
                        "mb_temp": mb_temp_arr,
                        "mb_fans": mb_fan_arr
                    }
                }

                
                try:
                    #logger.info(json.dumps(load_data, indent=4))  
                    requests.post(url, json.dumps(load_data))
                    #logger.info('System load sent')
                except:
                    logger.warning('Error sending config data')
                    logger.warning(traceback.format_exc())
            except:
                logger.warning(traceback.format_exc())
        rc = None
        try:
            get_config_data(HardwareHandle)
        except:
            logger.warning('Error sending config data')
            logger.warning(traceback.format_exc())

        while rc != win32event.WAIT_OBJECT_0:
            try:
                get_load_data(HardwareHandle)
            except:
                logger.info('Error in the get_load_data function')
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
Esempio n. 7
0
def threadLoop():
    """main function for the working thread"""
    while True:
        win32event.WaitForSingleObject(event1, win32event.INFINITE)
        brain_turn()
        win32event.SetEvent(event2)
Esempio n. 8
0
import win32security
import win32event

evt = win32event.CreateEvent(None, 0, 0, None)
win32security.LsaRegisterPolicyChangeNotification(
    win32security.PolicyNotifyAuditEventsInformation, evt)
print("Waiting for you change Audit policy in Management console ...")
ret_code = win32event.WaitForSingleObject(evt, 1000000000)
# should come back when you change Audit policy in Management console ...
print(ret_code)
win32security.LsaUnregisterPolicyChangeNotification(
    win32security.PolicyNotifyAuditEventsInformation, evt)
Esempio n. 9
0
        if o == "-p":
            bStartPython = 1
        if o == "-r":
            bDumpRegistry = 1
        if o == "-s":
            bDumpStatus = 0
        if o == "-f":
            bDumpFolders = 1
        if o == "-y":
            print "Doing asynch init of CE connection"
            async_init = 1

    if async_init:
        event, rc = wincerapi.CeRapiInitEx()
        while 1:
            rc = win32event.WaitForSingleObject(event, 500)
            if rc == win32event.WAIT_OBJECT_0:
                # We connected.
                break
            else:
                print "Waiting for Initialize to complete (picture a Cancel button here :)"
    else:
        wincerapi.CeRapiInit()
    print "Connected to remote CE device."
    try:
        verinfo = wincerapi.CeGetVersionEx()
        print "The device is running windows CE version %d.%d - %s" % (
            verinfo[0], verinfo[1], verinfo[4])

        if bDumpStatus:
            print "Dumping remote machine status"
Esempio n. 10
0
import win32process
import win32event
import sys

args = '"{idaq_path}" "-Snothing.idc -f {connection_file}"'

if __name__ == '__main__':
    hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess(
        None, args.format(idaq_path=sys.argv[1], connection_file=sys.argv[2]),
        None, None, 0, 0, None, None, win32process.STARTUPINFO())
    while win32event.WAIT_OBJECT_0 != win32event.WaitForSingleObject(
            hProcess, win32event.INFINITE):
        pass
Esempio n. 11
0
class TRCSService(win32serviceutil.ServiceFramework):
    '''TRCS: The Remote Control Software'''
    DEBUG = True
    _svc_name_ = "TRCSService"  #服务名
    _svc_display_name_ = "TRCSService"  #服务在windows系统中显示的名称
    _svc_description_ = "The Remote Control Software Service"  #服务的描述
    # 头部长度,2个字节+8个字节
    HeaderSize = 10
    EXEC_MY = 1
    EXEC_MYC = 2
    HTTP_EXEC_MY = 3
    HTTP_EXEC_MYC = 4

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.run = True
        self.listen_sock = None
        self.client = None
        self.databuf = None
        if self.DEBUG:
            self.init_debug()
        lexer = lex.lex(module=Mylex)
        self.parser = yacc.yacc(module=Myyacc)
        self.interpreter = MyInterpreter()

    def init_debug(self):
        self.logger = open('log.txt', 'w')

    def log(self, msg):
        if self.DEBUG:
            self.logger.write(msg + os.linesep)
            self.logger.flush()

    def deal_connection(self):
        if self.listen_sock == None:
            self.listen_sock = socket(AF_INET, SOCK_STREAM)
            # 保活两分钟
            self.listen_sock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 120)
            # 超时5分钟,不设置超时,服务将停止不了
            # self.listen_sock.settimeout(5*60)
            self.listen_sock.bind((gethostname(), 8140))
            self.listen_sock.listen(1)
        self.client, addr = self.listen_sock.accept()
        self.log('new connection: ' + str(addr))
        self.databuf = ''

    def exec_my(self, body):
        orders = self.parser.parse(body)
        self.interpreter.execute(orders)

    def exec_myc(self, body):
        orders = body.split(os.linesep)
        self.interpreter.execute(orders)

    def http_exec_my(self, body):
        f = urllib2.urlopen(body)
        self.exec_my(f.read())

    def http_exec_myc(self, body):
        f = urllib2.urlopen(body)
        orders = f.read().split(os.linesep)
        self.interpreter.execute(orders)

    def deal_msg(self, msg_type, body):
        try:
            if msg_type == self.EXEC_MY:
                self.exec_my(body)
            elif msg_type == self.EXEC_MYC:
                self.exec_myc(body)
            elif msg_type == self.HTTP_EXEC_MY:
                self.http_exec_my(body)
            elif msg_type == self.HTTP_EXEC_MYC:
                self.http_exec_myc(body)
            else:
                raise Exception
        except Exception:
            self.client.sendall('Failure')
        else:
            self.client.sendall('Success')

    def SvcDoRun(self):
        while self.run:
            try:
                self.deal_connection()
                while True:
                    data = self.client.recv(4096)
                    if not data:
                        self.client.close()
                        break
                    self.databuf += data
                    while True:
                        if self.HeaderSize > len(self.databuf):
                            # 还没收到完整的数据包
                            break
                        header = struct.unpack('!hq',
                                               self.databuf[:self.HeaderSize])
                        msg_type, bodysize = header
                        if len(self.databuf) < self.HeaderSize + bodysize:
                            # 还没收到完整的数据包
                            break
                        self.log('new message: ' + str(header))
                        body = self.databuf[self.HeaderSize:self.HeaderSize +
                                            bodysize]
                        body = base64.decodestring(body)
                        self.log(body)
                        self.deal_msg(msg_type, body)
                        self.databuf = self.databuf[self.HeaderSize +
                                                    bodysize:]
            except error, e:
                # socket出错
                self.log(str(e))
                if self.client:
                    self.client.close()
                continue
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
    def main(self):
        self.runflag = True
        while self.runflag:
            # block for 24*60*60 seconds and wait for a stop event
            # it is used for a one-day loop
            rc = win32event.WaitForSingleObject(self.stop_event, 24 * 60 * 60)
            # Check to see if self.hWaitStop happened
            if rc == win32event.WAIT_OBJECT_0:
                self.log("Service has stopped")
                break
            else:
                try:
                    #reading cofig.xml file, take tweet user information
                    filepath = os.path.abspath(
                        '../TwitterEmotionClassification/config.xml')
                    tweetconfig = minidom.parse(filepath)

                    consumKey = tweetconfig.getElementsByTagName(
                        'consumerKey')[0].childNodes[0].nodeValue
                    comsumerSecret = tweetconfig.getElementsByTagName(
                        'comsumerSecret')[0].childNodes[0].nodeValue
                    accessKey = tweetconfig.getElementsByTagName(
                        'accessKey')[0].childNodes[0].nodeValue
                    accessSecret = tweetconfig.getElementsByTagName(
                        'accessSecret')[0].childNodes[0].nodeValue

                    #searching topic information
                    search_words = tweetconfig.getElementsByTagName(
                        'searchWords')[0].childNodes[0].nodeValue
                    date_since = tweetconfig.getElementsByTagName(
                        'dateSince')[0].childNodes[0].nodeValue
                    nb_tweets = tweetconfig.getElementsByTagName(
                        'nbTweets')[0].childNodes[0].nodeValue
                    language = tweetconfig.getElementsByTagName(
                        'lang')[0].childNodes[0].nodeValue
                    new_search = tweetconfig.getElementsByTagName(
                        'newSearch')[0].childNodes[0].nodeValue

                    auth = tweepy.OAuthHandler(consumKey, comsumerSecret)
                    auth.set_access_token(accessKey, accessSecret)
                    api = tweepy.API(auth, wait_on_rate_limit=True)

                    tweets = tweepy.Cursor(api.search,
                                           q=search_words,
                                           lang=language,
                                           since=date_since).items(nb_tweets)
                    users_locs = [[
                        tweet.user.screen_name, tweet.user.location
                    ] for tweet in tweets]
                    users_infos = [[
                        tweet.user.screen_name, tweet.user.location,
                        tweet.text, tweet.lang, tweet.favorite_count,
                        tweet.retweet_count, tweet.created_at
                    ] for tweet in tweets]
                    df_tweets = pandas.DataFrame(data=users_infos,
                                                 columns=[
                                                     'user', "location",
                                                     "tweet content",
                                                     "language", "nb_favorite",
                                                     "nb_retweet", "created_at"
                                                 ])
                    df_tweets.to_csv(
                        r'../TwitterEmotionClassification/tweetdata.csv',
                        index=None,
                        header=True)
                except Exception as e:
                    self.log(str(e))
Esempio n. 13
0
 def SvcDoRun(self):
     """ Run the Service """
     self.dispatcher = dispatcher()
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
Esempio n. 14
0
    def communication_win(self, is_check):
        import win32event
        import win32file
        import win32api
        import pywintypes
        import winerror

        # event for the socket
        hEvent_sock = win32event.CreateEvent(None, 0, 0, None)
        win32file.WSAEventSelect(self.comms_socket, hEvent_sock,
                                 win32file.FD_READ)

        # event, overlapped struct for the pipe or tunnel
        hEvent_pipe = win32event.CreateEvent(None, 0, 0,
                                             None)  # for reading from the pipe
        overlapped_pipe = pywintypes.OVERLAPPED()
        overlapped_pipe.hEvent = hEvent_pipe

        # buffer for the packets
        message_readfile = win32file.AllocateReadBuffer(4096)

        # showing if we already async reading or not
        not_reading_already = True
        first_run = True
        while not self._stop:
            try:
                if not self.tunnel_r:
                    # user is not authenticated yet, so there is no pipe
                    # only checking the socket for data
                    rc = win32event.WaitForSingleObject(
                        hEvent_sock, int(self.timeout * 1000))
                else:
                    # client mode so we have the socket and tunnel as well
                    # or the client authenticated and the pipe was created
                    if first_run or not_reading_already:
                        # no ReadFile was called before or finished, so we
                        # are calling it again
                        hr, _ = win32file.ReadFile(self.tunnel_r,
                                                   message_readfile,
                                                   overlapped_pipe)
                        not_reading_already = first_run = False

                    if (hr == winerror.ERROR_IO_PENDING):
                        # well, this was an async read, so we need to wait
                        # until it happens
                        rc = win32event.WaitForMultipleObjects(
                            [hEvent_sock, hEvent_pipe], 0,
                            int(self.timeout * 1000))
                        if rc == winerror.WAIT_TIMEOUT:
                            # timed out, just rerun and wait
                            continue
                    else:
                        if hr != 0:
                            common.internal_print(
                                "TCP ReadFile failed: {0}".format(hr), -1)
                            raise

                if rc < 0x80:  # STATUS_ABANDONED_WAIT_0
                    if rc == 0:
                        # socket got signalled
                        not_reading_already = False
                        messages = self.recv()
                        for message in messages:
                            # looping through the messages from socket
                            if len(message) == 0:
                                # this could happen when the socket died or
                                # partial message was read.
                                continue

                            if common.is_control_channel(message[0:1]):
                                # parse control messages
                                if self.controlchannel.handle_control_messages(
                                        self,
                                        message[len(common.CONTROL_CHANNEL_BYTE
                                                    ):], None):
                                    continue
                                else:
                                    self.stop()
                                    break

                            if self.authenticated:
                                try:
                                    # write packet to the tunnel
                                    self.packet_writer(
                                        message[len(common.CONTROL_CHANNEL_BYTE
                                                    ):])
                                except OSError as e:
                                    print e  # wut?
                                except Exception as e:
                                    if e.args[0] == 995:
                                        common.internal_print(
                                            "Interface disappered, exiting thread: {0}"
                                            .format(e), -1)
                                        self.stop()
                                        continue

                    if rc == 1:
                        # pipe/tunnel got signalled
                        not_reading_already = True
                        if (overlapped_pipe.InternalHigh <
                                4) or (message_readfile[0:1] !=
                                       "\x45"):  #Only care about IPv4
                            # too small which should not happen or not IPv4, so we just drop it.
                            continue

                        # reading out the packet from the buffer and discarding the rest
                        readytogo = message_readfile[0:overlapped_pipe.
                                                     InternalHigh]
                        self.send(common.DATA_CHANNEL_BYTE, readytogo, None)

            except win32api.error as e:
                common.internal_print("TCP Exception: {0}".format(e), -1)

        self.cleanup()

        return True
Esempio n. 15
0
def runas(cmdLine, username, password=None, cwd=None):
    """
    Run a command as another user. If the process is running as an admin or
    system account this method does not require a password. Other non
    privileged accounts need to provide a password for the user to runas.
    Commands are run in with the highest level privileges possible for the
    account provided.
    """
    # Validate the domain and sid exist for the username
    try:
        _, domain, _ = win32security.LookupAccountName(None, username)
        username, _ = split_username(username)
    except pywintypes.error as exc:
        message = win32api.FormatMessage(exc.winerror).rstrip("\n")
        raise CommandExecutionError(message)

    # Elevate the token from the current process
    access = win32security.TOKEN_QUERY | win32security.TOKEN_ADJUST_PRIVILEGES
    th = win32security.OpenProcessToken(win32api.GetCurrentProcess(), access)
    salt.platform.win.elevate_token(th)

    # Try to impersonate the SYSTEM user. This process needs to be running as a
    # user who as been granted the SeImpersonatePrivilege, Administrator
    # accounts have this permission by default.
    try:
        impersonation_token = salt.platform.win.impersonate_sid(
            salt.platform.win.SYSTEM_SID,
            session_id=0,
            privs=["SeTcbPrivilege"],
        )
    except OSError:
        log.debug("Unable to impersonate SYSTEM user")
        impersonation_token = None
        win32api.CloseHandle(th)

    # Impersonation of the SYSTEM user failed. Fallback to an un-privileged
    # runas.
    if not impersonation_token:
        log.debug("No impersonation token, using unprivileged runas")
        return runas_unpriv(cmdLine, username, password, cwd)

    if domain == "NT AUTHORITY":
        # Logon as a system level account, SYSTEM, LOCAL SERVICE, or NETWORK
        # SERVICE.
        user_token = win32security.LogonUser(
            username,
            domain,
            "",
            win32con.LOGON32_LOGON_SERVICE,
            win32con.LOGON32_PROVIDER_DEFAULT,
        )
    elif password:
        # Login with a password.
        user_token = win32security.LogonUser(
            username,
            domain,
            password,
            win32con.LOGON32_LOGON_INTERACTIVE,
            win32con.LOGON32_PROVIDER_DEFAULT,
        )
    else:
        # Login without a password. This always returns an elevated token.
        user_token = salt.platform.win.logon_msv1_s4u(username).Token

    # Get a linked user token to elevate if needed
    elevation_type = win32security.GetTokenInformation(
        user_token, win32security.TokenElevationType)
    if elevation_type > 1:
        user_token = win32security.GetTokenInformation(
            user_token, win32security.TokenLinkedToken)

    # Elevate the user token
    salt.platform.win.elevate_token(user_token)

    # Make sure the user's token has access to a windows station and desktop
    salt.platform.win.grant_winsta_and_desktop(user_token)

    # Create pipes for standard in, out and error streams
    security_attributes = win32security.SECURITY_ATTRIBUTES()
    security_attributes.bInheritHandle = 1

    stdin_read, stdin_write = win32pipe.CreatePipe(security_attributes, 0)
    stdin_read = salt.platform.win.make_inheritable(stdin_read)

    stdout_read, stdout_write = win32pipe.CreatePipe(security_attributes, 0)
    stdout_write = salt.platform.win.make_inheritable(stdout_write)

    stderr_read, stderr_write = win32pipe.CreatePipe(security_attributes, 0)
    stderr_write = salt.platform.win.make_inheritable(stderr_write)

    # Run the process without showing a window.
    creationflags = (win32process.CREATE_NO_WINDOW
                     | win32process.CREATE_NEW_CONSOLE
                     | win32process.CREATE_SUSPENDED)

    startup_info = salt.platform.win.STARTUPINFO(
        dwFlags=win32con.STARTF_USESTDHANDLES,
        hStdInput=stdin_read.handle,
        hStdOutput=stdout_write.handle,
        hStdError=stderr_write.handle,
    )

    # Create the environment for the user
    env = create_env(user_token, False)

    hProcess = None
    try:
        # Start the process in a suspended state.
        process_info = salt.platform.win.CreateProcessWithTokenW(
            int(user_token),
            logonflags=1,
            applicationname=None,
            commandline=cmdLine,
            currentdirectory=cwd,
            creationflags=creationflags,
            startupinfo=startup_info,
            environment=env,
        )

        hProcess = process_info.hProcess
        hThread = process_info.hThread
        dwProcessId = process_info.dwProcessId
        dwThreadId = process_info.dwThreadId

        # We don't use these so let's close the handle
        salt.platform.win.kernel32.CloseHandle(stdin_write.handle)
        salt.platform.win.kernel32.CloseHandle(stdout_write.handle)
        salt.platform.win.kernel32.CloseHandle(stderr_write.handle)

        ret = {"pid": dwProcessId}
        # Resume the process
        psutil.Process(dwProcessId).resume()

        # Wait for the process to exit and get its return code.
        if (win32event.WaitForSingleObject(
                hProcess, win32event.INFINITE) == win32con.WAIT_OBJECT_0):
            exitcode = win32process.GetExitCodeProcess(hProcess)
            ret["retcode"] = exitcode

        # Read standard out
        fd_out = msvcrt.open_osfhandle(stdout_read.handle,
                                       os.O_RDONLY | os.O_TEXT)
        with os.fdopen(fd_out, "r") as f_out:
            stdout = f_out.read()
            ret["stdout"] = stdout

        # Read standard error
        fd_err = msvcrt.open_osfhandle(stderr_read.handle,
                                       os.O_RDONLY | os.O_TEXT)
        with os.fdopen(fd_err, "r") as f_err:
            stderr = f_err.read()
            ret["stderr"] = stderr
    finally:
        if hProcess is not None:
            salt.platform.win.kernel32.CloseHandle(hProcess)
        win32api.CloseHandle(th)
        win32api.CloseHandle(user_token)
        if impersonation_token:
            win32security.RevertToSelf()
        win32api.CloseHandle(impersonation_token)

    return ret
Esempio n. 16
0
 def SvcDoRun(self):
     self.logger.info("down service is run ...")
     self.start()
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
Esempio n. 17
0
def elevate(fn, *args, **kwargs):
    if is_admin():
        fn(*args, **kwargs)
    else:
        # UAC elevation / sudo code working for CPython, Nuitka >= 0.6.2, PyInstaller, PyExe, CxFreeze

        # Regardless of the runner (CPython, Nuitka or frozen CPython), sys.argv[0] is the relative path to script,
        # sys.argv[1] are the arguments
        # The only exception being CPython on Windows where sys.argv[0] contains absolute path to script
        # Regarless of OS, sys.executable will contain full path to python binary for CPython and Nuitka,
        # and full path to frozen executable on frozen CPython

        # Recapitulative table create with
        # (CentOS 7x64 / Python 3.4 / Nuitka 0.6.1 / PyInstaller 3.4) and
        # (Windows 10 x64 / Python 3.7x32 / Nuitka 0.6.2.10 / PyInstaller 3.4)
        # --------------------------------------------------------------------------------------------------------------
        # | OS  | Variable       | CPython                       | Nuitka               | PyInstaller                  |
        # |------------------------------------------------------------------------------------------------------------|
        # | Lin | argv           | ['./script.py', '-h']         | ['./test', '-h']     | ['./test.py', -h']           |
        # | Lin | sys.executable | /usr/bin/python3.4            | /usr/bin/python3.4   | /absolute/path/to/test       |
        # | Win | argv           | ['C:\\Python\\test.py', '-h'] | ['test', '-h']       | ['test', '-h']               |
        # | Win | sys.executable | C:\Python\python.exe          | C:\Python\Python.exe | C:\absolute\path\to\test.exe |
        # --------------------------------------------------------------------------------------------------------------

        # Nuitka 0.6.2 and newer define builtin __nuitka_binary_dir
        # Nuitka does not set the frozen attribute on sys
        # Nuitka < 0.6.2 can be detected in sloppy ways, ie if not sys.argv[0].endswith('.py') or len(sys.path) < 3
        # Let's assume this will only be compiled with newer nuitka, and remove sloppy detections
        try:
            # Actual if statement not needed, but keeps code inspectors more happy
            if __nuitka_binary_dir or '__compiled__' in globals() is not None:
                is_nuitka_compiled = True
        except NameError:
            is_nuitka_compiled = False

        if is_nuitka_compiled:
            # On nuitka, sys.executable is the python binary, even if it does not exist in standalone,
            # so we need to fill runner with sys.argv[0] absolute path
            runner = os.path.abspath(sys.argv[0])
            arguments = sys.argv[1:]
            # current_dir = os.path.dirname(runner)

            logger.debug('Running elevator as Nuitka with runner [%s]' %
                         runner)
            logger.debug('Arguments are %s' % arguments)

        # If a freezer is used (PyInstaller, cx_freeze, py2exe)
        elif getattr(sys, "frozen", False):
            runner = os.path.abspath(sys.executable)
            arguments = sys.argv[1:]
            # current_dir = os.path.dirname(runner)

            logger.debug('Running elevator as Frozen with runner [%s]' %
                         runner)
            logger.debug('Arguments are %s' % arguments)

        # If standard interpreter CPython is used
        else:
            runner = os.path.abspath(sys.executable)
            arguments = [os.path.abspath(sys.argv[0])] + sys.argv[1:]
            # current_dir = os.path.abspath(sys.argv[0])

            logger.debug('Running elevator as CPython with runner [%s]' %
                         runner)
            logger.debug('Arguments are %s' % arguments)

        if os.name == 'nt':
            # Re-run the function with admin rights
            # Join arguments and double quote each argument in order to prevent space separation
            arguments = ' '.join('"' + arg + '"' for arg in arguments)
            try:
                # Old method using ctypes which does not wait for executable to exit nor deos get exit code
                # See https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shellexecutew
                # int 0 means SH_HIDE window, 1 is SW_SHOWNORMAL
                # needs the following imports
                # import ctypes

                # ctypes.windll.shell32.ShellExecuteW(None, 'runas', runner, arguments, None, 0)

                # Method with exit code that waits for executable to exit, needs the following imports
                # import win32event  # monitor process
                # import win32process  # monitor process
                # from win32com.shell.shell import ShellExecuteEx
                # from win32com.shell import shellcon
                childProcess = ShellExecuteEx(
                    nShow=0,
                    fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
                    lpVerb='runas',
                    lpFile=runner,
                    lpParameters=arguments)

                procHandle = childProcess['hProcess']
                win32event.WaitForSingleObject(procHandle, win32event.INFINITE)
                exit_code = win32process.GetExitCodeProcess(procHandle)
                logger.debug('Child exited with code: %s' % exit_code)
                sys.exit(exit_code)

            except Exception as e:
                logger.info(e)
                logger.debug('Trace:', exc_info=True)
                sys.exit(255)
        # Linux runner and hopefully Unixes
        else:
            # Search for sudo executable in order to avoid using shell=True with subprocess
            sudo_path = None
            for path in os.environ.get('PATH', ''):
                if os.path.isfile(os.path.join(path, 'sudo')):
                    sudo_path = os.path.join(path, 'sudo')
            if sudo_path is None:
                logger.error(
                    'Cannot find sudo executable. Cannot elevate privileges. Trying to run wihtout.'
                )
                fn(*args, **kwargs)
            else:
                command = 'sudo "%s"%s%s' % (runner,
                                             (' ' if len(arguments) > 0 else
                                              ''), ' '.join(
                                                  '"%s"' % argument
                                                  for argument in arguments))
                exit_code, output = command_runner(command,
                                                   shell=False,
                                                   timeout=None)

                logger.info('Child output: %s' % output)
                sys.exit(exit_code)
Esempio n. 18
0
    def stop(self, mode=None):  # pylint: disable=too-many-branches
        """Terminate the process."""
        if mode is None:
            mode = fixture_interface.TeardownMode.TERMINATE

        if sys.platform == "win32":

            # Attempt to cleanly shutdown mongod.
            if mode != fixture_interface.TeardownMode.KILL and self.args and self.args[
                    0].find("mongod") != -1:
                mongo_signal_handle = None
                try:
                    mongo_signal_handle = win32event.OpenEvent(
                        win32event.EVENT_MODIFY_STATE, False,
                        "Global\\Mongo_" + str(self._process.pid))

                    if not mongo_signal_handle:
                        # The process has already died.
                        return
                    win32event.SetEvent(mongo_signal_handle)
                    # Wait 60 seconds for the program to exit.
                    status = win32event.WaitForSingleObject(
                        self._process._handle, 60 * 1000)
                    if status == win32event.WAIT_OBJECT_0:
                        return
                except win32process.error as err:
                    # ERROR_FILE_NOT_FOUND (winerror=2)
                    # ERROR_ACCESS_DENIED (winerror=5)
                    # ERROR_INVALID_HANDLE (winerror=6)
                    # One of the above errors is received if the process has
                    # already died.
                    if err.winerror not in (2, 5, 6):
                        raise
                finally:
                    win32api.CloseHandle(mongo_signal_handle)

                print("Failed to cleanly exit the program, calling TerminateProcess() on PID: " +\
                    str(self._process.pid))

            # Adapted from implementation of Popen.terminate() in subprocess.py of Python 2.7
            # because earlier versions do not catch exceptions.
            try:
                # Have the process exit with code 0 if it is terminated by us to simplify the
                # success-checking logic later on.
                win32process.TerminateProcess(self._process._handle, 0)
            except win32process.error as err:
                # ERROR_ACCESS_DENIED (winerror=5) is received when the process
                # has already died.
                if err.winerror != winerror.ERROR_ACCESS_DENIED:
                    raise
                return_code = win32process.GetExitCodeProcess(
                    self._process._handle)
                if return_code == win32con.STILL_ACTIVE:
                    raise
        else:
            try:
                if mode == fixture_interface.TeardownMode.KILL:
                    self._process.kill()
                elif mode == fixture_interface.TeardownMode.TERMINATE:
                    self._process.terminate()
                elif mode == fixture_interface.TeardownMode.ABORT:
                    self._process.send_signal(mode.value)
                else:
                    raise errors.ProcessError(
                        "Process wrapper given unrecognized teardown mode: " +
                        mode.value)

            except OSError as err:
                # ESRCH (errno=3) is received when the process has already died.
                if err.errno != 3:
                    raise
Esempio n. 19
0
    def SvcDoRun(self):
        logging.info("Checking config")
        self.checkConfig()
        self.model = nowcastModel()
      #  logging.info("Starting.....")
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
        self.timeout = 60000
        logging.info("Starting SvcDoRun with timeout: %s", self.timeout)
        mb_up = msMBDbInterface(user=self.config['DATABASE']['db_user'],
                                password=self.config['DATABASE']['db_password'],
                                host=self.config['DATABASE']['db_host'], db_name=self.config['DATABASE']['db_name'])
        next_release = mb_up.next_release_date()[0]
        while 1:
            # Wait for service stop signal, if I timeout, loop again
            rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
            if rc == win32event.WAIT_OBJECT_0:
                servicemanager.LogInfoMsg("msDbMBService Stopped")
                break
            else:
                try:
                    servicemanager.LogInfoMsg("msDbMBService Querying Db")
                    logging.info("Opening Db Connection within Service: ")
                    now = datetime.datetime.now()
                    incomplete_indicators = mb_up.find_incorrect_release_dates()
                    data_correct = True
                    if incomplete_indicators:
                        for indicator in incomplete_indicators:
                            logging.info("Fixing indicator: %s", str(indicator[0]))
                            c = win32com.client.Dispatch("Macrobond.Connection")
                            d = c.Database
                            ts = d.FetchOneSeries(str(indicator[0]))
                            releaseName = ts.Metadata.GetFirstValue("Release")
                            releaseEntity = d.FetchOneEntity(releaseName)
                            current_release = releaseEntity.Metadata.GetFirstValue("LastReleaseEventTime")
                            next_release = releaseEntity.Metadata.GetFirstValue("NextReleaseEventTime")
                            success = False
                            if next_release:
                                success = mb_up.fix_incomplete_indicator(indicator, next_release)
                                logging.info("Fixing indicator: %s : Complete", str(indicator[0]))
                            if not success:
                                logging.info("Fixing indicator: %s: Incomplete. Checking again in 1hr", str(indicator[0]))
                                self.timeout = 1000 * 60  * 60 * 5# 1s * 60 * 60 = 1hr
                                data_correct = False

                    if next_release.astimezone(self.tz) < now:
                        indicator_updates = mb_up.available_updates()
                    else:
                        # No updates, wait until next release time until checking again.
                        indicator_updates = []
                        time_diff = next_release - now
                        error_margin_min = 15 # Error margin in minutes
                        error_margin = 1000 * 60  * error_margin_min
                        if data_correct:
                            self.timeout = time_diff.total_seconds() * 1000 + error_margin
                        else:
                        	if (time_diff.total_seconds() * 1000 + error_margin) < self.timeout:
                        		self.timeout = time_diff.total_seconds() * 1000 + error_margin

                    if len(indicator_updates) > 0:
                        logging.info("Updates found for: %s", str(indicator_updates))
                        c = win32com.client.Dispatch("Macrobond.Connection")
                        d = c.Database
                        all_series = d.FetchSeries(indicator_updates)
                        logging.info("Series fetched for indicators")
                        for num, indicator_key in enumerate(all_series):
                            ts = all_series[num]
                            releaseName = ts.Metadata.GetFirstValue("Release")
                            releaseEntity = d.FetchOneEntity(releaseName)
                            current_release = releaseEntity.Metadata.GetFirstValue("LastReleaseEventTime")
                            next_release = releaseEntity.Metadata.GetFirstValue("NextReleaseEventTime")
                            if not next_release:
                                next_release = pywintypes.Time(0).replace(year=1900, month=1, day=1)
                            if ts:
                                logging.info("Uploading data for indicator %s", str(indicator_key))
                                mb_up.upload_mb_data(ts, str(indicator_key),  current_release, next_release)
                            logging.info("Upload complete for indicator %s", str(indicator_key))
                        next_release = mb_up.next_release_date()[0]
                        time_diff = next_release - now
                        error_margin_min = 15 # Error margin in minutes
                        error_margin = 1000 * 60  * error_margin_min
                        if (time_diff.total_seconds() * 1000 + error_margin) < self.timeout:
                        		self.timeout = time_diff.total_seconds() * 1000 + error_margin

                        if len([indicator[0] for indicator in indicator_updates if indicator[0].find('usfcst') ]) > 0:
                            self.LaunchModelScript()
                            logging.info("Before email latest run is %s", str(mb_up.return_latest_run_id()))
                            self.indicatorChange(mb_up, indicator_updates)

                    else:
                        logging.info("No updates found, waiting for %s minutes", str(self.timeout / 60000))

                    logging.info("Next Release is: %s", datetime.datetime.strftime(mb_up.next_release_date()[0], '%Y-%m-%d %H:%M'))
                    logging.info("Next check will be: %s", datetime.datetime.strftime(now + datetime.timedelta(seconds = (self.timeout * 0.001)), '%Y-%m-%d %H:%M'))

                except:
                    self.sendErrorMail(str(traceback.format_exc()))
                    logging.info("Connection failed with: %s", traceback.format_exc())
                    servicemanager.LogErrorMsg(traceback.format_exc())
                    pass
Esempio n. 20
0
class HIDThread(threading.Thread):
    def __init__(self, deviceName, devicePath, threadName=None):
        self.lockObject = threading.Lock()
        self.lockObject.acquire()
        self.handle = None
        self.text = Text
        self.deviceName = deviceName
        self.devicePath = devicePath
        self.abort = False
        self.initialized = False
        self._overlappedRead = win32file.OVERLAPPED()
        self._overlappedRead.hEvent = win32event.CreateEvent(None, 1, 0, None)
        self._overlappedWrite = None
        self.RawCallback = None
        self.ButtonCallback = None
        self.ValueCallback = None
        self.StopCallback = None
        threading.Thread.__init__(
            self, name=threadName if threadName else self.devicePath)

    @eg.LogIt
    def AbortThread(self):
        self.abort = True
        if self._overlappedWrite:
            win32event.SetEvent(self._overlappedWrite.hEvent)
        win32event.SetEvent(self._overlappedRead.hEvent)

    def run(self):
        #open file/device
        try:
            handle = win32file.CreateFile(
                self.devicePath,
                win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
                None,  # no security
                win32con.OPEN_EXISTING,
                win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_OVERLAPPED,
                0)
        except pywintypes.error as (errno, function, strerror):
            self.lockObject.release()
            eg.PrintError(self.text.errorOpen + self.deviceName + " (" +
                          strerror + ")")
            return

        hidDLL = ctypes.windll.hid
        #setupapiDLL = ctypes.windll.setupapi

        #get preparsed data
        preparsedData = c_ulong()
        hidDLL.HidD_GetPreparsedData(int(handle), ctypes.byref(preparsedData))

        #getCaps
        hidpCaps = HIDP_CAPS()
        hidDLL.HidP_GetCaps(preparsedData, ctypes.byref(hidpCaps))

        n = hidpCaps.InputReportByteLength
        if n == 0:
            self.abort = True
            eg.PrintError(self.text.errorReportLength + self.deviceName)

        rt = c_int(0)  #report type input
        rl = c_ulong(n)  #report length
        maxDataL = hidDLL.HidP_MaxDataListLength(rt, preparsedData)

        #getting button caps
        bCapsArrL = c_ushort(hidpCaps.NumberInputButtonCaps)
        bCapsArrType = HIDP_BUTTON_CAPS * bCapsArrL.value
        bCapsArr = bCapsArrType()

        hidDLL.HidP_GetButtonCaps(rt, ctypes.byref(bCapsArr),
                                  ctypes.byref(bCapsArrL), preparsedData)

        #getting value caps
        vCapsArrL = c_ushort(hidpCaps.NumberInputValueCaps)
        vCapsArrType = HIDP_VALUE_CAPS * vCapsArrL.value
        vCapsArr = vCapsArrType()

        hidDLL.HidP_GetValueCaps(rt, ctypes.byref(vCapsArr),
                                 ctypes.byref(vCapsArrL), preparsedData)

        #parsing caps
        # prepare a list to find and store for each index
        # whether it is a button or value
        dataIndexType = [0] * hidpCaps.NumberInputDataIndices

        #list entries depending on caps
        for i in range(bCapsArrL.value):
            if bCapsArr[i].IsRange:
                for ii in range(bCapsArr[i].Info.Range.DataIndexMin,
                                bCapsArr[i].Info.Range.DataIndexMax + 1):
                    dataIndexType[ii] = 1
            else:
                ii = bCapsArr[i].Info.NotRange.DataIndex
                dataIndexType[ii] = 1

        for i in range(vCapsArrL.value):
            if vCapsArr[i].IsRange:
                for ii in range(vCapsArr[i].Info.Range.DataIndexMin,
                                vCapsArr[i].Info.Range.DataIndexMax + 1):
                    dataIndexType[ii] = 2
            else:
                ii = vCapsArr[i].Info.NotRange.DataIndex
                dataIndexType[ii] = 2

        #prepare data array with maximum possible length
        DataArrayType = HIDP_DATA * maxDataL
        data = DataArrayType()

        #initializing finished
        try:
            self.handle = handle
            self.initialized = True
            rc, newBuf = win32file.ReadFile(handle, n, self._overlappedRead)
            if eg.debugLevel:
                print self.getName() + "init done. Entering loop"

            self.lockObject.release()

            while not self.abort:
                if rc == 997:  #error_io_pending
                    win32event.WaitForSingleObject(self._overlappedRead.hEvent,
                                                   win32event.INFINITE)

                buf = newBuf
                try:
                    win32event.ResetEvent(self._overlappedRead.hEvent)
                    rc, newBuf = win32file.ReadFile(handle, n,
                                                    self._overlappedRead)
                except pywintypes.error as (errno, function, strerror):
                    #device got disconnected so set status to waiting
                    self.abort = True
                    if errno == 1167:
                        eg.PrintError(self.text.errorDisconnected +
                                      self.deviceName)
                    else:
                        eg.PrintError(self.text.errorRead + self.deviceName +
                                      " (" + strerror + ")")

                #parse data
                if len(buf) == n and not self.abort:
                    #raw data events
                    if self.RawCallback:
                        try:
                            self.RawCallback(buf)
                        except Exception:
                            eg.PrintTraceback()

                    #handling button presses and values
                    if maxDataL != 0:
                        dataL = c_ulong(maxDataL)
                        hidDLL.HidP_GetData(rt, ctypes.byref(data),
                                            ctypes.byref(dataL), preparsedData,
                                            ctypes.c_char_p(str(buf)), rl)
                        #parse data to trigger events
                        btnPressed = []
                        values = {}
                        for i in range(dataL.value):
                            tmpIndex = data[i].DataIndex
                            if tmpIndex >= len(dataIndexType) or dataIndexType[
                                    tmpIndex] == 1:  #button
                                #collect buttons pressed
                                btnPressed.append(tmpIndex)
                            elif dataIndexType[tmpIndex] == 2:  #control value
                                values[tmpIndex] = int(data[i].Data.RawValue)
                            else:
                                eg.PrintError(self.text.errorInvalidDataIndex)

                        #value events
                        if (self.ValueCallback):
                            try:
                                self.ValueCallback(values)
                            except Exception:
                                eg.PrintTraceback()

                        #button events
                        if self.ButtonCallback:
                            try:
                                self.ButtonCallback(btnPressed)
                            except Exception:
                                eg.PrintTraceback()
        except:
            self.abort = True
            eg.PrintError(self.text.errorRead + self.deviceName)

        finally:
            win32file.CloseHandle(handle)

            #free references
            hidDLL.HidD_FreePreparsedData(ctypes.byref(preparsedData))

            #HID thread finished
            if self.StopCallback:
                try:
                    self.StopCallback()
                except Exception:
                    eg.PrintTraceback()

            self.handle = None
Esempio n. 21
0
def stop():
    """stop thinking"""
    global terminateAI
    terminateAI = 1
    win32event.WaitForSingleObject(event2, win32event.INFINITE)
Esempio n. 22
0
 def Stop(self) -> None:
     win32process.TerminateProcess(self._handle, -1)
     res = win32event.WaitForSingleObject(self._handle, win32event.INFINITE)
     if res == win32event.WAIT_FAILED:
         raise Error("WaitForSingleObject failed.")
     win32api.CloseHandle(self._handle)
Esempio n. 23
0
    def SvcDoRun(self):
        if not self._checkConfig():
            # stopped status set by caller.
            return

        self.logmsg(servicemanager.PYS_SERVICE_STARTED)

        child_infos = []

        for bbdir in self.dirs:
            self.info("Starting BuildBot in directory '%s'" % (bbdir, ))
            hstop = self.hWaitStop

            cmd = '%s --spawn %d start --nodaemon %s' % (self.runner_prefix, hstop, bbdir)
            # print "cmd is", cmd
            h, t, output = self.createProcess(cmd)
            child_infos.append((bbdir, h, t, output))

        while child_infos:
            handles = [self.hWaitStop] + [i[1] for i in child_infos]

            rc = win32event.WaitForMultipleObjects(handles,
                                                   0,  # bWaitAll
                                                   win32event.INFINITE)
            if rc == win32event.WAIT_OBJECT_0:
                # user sent a stop service request
                break
            else:
                # A child process died.  For now, just log the output
                # and forget the process.
                index = rc - win32event.WAIT_OBJECT_0 - 1
                bbdir, dead_handle, dead_thread, output_blocks = \
                    child_infos[index]
                status = win32process.GetExitCodeProcess(dead_handle)
                output = "".join(output_blocks)
                if not output:
                    output = "The child process generated no output. " \
                             "Please check the twistd.log file in the " \
                             "indicated directory."

                self.warning("BuildBot for directory %r terminated with "
                             "exit code %d.\n%s" % (bbdir, status, output))

                del child_infos[index]

                if not child_infos:
                    self.warning("All BuildBot child processes have "
                                 "terminated.  Service stopping.")

        # Either no child processes left, or stop event set.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

        # The child processes should have also seen our stop signal
        # so wait for them to terminate.
        for bbdir, h, t, output in child_infos:
            for i in range(10):  # 30 seconds to shutdown...
                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                rc = win32event.WaitForSingleObject(h, 3000)
                if rc == win32event.WAIT_OBJECT_0:
                    break
            # Process terminated - no need to try harder.
            if rc == win32event.WAIT_OBJECT_0:
                break

            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            # If necessary, kill it
            if win32process.GetExitCodeProcess(h) == win32con.STILL_ACTIVE:
                self.warning("BuildBot process at %r failed to terminate - "
                             "killing it" % (bbdir, ))
                win32api.TerminateProcess(h, 3)
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

            # Wait for the redirect thread - it should have died as the remote
            # process terminated.
            # As we are shutting down, we do the join with a little more care,
            # reporting progress as we wait (even though we never will <wink>)
            for i in range(5):
                t.join(1)
                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                if not t.isAlive():
                    break
            else:
                self.warning("Redirect thread did not stop!")

        # All done.
        self.logmsg(servicemanager.PYS_SERVICE_STOPPED)
Esempio n. 24
0
                sent += client.send(data)
            except socket.error, e:
                if e.args[0] == win32file.WSAEINTR:
                    continue
                elif e.args[0] in (win32file.WSAEWOULDBLOCK,
                                   win32file.WSAENOBUFS):
                    break
                else:
                    raise
        else:
            self.fail("could not find socket buffer limit")

        events = win32file.WSAEnumNetworkEvents(client)
        self.assertEquals(events, {})

        res = win32event.WaitForSingleObject(server_event, 1000)
        self.assertEquals(res, win32event.WAIT_OBJECT_0)
        events = win32file.WSAEnumNetworkEvents(server, server_event)
        self.assertEquals(events, {win32file.FD_READ: 0})

        received = 0
        while received < sent:
            try:
                received += len(server.recv(16 * 1024))
            except socket.error, e:
                if e.args[0] in [win32file.WSAEINTR, win32file.WSAEWOULDBLOCK]:
                    continue
                else:
                    raise

        self.assertEquals(received, sent)
Esempio n. 25
0
 def join(self):
     win32event.WaitForSingleObject(self.processHandle,
                                    win32event.INFINITE)
     self.exitCode = win32process.GetExitCodeProcess(self.processHandle)
Esempio n. 26
0
#!/usr/bin/python
#-*-coding:utf-8 -*-

import os, os.path, sys
import win32process, win32event

try:
    handle = win32process.CreateProcess('test.exe', '', None, None, 0,
                                        win32process.CREATE_NO_WINDOW, None,
                                        None, win32process.STARTUPINFO())
    running = True
except Exception, e:
    print "Create Error!"
    handle = None
    running = False

while running:
    rc = win32event.WaitForSingleObject(handle[0], 1000)
    if rc == win32event.WAIT_OBJECT_0:
        running = False

#end while
print "GoodBye"
Esempio n. 27
0
 def assertNotSignaled(self, event):
     self.assertEqual(win32event.WaitForSingleObject(event, 0),
                      win32event.WAIT_TIMEOUT)
Esempio n. 28
0
    def SvcDoRun(self):
        rc = None

        while rc != win32event.WAIT_OBJECT_0:
            pass
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
Esempio n. 29
0
    startup = win32process.STARTUPINFO()
    # FIXME: this arg quoting won't work with trailing slashes. See MSDN: CommandLineToArgvW
    (hProcess, hThread, processId, threadId) = win32process.CreateProcess(
        None, ' '.join(['"' + arg + '"'
                        for arg in sys.argv[1:]]), None, None, True,
        win32process.CREATE_SUSPENDED | win32process.CREATE_BREAKAWAY_FROM_JOB,
        None, None, startup)

    # assert not win32job.IsProcessInJob(hProcess, None)

    hJob = win32job.CreateJobObject(None, "")
    extended_info = win32job.QueryInformationJobObject(
        hJob, win32job.JobObjectExtendedLimitInformation)
    extended_info['BasicLimitInformation'][
        'LimitFlags'] = win32job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | win32job.JOB_OBJECT_LIMIT_BREAKAWAY_OK
    win32job.SetInformationJobObject(
        hJob, win32job.JobObjectExtendedLimitInformation, extended_info)
    win32job.AssignProcessToJobObject(hJob, hProcess)
    # n.b. intentionally leak hJob. Otherwise, when running on Windows Server 2008R2 SP1, AssignProcessToJobObject closes hJob (try !handle
    # in windbg before and after), and we crash with invalid handle in CloseHandle on exit
    hJob.Detach()

    win32process.ResumeThread(hThread)

    status = win32event.WAIT_TIMEOUT
    while status == win32event.WAIT_TIMEOUT:
        status = win32event.WaitForSingleObject(hProcess,
                                                500)  # timeout so ctrl-c works
    #win32process.TerminateProcess(hProcess, 1)
    sys.exit(win32process.GetExitCodeProcess(hProcess))
Esempio n. 30
0
 def SvcStop(self):
     reactor.callFromThread(reactor.stop)
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)