annotate src/daemon.c @ 27:9298f8b00db2 stable-1-0-10

patches from Takao Abe add switches for config and pid files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 24 May 2018 10:35:45 -0700
parents 8ebecad6530f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*Add by Sergey Shapovalov */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 #include <unistd.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 #include <stdlib.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 #include <fcntl.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 #include <signal.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 #include <sys/types.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 #include <sys/wait.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 #include <errno.h>
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 /* closeall() -- close all FDs >= a specified value */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 void closeall(int fd)
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 {
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 int fdlimit = sysconf(_SC_OPEN_MAX);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 while (fd < fdlimit)
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 close(fd++);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 }
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 /* daemon() - detach process from user and disappear into the background
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 * returns -1 on failure, but you can't do much except exit in that case
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 * since we may already have forked. This is based on the BSD version,
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 * so the caller is responsible for things like the umask, etc.
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 /* believed to work on all Posix systems */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 int daemon(int nochdir, int noclose)
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 {
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 switch (fork())
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 {
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 case 0: break;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 case -1: return -1;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 default: _exit(0); /* exit the original process */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 }
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 if (setsid() < 0) /* shoudn't fail */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 return -1;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 /* dyke out this switch if you want to acquire a control tty in */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 /* the future -- not normally advisable for daemons */
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 switch (fork())
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 {
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 case 0: break;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 case -1: return -1;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 default: _exit(0);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 }
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 if (!nochdir)
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 chdir("/");
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 if (!noclose)
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 {
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 closeall(0);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 open("/dev/null",O_RDWR);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 dup(0); dup(0);
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 }
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 return 0;
8ebecad6530f Add src/daemon* missing from source control.
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 }