1 /***
2 This file is part of libdaemon.
3
4 Copyright 2003-2008 Lennart Poettering
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23
24 ***/
25
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/unistd.h>
32 #include <sys/select.h>
33
34 #include <libdaemon/dfork.h>
35 #include <libdaemon/dsignal.h>
36 #include <libdaemon/dlog.h>
37 #include <libdaemon/dpid.h>
38 #include <libdaemon/dexec.h>
39
main(int argc,char * argv[])40 int main(int argc, char *argv[]) {
41 pid_t pid;
42
43 /* Reset signal handlers */
44 if (daemon_reset_sigs(-1) < 0) {
45 daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno));
46 return 1;
47 }
48
49 /* Unblock signals */
50 if (daemon_unblock_sigs(-1) < 0) {
51 daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno));
52 return 1;
53 }
54
55 /* Set indetification string for the daemon for both syslog and PID file */
56 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
57
58 /* Check if we are called with -k parameter */
59 if (argc >= 2 && !strcmp(argv[1], "-k")) {
60 int ret;
61
62 /* Kill daemon with SIGTERM */
63
64 /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */
65 if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0)
66 daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
67
68 return ret < 0 ? 1 : 0;
69 }
70
71 /* Check that the daemon is not rung twice a the same time */
72 if ((pid = daemon_pid_file_is_running()) >= 0) {
73 daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid);
74 return 1;
75 }
76
77 /* Prepare for return value passing from the initialization procedure of the daemon process */
78 if (daemon_retval_init() < 0) {
79 daemon_log(LOG_ERR, "Failed to create pipe.");
80 return 1;
81 }
82
83 /* Do the fork */
84 if ((pid = daemon_fork()) < 0) {
85
86 /* Exit on error */
87 daemon_retval_done();
88 return 1;
89
90 } else if (pid) { /* The parent */
91 int ret;
92
93 /* Wait for 20 seconds for the return value passed from the daemon process */
94 if ((ret = daemon_retval_wait(20)) < 0) {
95 daemon_log(LOG_ERR, "Could not recieve return value from daemon process: %s", strerror(errno));
96 return 255;
97 }
98
99 daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret);
100 return ret;
101
102 } else { /* The daemon */
103 int fd, quit = 0;
104 fd_set fds;
105
106 /* Close FDs */
107 if (daemon_close_all(-1) < 0) {
108 daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno));
109
110 /* Send the error condition to the parent process */
111 daemon_retval_send(1);
112 goto finish;
113 }
114
115 /* Create the PID file */
116 if (daemon_pid_file_create() < 0) {
117 daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno));
118 daemon_retval_send(2);
119 goto finish;
120 }
121
122 /* Initialize signal handling */
123 if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, 0) < 0) {
124 daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno));
125 daemon_retval_send(3);
126 goto finish;
127 }
128
129 /*... do some further init work here */
130
131
132 /* Send OK to parent process */
133 daemon_retval_send(0);
134
135 daemon_log(LOG_INFO, "Sucessfully started");
136
137 /* Prepare for select() on the signal fd */
138 FD_ZERO(&fds);
139 fd = daemon_signal_fd();
140 FD_SET(fd, &fds);
141
142 while (!quit) {
143 fd_set fds2 = fds;
144
145 /* Wait for an incoming signal */
146 if (select(FD_SETSIZE, &fds2, 0, 0, 0) < 0) {
147
148 /* If we've been interrupted by an incoming signal, continue */
149 if (errno == EINTR)
150 continue;
151
152 daemon_log(LOG_ERR, "select(): %s", strerror(errno));
153 break;
154 }
155
156 /* Check if a signal has been recieved */
157 if (FD_ISSET(fd, &fds2)) {
158 int sig;
159
160 /* Get signal */
161 if ((sig = daemon_signal_next()) <= 0) {
162 daemon_log(LOG_ERR, "daemon_signal_next() failed: %s", strerror(errno));
163 break;
164 }
165
166 /* Dispatch signal */
167 switch (sig) {
168
169 case SIGINT:
170 case SIGQUIT:
171 case SIGTERM:
172 daemon_log(LOG_WARNING, "Got SIGINT, SIGQUIT or SIGTERM.");
173 quit = 1;
174 break;
175
176 case SIGHUP:
177 daemon_log(LOG_INFO, "Got a HUP");
178 daemon_exec("/", NULL, "/bin/ls", "ls", (char*) NULL);
179 break;
180
181 }
182 }
183 }
184
185 /* Do a cleanup */
186 finish:
187 daemon_log(LOG_INFO, "Exiting...");
188 daemon_retval_send(255);
189 daemon_signal_done();
190 daemon_pid_file_remove();
191
192 return 0;
193 }
194 }
195