annotate toys/pending/syslogd.c @ 1027:4b0ad1c7af42 draft

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