def killall(name, signum, group=False): exception = None knownPgs = set() pidList = utils.pgrep(name) if len(pidList) == 0: raise OSError(errno.ESRCH, "Could not find processes named `%s`" % name) for pid in pidList: try: if group: pgid = os.getpgid(pid) if pgid in knownPgs: # Signal already sent, ignore continue knownPgs.add(pgid) os.killpg(pgid, signum) else: os.kill(pid, signum) except OSError as e: if e.errno == errno.ESRCH: # process died in the interim, ignore continue exception = e if exception is not None: raise exception
def _pid_lookup(device_name, family): for pid in pgrep('dhclient'): try: with open('/proc/%s/cmdline' % pid) as cmdline: args = cmdline.read().strip('\0').split('\0') except IOError as ioe: if ioe.errno == errno.ENOENT: # exited before we read cmdline continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location running_family = 4 for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None elif token == '-6': running_family = 6 if running_family != family: continue yield pid, pid_file
def kill_dhclient(device_name, family=4): for pid in pgrep('dhclient'): try: with open('/proc/%s/cmdline' % pid) as cmdline: args = cmdline.read().strip('\0').split('\0') except IOError as ioe: if ioe.errno == errno.ENOENT: # exited before we read cmdline continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location running_family = 4 for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None elif token == '-6': running_family = 6 if running_family != family: continue logging.info('Stopping dhclient -%s before running our own on %s', family, device_name) kill_and_rm_pid(pid, pid_file) # In order to be able to configure the device with dhclient again. It is # necessary that dhclient does not find it configured with any IP address # (except 0.0.0.0 which is fine, or IPv6 link-local address needed for # DHCPv6). ipwrapper.addrFlush(device_name, family)
def __handleStuckUmount(cls, masterDir): umountPids = utils.pgrep("umount") try: masterMount = mount.getMountFromTarget(masterDir) except OSError as ex: if ex.errno == errno.ENOENT: return raise for umountPid in umountPids: try: state = utils.pidStat(umountPid).state mountPoint = utils.getCmdArgs(umountPid)[-1] except: # Process probably exited continue if mountPoint != masterDir: continue if state != "D": # If the umount is not in d state there # is a possibility that the world might # be in flux and umount will get stuck # in an unkillable state that is not D # which I don't know about, perhaps a # bug in umount will cause umount to # wait for something unrelated that is # not the syscall. Waiting on a process # which is not your child is race prone # I will just call for another umount # and wait for it to finish. That way I # know that a umount ended. try: masterMount.umount() except mount.MountError: # timeout! we are stuck again. # if you are here spmprotect forgot to # reboot the machine but in any case # continue with the disconnection. pass try: vgName = masterDir.rsplit("/", 2)[1] masterDev = os.path.join( "/dev/mapper", vgName.replace("-", "--") + "-" + MASTERLV) except KeyError: # Umount succeeded after all return cls.log.warn( "master mount resource is `%s`, trying to disconnect " "underlying storage", masterDev) iscsi.disconnectFromUndelyingStorage(masterDev)
def __handleStuckUmount(cls, masterDir): umountPids = utils.pgrep("umount") try: masterMount = mount.getMountFromTarget(masterDir) except OSError as ex: if ex.errno == errno.ENOENT: return raise for umountPid in umountPids: try: state = utils.pidStat(umountPid).state mountPoint = utils.getCmdArgs(umountPid)[-1] except: # Process probably exited continue if mountPoint != masterDir: continue if state != "D": # If the umount is not in d state there # is a possibility that the world might # be in flux and umount will get stuck # in an unkillable state that is not D # which I don't know about, perhaps a # bug in umount will cause umount to # wait for something unrelated that is # not the syscall. Waiting on a process # which is not your child is race prone # I will just call for another umount # and wait for it to finish. That way I # know that a umount ended. try: masterMount.umount() except mount.MountError: # timeout! we are stuck again. # if you are here spmprotect forgot to # reboot the machine but in any case # continue with the disconnection. pass try: vgName = masterDir.rsplit("/", 2)[1] masterDev = os.path.join( "/dev/mapper", vgName.replace("-", "--") + "-" + MASTERLV) except KeyError: # Umount succeeded after all return cls.log.warn("master mount resource is `%s`, trying to disconnect " "underlying storage", masterDev) iscsi.disconnectFromUndelyingStorage(masterDev)
def test(self): sleepProcs = [] for i in range(3): sleepProcs.append(commands.execCmd([EXT_SLEEP, "3"], sync=False, sudo=False)) pids = utils.pgrep(EXT_SLEEP) for proc in sleepProcs: self.assertTrue(proc.pid in pids, "pid %d was not located by pgrep" % proc.pid) for proc in sleepProcs: proc.kill() proc.wait()
def test(self): sleepProcs = [] for i in range(3): sleepProcs.append( utils.execCmd([EXT_SLEEP, "3"], sync=False, sudo=False)) pids = utils.pgrep(EXT_SLEEP) for proc in sleepProcs: self.assertTrue(proc.pid in pids, "pid %d was not located by pgrep" % proc.pid) for proc in sleepProcs: proc.kill() proc.wait()
def dhcp_info(devices): info = {devname: {DHCP4: False, DHCP6: False} for devname in devices} for pid in pgrep('dhclient'): args = _read_cmdline(pid) if not args: continue dev = args[-1] if dev not in info: continue dhcp_version_key = DHCP6 if '-6' in args[:-1] else DHCP4 info[dev][dhcp_version_key] = True return info
def test(self): sleepProcs = [] try: for i in range(3): proc = commands.execCmd([EXT_SLEEP, "3"], sync=False) sleepProcs.append(proc) # There is no guarantee which process run first after forking a # child process, make sure all the children are runing before we # look for them. time.sleep(0.5) pids = utils.pgrep(EXT_SLEEP) for proc in sleepProcs: self.assertIn(proc.pid, pids) finally: for proc in sleepProcs: proc.kill() proc.wait()
def _pid_lookup(device_name, family): for pid in pgrep('dhclient'): args = _read_cmdline(pid) if not args: continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location running_family = 4 for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None elif token == '-6': running_family = 6 if running_family != family: continue yield pid, pid_file
def kill_dhclient(device_name): for pid in pgrep('dhclient'): try: with open('/proc/%s/cmdline' % pid) as cmdline: args = cmdline.read().strip('\0').split('\0') except IOError as ioe: if ioe.errno == errno.ENOENT: # exited before we read cmdline continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None _kill_and_rm_pid(pid, pid_file) # In order to be able to configure the device with dhclient again. It is # necessary that dhclient does not find it configured with any IP address # (except 0.0.0.0 which is fine). ipwrapper.addrFlush(device_name)