Add a setgid/setuid function
Signed-off-by: Scott Zawalski <[email protected]>
--- autotest/utils/service_helper.py 2010-02-23 18:21:10.000000000 -0800
+++ autotest/utils/service_helper.py 2010-02-24 11:19:06.000000000 -0800
@@ -1,11 +1,14 @@
#!/usr/bin/python
"""Service launcher that creates pidfiles and can redirect output to a file."""
-import subprocess, sys, os, optparse, signal, pwd
+import subprocess, sys, os, optparse, signal, pwd, grp, re
def stop_service(pidfile):
- """Read the first line of a file for an integer that should refer to a pid,
- send a SIGTERM to that pid #.
+ """
+ Stop a service using a pidfile.
+
+ Read the first line of a file for an integer that should refer to a pid,
+ send a SIGTERM to that pid #.
@param pidfile: file to read for the process id number
"""
pidfh = open(pidfile)
@@ -13,21 +16,15 @@
os.kill(pid, signal.SIGTERM)
-def start_service(cmd, pidfile, logfile=os.devnull, chdir=None, chuid=None):
- """Start cmd in the background and write the pid to pidfile.
- @param cmd: command to run with arguments
- @param pidfile: pidfile to write the pid to
- @param logfile: file to write stderr/stdout to
- @param chdir: Directory to change to before starting the application
- @param chuid: Change the user id the program is run under
+def start_service(cmd, pidfile, logfile=os.devnull, chdir=None):
+ """
+ Start cmd in the background and write the pid to pidfile.
+
+ @param cmd: command to run with arguments
+ @param pidfile: pidfile to write the pid to
+ @param logfile: file to write stderr/stdout to
+ @param chdir: Directory to change to before starting the application
"""
- if chuid:
- # Set environment for programs that use those to find running user
- for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
- os.environ[name] = chuid
- uid = pwd.getpwnam(chuid)[2]
- os.setuid(uid)
- os.seteuid(uid)
logfh = open(logfile, 'a')
pidfh = open(pidfile, 'w')
proc = subprocess.Popen(cmd, stdout=logfh, stderr=logfh, cwd=chdir)
@@ -35,6 +32,61 @@
pidfh.close()
+def get_user_name_id(user):
+ """
+ Get the user id # and name.
+
+ @param user: integer or string containing either the uid #
+ or a string username
+
+ @returns a tuple of the user name, user id
+ """
+ if re.match('\d+', str(user)):
+ pass_info = pwd.getpwuid(user)
+ else:
+ pass_info = pwd.getpwnam(user)
+
+ return pass_info[0], pass_info[2]
+
+
+def get_group_name_id(group):
+ """
+ Get the group id # and name
+
+ @param group: integer or string containing either the uid #
+ or a string username
+
+ @returns a tuple of group name, group id
+ """
+ if re.match('\d+', str(group)):
+ group_info = grp.getgrgid(group)
+ else:
+ group_info = grp.getgrnam(group)
+
+ return group_info[0], group_info[2]
+
+
+def set_group_user(group=None, user=None):
+ """
+ Set the group and user id if gid or uid is defined.
+
+ @param group: Change the group id the program is run under
+ @param user: Change the user id the program is run under
+ """
+ if group:
+ _, gid = get_group_name_id(group)
+ os.setgid(gid)
+ os.setegid(gid)
+
+ if user:
+ username, uid = get_user_name_id(user)
+ # Set environment for programs that use those to find running user
+ for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
+ os.environ[name] = username
+ os.setuid(uid)
+ os.seteuid(uid)
+
+
def main():
parser = optparse.OptionParser()
parser.allow_interspersed_args = False
@@ -56,6 +108,9 @@
parser.add_option('-u', '--chuid', action='store',
default=None,
help='UID to run process as')
+ parser.add_option('-g', '--chgid', action='store',
+ default=None,
+ help='GID to run process as')
@@ -66,9 +121,9 @@
parser.print_help()
sys.exit(1)
+ set_group_user(gid=options.chgid, uid=options.chuid)
if options.start_service:
- start_service(args, options.pidfile, options.logfile, options.chdir,
- options.chuid)
+ start_service(args, options.pidfile, options.logfile, options.chdir)
elif options.stop_service:
stop_service(options.pidfile)
else:
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest