def run(tset, args, usr_par=None, grp_par=None): if isinstance(tset, str): s = cset.unique_set(tset) elif not isinstance(tset, cset.CpuSet): raise CpusetException( "passed set=%s, which is not a string or CpuSet" % tset) else: s = tset log.debug('entering run, set=%s args=%s ', s.path, args) set.active(s) # check user if usr_par: try: user = pwd.getpwnam(usr_par)[2] except KeyError: try: user = pwd.getpwuid(int(usr_par))[2] except: raise CpusetException('unknown user: "******"' % usr_par) if grp_par: try: group = grp.getgrnam(grp_par)[2] except KeyError: try: group = grp.getgrgid(int(grp_par))[2] except: raise CpusetException('unknown group: "%s"' % grp_par) elif usr_par: # if user is specified but group is not, and user is not root, # then use the users group if user != 0: try: group = grp.getgrnam('users')[2] grp_par = True except: pass # just forget it # move myself into target cpuset and exec child move_pidspec(str(os.getpid()), s) log.info( '--> last message, executed args into cpuset "%s", new pid is: %s', s.path, os.getpid()) # change user and group before exec if grp_par: os.setgid(group) if usr_par: os.setuid(user) os.environ["LOGNAME"] = usr_par os.environ["USERNAME"] = usr_par os.environ["USER"] = usr_par os.execvp(args[0], args)
def run(tset, args, usr_par=None, grp_par=None): if isinstance(tset, str): s = cset.unique_set(tset) elif not isinstance(tset, cset.CpuSet): raise CpusetException( "passed set=%s, which is not a string or CpuSet" % tset) else: s = tset log.debug('entering run, set=%s args=%s ', s.path, args) set.active(s) # check user if usr_par: try: user = pwd.getpwnam(usr_par)[2] except KeyError: try: user = pwd.getpwuid(int(usr_par))[2] except: raise CpusetException('unknown user: "******"' % usr_par) if grp_par: try: group = grp.getgrnam(grp_par)[2] except KeyError: try: group = grp.getgrgid(int(grp_par))[2] except: raise CpusetException('unknown group: "%s"' % grp_par) elif usr_par: # if user is specified but group is not, and user is not root, # then use the users group if user != 0: try: group = grp.getgrnam('users')[2] grp_par = True except: pass # just forget it # move myself into target cpuset and exec child move_pidspec(str(os.getpid()), s) log.info('--> last message, executed args into cpuset "%s", new pid is: %s', s.path, os.getpid()) # change user and group before exec if grp_par: os.setgid(group) if usr_par: os.setuid(user) os.environ["LOGNAME"] = usr_par os.environ["USERNAME"] = usr_par os.environ["USER"] = usr_par os.execvp(args[0], args)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() tset = None if options.list or options.exc: if options.set: tset = cset.unique_set(options.set) elif options.toset: tset = cset.unique_set(options.toset) elif len(args) > 0: if options.exc: tset = cset.unique_set(args[0]) del args[0] else: tset = args else: raise CpusetException("cpuset not specified") try: log.debug("operating on set %s", tset.path) except: log.debug("operating on sets %s", tset) if options.exc: run(tset, args, options.user, options.group) if options.list: list_sets(tset) return if options.move or options.kthread: fset = None tset = None # first, we need to know the destination if options.toset: tset = cset.unique_set(options.toset) elif options.set and options.pid: tset = cset.unique_set(options.set) elif options.set and options.fromset: tset = cset.unique_set(options.set) elif len(args) > 0: if len(args) > 1 and options.pid == None: options.pid = args[0] if len(args) < 3: tset = cset.unique_set(args[1]) else: # "-m 123 set1 set2" shortcut fset = cset.unique_set(args[1]) tset = cset.unique_set(args[2]) # take care of set1->set2 shortcut pids = pidspec_to_list(options.pid, threads=options.threads) if len(pids) == 1: try: fset = cset.unique_set(pids[0]) options.pid = None except: pass # must be real pidspec else: if len(args) < 2: tset = cset.unique_set(args[0]) else: fset = cset.unique_set(args[0]) tset = cset.unique_set(args[1]) else: raise CpusetException("destination cpuset not specified") set.active(tset) # next, if there is a pidspec, move just that if options.pid: if options.fromset and not options.force: fset = cset.unique_set(options.fromset) elif options.toset and options.set: fset = cset.unique_set(options.set) pids = pidspec_to_list(options.pid, fset, options.threads) if len(pids): log.info('moving following pidspec: %s' % ','.join(pids)) selective_move(None, tset, pids, options.kthread, options.force) else: log.info('**> no tasks moved') log.info('done') else: fset = None # here we assume move everything from fromset to toset if options.fromset: fset = cset.unique_set(options.fromset) elif options.set: fset = cset.unique_set(options.set) elif len(args) > 0: # this must be the fromset, then... fset = cset.unique_set(args[0]) if fset == None: raise CpusetException("origination cpuset not specified") nt = len(fset.tasks) if nt == 0: raise CpusetException('no tasks to move from cpuset "%s"' % fset.path) if options.move: log.info('moving all tasks from %s to %s', fset.name, tset.path) selective_move(fset, tset, None, options.kthread, options.force, options.threads) else: log.info('moving all kernel threads from %s to %s', fset.path, tset.path) # this is a -k "move", so only move kernel threads pids = [] for task in fset.tasks: try: os.readlink('/proc/' + task + '/exe') except: pids.append(task) selective_move(fset, tset, pids, options.kthread, options.force) log.info('done') return # default no options is list list_sets(args)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() tset = None if options.list or options.exc: if options.set: tset = cset.unique_set(options.set) elif options.toset: tset = cset.unique_set(options.toset) elif len(args) > 0: if options.exc: tset = cset.unique_set(args[0]) del args[0] else: tset = args else: raise CpusetException("cpuset not specified") try: log.debug("operating on set %s", tset.path) except: log.debug("operating on sets %s", tset) if options.exc: run(tset, args, options.user, options.group) if options.list: list_sets(tset) return if options.move or options.kthread: fset = None tset = None # first, we need to know the destination if options.toset: tset = cset.unique_set(options.toset) elif options.set and options.pid: tset = cset.unique_set(options.set) elif options.set and options.fromset: tset = cset.unique_set(options.set) elif len(args) > 0: if len(args) > 1 and options.pid == None: options.pid = args[0] if len(args) < 3: tset = cset.unique_set(args[1]) else: # "-m 123 set1 set2" shortcut fset = cset.unique_set(args[1]) tset = cset.unique_set(args[2]) # take care of set1->set2 shortcut pids = pidspec_to_list(options.pid, threads=options.threads) if len(pids) == 1: try: fset = cset.unique_set(pids[0]) options.pid = None except: pass # must be real pidspec else: if len(args) < 2: tset = cset.unique_set(args[0]) else: fset = cset.unique_set(args[0]) tset = cset.unique_set(args[1]) else: raise CpusetException("destination cpuset not specified") set.active(tset) # next, if there is a pidspec, move just that if options.pid: if options.fromset and not options.force: fset = cset.unique_set(options.fromset) elif options.toset and options.set: fset = cset.unique_set(options.set) pids = pidspec_to_list(options.pid, fset, options.threads) if len(pids): log.info('moving following pidspec: %s' % ','.join(pids)) selective_move(None, tset, pids, options.kthread, options.force) else: log.info('**> no tasks moved') log.info('done') else: fset = None # here we assume move everything from fromset to toset if options.fromset: fset = cset.unique_set(options.fromset) elif options.set: fset = cset.unique_set(options.set) elif len(args) > 0: # this must be the fromset, then... fset = cset.unique_set(args[0]) if fset == None: raise CpusetException("origination cpuset not specified") nt = len(fset.tasks) if nt == 0: raise CpusetException('no tasks to move from cpuset "%s"' % fset.path) if options.move: log.info('moving all tasks from %s to %s', fset.name, tset.path) selective_move(fset, tset, None, options.kthread, options.force, options.threads) else: log.info('moving all kernel threads from %s to %s', fset.path, tset.path) # this is a -k "move", so only move kernel threads pids = [] for task in fset.tasks: try: os.readlink('/proc/'+task+'/exe') except: pids.append(task) selective_move(fset, tset, pids, options.kthread, options.force) log.info('done') return # default no options is list list_sets(args)