summaryrefslogtreecommitdiff
path: root/app/xfs/os/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/xfs/os/daemon.c')
-rw-r--r--app/xfs/os/daemon.c106
1 files changed, 39 insertions, 67 deletions
diff --git a/app/xfs/os/daemon.c b/app/xfs/os/daemon.c
index 2b23de328..e1f826ee0 100644
--- a/app/xfs/os/daemon.c
+++ b/app/xfs/os/daemon.c
@@ -27,81 +27,33 @@ other dealings in this Software without prior written authorization
from the X Consortium.
*/
-/* $XFree86: xc/programs/xfs/os/daemon.c,v 1.12 2002/10/20 21:42:50 tsi Exp $ */
+
+#include "xfs-config.h"
#include <X11/Xos.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
-#if defined(USG)
-# include <termios.h>
-#else
-# include <sys/ioctl.h>
-#endif
-#ifdef hpux
-# include <sys/ptyio.h>
-#endif
-
-#ifdef X_NOT_POSIX
-# define Pid_t int
-#else
-# define Pid_t pid_t
-#endif
-
#include "os.h"
-#if defined(__GLIBC__) || defined(CSRG_BASED)
-#define HAS_DAEMON
-#endif
-
-#ifndef X_NOT_POSIX
-#define HAS_SETSID
-#endif
-
-#ifndef HAS_SETSID
-
-#define setsid() MySetsid()
-
-static Pid_t
-MySetsid(void)
-{
-#if defined(TIOCNOTTY) || defined(TCCLRCTTY) || defined(TIOCTTY)
- int fd;
-#endif
- int stat;
-
- fd = open("/dev/tty", O_RDWR);
- if (fd >= 0) {
-#if defined(USG) && defined(TCCLRCTTY)
- int zero = 0;
- (void) ioctl (fd, TCCLRCTTY, &zero);
-#elif (defined(SYSV) || defined(SVR4)) && defined(TIOCTTY)
- int zero = 0;
- (void) ioctl (i, TIOCTTY, &zero);
-#elif defined(TIOCNOTTY)
- (void) ioctl (i, TIOCNOTTY, (char *) 0); /* detach, BSD style */
-#endif
- close(fd);
- }
-
-#if defined(SYSV) || defined(__QNXNTO__)
- return setpgrp();
-#else
- return setpgid(0, getpid());
-#endif
-}
-
-#endif /* !HAS_SETSID */
-
-
/* detach */
void
-BecomeDaemon ()
+BecomeDaemon (void)
{
/* If our C library has the daemon() function, just use it. */
-#ifdef HAS_DAEMON
- daemon (0, 0);
+#ifdef HAVE_DAEMON
+ if (daemon (0, 0) < 0) {
+ /* error */
+ FatalError("daemon() failed, %s\n", strerror(errno));
+ }
+
+ /* Open/reopen log file on stderr */
+#ifdef USE_SYSLOG
+ if (!UseSyslog)
+#endif
+ CloseErrors();
+ InitErrors();
#else
switch (fork()) {
@@ -123,6 +75,14 @@ BecomeDaemon ()
chdir("/");
+ DetachStdio();
+#endif /* HAVE_DAEMON */
+}
+
+void
+DetachStdio (void)
+{
+ int nullfd;
close (0);
close (1);
close (2);
@@ -130,9 +90,21 @@ BecomeDaemon ()
/*
* Set up the standard file descriptors.
*/
- (void) open ("/dev/null", O_RDWR);
- (void) dup2 (0, 1);
- (void) dup2 (0, 2);
+ nullfd = open ("/dev/null", O_RDWR);
+ if (nullfd != 0) {
+ dup2(nullfd, 0);
+ close(nullfd);
+ }
+ dup2 (0, 1);
+
+#ifdef USE_SYSLOG
+ if (UseSyslog) {
+ dup2 (0, 2);
+ return;
+ }
+#endif
-#endif /* HAS_DAEMON */
+ /* open/reopen log file on stderr */
+ CloseErrors();
+ InitErrors();
}