annotate toys/pending/syslogd.c @ 1024:63b8e54d2c6f

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