annotate toys/pending/syslogd.c @ 960:c0627c163893

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