From 5d7ea6cf9d666e9a9260bda3d1029680fcf1d2ec Mon Sep 17 00:00:00 2001 From: Christopher Allen Wing Date: Sun, 24 Apr 2005 15:27:52 +0000 Subject: [PATCH] STABLE14-use-daemon-in-bosserver-and-afsd-20050424 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 | 1 + src/afsd/afsd.c | 8 +++++ src/bozo/bosserver.c | 8 +++-- src/util/Makefile.in | 6 +++- src/util/afsutil_prototypes.h | 5 ++++ src/util/daemon.c | 56 +++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/util/daemon.c diff --git a/acinclude.m4 b/acinclude.m4 index 66de01836..84484bcea 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -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 diff --git a/src/afsd/afsd.c b/src/afsd/afsd.c index 44d4f212d..13e91f042 100644 --- a/src/afsd/afsd.c +++ b/src/afsd/afsd.c @@ -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); } diff --git a/src/bozo/bosserver.c b/src/bozo/bosserver.c index c5bbf8b11..15f3343c6 100644 --- a/src/bozo/bosserver.c +++ b/src/bozo/bosserver.c @@ -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) diff --git a/src/util/Makefile.in b/src/util/Makefile.in index 58ca05e0a..d4f3a58b7 100644 --- a/src/util/Makefile.in +++ b/src/util/Makefile.in @@ -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 # diff --git a/src/util/afsutil_prototypes.h b/src/util/afsutil_prototypes.h index 76f2cfd8c..89f5b71c0 100644 --- a/src/util/afsutil_prototypes.h +++ b/src/util/afsutil_prototypes.h @@ -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 index 000000000..e08845cf8 --- /dev/null +++ b/src/util/daemon.c @@ -0,0 +1,56 @@ +#include +#include + +#ifndef HAVE_DAEMON + +#include +#include +#include + +#include + +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 -- 2.39.5