Mercurial > hg > toybox
annotate toys/pending/syslogd.c @ 1028:58bfd974216d draft
syslogd: cleanup
- fix bugs introduced in the cleanups
- inline addrfds() and open_unix_socks() and simplify them
- use xpidfile()
- remove isNetwork from struct logfile
- invert the meaning of facility and level in struct logfile so
that they are automatically correctly initialized
- fix memory leak regarding the filenames of logfiles
- TT.sd was unused
author | Felix Janda <felix.janda at posteo.de> |
---|---|
date | Mon, 26 Aug 2013 21:55:33 +0200 |
parents | 4b0ad1c7af42 |
children | cbc467592b2e |
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 | |
1021 | 14 Usage: syslogd [-a socket] [-O logfile] [-f config file] [-m interval] |
960 | 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 | |
1019
f9271a80fedc
In logger and syslogd remove duplicated definitions of facilities and priorities
Felix Janda <felix.janda@posteo.de>
parents:
997
diff
changeset
|
36 #define SYSLOG_NAMES |
960 | 37 #include "toys.h" |
38 #include "toynet.h" | |
39 | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
40 // UNIX Sockets for listening |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
41 struct unsocks { |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
42 struct unsocks *next; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
43 char *path; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
44 struct sockaddr_un sdu; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
45 int sd; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
46 }; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
47 |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
48 // Log file entry to log into. |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
49 struct logfile { |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
50 struct logfile *next; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
51 char *filename; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
52 uint32_t facility[8]; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
53 uint8_t level[LOG_NFACILITIES]; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
54 int logfd; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
55 struct sockaddr_in saddr; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
56 }; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
57 |
960 | 58 GLOBALS( |
59 char *socket; | |
60 char *config_file; | |
61 char *unix_socket; | |
62 char *logfile; | |
63 long interval; | |
64 long rot_size; | |
65 long rot_count; | |
66 char *remote_log; | |
67 long log_prio; | |
68 | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
69 struct unsocks *lsocks; // list of listen sockets |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
70 struct logfile *lfiles; // list of write logfiles |
1021 | 71 int sigfd[2]; |
960 | 72 ) |
73 | |
1025 | 74 // Lookup numerical code from name |
75 // Also used in logger | |
76 int logger_lookup(int where, char *key) | |
77 { | |
78 CODE *w = ((CODE *[]){facilitynames, prioritynames})[where]; | |
79 | |
80 for (; w->c_name; w++) | |
81 if (!strcasecmp(key, w->c_name)) return w->c_val; | |
82 | |
83 return -1; | |
84 } | |
85 | |
86 //search the given name and return its value | |
87 static char *dec(int val, CODE *clist) | |
88 { | |
89 for (; clist->c_name; clist++) | |
90 if (val == clist->c_val) return clist->c_name; | |
91 return itoa(val); | |
92 } | |
93 | |
960 | 94 /* |
95 * recurses the logfile list and resolves config | |
96 * for evry file and updates facilty and log level bits. | |
97 */ | |
1027 | 98 static int resolve_config(struct logfile *file, char *config) |
960 | 99 { |
1027 | 100 char *tk; |
960 | 101 |
1027 | 102 for (tk = strtok(config, "; \0"); tk; tk = strtok(NULL, "; \0")) { |
1025 | 103 char *fac = tk, *lvl; |
104 int i = 0; | |
105 unsigned facval = 0; | |
106 uint8_t set, levval, bits = 0; | |
107 | |
960 | 108 tk = strchr(fac, '.'); |
109 if (!tk) return -1; | |
110 *tk = '\0'; | |
111 lvl = tk + 1; | |
112 | |
1025 | 113 for (;;) { |
114 char *nfac = strchr(fac, ','); | |
115 | |
116 if (nfac) *nfac = '\0'; | |
960 | 117 if (*fac == '*') { |
118 facval = 0xFFFFFFFF; | |
1025 | 119 if (fac[1]) return -1; |
120 } else { | |
121 if ((i = logger_lookup(0, fac)) == -1) return -1; | |
122 facval |= (1 << LOG_FAC(i)); | |
960 | 123 } |
1025 | 124 if (nfac) fac = nfac + 1; |
960 | 125 else break; |
126 } | |
127 | |
128 levval = 0; | |
1027 | 129 for (tk = "!=*"; *tk; tk++, bits <<= 1) { |
1025 | 130 if (*lvl == *tk) { |
131 bits++; | |
132 lvl++; | |
133 } | |
960 | 134 } |
1028 | 135 if (bits & 2) levval = 0xff; |
136 if (*lvl) { | |
1025 | 137 if ((i = logger_lookup(1, lvl)) == -1) return -1; |
1028 | 138 levval |= (bits & 4) ? LOG_MASK(i) : LOG_UPTO(i); |
139 if (bits & 8) levval = ~levval; | |
960 | 140 } |
141 | |
1025 | 142 for (i = 0, set = levval; set; set >>= 1, i++) |
1028 | 143 if (set & 0x1) file->facility[i] |= ~facval; |
1025 | 144 for (i = 0; i < LOG_NFACILITIES; facval >>= 1, i++) |
1028 | 145 if (facval & 0x1) file->level[i] |= ~levval; |
960 | 146 } |
147 | |
148 return 0; | |
149 } | |
150 | |
151 // Parse config file and update the log file list. | |
152 static int parse_config_file(void) | |
153 { | |
1021 | 154 struct logfile *file; |
1025 | 155 FILE *fp; |
1027 | 156 char *confline, *tk[2]; |
157 int len, lineno = 0; | |
1025 | 158 size_t linelen; |
960 | 159 /* |
160 * if -K then open only /dev/kmsg | |
161 * all other log files are neglected | |
162 * thus no need to open config either. | |
163 */ | |
1024 | 164 if (toys.optflags & FLAG_K) { |
1021 | 165 file = xzalloc(sizeof(struct logfile)); |
1028 | 166 file->filename = xstrdup("/dev/kmsg"); |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
167 TT.lfiles = file; |
960 | 168 return 0; |
169 } | |
170 /* | |
171 * if -R then add remote host to log list | |
172 * if -L is not provided all other log | |
173 * files are neglected thus no need to | |
174 * open config either so just return. | |
175 */ | |
1025 | 176 if (toys.optflags & FLAG_R) { |
177 file = xzalloc(sizeof(struct logfile)); | |
178 file->filename = xmsprintf("@%s",TT.remote_log); | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
179 TT.lfiles = file; |
1025 | 180 if (!(toys.optflags & FLAG_L)) return 0; |
181 } | |
960 | 182 /* |
183 * Read config file and add logfiles to the list | |
184 * with their configuration. | |
185 */ | |
1028 | 186 if (!(fp = fopen(TT.config_file, "r")) && (toys.optflags & FLAG_f)) |
960 | 187 perror_exit("can't open '%s'", TT.config_file); |
188 | |
1027 | 189 for (linelen = 0; fp;) { |
190 confline = NULL; | |
1025 | 191 len = getline(&confline, &linelen, fp); |
960 | 192 if (len <= 0) break; |
193 lineno++; | |
194 for (; *confline == ' '; confline++, len--) ; | |
195 if ((confline[0] == '#') || (confline[0] == '\n')) continue; | |
1027 | 196 tk[0] = confline; |
197 for (; len && !(*tk[0]==' ' || *tk[0]=='\t'); tk[0]++, len--); | |
198 for (tk[1] = tk[0]; len && (*tk[1]==' ' || *tk[1]=='\t'); tk[1]++, len--); | |
199 if (!len || (len == 1 && *tk[1] == '\n')) { | |
200 error_msg("error in '%s' at line %d", TT.config_file, lineno); | |
201 return -1; | |
202 } | |
203 else if (*(tk[1] + len - 1) == '\n') *(tk[1] + len - 1) = '\0'; | |
204 *tk[0] = '\0'; | |
205 if (*tk[1] != '*') { | |
206 file = TT.lfiles; | |
207 while (file && strcmp(file->filename, tk[1])) file = file->next; | |
208 if (!file) { | |
209 file = xzalloc(sizeof(struct logfile)); | |
210 file->filename = xstrdup(tk[1]); | |
211 file->next = TT.lfiles; | |
212 TT.lfiles = file; | |
213 } | |
214 if (resolve_config(file, confline) == -1) { | |
960 | 215 error_msg("error in '%s' at line %d", TT.config_file, lineno); |
216 return -1; | |
217 } | |
218 } | |
219 free(confline); | |
220 } | |
221 /* | |
222 * Can't open config file or support is not enabled | |
223 * adding default logfile to the head of list. | |
224 */ | |
225 if (!fp){ | |
1021 | 226 file = xzalloc(sizeof(struct logfile)); |
1028 | 227 file->filename = xstrdup((toys.optflags & FLAG_O) ? |
228 TT.logfile : "/var/log/messages"); //DEFLOGFILE | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
229 file->next = TT.lfiles; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
230 TT.lfiles = file; |
1025 | 231 } else fclose(fp); |
960 | 232 return 0; |
233 } | |
234 | |
235 // open every log file in list. | |
236 static void open_logfiles(void) | |
237 { | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
238 struct logfile *tfd; |
1024 | 239 |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
240 for (tfd = TT.lfiles; tfd; tfd = tfd->next) { |
1024 | 241 char *p, *tmpfile; |
242 long port = 514; | |
243 | |
1028 | 244 if (*tfd->filename == '@') { // network |
1024 | 245 struct addrinfo *info, rp; |
246 | |
247 tmpfile = xstrdup(tfd->filename + 1); | |
248 if ((p = strchr(tmpfile, ':'))) { | |
249 char *endptr; | |
960 | 250 |
251 *p = '\0'; | |
1024 | 252 port = strtol(++p, &endptr, 10); |
253 if (*endptr || endptr == p || port < 0 || port > 65535) | |
254 error_exit("bad port in %s", tfd->filename); | |
960 | 255 } |
1024 | 256 memset(&rp, 0, sizeof(rp)); |
257 rp.ai_family = AF_INET; | |
258 rp.ai_socktype = SOCK_DGRAM; | |
259 rp.ai_protocol = IPPROTO_UDP; | |
260 | |
261 if (getaddrinfo(tmpfile, NULL, &rp, &info) || !info) | |
262 perror_exit("BAD ADDRESS: can't find : %s ", tmpfile); | |
263 ((struct sockaddr_in*)info->ai_addr)->sin_port = htons(port); | |
264 memcpy(&tfd->saddr, info->ai_addr, info->ai_addrlen); | |
265 freeaddrinfo(info); | |
266 | |
267 tfd->logfd = xsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
960 | 268 free(tmpfile); |
269 } else tfd->logfd = open(tfd->filename, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
270 if (tfd->logfd < 0) { | |
271 tfd->filename = "/dev/console"; | |
272 tfd->logfd = open(tfd->filename, O_APPEND); | |
273 } | |
274 } | |
275 } | |
276 | |
277 //write to file with rotation | |
1021 | 278 static int write_rotate(struct logfile *tf, int len) |
960 | 279 { |
280 int size, isreg; | |
281 struct stat statf; | |
282 isreg = (!fstat(tf->logfd, &statf) && S_ISREG(statf.st_mode)); | |
283 size = statf.st_size; | |
284 | |
1024 | 285 if ((toys.optflags & FLAG_s) || (toys.optflags & FLAG_b)) { |
960 | 286 if (TT.rot_size && isreg && (size + len) > (TT.rot_size*1024)) { |
287 if (TT.rot_count) { /* always 0..99 */ | |
288 int i = strlen(tf->filename) + 3 + 1; | |
289 char old_file[i]; | |
290 char new_file[i]; | |
291 i = TT.rot_count - 1; | |
292 while (1) { | |
293 sprintf(new_file, "%s.%d", tf->filename, i); | |
294 if (!i) break; | |
295 sprintf(old_file, "%s.%d", tf->filename, --i); | |
296 rename(old_file, new_file); | |
297 } | |
298 rename(tf->filename, new_file); | |
299 unlink(tf->filename); | |
300 close(tf->logfd); | |
301 tf->logfd = open(tf->filename, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
302 } | |
303 ftruncate(tf->logfd, 0); | |
304 } | |
305 } | |
306 return write(tf->logfd, toybuf, len); | |
307 } | |
308 | |
309 //Parse messege and write to file. | |
310 static void logmsg(char *msg, int len) | |
311 { | |
312 time_t now; | |
313 char *p, *ts, *lvlstr, *facstr; | |
314 struct utsname uts; | |
315 int pri = 0; | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
316 struct logfile *tf = TT.lfiles; |
960 | 317 |
318 char *omsg = msg; | |
319 int olen = len, fac, lvl; | |
320 | |
321 if (*msg == '<') { // Extract the priority no. | |
322 pri = (int) strtoul(msg + 1, &p, 10); | |
323 if (*p == '>') msg = p + 1; | |
324 } | |
325 /* Jan 18 00:11:22 msg... | |
326 * 01234567890123456 | |
327 */ | |
328 if (len < 16 || msg[3] != ' ' || msg[6] != ' ' || msg[9] != ':' | |
329 || msg[12] != ':' || msg[15] != ' ') { | |
330 time(&now); | |
331 ts = ctime(&now) + 4; /* skip day of week */ | |
332 } else { | |
333 now = 0; | |
334 ts = msg; | |
335 msg += 16; | |
336 } | |
337 ts[15] = '\0'; | |
338 fac = LOG_FAC(pri); | |
339 lvl = LOG_PRI(pri); | |
340 | |
1024 | 341 if (toys.optflags & FLAG_K) len = sprintf(toybuf, "<%d> %s\n", pri, msg); |
342 else { | |
1027 | 343 facstr = dec(pri & LOG_FACMASK, facilitynames); |
1025 | 344 lvlstr = dec(LOG_PRI(pri), prioritynames); |
960 | 345 |
1024 | 346 p = "local"; |
347 if (!uname(&uts)) p = uts.nodename; | |
348 if (toys.optflags & FLAG_S) len = sprintf(toybuf, "%s %s\n", ts, msg); | |
349 else len = sprintf(toybuf, "%s %s %s.%s %s\n", ts, p, facstr, lvlstr, msg); | |
350 } | |
960 | 351 if (lvl >= TT.log_prio) return; |
352 | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
353 for (; tf; tf = tf->next) { |
960 | 354 if (tf->logfd > 0) { |
1028 | 355 if (!((tf->facility[lvl] & (1 << fac)) || (tf->level[fac] & (1<<lvl)))) { |
356 int wlen, isNetwork = *tf->filename == '@'; | |
357 if (isNetwork) | |
960 | 358 wlen = sendto(tf->logfd, omsg, olen, 0, (struct sockaddr*)&tf->saddr, sizeof(tf->saddr)); |
359 else wlen = write_rotate(tf, len); | |
1028 | 360 if (wlen < 0) perror_msg("write failed file : %s ", tf->filename + isNetwork); |
960 | 361 } |
362 } | |
363 } | |
364 } | |
365 | |
366 /* | |
367 * closes all read and write fds | |
368 * and frees all nodes and lists | |
369 */ | |
370 static void cleanup(void) | |
371 { | |
372 while (TT.lsocks) { | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
373 struct unsocks *fnode = TT.lsocks; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
374 |
1028 | 375 if (fnode->sd >= 0) { |
376 close(fnode->sd); | |
377 unlink(fnode->path); | |
378 } | |
960 | 379 TT.lsocks = fnode->next; |
380 free(fnode); | |
381 } | |
382 | |
383 while (TT.lfiles) { | |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
384 struct logfile *fnode = TT.lfiles; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
385 |
1028 | 386 free(fnode->filename); |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
387 if (fnode->logfd >= 0) close(fnode->logfd); |
960 | 388 TT.lfiles = fnode->next; |
389 free(fnode); | |
390 } | |
391 } | |
392 | |
393 static void signal_handler(int sig) | |
394 { | |
395 unsigned char ch = sig; | |
1021 | 396 if (write(TT.sigfd[1], &ch, 1) != 1) error_msg("can't send signal"); |
960 | 397 } |
398 | |
399 void syslogd_main(void) | |
400 { | |
1021 | 401 struct unsocks *tsd; |
1028 | 402 int nfds, retval, last_len=0; |
960 | 403 struct timeval tv; |
1028 | 404 fd_set rfds; // fds for reading |
960 | 405 char *temp, *buffer = (toybuf +2048), *last_buf = (toybuf + 3072); //these two buffs are of 1K each |
406 | |
1024 | 407 if ((toys.optflags & FLAG_p) && (strlen(TT.unix_socket) > 108)) |
960 | 408 error_exit("Socket path should not be more than 108"); |
409 | |
1024 | 410 TT.config_file = (toys.optflags & FLAG_f) ? |
411 TT.config_file : "/etc/syslog.conf"; //DEFCONFFILE | |
960 | 412 init_jumpin: |
1021 | 413 tsd = xzalloc(sizeof(struct unsocks)); |
960 | 414 |
1024 | 415 tsd->path = (toys.optflags & FLAG_p) ? TT.unix_socket : "/dev/log"; // DEFLOGSOCK |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
416 TT.lsocks = tsd; |
960 | 417 |
1024 | 418 if (toys.optflags & FLAG_a) { |
960 | 419 for (temp = strtok(TT.socket, ":"); temp; temp = strtok(NULL, ":")) { |
420 if (strlen(temp) > 107) temp[108] = '\0'; | |
1021 | 421 tsd = xzalloc(sizeof(struct unsocks)); |
960 | 422 tsd->path = temp; |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
423 tsd->next = TT.lsocks; |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
424 TT.lsocks = tsd; |
960 | 425 } |
426 } | |
1028 | 427 /* |
428 * initializes unsock_t structure | |
429 * and opens socket for reading | |
430 * and adds to global lsock list. | |
431 */ | |
432 nfds = 0; | |
433 for (tsd = TT.lsocks; tsd; tsd = tsd->next) { | |
434 tsd->sdu.sun_family = AF_UNIX; | |
435 strcpy(tsd->sdu.sun_path, tsd->path); | |
436 tsd->sd = socket(AF_UNIX, SOCK_DGRAM, 0); | |
437 if (tsd->sd < 0) { | |
438 perror_msg("OPEN SOCKS : failed"); | |
439 continue; | |
440 } | |
441 unlink(tsd->sdu.sun_path); | |
442 if (bind(tsd->sd, (struct sockaddr *) &tsd->sdu, sizeof(tsd->sdu))) { | |
443 perror_msg("BIND SOCKS : failed sock : %s", tsd->sdu.sun_path); | |
444 close(tsd->sd); | |
445 continue; | |
446 } | |
447 chmod(tsd->path, 0777); | |
448 nfds++; | |
449 } | |
450 if (!nfds) { | |
960 | 451 error_msg("Can't open single socket for listenning."); |
452 goto clean_and_exit; | |
453 } | |
1021 | 454 |
455 // Setup signals | |
456 if (pipe(TT.sigfd) < 0) error_exit("pipe failed\n"); | |
457 | |
458 fcntl(TT.sigfd[1] , F_SETFD, FD_CLOEXEC); | |
459 fcntl(TT.sigfd[0] , F_SETFD, FD_CLOEXEC); | |
460 int flags = fcntl(TT.sigfd[1], F_GETFL); | |
461 fcntl(TT.sigfd[1], F_SETFL, flags | O_NONBLOCK); | |
462 signal(SIGHUP, signal_handler); | |
463 signal(SIGTERM, signal_handler); | |
464 signal(SIGINT, signal_handler); | |
465 signal(SIGQUIT, signal_handler); | |
466 | |
960 | 467 if (parse_config_file() == -1) goto clean_and_exit; |
468 open_logfiles(); | |
1024 | 469 if (!(toys.optflags & FLAG_n)) { |
1028 | 470 daemonize(); |
995
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
471 //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
|
472 toys.optflags |= FLAG_n; |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
960
diff
changeset
|
473 } |
1028 | 474 xpidfile("syslogd"); |
960 | 475 |
476 logmsg("<46>Toybox: syslogd started", 27); //27 : the length of message | |
477 for (;;) { | |
1028 | 478 // Add opened socks to rfds for select() |
479 FD_ZERO(&rfds); | |
480 for (tsd = TT.lsocks; tsd; tsd = tsd->next) FD_SET(tsd->sd, &rfds); | |
481 FD_SET(TT.sigfd[0], &rfds); | |
960 | 482 tv.tv_usec = 0; |
483 tv.tv_sec = TT.interval*60; | |
484 | |
1028 | 485 retval = select(TT.sigfd[0] + 1, &rfds, NULL, NULL, (TT.interval)?&tv:NULL); |
486 if (retval < 0) { | |
487 if (errno != EINTR) perror_msg("Error in select "); | |
960 | 488 } |
1028 | 489 else if (!retval) logmsg("<46>-- MARK --", 14); |
490 else if (FD_ISSET(TT.sigfd[0], &rfds)) { /* May be a signal */ | |
960 | 491 unsigned char sig; |
492 | |
1021 | 493 if (read(TT.sigfd[0], &sig, 1) != 1) { |
960 | 494 error_msg("signal read failed.\n"); |
495 continue; | |
496 } | |
497 switch(sig) { | |
498 case SIGTERM: /* FALLTHROUGH */ | |
499 case SIGINT: /* FALLTHROUGH */ | |
500 case SIGQUIT: | |
501 logmsg("<46>syslogd exiting", 19); | |
502 if (CFG_TOYBOX_FREE ) cleanup(); | |
503 signal(sig, SIG_DFL); | |
504 sigset_t ss; | |
505 sigemptyset(&ss); | |
506 sigaddset(&ss, sig); | |
507 sigprocmask(SIG_UNBLOCK, &ss, NULL); | |
508 raise(sig); | |
509 _exit(1); /* Should not reach it */ | |
510 break; | |
511 case SIGHUP: | |
512 logmsg("<46>syslogd exiting", 19); | |
513 cleanup(); //cleanup is done, as we restart syslog. | |
514 goto init_jumpin; | |
515 default: break; | |
516 } | |
1028 | 517 } else { /* Some activity on listen sockets. */ |
1026
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
518 for (tsd = TT.lsocks; tsd; tsd = tsd->next) { |
09cc81f6e411
syslogd: stop abusing arg_list
Felix Janda <felix.janda at posteo.de>
parents:
1025
diff
changeset
|
519 int sd = tsd->sd; |
1028 | 520 if (FD_ISSET(sd, &rfds)) { |
960 | 521 int len = read(sd, buffer, 1023); //buffer is of 1K, hence readingonly 1023 bytes, 1 for NUL |
522 if (len > 0) { | |
523 buffer[len] = '\0'; | |
1024 | 524 if((toys.optflags & FLAG_D) && (len == last_len)) |
960 | 525 if (!memcmp(last_buf, buffer, len)) break; |
526 | |
527 memcpy(last_buf, buffer, len); | |
528 last_len = len; | |
529 logmsg(buffer, len); | |
530 } | |
531 break; | |
532 } | |
533 } | |
534 } | |
535 } | |
536 clean_and_exit: | |
537 logmsg("<46>syslogd exiting", 19); | |
538 if (CFG_TOYBOX_FREE ) cleanup(); | |
539 } |