chown the pidfile to the correct user

This commit is contained in:
David Boucha 2012-10-31 12:23:29 -06:00
parent a235f79a3f
commit 51c5944cd7
2 changed files with 25 additions and 2 deletions

View File

@ -368,7 +368,7 @@ class PidfileMixin(object):
def set_pidfile(self):
from salt.utils.process import set_pidfile
set_pidfile(self.config['pidfile'])
set_pidfile(self.config['pidfile'], self.config['user'])
class TargetOptionsMixIn(object):

View File

@ -6,7 +6,7 @@ import time
log = logging.getLogger(__name__)
def set_pidfile(pidfile):
def set_pidfile(pidfile, user):
'''
Save the pidfile
'''
@ -18,6 +18,29 @@ def set_pidfile(pidfile):
f.write(str(os.getpid()))
except IOError:
pass
log.debug(('Created pidfile: {0}').format(pidfile))
if 'os' in os.environ:
if os.environ['os'].startswith('Windows'):
return True
import pwd # after confirming not running Windows
import grp
try:
pwnam = pwd.getpwnam(user)
uid = pwnam[2]
gid = pwnam[3]
groups = [g.gr_gid for g in grp.getgrall() if user in g.gr_mem]
except KeyError:
err = ('Failed to set the pid to user: '
'{0}. The user is not available.\n').format(user)
sys.stderr.write(err)
sys.exit(2)
try:
os.chown(pidfile, uid, gid)
except Error as e:
msg = ('Failed to set the pid to user {0}').format(ser)
sys.stderr.write(msg)
sys.exit(e.errno)
log.debug(('Chowned pidfile: {0} to user: {1}').format(pidfile, user))
def clean_proc(proc, wait_for_kill=10):