summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--common/sys/Makefile2
-rw-r--r--common/sys/daemon.c90
-rw-r--r--daemon/fromd/fromd.c21
-rw-r--r--include/cmsys.h3
4 files changed, 95 insertions, 21 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;
+}
diff --git a/daemon/fromd/fromd.c b/daemon/fromd/fromd.c
index de1ad44c..c350c4fa 100644
--- a/daemon/fromd/fromd.c
+++ b/daemon/fromd/fromd.c
@@ -55,25 +55,6 @@ static void listen_cb(int fd, short event, void *arg)
event_add(ev, &tv);
}
-void daemonize()
-{
- pid_t pid;
-
- if ( (pid = fork()) < 0)
- exit(1);
-
- if (pid > 0)
- exit(0);
-
- umask(0);
-
- if (setsid() < 0)
- exit(-1);
-
- if (chdir("/") < 0)
- exit(-1);
-}
-
int main(int argc, char *argv[])
{
int ch, sfd;
@@ -95,7 +76,7 @@ int main(int argc, char *argv[])
if ( (sfd = tobind(iface_ip)) < 0 )
return 1;
- daemonize();
+ daemonize(BBSHOME "/run/fromd.pid", NULL);
ip_desc_db_reload(cfgfile);
diff --git a/include/cmsys.h b/include/cmsys.h
index 572ce2a8..05454274 100644
--- a/include/cmsys.h
+++ b/include/cmsys.h
@@ -43,6 +43,9 @@ typedef time_t time4_t;
/* crypt.c */
char *fcrypt(const char *key, const char *salt);
+/* daemon.c */
+extern int daemonize(const char * pidfile, const char * logfile);
+
/* file.c */
extern off_t dashs(const char *fname);
extern time4_t dasht(const char *fname);