Refactored sys_fork() and sys_pid() into shared util library
authorSteven Danneman <steven.danneman@isilon.com>
Tue, 24 Feb 2009 04:46:11 +0000 (20:46 -0800)
committerSteven Danneman <steven.danneman@isilon.com>
Tue, 24 Feb 2009 05:17:12 +0000 (21:17 -0800)
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().

lib/util/become_daemon.c
lib/util/system.c
lib/util/util.h
source3/Makefile.in
source3/include/proto.h
source3/lib/system.c

index 5a97b65407c62b506ea29e81881363dcbdda99a7..3d06a4363df9b9097ac8dabe04e78de196ee0b95 100644 (file)
@@ -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);
                }
        }
index 9bd180023356cff1970acf352e9c1776ddbe37b6..9bf5de1a83ee7ca605ffea6ba653585cd0ad7b39 100644 (file)
@@ -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;
+}
index d3e446f488c1923ffc51b062a2be7d3546c1da91..27f94cd68583c4bcdc1f03b01dcdee1dd2f88bfe 100644 (file)
@@ -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  */
 
 /**
index 7564659dd3f277f1e3774b92dc1447b187d7ac04..a403f143bb15cb7d38253fc7793cd7f32c5cc29c 100644 (file)
@@ -339,7 +339,7 @@ UTIL_OBJ = ../lib/util/rbtree.o ../lib/util/signal.o ../lib/util/time.o \
                   ../lib/util/util.o ../lib/util/fsusage.o \
                   ../lib/util/params.o ../lib/util/talloc_stack.o \
                   ../lib/util/genrand.o ../lib/util/util_net.o \
-                  ../lib/util/become_daemon.o
+                  ../lib/util/become_daemon.o ../lib/util/system.o
 
 CRYPTO_OBJ = ../lib/crypto/crc32.o ../lib/crypto/md5.o \
                         ../lib/crypto/hmacmd5.o ../lib/crypto/arcfour.o \
index faf47d38441ef1ee95de8905f8d8d9cf2ddd6d99..14241d5ce37091ed99bfc32981e3881a645e4376 100644 (file)
@@ -972,8 +972,6 @@ struct passwd *sys_getpwnam(const char *name);
 struct passwd *sys_getpwuid(uid_t uid);
 struct group *sys_getgrnam(const char *name);
 struct group *sys_getgrgid(gid_t gid);
-pid_t sys_fork(void);
-pid_t sys_getpid(void);
 int sys_popen(const char *command);
 int sys_pclose(int fd);
 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size);
index 8bdc9fa92bf95064642df91e4550d8ec0483c5a8..10b55f662d274f905f7105072c2a48bd28201bee 100644 (file)
@@ -1042,35 +1042,6 @@ static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
        return NULL;
 }
 
-/**************************************************************************
- Wrapper for fork. Ensures that mypid is reset. Used so we can write
- a sys_getpid() that only does a system call *once*.
-****************************************************************************/
-
-static pid_t mypid = (pid_t)-1;
-
-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*.
-****************************************************************************/
-
-pid_t sys_getpid(void)
-{
-       if (mypid == (pid_t)-1)
-               mypid = getpid();
-
-       return mypid;
-}
-
 /**************************************************************************
  Wrapper for popen. Safer as it doesn't search a path.
  Modified from the glibc sources.