Mercurial > hg > toybox
annotate toys/pending/syslogd.c @ 997:8b1814e4c987
Ashwini Sharma said that Kyungwan Han should be in the contact info for the commands he sent recently.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 11 Aug 2013 21:56:08 -0500 |
parents | 893c86bbe452 |
children | f9271a80fedc |
rev | line source |
---|---|
960 | 1 /* syslogd.c - a system logging utility. |
2 * | |
3 * Copyright 2013 Madhur Verma <mad.flexi@gmail.com> | |
997
8b1814e4c987
Ashwini Sharma said that Kyungwan Han should be in the contact info for the commands he sent recently.
Rob Landley <rob@landley.net>
parents:
995
diff
changeset
|
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> |
960 | 5 * |
6 * No Standard | |
7 | |
8 USE_SYSLOGD(NEWTOY(syslogd,">0l#<1>8=8R:b#<0>99=1s#<0=200m#<0>71582787=20O:p:f:a:nSKLD", TOYFLAG_SBIN|TOYFLAG_STAYROOT)) | |
9 | |
10 config SYSLOGD | |
11 bool "syslogd" | |
12 default n | |
13 help | |
14 Usage: syslogd [-a socket] [-p socket] [-O logfile] [-f config file] [-m interval] | |
15 [-p socket] [-s SIZE] [-b N] [-R HOST] [-l N] [-nSLKD] | |
16 | |
17 System logging utility | |
18 | |
19 -a Extra unix socket for listen | |
20 -O FILE Default log file <DEFAULT: /var/log/messages> | |
21 -f FILE Config file <DEFAULT: /etc/syslog.conf> | |
22 -p Alternative unix domain socket <DEFAULT : /dev/log> | |
23 -n Avoid auto-backgrounding. | |
24 -S Smaller output | |
25 -m MARK interval <DEFAULT: 20 minutes> (RANGE: 0 to 71582787) | |
26 -R HOST Log to IP or hostname on PORT (default PORT=514/UDP)" | |
27 -L Log locally and via network (default is network only if -R)" | |
28 -s SIZE Max size (KB) before rotation (default:200KB, 0=off) | |
29 -b N rotated logs to keep (default:1, max=99, 0=purge) | |
30 -K Log to kernel printk buffer (use dmesg to read it) | |
31 -l N Log only messages more urgent than prio(default:8 max:8 min:1) | |
32 -D Drop duplicates | |
33 */ | |
34 | |
35 #define FOR_syslogd | |
36 #include "toys.h" | |
37 #include "toynet.h" | |
38 | |
39 GLOBALS( | |
40 char *socket; | |
41 char *config_file; | |
42 char *unix_socket; | |
43 char *logfile; | |
44 long interval; | |
45 long rot_size; | |
46 long rot_count; | |
47 char *remote_log; | |
48 long log_prio; | |
49 | |
50 struct arg_list *lsocks; // list of listen sockets | |
51 struct arg_list *lfiles; // list of write logfiles | |
52 fd_set rfds; // fds for reading | |
53 int sd; // socket for logging remote messeges. | |
54 ) | |
55 | |
56 #define flag_get(f,v,d) ((toys.optflags & f) ? v : d) | |
57 #define flag_chk(f) ((toys.optflags & f) ? 1 : 0) | |
58 | |
59 #ifndef SYSLOG_NAMES | |
60 #define INTERNAL_NOPRI 0x10 | |
61 #define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES, 0) | |
62 | |
63 typedef struct _code { | |
64 char *c_name; | |
65 int c_val; | |
66 } CODE; | |
67 | |
68 static CODE prioritynames[] = | |
69 { | |
70 { "alert", LOG_ALERT }, | |
71 { "crit", LOG_CRIT }, | |
72 { "debug", LOG_DEBUG }, | |
73 { "emerg", LOG_EMERG }, | |
74 { "err", LOG_ERR }, | |
75 { "error", LOG_ERR }, /* DEPRECATED */ | |
76 { "info", LOG_INFO }, | |
77 { "none", INTERNAL_NOPRI }, /* INTERNAL */ | |
78 { "notice", LOG_NOTICE }, | |
79 { "panic", LOG_EMERG }, /* DEPRECATED */ | |
80 { "warn", LOG_WARNING }, /* DEPRECATED */ | |
81 { "warning", LOG_WARNING }, | |
82 { NULL, -1 } | |
83 }; | |
84 | |
85 static CODE facilitynames[] = | |
86 { | |
87 { "auth", LOG_AUTH }, | |
88 { "authpriv", LOG_AUTHPRIV }, | |
89 { "cron", LOG_CRON }, | |
90 { "daemon", LOG_DAEMON }, | |
91 { "ftp", LOG_FTP }, | |
92 { "kern", LOG_KERN }, | |
93 { "lpr", LOG_LPR }, | |
94 { "mail", LOG_MAIL }, | |
95 { "mark", INTERNAL_MARK }, /* INTERNAL */ | |
96 { "news", LOG_NEWS }, | |
97 { "security", LOG_AUTH }, /* DEPRECATED */ | |
98 { "syslog", LOG_SYSLOG }, | |
99 { "user", LOG_USER }, | |
100 { "uucp", LOG_UUCP }, | |
101 { "local0", LOG_LOCAL0 }, | |
102 { "local1", LOG_LOCAL1 }, | |
103 { "local2", LOG_LOCAL2 }, | |
104 { "local3", LOG_LOCAL3 }, | |
105 { "local4", LOG_LOCAL4 }, | |
106 { "local5", LOG_LOCAL5 }, | |
107 { "local6", LOG_LOCAL6 }, | |
108 { "local7", LOG_LOCAL7 }, | |
109 { NULL, -1 } | |
110 }; | |
111 #endif | |
112 | |
113 | |
114 // Signal handling | |
115 struct fd_pair { int rd; int wr; }; | |
116 static struct fd_pair sigfd; | |
117 | |
118 // UNIX Sockets for listening | |
119 typedef struct unsocks_s { | |
120 char *path; | |
121 struct sockaddr_un sdu; | |
122 int sd; | |
123 } unsocks_t; | |
124 | |
125 // Log file entry to log into. | |
126 typedef struct logfile_s { | |
127 char *filename; | |
128 char *config; | |
129 uint8_t isNetwork; | |
130 uint32_t facility[8]; | |
131 uint8_t level[LOG_NFACILITIES]; | |
132 int logfd; | |
133 struct sockaddr_in saddr; | |
134 } logfile_t; | |
135 | |
136 // Adds opened socks to rfds for select() | |
137 static int addrfds(void) | |
138 { | |
139 unsocks_t *sock; | |
140 int ret = 0; | |
141 struct arg_list *node = TT.lsocks; | |
142 FD_ZERO(&TT.rfds); | |
143 | |
144 while (node) { | |
145 sock = (unsocks_t*) node->arg; | |
146 if (sock->sd > 2) { | |
147 FD_SET(sock->sd, &TT.rfds); | |
148 ret = sock->sd; | |
149 } | |
150 node = node->next; | |
151 } | |
152 FD_SET(sigfd.rd, &TT.rfds); | |
153 return (sigfd.rd > ret)?sigfd.rd:ret; | |
154 } | |
155 | |
156 /* | |
157 * initializes unsock_t structure | |
158 * and opens socket for reading | |
159 * and adds to global lsock list. | |
160 */ | |
161 static int open_unix_socks(void) | |
162 { | |
163 struct arg_list *node; | |
164 unsocks_t *sock; | |
165 int ret = 0; | |
166 | |
167 for(node = TT.lsocks; node; node = node->next) { | |
168 sock = (unsocks_t*) node->arg; | |
169 sock->sdu.sun_family = AF_UNIX; | |
170 strcpy(sock->sdu.sun_path, sock->path); | |
171 sock->sd = socket(AF_UNIX, SOCK_DGRAM, 0); | |
172 if (sock->sd < 0) { | |
173 perror_msg("OPEN SOCKS : failed"); | |
174 continue; | |
175 } | |
176 unlink(sock->sdu.sun_path); | |
177 if (bind(sock->sd, (struct sockaddr *) &sock->sdu, sizeof(sock->sdu))) { | |
178 perror_msg("BIND SOCKS : failed sock : %s", sock->sdu.sun_path); | |
179 close(sock->sd); | |
180 continue; | |
181 } | |
182 chmod(sock->path, 0777); | |
183 ret++; | |
184 } | |
185 return ret; | |
186 } | |
187 | |
188 /* | |
189 * creates a socket of family INET and protocol UDP | |
190 * if successful then returns SOCK othrwise error | |
191 */ | |
192 static int open_udp_socks(char *host, int port, struct sockaddr_in *sadd) | |
193 { | |
194 struct addrinfo *info, rp; | |
195 | |
196 memset(&rp, 0, sizeof(rp)); | |
197 rp.ai_family = AF_INET; | |
198 rp.ai_socktype = SOCK_DGRAM; | |
199 rp.ai_protocol = IPPROTO_UDP; | |
200 | |
201 if (getaddrinfo(host, NULL, &rp, &info) || !info) | |
202 perror_exit("BAD ADDRESS: can't find : %s ", host); | |
203 ((struct sockaddr_in*)info->ai_addr)->sin_port = htons(port); | |
204 memcpy(sadd, info->ai_addr, info->ai_addrlen); | |
205 freeaddrinfo(info); | |
206 | |
207 return xsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
208 } | |
209 | |
210 // Returns node having filename | |
211 static struct arg_list *get_file_node(char *filename, struct arg_list *list) | |
212 { | |
213 while (list) { | |
214 if (!strcmp(((logfile_t*) list->arg)->filename, filename)) return list; | |
215 list = list->next; | |
216 } | |
217 return list; | |
218 } | |
219 | |
220 /* | |
221 * recurses the logfile list and resolves config | |
222 * for evry file and updates facilty and log level bits. | |
223 */ | |
224 static int resolve_config(logfile_t *file) | |
225 { | |
226 char *tk, *fac, *lvl, *tmp, *nfac; | |
227 int count = 0; | |
228 unsigned facval = 0; | |
229 uint8_t set, levval, neg; | |
230 CODE *val = NULL; | |
231 | |
232 tmp = xstrdup(file->config); | |
233 for (tk = strtok(tmp, "; \0"); tk; tk = strtok(NULL, "; \0")) { | |
234 fac = tk; | |
235 tk = strchr(fac, '.'); | |
236 if (!tk) return -1; | |
237 *tk = '\0'; | |
238 lvl = tk + 1; | |
239 | |
240 while(1) { | |
241 count = 0; | |
242 if (*fac == '*') { | |
243 facval = 0xFFFFFFFF; | |
244 fac++; | |
245 } | |
246 nfac = strchr(fac, ','); | |
247 if (nfac) *nfac = '\0'; | |
248 while (*fac && ((CODE*) &facilitynames[count])->c_name) { | |
249 val = (CODE*) &facilitynames[count]; | |
250 if (!strcmp(fac, val->c_name)) { | |
251 facval |= (1<<LOG_FAC(val->c_val)); | |
252 break; | |
253 } | |
254 count++; | |
255 } | |
256 if (((CODE*) &facilitynames[count])->c_val == -1) | |
257 return -1; | |
258 | |
259 if (nfac) fac = nfac+1; | |
260 else break; | |
261 } | |
262 | |
263 count = 0; | |
264 set = 0; | |
265 levval = 0; | |
266 neg = 0; | |
267 if (*lvl == '!') { | |
268 neg = 1; | |
269 lvl++; | |
270 } | |
271 if (*lvl == '=') { | |
272 set = 1; | |
273 lvl++; | |
274 } | |
275 if (*lvl == '*') { | |
276 levval = 0xFF; | |
277 lvl++; | |
278 } | |
279 while (*lvl && ((CODE*) &prioritynames[count])->c_name) { | |
280 val = (CODE*) &prioritynames[count]; | |
281 if (!strcmp(lvl, val->c_name)) { | |
282 levval |= set ? LOG_MASK(val->c_val):LOG_UPTO(val->c_val); | |
283 if (neg) levval = ~levval; | |
284 break; | |
285 } | |
286 count++; | |
287 } | |
288 if (((CODE*) &prioritynames[count])->c_val == -1) return -1; | |
289 | |
290 count = 0; | |
291 set = levval; | |
292 while(set) { | |
293 if (set & 0x1) file->facility[count] |= facval; | |
294 set >>= 1; | |
295 count++; | |
296 } | |
297 for (count = 0; count < LOG_NFACILITIES; count++) { | |
298 if (facval & 0x1) file->level[count] |= levval; | |
299 facval >>= 1; | |
300 } | |
301 } | |
302 free(tmp); | |
303 | |
304 return 0; | |
305 } | |
306 | |
307 // Parse config file and update the log file list. | |
308 static int parse_config_file(void) | |
309 { | |
310 logfile_t *file; | |
311 FILE *fp = NULL; | |
312 char *confline = NULL, *tk = NULL, *tokens[2] = {NULL, NULL}; | |
313 int len, linelen, tcount, lineno = 0; | |
314 struct arg_list *node; | |
315 /* | |
316 * if -K then open only /dev/kmsg | |
317 * all other log files are neglected | |
318 * thus no need to open config either. | |
319 */ | |
320 if (flag_chk(FLAG_K)) { | |
321 node = xzalloc(sizeof(struct arg_list)); | |
322 file = xzalloc(sizeof(logfile_t)); | |
323 file->filename = "/dev/kmsg"; | |
324 file->config = "*.*"; | |
325 memset(file->level, 0xFF, sizeof(file->level)); | |
326 memset(file->facility, 0xFFFFFFFF, sizeof(file->facility)); | |
327 node->arg = (char*) file; | |
328 TT.lfiles = node; | |
329 return 0; | |
330 } | |
331 /* | |
332 * if -R then add remote host to log list | |
333 * if -L is not provided all other log | |
334 * files are neglected thus no need to | |
335 * open config either so just return. | |
336 */ | |
337 if (flag_chk(FLAG_R)) { | |
338 node = xzalloc(sizeof(struct arg_list)); | |
339 file = xzalloc(sizeof(logfile_t)); | |
340 file->filename = xmsprintf("@%s",TT.remote_log); | |
341 file->isNetwork = 1; | |
342 file->config = "*.*"; | |
343 memset(file->level, 0xFF, sizeof(file->level)); | |
344 memset(file->facility, 0xFFFFFFFF, sizeof(file->facility)); | |
345 node->arg = (char*) file; | |
346 TT.lfiles = node; | |
347 if (!flag_chk(FLAG_L))return 0; | |
348 } | |
349 /* | |
350 * Read config file and add logfiles to the list | |
351 * with their configuration. | |
352 */ | |
353 fp = fopen(TT.config_file, "r"); | |
354 if (!fp && flag_chk(FLAG_f)) | |
355 perror_exit("can't open '%s'", TT.config_file); | |
356 | |
357 for (len = 0, linelen = 0; fp;) { | |
358 len = getline(&confline, (size_t*) &linelen, fp); | |
359 if (len <= 0) break; | |
360 lineno++; | |
361 for (; *confline == ' '; confline++, len--) ; | |
362 if ((confline[0] == '#') || (confline[0] == '\n')) continue; | |
363 for (tcount = 0, tk = strtok(confline, " \t"); tk && (tcount < 2); tk = | |
364 strtok(NULL, " \t"), tcount++) { | |
365 if (tcount == 2) { | |
366 error_msg("error in '%s' at line %d", TT.config_file, lineno); | |
367 return -1; | |
368 } | |
369 tokens[tcount] = xstrdup(tk); | |
370 } | |
371 if (tcount <= 1 || tcount > 2) { | |
372 if (tokens[0]) free(tokens[0]); | |
373 error_msg("bad line %d: 1 tokens found, 2 needed", lineno); | |
374 return -1; | |
375 } | |
376 tk = (tokens[1] + (strlen(tokens[1]) - 1)); | |
377 if (*tk == '\n') *tk = '\0'; | |
378 if (*tokens[1] == '\0') { | |
379 error_msg("bad line %d: 1 tokens found, 2 needed", lineno); | |
380 return -1; | |
381 } | |
382 if (*tokens[1] == '*') goto loop_again; | |
383 | |
384 node = get_file_node(tokens[1], TT.lfiles); | |
385 if (!node) { | |
386 node = xzalloc(sizeof(struct arg_list)); | |
387 file = xzalloc(sizeof(logfile_t)); | |
388 file->config = xstrdup(tokens[0]); | |
389 if (resolve_config(file)==-1) { | |
390 error_msg("error in '%s' at line %d", TT.config_file, lineno); | |
391 return -1; | |
392 } | |
393 file->filename = xstrdup(tokens[1]); | |
394 if (*file->filename == '@') file->isNetwork = 1; | |
395 node->arg = (char*) file; | |
396 node->next = TT.lfiles; | |
397 TT.lfiles = node; | |
398 } else { | |
399 file = (logfile_t*) node->arg; | |
400 int rel = strlen(file->config) + strlen(tokens[0]) + 2; | |
401 file->config = xrealloc(file->config, rel); | |
402 sprintf(file->config, "%s;%s", file->config, tokens[0]); | |
403 } | |
404 loop_again: | |
405 if (tokens[0]) free(tokens[0]); | |
406 if (tokens[1]) free(tokens[1]); | |
407 free(confline); | |
408 confline = NULL; | |
409 } | |
410 /* | |
411 * Can't open config file or support is not enabled | |
412 * adding default logfile to the head of list. | |
413 */ | |
414 if (!fp){ | |
415 node = xzalloc(sizeof(struct arg_list)); | |
416 file = xzalloc(sizeof(logfile_t)); | |
417 file->filename = flag_get(FLAG_O, TT.logfile, "/var/log/messages"); //DEFLOGFILE | |
418 file->isNetwork = 0; | |
419 file->config = "*.*"; | |
420 memset(file->level, 0xFF, sizeof(file->level)); | |
421 memset(file->facility, 0xFFFFFFFF, sizeof(file->facility)); | |
422 node->arg = (char*) file; | |
423 node->next = TT.lfiles; | |
424 TT.lfiles = node; | |
425 } | |
426 if (fp) { | |
427 fclose(fp); | |
428 fp = NULL; | |
429 } | |
430 return 0; | |
431 } | |
432 | |
433 static int getport(char *str, char *filename) | |
434 { | |
435 char *endptr = NULL; | |
436 int base = 10; | |
437 errno = 0; | |
438 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { | |
439 base = 16; | |
440 str += 2; | |
441 } | |
442 long port = strtol(str, &endptr, base); | |
443 if (errno || *endptr!='\0'|| endptr == str | |
444 || port < 0 || port > 65535) error_exit("wrong port no in %s", filename); | |
445 return (int)port; | |
446 } | |
447 | |
448 // open every log file in list. | |
449 static void open_logfiles(void) | |
450 { | |
451 logfile_t *tfd; | |
452 char *p, *tmpfile; | |
453 int port = -1; | |
454 struct arg_list *node = TT.lfiles; | |
455 | |
456 while (node) { | |
457 tfd = (logfile_t*) node->arg; | |
458 if (tfd->isNetwork) { | |
459 tmpfile = xstrdup(tfd->filename +1); | |
460 if ((p = strchr(tmpfile, ':'))) { | |
461 *p = '\0'; | |
462 port = getport(p + 1, tfd->filename); | |
463 } | |
464 tfd->logfd = open_udp_socks(tmpfile, (port>=0)?port:514, &tfd->saddr); | |
465 free(tmpfile); | |
466 } else tfd->logfd = open(tfd->filename, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
467 if (tfd->logfd < 0) { | |
468 tfd->filename = "/dev/console"; | |
469 tfd->logfd = open(tfd->filename, O_APPEND); | |
470 } | |
471 node = node->next; | |
472 } | |
473 } | |
474 | |
475 //write to file with rotation | |
476 static int write_rotate( logfile_t *tf, int len) | |
477 { | |
478 int size, isreg; | |
479 struct stat statf; | |
480 isreg = (!fstat(tf->logfd, &statf) && S_ISREG(statf.st_mode)); | |
481 size = statf.st_size; | |
482 | |
483 if (flag_chk(FLAG_s) || flag_chk(FLAG_b)) { | |
484 if (TT.rot_size && isreg && (size + len) > (TT.rot_size*1024)) { | |
485 if (TT.rot_count) { /* always 0..99 */ | |
486 int i = strlen(tf->filename) + 3 + 1; | |
487 char old_file[i]; | |
488 char new_file[i]; | |
489 i = TT.rot_count - 1; | |
490 while (1) { | |
491 sprintf(new_file, "%s.%d", tf->filename, i); | |
492 if (!i) break; | |
493 sprintf(old_file, "%s.%d", tf->filename, --i); | |
494 rename(old_file, new_file); | |
495 } | |
496 rename(tf->filename, new_file); | |
497 unlink(tf->filename); | |
498 close(tf->logfd); | |
499 tf->logfd = open(tf->filename, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
500 } | |
501 ftruncate(tf->logfd, 0); | |
502 } | |
503 } | |
504 return write(tf->logfd, toybuf, len); | |
505 } | |
506 | |
507 //search the given name and return its value | |
508 static char *dec(int val, CODE *clist) | |
509 { | |
510 const CODE *c; | |
511 | |
512 for (c = clist; c->c_name; c++) | |
513 if (val == c->c_val) return c->c_name; | |
514 return itoa(val); | |
515 } | |
516 | |
517 // Compute priority from "facility.level" pair | |
518 static void priority_to_string(int pri, char **facstr, char **lvlstr) | |
519 { | |
520 int fac,lev; | |
521 | |
522 fac = LOG_FAC(pri); | |
523 lev = LOG_PRI(pri); | |
524 *facstr = dec(fac<<3, facilitynames); | |
525 *lvlstr = dec(lev, prioritynames); | |
526 } | |
527 | |
528 //Parse messege and write to file. | |
529 static void logmsg(char *msg, int len) | |
530 { | |
531 time_t now; | |
532 char *p, *ts, *lvlstr, *facstr; | |
533 struct utsname uts; | |
534 int pri = 0; | |
535 struct arg_list *lnode = TT.lfiles; | |
536 | |
537 char *omsg = msg; | |
538 int olen = len, fac, lvl; | |
539 | |
540 if (*msg == '<') { // Extract the priority no. | |
541 pri = (int) strtoul(msg + 1, &p, 10); | |
542 if (*p == '>') msg = p + 1; | |
543 } | |
544 /* Jan 18 00:11:22 msg... | |
545 * 01234567890123456 | |
546 */ | |
547 if (len < 16 || msg[3] != ' ' || msg[6] != ' ' || msg[9] != ':' | |
548 || msg[12] != ':' || msg[15] != ' ') { | |
549 time(&now); | |
550 ts = ctime(&now) + 4; /* skip day of week */ | |
551 } else { | |
552 now = 0; | |
553 ts = msg; | |
554 msg += 16; | |
555 } | |
556 ts[15] = '\0'; | |
557 fac = LOG_FAC(pri); | |
558 lvl = LOG_PRI(pri); | |
559 | |
560 if (flag_chk(FLAG_K)) { | |
561 len = sprintf(toybuf, "<%d> %s\n", pri, msg); | |
562 goto do_log; | |
563 } | |
564 priority_to_string(pri, &facstr, &lvlstr); | |
565 | |
566 p = "local"; | |
567 if (!uname(&uts)) p = uts.nodename; | |
568 if (flag_chk(FLAG_S)) len = sprintf(toybuf, "%s %s\n", ts, msg); | |
569 else len = sprintf(toybuf, "%s %s %s.%s %s\n", ts, p, facstr, lvlstr, msg); | |
570 | |
571 do_log: | |
572 if (lvl >= TT.log_prio) return; | |
573 | |
574 while (lnode) { | |
575 logfile_t *tf = (logfile_t*) lnode->arg; | |
576 if (tf->logfd > 0) { | |
577 if ((tf->facility[lvl] & (1 << fac)) && (tf->level[fac] & (1<<lvl))) { | |
578 int wlen; | |
579 if (tf->isNetwork) | |
580 wlen = sendto(tf->logfd, omsg, olen, 0, (struct sockaddr*)&tf->saddr, sizeof(tf->saddr)); | |
581 else wlen = write_rotate(tf, len); | |
582 if (wlen < 0) perror_msg("write failed file : %s ", (tf->isNetwork)?(tf->filename+1):tf->filename); | |
583 } | |
584 } | |
585 lnode = lnode->next; | |
586 } | |
587 } | |
588 | |
589 /* | |
590 * closes all read and write fds | |
591 * and frees all nodes and lists | |
592 */ | |
593 static void cleanup(void) | |
594 { | |
595 struct arg_list *fnode; | |
596 while (TT.lsocks) { | |
597 fnode = TT.lsocks; | |
598 if (((unsocks_t*) fnode->arg)->sd >= 0) | |
599 close(((unsocks_t*) fnode->arg)->sd); | |
600 free(fnode->arg); | |
601 TT.lsocks = fnode->next; | |
602 free(fnode); | |
603 } | |
604 unlink("/dev/log"); | |
605 | |
606 while (TT.lfiles) { | |
607 fnode = TT.lfiles; | |
608 if (((logfile_t*) fnode->arg)->logfd >= 0) | |
609 close(((logfile_t*) fnode->arg)->logfd); | |
610 free(fnode->arg); | |
611 TT.lfiles = fnode->next; | |
612 free(fnode); | |
613 } | |
614 } | |
615 | |
616 static void signal_handler(int sig) | |
617 { | |
618 unsigned char ch = sig; | |
619 if (write(sigfd.wr, &ch, 1) != 1) error_msg("can't send signal"); | |
620 } | |
621 | |
622 static void setup_signal() | |
623 { | |
624 if (pipe((int *)&sigfd) < 0) error_exit("pipe failed\n"); | |
625 | |
626 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC); | |
627 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC); | |
628 int flags = fcntl(sigfd.wr, F_GETFL); | |
629 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK); | |
630 signal(SIGHUP, signal_handler); | |
631 signal(SIGTERM, signal_handler); | |
632 signal(SIGINT, signal_handler); | |
633 signal(SIGQUIT, signal_handler); | |
634 } | |
635 | |
636 void syslogd_main(void) | |
637 { | |
638 unsocks_t *tsd; | |
639 int maxfd, retval, last_len=0; | |
640 struct timeval tv; | |
641 struct arg_list *node; | |
642 char *temp, *buffer = (toybuf +2048), *last_buf = (toybuf + 3072); //these two buffs are of 1K each | |
643 | |
644 if (flag_chk(FLAG_p) && strlen(TT.unix_socket) > 108) | |
645 error_exit("Socket path should not be more than 108"); | |
646 | |
647 TT.config_file = flag_get(FLAG_f, TT.config_file, "/etc/syslog.conf"); //DEFCONFFILE | |
648 init_jumpin: | |
649 TT.lsocks = xzalloc(sizeof(struct arg_list)); | |
650 tsd = xzalloc(sizeof(unsocks_t)); | |
651 | |
652 tsd->path = flag_get(FLAG_p, TT.unix_socket , "/dev/log"); // DEFLOGSOCK | |
653 TT.lsocks->arg = (char*) tsd; | |
654 | |
655 if (flag_chk(FLAG_a)) { | |
656 for (temp = strtok(TT.socket, ":"); temp; temp = strtok(NULL, ":")) { | |
657 struct arg_list *ltemp = xzalloc(sizeof(struct arg_list)); | |
658 if (strlen(temp) > 107) temp[108] = '\0'; | |
659 tsd = xzalloc(sizeof(unsocks_t)); | |
660 tsd->path = temp; | |
661 ltemp->arg = (char*) tsd; | |
662 ltemp->next = TT.lsocks; | |
663 TT.lsocks = ltemp; | |
664 } | |
665 } | |
666 if (!open_unix_socks()) { | |
667 error_msg("Can't open single socket for listenning."); | |
668 goto clean_and_exit; | |
669 } | |
670 setup_signal(); | |
671 if (parse_config_file() == -1) goto clean_and_exit; | |
672 open_logfiles(); | |
995
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
673 if (!flag_chk(FLAG_n)) { |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
674 //don't daemonize again if SIGHUP received. |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
675 toys.optflags |= FLAG_n; |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
676 } |
960 | 677 { |
678 int pid_fd = open("/var/run/syslogd.pid", O_CREAT | O_WRONLY | O_TRUNC, 0666); | |
679 if (pid_fd > 0) { | |
680 unsigned pid = getpid(); | |
681 int len = sprintf(toybuf, "%u\n", pid); | |
682 write(pid_fd, toybuf, len); | |
683 close(pid_fd); | |
684 } | |
685 } | |
686 | |
687 logmsg("<46>Toybox: syslogd started", 27); //27 : the length of message | |
688 for (;;) { | |
689 maxfd = addrfds(); | |
690 tv.tv_usec = 0; | |
691 tv.tv_sec = TT.interval*60; | |
692 | |
693 retval = select(maxfd + 1, &TT.rfds, NULL, NULL, (TT.interval)?&tv:NULL); | |
694 if (retval < 0) { /* Some error. */ | |
695 if (errno == EINTR) continue; | |
696 perror_msg("Error in select "); | |
697 continue; | |
698 } | |
699 if (!retval) { /* Timed out */ | |
700 logmsg("<46>-- MARK --", 14); | |
701 continue; | |
702 } | |
703 if (FD_ISSET(sigfd.rd, &TT.rfds)) { /* May be a signal */ | |
704 unsigned char sig; | |
705 | |
706 if (read(sigfd.rd, &sig, 1) != 1) { | |
707 error_msg("signal read failed.\n"); | |
708 continue; | |
709 } | |
710 switch(sig) { | |
711 case SIGTERM: /* FALLTHROUGH */ | |
712 case SIGINT: /* FALLTHROUGH */ | |
713 case SIGQUIT: | |
714 logmsg("<46>syslogd exiting", 19); | |
715 if (CFG_TOYBOX_FREE ) cleanup(); | |
716 signal(sig, SIG_DFL); | |
717 sigset_t ss; | |
718 sigemptyset(&ss); | |
719 sigaddset(&ss, sig); | |
720 sigprocmask(SIG_UNBLOCK, &ss, NULL); | |
721 raise(sig); | |
722 _exit(1); /* Should not reach it */ | |
723 break; | |
724 case SIGHUP: | |
725 logmsg("<46>syslogd exiting", 19); | |
726 cleanup(); //cleanup is done, as we restart syslog. | |
727 goto init_jumpin; | |
728 default: break; | |
729 } | |
730 } | |
731 if (retval > 0) { /* Some activity on listen sockets. */ | |
732 node = TT.lsocks; | |
733 while (node) { | |
734 int sd = ((unsocks_t*) node->arg)->sd; | |
735 if (FD_ISSET(sd, &TT.rfds)) { | |
736 int len = read(sd, buffer, 1023); //buffer is of 1K, hence readingonly 1023 bytes, 1 for NUL | |
737 if (len > 0) { | |
738 buffer[len] = '\0'; | |
739 if(flag_chk(FLAG_D) && (len == last_len)) | |
740 if (!memcmp(last_buf, buffer, len)) break; | |
741 | |
742 memcpy(last_buf, buffer, len); | |
743 last_len = len; | |
744 logmsg(buffer, len); | |
745 } | |
746 break; | |
747 } | |
748 node = node->next; | |
749 } | |
750 } | |
751 } | |
752 clean_and_exit: | |
753 logmsg("<46>syslogd exiting", 19); | |
754 if (CFG_TOYBOX_FREE ) cleanup(); | |
755 } |