def AppendLogFile(logfile, appendfile, maxsize): try: # create logs folder before appending if logfile: logsdir = os.path.split(logfile)[0] if logsdir and not os.path.exists(logsdir): os.makedirs(logsdir) # we need to synchronise log file access here # to avoid race conditions if sys.platform.startswith("win"): name = 'ClamWinLogFileUpdate-' + os.path.split(logfile)[1] try: # try to acquire our logupdate mutex hMutex = win32event.OpenMutex(win32con.SYNCHRONIZE, False, name) # wait until it is released win32event.WaitForSingleObject(hMutex, win32event.INFINITE) win32api.CloseHandle(hMutex) except win32api.error: pass # create and own the mutex now to prevent others from modifying the log file # whilst we append to it hMutex = None try: hMutex = win32event.CreateMutex(None, True, name) except win32api.error, e: print('Could not create mutex %s. Error: %s' % (name, str(e))) ftemp = file(appendfile, 'rt') # check if the file is larger then maxsize and read last maxsize bytes # go to end of file ftemp.seek(0, 2) tempsize = ftemp.tell() if tempsize > maxsize: ftemp.seek(-maxsize, 2) tempsize = maxsize else: # read from the beginning ftemp.seek(0, 0) # open main file for appending f = file(logfile, 'a+t') # get the file size f.seek(0, 2) # empty the file if longer than log size limit # shall implement rotation here, when have time if f.tell() > maxsize - tempsize: f.truncate(0) # copy data in using 64Kb buffer bufsize = 65535 pos = 0 while pos < tempsize: # read text from our temporary log file text = ftemp.read(min(bufsize, tempsize - pos)).replace('\r\n', '\n') f.write(text) pos = pos + bufsize
def _is_mutex_exist(self): try: hMutex = win32event.OpenMutex(MUTEX_ALL_ACCESS, False, self.mutex_name) win32api.CloseHandle(hMutex) except: return False return True
def SNPIsRunning(self, onlyShow=None): MutexName = "ShowNumbersPlusGMUTEX" MUTEX_ALL_ACCESS = 0X1F0001 try: SingleAppHandle = win32event.OpenMutex(MUTEX_ALL_ACCESS, 0, MutexName) except pywintypes.error, details: if details[0] == 2: self.MutexHndle = None return
def check_mutants(curs, sample): curs.execute("SELECT * FROM mutants WHERE sid=?", (sample[0], )) mutants = curs.fetchall() for mutant in mutants: try: handle = win32event.OpenMutex(win32con.READ_CONTROL, 0, mutant[2]) except: continue print "[Mutex] %s\n [REF] http://www.threatexpert.com/report.aspx?md5=%s" % ( mutant[2], sample[1])
def wait_mutex_created(name): """ 指定名称のmutexが生成されるまでブロックする。 :param name: :return: """ import win32con import win32event import pywintypes MUTEX_ALL_ACCESS = win32con.STANDARD_RIGHTS_REQUIRED | win32con.SYNCHRONIZE | win32con.MUTANT_QUERY_STATE while True: try: hWait = win32event.OpenMutex(MUTEX_ALL_ACCESS, False, name) except pywintypes.error: time.sleep(0.1) else: break
def main(): # set C locale, otherwise python and wxpython complain locale.setlocale(locale.LC_ALL, 'C') # get the directory of our exetutable file # when running as pyexe built module currentDir = Utils.GetCurrentDir(True) os.chdir(currentDir) Utils.CreateProfile() # see if we are already running and exit if so try: # try to acquire our active mutex hMutex = win32event.OpenMutex(win32con.SYNCHRONIZE, False, MainWindow.ACTIVE_MUTEX) # could open it - most likely another window is active # just to be sure wait for it to see if it is claimed if win32event.WaitForSingleObject(hMutex, 0) == win32event.WAIT_TIMEOUT: # mutex is claimed, another window is already running - terminate return win32api.CloseHandle(hMutex) except win32api.error: pass conf_file = None for arg in sys.argv[1:]: if arg.find('--config_file=') == 0: conf_file = Utils.SafeExpandEnvironmentStrings( arg[len('--config_file='):]) if conf_file is None: conf_file = os.path.join(Utils.GetProfileDir(True), 'ClamWin.conf') if not os.path.isfile(conf_file): conf_file = 'ClamWin.conf' config = Config.Settings(conf_file) config.Read() logon = False for arg in sys.argv[1:]: if arg == '--logon': logon = True w = MainWindow(config, logon) win32gui.PumpMessages()
# argv[2]=mutex key # argv[3]=shmem size # argv[4]=model path # argv[5]=default threshold # shmem_key = sys.argv[1] mutex_key = sys.argv[2] shmem_size = int(sys.argv[3]) model_path = sys.argv[4] threshold = float(sys.argv[5]) hwnd = int(sys.argv[6], 16) MODEL = sys.argv[7] shm = mmap.mmap(0, shmem_size, shmem_key) mutex = win32event.OpenMutex(MUTEX_ALL_ACCESS, False, mutex_key) if not (mutex): print("mutex fail") sys.exit(1) if not (shm): print("shmem fail") sys.exit(1) if (MODEL == "VGG16"): cfg = get_configuration_vgg16() if (MODEL == "VGG19"): cfg = get_configuration_vgg19() if (MODEL == "AlexNet"): cfg = get_configuration_alexnet() prepare_detect(cfg, False) model = load_model(model_path)
elif close_watch_method == "process": print("Watching for launcher process to die") while True: if launcher_exec_name in (get_process_name(p) for p in psutil.process_iter()): sleep(2) reset_launcher_resolution(gamestream_width, gamestream_height, cfg_launcher_window_name) else: break elif close_watch_method == "playnite_mutex": while True: try: #Instance is spelled wrong in the Playnite source code, this may need to be fixed someday #for now it must be spelled Instace playnite_mutex_handle = win32event.OpenMutex( win32event.SYNCHRONIZE, False, "PlayniteInstaceMutex") break except Exception as e: print(f"Exception attempting to open Playnite mutex:{e}") sleep(0.1) #Playnite creates and locks a mutex so if we can lock the mutex it means Playnite has quit win32event.WaitForSingleObject(playnite_mutex_handle, 0xffffffff) #We need to tear down the mutex or Playnite won't start again win32event.ReleaseMutex(playnite_mutex_handle) win32api.CloseHandle(playnite_mutex_handle) else: print( "No valid close_watch_method in the config. Press Enter when you're done." ) input()