Changeset 7647

Show
Ignore:
Timestamp:
12/13/06 15:41:57 (2 years ago)
Author:
asterix
Message:

[mdt] better pid file behaviour for windows. fixes #2165

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/gajim.py

    r7612 r7647  
    151151import dialogs 
    152152def pid_alive(): 
    153         if os.name == 'nt': 
    154                 return os.path.exists(pid_filename) 
    155153        try: 
    156154                pf = open(pid_filename) 
     
    158156                # probably file not found 
    159157                return False 
     158        pid = int(pf.read().strip()) 
     159        pf.close() 
     160 
     161        if os.name == 'nt': 
     162                try: 
     163                        from ctypes import (windll, c_ulong, c_int, Structure, c_char, POINTER, pointer, ) 
     164                except: 
     165                        return True 
     166 
     167                class PROCESSENTRY32(Structure): 
     168                        _fields_ = [ 
     169                                ('dwSize', c_ulong, ), 
     170                                ('cntUsage', c_ulong, ), 
     171                                ('th32ProcessID', c_ulong, ), 
     172                                ('th32DefaultHeapID', c_ulong, ), 
     173                                ('th32ModuleID', c_ulong, ), 
     174                                ('cntThreads', c_ulong, ), 
     175                                ('th32ParentProcessID', c_ulong, ), 
     176                                ('pcPriClassBase', c_ulong, ), 
     177                                ('dwFlags', c_ulong, ), 
     178                                ('szExeFile', c_char*512, ), 
     179                                ] 
     180                        def __init__(self): 
     181                                Structure.__init__(self, 512+9*4) 
     182 
     183                k = windll.kernel32 
     184                k.CreateToolhelp32Snapshot.argtypes = c_ulong, c_ulong, 
     185                k.CreateToolhelp32Snapshot.restype = c_int 
     186                k.Process32First.argtypes = c_int, POINTER(PROCESSENTRY32), 
     187                k.Process32First.restype = c_int 
     188                k.Process32Next.argtypes = c_int, POINTER(PROCESSENTRY32), 
     189                k.Process32Next.restype = c_int 
     190 
     191                def get_p(p): 
     192                        h = k.CreateToolhelp32Snapshot(2, 0) # TH32CS_SNAPPROCESS 
     193                        assert h > 0, 'CreateToolhelp32Snapshot failed' 
     194                        b = pointer(PROCESSENTRY32()) 
     195                        f = k.Process32First(h, b) 
     196                        while f: 
     197                                if b.contents.th32ProcessID == p: 
     198                                        return b.contents.szExeFile 
     199                                f = k.Process32Next(h, b) 
     200 
     201                if get_p(pid) == 'python.exe': 
     202                        return True 
     203                return False 
    160204        try: 
    161                 pid = int(pf.read().strip()) 
    162                 pf.close() 
    163  
    164205                if not os.path.exists('/proc'): 
    165206                        return True # no /proc, assume Gajim is running