]> git.michaelhowe.org Git - packages/o/openafs.git/commitdiff
STABLE14-use-daemon-in-bosserver-and-afsd-20050424
authorChristopher Allen Wing <wingc@engin.umich.edu>
Sun, 24 Apr 2005 15:27:52 +0000 (15:27 +0000)
committerDerrick Brashear <shadow@dementia.org>
Sun, 24 Apr 2005 15:27:52 +0000 (15:27 +0000)
provide daemon() if none exists. call it in bosserver and in afsd for afsdb handler

====================
This delta was composed from multiple commits as part of the CVS->Git migration.
The checkin message with each commit was inconsistent.
The following are the additional commit messages.
====================

add the file, too

(cherry picked from commit 21610a43f7bd4a0f1df1c1184308c04b26d77785)

acinclude.m4
src/afsd/afsd.c
src/bozo/bosserver.c
src/util/Makefile.in
src/util/afsutil_prototypes.h
src/util/daemon.c [new file with mode: 0644]

index 66de0183682f94c12ac85a8e7681cc3c6ec13541..84484bceabefd8c0d3a0c9ae6c35960a9f5be040 100644 (file)
@@ -1008,6 +1008,7 @@ AC_CHECK_TYPE(ssize_t, int)
 AC_SIZEOF_TYPE(long)
 
 AC_CHECK_FUNCS(timegm)
+AC_CHECK_FUNCS(daemon)
 
 dnl Directory PATH handling
 if test "x$enable_transarc_paths" = "xyes"  ; then 
index 44d4f212d03c3e450042d09c0252ace9b6452453..13e91f042d5cb076af0af83d1ef2944b8e0b7026 100644 (file)
@@ -1761,6 +1761,14 @@ mainproc(as, arock)
            printf("%s: Forking AFSDB lookup handler.\n", rn);
        code = fork();
        if (code == 0) {
+           /* Since the AFSDB lookup handler runs as a user process, 
+            * need to drop the controlling TTY, etc.
+            */
+           if (daemon(0, 0) == -1) {
+               printf("Error starting AFSDB lookup handler: %s\n",
+                       strerror(errno));
+               exit(1);
+           }
            AfsdbLookupHandler();
            exit(1);
        }
index c5bbf8b11704756e77716f100d4a1d180221a5ed..15f3343c6b67f3aa7c9cc66acb42e2597dc5e86d 100644 (file)
@@ -580,6 +580,7 @@ tweak_config()
 }
 #endif
 
+#if 0
 /*
  * This routine causes the calling process to go into the background and
  * to lose its controlling tty.
@@ -701,6 +702,7 @@ background(void)
     }
 }
 #endif /* ! AFS_NT40_ENV */
+#endif
 
 /* start a process and monitor it */
 
@@ -857,11 +859,13 @@ main(int argc, char **argv, char **envp)
     fflush(stdout);
 #endif
 
-    /* go into the background and remove our controlling tty */
+    /* go into the background and remove our controlling tty, close open 
+       file desriptors
+     */
 
 #ifndef AFS_NT40_ENV
     if (!nofork)
-       background();
+       daemon(0, 0);
 #endif /* ! AFS_NT40_ENV */
 
     if ((!DoSyslog)
index 58ca05e0ae07f494d7c27cf90876b84cb8dbe139..d4f3a58b742db867e9294a803ca3443ee0dc9264 100644 (file)
@@ -13,7 +13,8 @@ HELPER_SPLINT=@HELPER_SPLINT@
 objects = assert.o base64.o casestrcpy.o ktime.o volparse.o hostparse.o \
         hputil.o kreltime.o isathing.o get_krbrlm.o uuid.o serverLog.o \
         dirpath.o fileutil.o netutils.o flipbase64.o \
-        afs_atomlist.o afs_lhash.o snprintf.o strlcat.o strlcpy.o ${REGEX_OBJ}
+        afs_atomlist.o afs_lhash.o snprintf.o strlcat.o strlcpy.o \
+        daemon.o ${REGEX_OBJ}
 
 includes = \
        ${TOP_INCDIR}/afs/dirpath.h \
@@ -130,6 +131,9 @@ strlcat.o: ${srcdir}/strlcat.c ${includes}
 strlcpy.o: ${srcdir}/strlcpy.c ${includes}
        ${CCOBJ} ${CFLAGS} -c ${srcdir}/strlcpy.c
 
+daemon.o: ${srcdir}/daemon.c ${includes}
+       ${CCOBJ} ${CFLAGS} -c ${srcdir}/daemon.c
+
 #
 # Install targets
 #
index 76f2cfd8cbda6dfc1672fdde122a379adfaef5fd..89f5b71c0298bb62c122fd28ba731cb2601a18dd 100644 (file)
@@ -33,6 +33,11 @@ extern char *lcstring(char *d, char *s, int n);
 extern char *ucstring(char *d, char *s, int n);
 extern char *strcompose(char *buf, size_t len, ...);
 
+/* daemon.c */
+#ifndef HAVE_DAEMON
+int daemon(int nochdir, int noclose);
+#endif
+
 /* dirpath.c */
 extern unsigned int initAFSDirPath(void);
 extern const char *getDirPath(afsdir_id_t string_id);
diff --git a/src/util/daemon.c b/src/util/daemon.c
new file mode 100644 (file)
index 0000000..e08845c
--- /dev/null
@@ -0,0 +1,56 @@
+#include <afsconfig.h>
+#include <afs/param.h>
+
+#ifndef HAVE_DAEMON
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+
+int daemon(int nochdir, int noclose)
+{
+       int err = -1;
+       pid_t pid;
+
+       pid = fork();
+       if (pid == -1) {
+               goto out;
+       } else if (pid) {
+               exit(0);
+       }
+
+       err = setsid();
+       if (err == -1) {
+               goto out;
+       }
+
+       if (!nochdir) {
+               err = chdir("/");
+               if (err == -1) {
+                       goto out;
+               }
+       }
+
+       err = -1;
+       if (!noclose) {
+               if (!freopen("/dev/null", "r", stdin)) {
+                       goto out;
+               }
+
+               if (!freopen("/dev/null", "w", stdout)) {
+                       goto out;
+               }
+
+               if (!freopen("/dev/null", "w", stderr)) {
+                       goto out;
+               }
+       }
+
+       err = 0;
+
+out:
+       return(err);
+}
+#endif