-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathdaemon.c
355 lines (306 loc) · 8.27 KB
/
daemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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
* of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*
* Contributor(s): Jiri Hnidek <[email protected]>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
static int running = 0;
static int delay = 1;
static int counter = 0;
static char *conf_file_name = NULL;
static char *pid_file_name = NULL;
static int pid_fd = -1;
static char *app_name = NULL;
static FILE *log_stream;
/**
* \brief Read configuration from config file
*/
int read_conf_file(int reload)
{
FILE *conf_file = NULL;
int ret = -1;
if (conf_file_name == NULL) return 0;
conf_file = fopen(conf_file_name, "r");
if (conf_file == NULL) {
syslog(LOG_ERR, "Can not open config file: %s, error: %s",
conf_file_name, strerror(errno));
return -1;
}
ret = fscanf(conf_file, "%d", &delay);
if (ret > 0) {
if (reload == 1) {
syslog(LOG_INFO, "Reloaded configuration file %s of %s",
conf_file_name,
app_name);
} else {
syslog(LOG_INFO, "Configuration of %s read from file %s",
app_name,
conf_file_name);
}
}
fclose(conf_file);
return ret;
}
/**
* \brief This function tries to test config file
*/
int test_conf_file(char *_conf_file_name)
{
FILE *conf_file = NULL;
int ret = -1;
conf_file = fopen(_conf_file_name, "r");
if (conf_file == NULL) {
fprintf(stderr, "Can't read config file %s\n",
_conf_file_name);
return EXIT_FAILURE;
}
ret = fscanf(conf_file, "%d", &delay);
if (ret <= 0) {
fprintf(stderr, "Wrong config file %s\n",
_conf_file_name);
}
fclose(conf_file);
if (ret > 0)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
/**
* \brief Callback function for handling signals.
* \param sig identifier of signal
*/
void handle_signal(int sig)
{
if (sig == SIGINT) {
fprintf(log_stream, "Debug: stopping daemon ...\n");
/* Unlock and close lockfile */
if (pid_fd != -1) {
lockf(pid_fd, F_ULOCK, 0);
close(pid_fd);
}
/* Try to delete lockfile */
if (pid_file_name != NULL) {
unlink(pid_file_name);
}
running = 0;
/* Reset signal handling to default behavior */
signal(SIGINT, SIG_DFL);
} else if (sig == SIGHUP) {
fprintf(log_stream, "Debug: reloading daemon config file ...\n");
read_conf_file(1);
} else if (sig == SIGCHLD) {
fprintf(log_stream, "Debug: received SIGCHLD signal\n");
}
}
/**
* \brief This function will daemonize this app
*/
static void daemonize()
{
pid_t pid = 0;
int fd;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* Success: Let the parent terminate */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* On success: The child process becomes session leader */
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
/* Ignore signal sent from child to parent process */
signal(SIGCHLD, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* Success: Let the parent terminate */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");
/* Close all open file descriptors */
for (fd = sysconf(_SC_OPEN_MAX); fd > 0; fd--) {
close(fd);
}
/* Reopen stdin (fd = 0), stdout (fd = 1), stderr (fd = 2) */
stdin = fopen("/dev/null", "r");
stdout = fopen("/dev/null", "w+");
stderr = fopen("/dev/null", "w+");
/* Try to write PID of daemon to lockfile */
if (pid_file_name != NULL)
{
char str[256];
pid_fd = open(pid_file_name, O_RDWR|O_CREAT, 0640);
if (pid_fd < 0) {
/* Can't open lockfile */
exit(EXIT_FAILURE);
}
if (lockf(pid_fd, F_TLOCK, 0) < 0) {
/* Can't lock file */
exit(EXIT_FAILURE);
}
/* Get current PID */
sprintf(str, "%d\n", getpid());
/* Write PID to lockfile */
write(pid_fd, str, strlen(str));
}
}
/**
* \brief Print help for this application
*/
void print_help(void)
{
printf("\n Usage: %s [OPTIONS]\n\n", app_name);
printf(" Options:\n");
printf(" -h --help Print this help\n");
printf(" -c --conf_file filename Read configuration from the file\n");
printf(" -t --test_conf filename Test configuration file\n");
printf(" -l --log_file filename Write logs to the file\n");
printf(" -d --daemon Daemonize this application\n");
printf(" -p --pid_file filename PID file used by daemonized app\n");
printf("\n");
}
/* Main function */
int main(int argc, char *argv[])
{
static struct option long_options[] = {
{"conf_file", required_argument, 0, 'c'},
{"test_conf", required_argument, 0, 't'},
{"log_file", required_argument, 0, 'l'},
{"help", no_argument, 0, 'h'},
{"daemon", no_argument, 0, 'd'},
{"pid_file", required_argument, 0, 'p'},
{NULL, 0, 0, 0}
};
int value, option_index = 0, ret;
char *log_file_name = NULL;
int start_daemonized = 0;
app_name = argv[0];
/* Try to process all command line arguments */
while ((value = getopt_long(argc, argv, "c:l:t:p:dh", long_options, &option_index)) != -1) {
switch (value) {
case 'c':
conf_file_name = strdup(optarg);
break;
case 'l':
log_file_name = strdup(optarg);
break;
case 'p':
pid_file_name = strdup(optarg);
break;
case 't':
return test_conf_file(optarg);
case 'd':
start_daemonized = 1;
break;
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
print_help();
return EXIT_FAILURE;
default:
break;
}
}
/* When daemonizing is requested at command line. */
if (start_daemonized == 1) {
/* It is also possible to use glibc function deamon()
* at this point, but it is useful to customize your daemon. */
daemonize();
}
/* Open system log and write message to it */
openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
syslog(LOG_INFO, "Started %s", app_name);
/* Daemon will handle two signals */
signal(SIGINT, handle_signal);
signal(SIGHUP, handle_signal);
/* Try to open log file to this daemon */
if (log_file_name != NULL) {
log_stream = fopen(log_file_name, "a+");
if (log_stream == NULL) {
syslog(LOG_ERR, "Can not open log file: %s, error: %s",
log_file_name, strerror(errno));
log_stream = stdout;
}
} else {
log_stream = stdout;
}
/* Read configuration from config file */
read_conf_file(0);
/* This global variable can be changed in function handling signal */
running = 1;
/* Never ending loop of server */
while (running == 1) {
/* Debug print */
ret = fprintf(log_stream, "Debug: %d\n", counter++);
if (ret < 0) {
syslog(LOG_ERR, "Can not write to log stream: %s, error: %s",
(log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
ret = fflush(log_stream);
if (ret != 0) {
syslog(LOG_ERR, "Can not fflush() log stream: %s, error: %s",
(log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
/* TODO: dome something useful here */
/* Real server should use select() or poll() for waiting at
* asynchronous event. Note: sleep() is interrupted, when
* signal is received. */
sleep(delay);
}
/* Close log file, when it is used. */
if (log_stream != stdout) {
fclose(log_stream);
}
/* Write system log and close it. */
syslog(LOG_INFO, "Stopped %s", app_name);
closelog();
/* Free allocated memory */
if (conf_file_name != NULL) free(conf_file_name);
if (log_file_name != NULL) free(log_file_name);
if (pid_file_name != NULL) free(pid_file_name);
return EXIT_SUCCESS;
}