summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert James Kaes <rjkaes@users.sourceforge.net>2002-05-23 04:39:32 +0000
committerRobert James Kaes <rjkaes@users.sourceforge.net>2002-05-23 04:39:32 +0000
commit2ee61db6afc88fcea904d0905df0dd920616eb54 (patch)
treef059f85b2effb0bc54fa3f9463ab2d0c624ed50c
parent65cf5ea29027e1ff91f284a40649ae6efb50722d (diff)
downloadtinyproxy-2ee61db6afc88fcea904d0905df0dd920616eb54.tar.gz
tinyproxy-2ee61db6afc88fcea904d0905df0dd920616eb54.zip
Initial addition into CVS. Moves various functions relating the daemon
processes. Added a portable signal handler setting function.
Diffstat (limited to '')
-rw-r--r--src/daemon.c74
-rw-r--r--src/daemon.h33
2 files changed, 107 insertions, 0 deletions
diff --git a/src/daemon.c b/src/daemon.c
new file mode 100644
index 0000000..ec553da
--- /dev/null
+++ b/src/daemon.c
@@ -0,0 +1,74 @@
+/* $Id: daemon.c,v 1.1 2002-05-23 04:39:32 rjkaes Exp $
+ *
+ * This file contains functions which are useful when writing a
+ * daemon process. The functions include a "makedaemon" function and
+ * a function to portably set a signal handler.
+ *
+ * Copyright (C) 2002 Robert James Kaes (rjkaes@flarenet.com)
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include "tinyproxy.h"
+
+#include "daemon.h"
+
+/*
+ * Fork a child process and then kill the parent so make the calling
+ * program a daemon process.
+ */
+void
+makedaemon(void)
+{
+ if (fork() != 0)
+ exit(0);
+
+ setsid();
+ set_signal_handler(SIGHUP, SIG_IGN);
+
+ if (fork() != 0)
+ exit(0);
+
+ chdir("/");
+ umask(077);
+
+ close(0);
+ close(1);
+ close(2);
+}
+
+/*
+ * Pass a signal number and a signal handling function into this function
+ * to handle signals sent to the process.
+ */
+signal_func *
+set_signal_handler(int signo, signal_func *func)
+{
+ struct sigaction act, oact;
+
+ act.sa_handler = func;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ if (signo == SIGALRM) {
+#ifdef SA_INTERRUPT
+ act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
+#endif
+ } else {
+#ifdef SA_RESTART
+ act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
+#endif
+ }
+
+ if (sigaction(signo, &act, &oact) < 0)
+ return SIG_ERR;
+
+ return oact.sa_handler;
+}
diff --git a/src/daemon.h b/src/daemon.h
new file mode 100644
index 0000000..ea2390e
--- /dev/null
+++ b/src/daemon.h
@@ -0,0 +1,33 @@
+/* $Id: daemon.h,v 1.1 2002-05-23 04:39:32 rjkaes Exp $
+ *
+ * See 'daemon.c' for a detailed description.
+ *
+ * Copyright (C) 2002 Robert James Kaes (rjkaes@flarenet.com)
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef TINYPROXY_DAEMON_H
+#define TINYPROXY_DAEMON_H
+
+typedef void signal_func(int);
+
+/*
+ * Pass a singal integer and a function to handle the signal.
+ */
+extern signal_func *set_signal_handler(int signo, signal_func *func);
+
+/*
+ * Make a program a daemon process
+ */
+extern void makedaemon(void);
+
+#endif