[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] Integrate new daemon code, adapted from submission by chris...
- To: or-cvs@freehaven.net
- Subject: [or-cvs] Integrate new daemon code, adapted from submission by chris...
- From: nickm@seul.org (Nick Mathewson)
- Date: Sat, 3 Jan 2004 17:40:51 -0500 (EST)
- Delivered-to: archiver@seul.org
- Delivered-to: or-cvs-outgoing@seul.org
- Delivered-to: or-cvs@seul.org
- Delivery-date: Sat, 03 Jan 2004 17:41:13 -0500
- Reply-to: or-dev@freehaven.net
- Sender: owner-or-cvs@freehaven.net
Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv6366/src/common
Modified Files:
util.c util.h
Log Message:
Integrate new daemon code, adapted from submission by christian grothoff
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -d -r1.51 -r1.52
--- util.c 17 Dec 2003 21:14:13 -0000 1.51
+++ util.c 3 Jan 2004 22:40:49 -0000 1.52
@@ -696,31 +696,97 @@
return uname_result;
}
-void daemonize(void) {
-#ifdef HAVE_DAEMON
- if (daemon(0 /* chdir to / */,
- 0 /* Redirect std* to /dev/null */)) {
- log_fn(LOG_ERR, "Daemon returned an error: %s", strerror(errno));
+#ifndef MS_WINDOWS
+/* Based on code contributed by christian grothoff */
+static int start_daemon_called = 0;
+static int finish_daemon_called = 0;
+static int daemon_filedes[2];
+void start_daemon(void)
+{
+ pid_t pid;
+
+ if (start_daemon_called)
+ return;
+ start_daemon_called = 1;
+
+ /* Don't hold the wrong FS mounted */
+ if (chdir("/") < 0) {
+ perror("chdir");
exit(1);
}
-#elif ! defined(MS_WINDOWS)
- /* Fork; parent exits. */
- if (fork())
- exit(0);
- /* Create new session; make sure we never get a terminal */
- setsid();
- if (fork())
- exit(0);
+ pipe(daemon_filedes);
+ pid = fork();
+ if (pid < 0) {
+ perror("fork");
+ exit(1);
+ }
+ if (pid) { /* Parent */
+ int ok;
+ char c;
- chdir("/");
- umask(000);
+ close(daemon_filedes[1]); /* we only read */
+ ok = -1;
+ while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
+ if (c == '.')
+ ok = 1;
+ }
+ fflush(stdout);
+ if (ok == 1)
+ exit(0);
+ else
+ exit(1); /* child reported error */
+ } else { /* Child */
+ close(daemon_filedes[0]); /* we only write */
- fclose(stdin);
- fclose(stdout);
- fclose(stderr);
-#endif
+ pid = setsid(); /* Detach from controlling terminal */
+ /*
+ * Fork one more time, so the parent (the session group leader) can exit.
+ * This means that we, as a non-session group leader, can never regain a
+ * controlling terminal. This part is recommended by Stevens's
+ * _Advanced Programming in the Unix Environment_.
+ */
+ if (fork() != 0) {
+ exit(0);
+ }
+ return;
+ }
+}
+
+void finish_daemon(void)
+{
+ int nullfd;
+ char c = '.';
+ if (finish_daemon_called)
+ return;
+ if (!start_daemon_called)
+ start_daemon();
+ finish_daemon_called = 1;
+
+ nullfd = open("/dev/null",
+ O_CREAT | O_RDWR | O_APPEND);
+ if (nullfd < 0) {
+ perror("/dev/null");
+ exit(1);
+ }
+ /* close fds linking to invoking terminal, but
+ * close usual incoming fds, but redirect them somewhere
+ * useful so the fds don't get reallocated elsewhere.
+ */
+ if (dup2(nullfd,0) < 0 ||
+ dup2(nullfd,1) < 0 ||
+ dup2(nullfd,2) < 0) {
+ perror("dup2"); /* Should never happen... */
+ exit(1);
+ }
+ write(daemon_filedes[1], &c, sizeof(char)); /* signal success */
+ close(daemon_filedes[1]);
}
+#else
+/* defined(MS_WINDOWS) */
+void start_daemon(void) {}
+void finish_daemon(void) {}
+#endif
void write_pidfile(char *filename) {
#ifndef MS_WINDOWS
Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -d -r1.29 -r1.30
--- util.h 17 Dec 2003 21:14:13 -0000 1.29
+++ util.h 3 Jan 2004 22:40:49 -0000 1.30
@@ -86,7 +86,19 @@
const char *get_uname(void);
-void daemonize(void);
+/* Start putting the process into daemon mode: fork and drop all resources
+ * except standard fds. The parent process never returns, but stays around
+ * until finish_daemon is called. (Note: it's safe to call this more
+ * than once: calls after the first are ignored.)
+ */
+void start_daemon(void);
+/* Finish putting the process into daemon mode: drop standard fds, and tell
+ * the parent process to exit. (Note: it's safe to call this more than once:
+ * calls after the first are ignored. Calls start_daemon first if it hasn't
+ * been called already.)
+ */
+void finish_daemon(void);
+
void write_pidfile(char *filename);
int switch_id(char *user, char *group);