mypidfile.py (nss-pam-ldapd-0.9.11) | : | mypidfile.py (nss-pam-ldapd-0.9.12) | ||
---|---|---|---|---|
# mypidfile.py - functions for properly locking a PIDFile | # mypidfile.py - functions for properly locking a PIDFile | |||
# | # | |||
# Copyright (C) 2010-2019 Arthur de Jong | # Copyright (C) 2010-2021 Arthur de Jong | |||
# | # | |||
# This library is free software; you can redistribute it and/or | # This library is free software; you can redistribute it and/or | |||
# modify it under the terms of the GNU Lesser General Public | # modify it under the terms of the GNU Lesser General Public | |||
# License as published by the Free Software Foundation; either | # License as published by the Free Software Foundation; either | |||
# version 2.1 of the License, or (at your option) any later version. | # version 2.1 of the License, or (at your option) any later version. | |||
# | # | |||
# This library is distributed in the hope that it will be useful, | # This library is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
# Lesser General Public License for more details. | # Lesser General Public License for more details. | |||
skipping to change at line 42 | skipping to change at line 42 | |||
def __init__(self, path): | def __init__(self, path): | |||
self.path = path | self.path = path | |||
def __enter__(self): | def __enter__(self): | |||
"""Lock the PID file and write the process ID to the file.""" | """Lock the PID file and write the process ID to the file.""" | |||
# create the directory for the pidfile if needed | # create the directory for the pidfile if needed | |||
piddir = os.path.dirname(self.path) | piddir = os.path.dirname(self.path) | |||
if not os.path.isdir(piddir): | if not os.path.isdir(piddir): | |||
os.mkdir(piddir) | os.mkdir(piddir) | |||
u, gid = cfg.get_usergid() | if cfg.uid is not None: | |||
os.chown(piddir, u.u.pw_uid, gid) | u, gid = cfg.get_usergid() | |||
os.chown(piddir, u.u.pw_uid, gid) | ||||
fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0o644) | fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0o644) | |||
try: | try: | |||
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) | fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) | |||
pidfile = os.fdopen(fd, 'w') | pidfile = os.fdopen(fd, 'w') | |||
except Exception: | except Exception: | |||
os.close(fd) | os.close(fd) | |||
raise | raise | |||
pidfile.write('%d\n' % os.getpid()) | pidfile.write('%d\n' % os.getpid()) | |||
pidfile.truncate() | pidfile.truncate() | |||
pidfile.flush() | pidfile.flush() | |||
End of changes. 2 change blocks. | ||||
3 lines changed or deleted | 4 lines changed or added |