diff options
author | Steven Danneman <steven.danneman@isilon.com> | 2009-02-23 20:46:11 -0800 |
---|---|---|
committer | Steven Danneman <steven.danneman@isilon.com> | 2009-02-23 21:17:12 -0800 |
commit | 8d63c596a0f512c96f5663c0a9bd49d3c98c6df9 (patch) | |
tree | ee571c114c671ca650b68561e9abf9b14d109afe /lib | |
parent | 3a1b4c00eb96634229fb730e9b38e8df5180756a (diff) | |
download | samba-8d63c596a0f512c96f5663c0a9bd49d3c98c6df9.tar.gz samba-8d63c596a0f512c96f5663c0a9bd49d3c98c6df9.tar.xz samba-8d63c596a0f512c96f5663c0a9bd49d3c98c6df9.zip |
Refactored sys_fork() and sys_pid() into shared util library
This fixes a bug in 116ce19b, where we didn't clear the pid cache in
become_daemon() and thus the /var/run/smbd.pid didn't match the actual
pid of the parent process.
Currently S4 will clear the pid cache on fork but doesn't yet take
advantage of the pid cache by using sys_pid() instead of the direct
get_pid().
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/become_daemon.c | 6 | ||||
-rw-r--r-- | lib/util/system.c | 29 | ||||
-rw-r--r-- | lib/util/util.h | 10 |
3 files changed, 42 insertions, 3 deletions
diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 5a97b65407c..3d06a4363df 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -66,10 +66,10 @@ _PUBLIC_ void close_low_fds(bool stderr_too) Become a daemon, discarding the controlling terminal. ****************************************************************************/ -_PUBLIC_ void become_daemon(bool Fork, bool no_process_group) +_PUBLIC_ void become_daemon(bool do_fork, bool no_process_group) { - if (Fork) { - if (fork()) { + if (do_fork) { + if (sys_fork()) { _exit(0); } } diff --git a/lib/util/system.c b/lib/util/system.c index 9bd18002335..9bf5de1a83e 100644 --- a/lib/util/system.c +++ b/lib/util/system.c @@ -88,3 +88,32 @@ _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host) return in2; } +/************************************************************************** + Wrapper for fork. Ensures we clear our pid cache. +****************************************************************************/ + +static pid_t mypid = (pid_t)-1; + +_PUBLIC_ pid_t sys_fork(void) +{ + pid_t forkret = fork(); + + if (forkret == (pid_t)0) { + /* Child - reset mypid so sys_getpid does a system call. */ + mypid = (pid_t) -1; + } + + return forkret; +} + +/************************************************************************** + Wrapper for getpid. Ensures we only do a system call *once*. +****************************************************************************/ + +_PUBLIC_ pid_t sys_getpid(void) +{ + if (mypid == (pid_t)-1) + mypid = getpid(); + + return mypid; +} diff --git a/lib/util/util.h b/lib/util/util.h index d3e446f488c..27f94cd6858 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -134,6 +134,16 @@ apparent reason. _PUBLIC_ struct hostent *sys_gethostbyname(const char *name); _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host); +/** + * Wrapper for fork used to invalid pid cache. + **/ +_PUBLIC_ pid_t sys_fork(void); + +/** + * Wrapper for getpid. Ensures we only do a system call *once*. + **/ +_PUBLIC_ pid_t sys_getpid(void); + /* The following definitions come from lib/util/genrand.c */ /** |