summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-08-21 10:01:03 +0800
committerwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-08-21 10:01:03 +0800
commitd62a6dd804cabfe12c37365bcd2007a232cdc775 (patch)
tree4aefba6a77e33cff824416b330decd6d1b94f0c5 /common
parent4f1d0b00cd97daf64d7646b04c4963fcbaba6b6c (diff)
downloadpttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar.gz
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar.bz2
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar.lz
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar.xz
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.tar.zst
pttbbs-d62a6dd804cabfe12c37365bcd2007a232cdc775.zip
Common daemonize function
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4399 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'common')
-rw-r--r--common/sys/Makefile2
-rw-r--r--common/sys/daemon.c90
2 files changed, 91 insertions, 1 deletions
diff --git a/common/sys/Makefile b/common/sys/Makefile
index fe9fb2dc..adace886 100644
--- a/common/sys/Makefile
+++ b/common/sys/Makefile
@@ -5,7 +5,7 @@ MKPIC:=no
SRCROOT= ../..
.include "$(SRCROOT)/pttbbs.mk"
-SRCS:= file.c lock.c log.c net.c sort.c string.c time.c crypt.c record.c vector.c
+SRCS:= daemon.c file.c lock.c log.c net.c sort.c string.c time.c crypt.c record.c vector.c
LIB:= cmsys
all: .depend
diff --git a/common/sys/daemon.c b/common/sys/daemon.c
new file mode 100644
index 00000000..829d0a9c
--- /dev/null
+++ b/common/sys/daemon.c
@@ -0,0 +1,90 @@
+/* $Id$ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include "osdep.h"
+#include "cmsys.h"
+
+int
+daemonize(const char * pidfile, const char * logfile)
+{
+ int fd;
+ char buf[32];
+ pid_t pid;
+
+ umask(022);
+
+ if ((pid = fork()) < 0) {
+ perror("Fork failed");
+ exit(1);
+ }
+
+ if (pid > 0)
+ exit(0);
+
+ if (setsid() < 0) {
+ perror("Setsid failed");
+ exit(-1);
+ }
+
+ if ((pid = fork()) < 0) {
+ perror("Fork failed");
+ exit(1);
+ }
+
+ if (pid > 0)
+ exit(0);
+
+ if (chdir("/") < 0) {
+ perror("Can't chdir to root");
+ exit(-1);
+ }
+
+ if (pidfile) {
+ if ((fd = creat(pidfile, 0644)) >= 0) {
+ snprintf(buf, sizeof(buf), "%d", (int)getpid());
+ write(fd, buf, strlen(buf));
+ close(fd);
+ } else
+ perror("Can't open PID file");
+ }
+
+ if (logfile) {
+ if ((fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) {
+ perror("Can't open logfile");
+ exit(1);
+ }
+ if (fd != 2) {
+ dup2(fd, 2);
+ close(fd);
+ }
+ }
+
+ fd = getdtablesize();
+ while (fd > 2)
+ close(--fd);
+
+ if ((fd = open("/dev/null", O_RDWR)) < 0) {
+ perror("Can't open /dev/null");
+ exit(1);
+ }
+
+ dup2(fd, 0);
+ dup2(fd, 1);
+ if (!logfile)
+ dup2(fd, 2);
+
+ if (fd > 2)
+ close(fd);
+
+ Signal(SIGHUP, SIG_IGN);
+ Signal(SIGCHLD, SIG_IGN);
+
+ return 0;
+}