annotate toys/pending/syslogd.c @ 1026:09cc81f6e411 draft

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