source: code/logger.c@ 1bcb666

Last change on this file since 1bcb666 was fe81e9a, checked in by Mike Belopuhov <mike@…>, 11 years ago

Sprinkle some func into the syslog messages

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2014 Mike Belopuhov
3 * Copyright (c) 2009 Michael Shalayeff
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
14 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/uio.h>
24#include <errno.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <syslog.h>
30#include <sysexits.h>
31#include <time.h>
32#include <login_cap.h>
33#include <event.h>
34#include <pwd.h>
35
36#include "icb.h"
37#include "icbd.h"
38
39void logger_ioerr(struct bufferevent *, short, void *);
40void logger_dispatch(struct bufferevent *, void *);
41FILE *logger_open(char *);
42void logger_tick(void);
43
44struct icbd_logentry {
45 char group[ICB_MAXGRPLEN];
46 char nick[ICB_MAXNICKLEN];
47 size_t length;
48};
49
50struct {
51 char group[ICB_MAXGRPLEN];
52 FILE *fp;
53} logfiles[10];
54int nlogfiles;
55
56int logger_pipe;
57
58char file_ts[sizeof "0000-00"];
59char line_ts[sizeof "00:00"];
60struct event ev_tick;
61
62extern char logprefix[MAXPATHLEN/2];
63extern int dologging;
64
65int
66logger_init(void)
67{
68 struct bufferevent *bev;
69 struct passwd *pw;
70 int pipes[2];
71
72 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
73 syslog(LOG_ERR, "%s: socketpair: %m", __func__);
74 exit(EX_OSERR);
75 }
76
77 switch (fork()) {
78 case -1:
79 syslog(LOG_ERR, "%s: fork: %m", __func__);
80 exit(EX_OSERR);
81 case 0:
82 break;
83
84 default:
85 close(pipes[1]);
86 logger_pipe = pipes[0];
87 return (0);
88 }
89
90 setproctitle("logger");
91 close(pipes[0]);
92
93 if ((pw = getpwnam(ICBD_USER)) == NULL) {
94 syslog(LOG_ERR, "%s: No passwd entry for %s", __func__,
95 ICBD_USER);
96 exit(EX_NOUSER);
97 }
98
99 if (setusercontext(NULL, pw, pw->pw_uid,
100 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
101 exit(EX_NOPERM);
102
103 if (chroot(pw->pw_dir) < 0) {
104 syslog(LOG_ERR, "%s: %s: %m", __func__, pw->pw_dir);
105 exit(EX_UNAVAILABLE);
106 }
107
108 if (chdir("/") < 0) {
109 syslog(LOG_ERR, "%s: chdir: %m", __func__);
110 exit(EX_UNAVAILABLE);
111 }
112
113 if (setuid(pw->pw_uid) < 0) {
114 syslog(LOG_ERR, "%s: %d: %m", __func__, pw->pw_uid);
115 exit(EX_NOPERM);
116 }
117
118 event_init();
119
120 /* event for message processing */
121 if ((bev = bufferevent_new(pipes[1], logger_dispatch, NULL,
122 logger_ioerr, NULL)) == NULL) {
123 syslog(LOG_ERR, "%s: bufferevent_new: %m", __func__);
124 exit(EX_UNAVAILABLE);
125 }
126 if (bufferevent_enable(bev, EV_READ)) {
127 syslog(LOG_ERR, "%s: bufferevent_enable: %m", __func__);
128 bufferevent_free(bev);
129 exit(EX_UNAVAILABLE);
130 }
131
132 evtimer_set(&ev_tick, (void (*)(int, short, void *))logger_tick, NULL);
133 logger_tick();
134 return event_dispatch();
135}
136
137void
138logger_ioerr(struct bufferevent *bev __attribute__((__unused__)), short what,
139 void *arg __attribute__((__unused__)))
140{
141 const char *cause = NULL;
142
143 if (what & EVBUFFER_TIMEOUT)
144 cause = "timeout";
145 else if (what & EVBUFFER_EOF)
146 cause = "eof";
147 else if (what & EVBUFFER_ERROR)
148 cause = what & EVBUFFER_READ ? "read" : "write";
149 syslog(LOG_ERR, "%s: %s", __func__, cause ? cause : "unknown");
150 exit(EX_IOERR);
151}
152
153void
154logger_dispatch(struct bufferevent *bev, void *arg __attribute__((unused)))
155{
156 struct icbd_logentry *e;
157 static char buf[sizeof *e + ICB_MSGSIZE];
158 static size_t nread = 0;
159 FILE *fp = NULL;
160 size_t res;
161 char *m;
162 int i;
163
164 e = (struct icbd_logentry *)buf;
165 m = buf + sizeof *e;
166
167 while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
168 if (nread == 0) {
169 bzero(e, sizeof *e);
170 /* read the log entry header */
171 res = bufferevent_read(bev, &buf[0], sizeof *e);
172 nread += res;
173 if (nread < sizeof *e)
174 return;
175 }
176 /* see if we got the whole header */
177 if (nread < sizeof *e) {
178 /* finish reading */
179 res = bufferevent_read(bev, &buf[nread],
180 sizeof *e - nread);
181 nread += res;
182 if (nread < sizeof *e)
183 return;
184 }
185 /* fetch the message */
186 res = bufferevent_read(bev, &buf[nread],
187 MIN(e->length, ICB_MSGSIZE));
188 nread += res;
189#ifdef DEBUG
190 {
191 printf("logger read %lu out of %lu:\n", res, e->length);
192 for (i = 0; i < (int)res; i++)
193 printf(" %02x", (unsigned char)m[i]);
194 printf("\n");
195 }
196#endif
197 if (nread < e->length)
198 return;
199 /* terminate the buffer */
200 m[MIN(nread - sizeof *e, ICB_MSGSIZE - 1)] = '\0';
201 /* find the appropriate log file */
202 for (i = 0; i < nlogfiles; i++)
203 if (strcmp(logfiles[i].group, e->group) == 0)
204 fp = logfiles[i].fp;
205 if (!fp && (fp = logger_open(e->group)) == NULL)
206 return;
207 if (strlen(e->nick) == 0)
208 fprintf(fp, "[%s] %s\n", line_ts, m);
209 else
210 fprintf(fp, "[%s] <%s> %s\n", line_ts, e->nick, m);
211 /* get ready for the next message */
212 nread = 0;
213 }
214}
215
216FILE *
217logger_open(char *group)
218{
219 char path[MAXPATHLEN];
220 FILE *fp = NULL;
221
222 /* make sure not to overflow the logfiles table */
223 if (nlogfiles == nitems(logfiles)) {
224 syslog(LOG_NOTICE, "%s: logfiles table is full", __func__);
225 return (NULL);
226 }
227 snprintf(path, sizeof path, "%s/%s", logprefix, group);
228 if (mkdir(path, 0755) < 0 && errno != EEXIST) {
229 syslog(LOG_ERR, "%s: %s: %m", __func__, group);
230 return (NULL);
231 }
232 snprintf(path, sizeof path, "%s/%s/%s", logprefix, group, file_ts);
233 if ((fp = fopen(path, "a")) == NULL) {
234 syslog(LOG_ERR, "%s: %s: %m", __func__, path);
235 return (NULL);
236 }
237 setvbuf(fp, NULL, _IOLBF, 0);
238 if (verbose)
239 syslog(LOG_NOTICE, "%s: %s", __func__, path);
240 strlcpy(logfiles[nlogfiles].group, group, ICB_MAXGRPLEN);
241 logfiles[nlogfiles++].fp = fp;
242 return (fp);
243}
244
245void
246logger(char *group, char *nick, char *what)
247{
248 struct icbd_logentry e;
249 struct iovec iov[2];
250 const char *defgrp = "1";
251
252 if (!dologging)
253 return;
254
255 if (strcmp(group, defgrp) == 0)
256 return;
257
258 strlcpy(e.group, group, ICB_MAXGRPLEN);
259 strlcpy(e.nick, nick, ICB_MAXNICKLEN);
260 e.length = strlen(what) + 1;
261
262 iov[0].iov_base = &e;
263 iov[0].iov_len = sizeof e;
264
265 iov[1].iov_base = what;
266 iov[1].iov_len = e.length;
267
268 if (writev(logger_pipe, iov, 2) == -1)
269 syslog(LOG_ERR, "%s: %m", __func__);
270}
271
272void
273logger_tick(void)
274{
275 static int last_mon = -1, last_mday = -1;
276 struct timeval tv = { 60, 0 };
277 char buf[128];
278 struct tm *tm;
279 time_t t;
280 int i;
281
282 time(&t);
283 tm = gmtime(&t);
284 if (last_mon != tm->tm_mon) {
285 snprintf(file_ts, sizeof file_ts, "%04d-%02d",
286 tm->tm_year + 1900, tm->tm_mon + 1);
287 last_mon = tm->tm_mon;
288 /* rotate log files */
289 for (i = 0; i < nlogfiles; i++) {
290 fclose(logfiles[i].fp);
291 logfiles[i].fp = NULL;
292 }
293 nlogfiles = 0;
294 }
295 if (tm->tm_mday != last_mday) {
296 strftime(buf, sizeof(buf),
297 "Today is %a %b %e %Y %H:%M %Z (%z)", tm);
298 for (i = 0; i < nlogfiles; i++)
299 fprintf(logfiles[i].fp, "%s\n", buf);
300 last_mday = tm->tm_mday;
301 }
302 snprintf(line_ts, sizeof line_ts, "%02d:%02d", tm->tm_hour,
303 tm->tm_min);
304 if (evtimer_add(&ev_tick, &tv) < 0) {
305 syslog(LOG_ERR, "%s: evtimer_add: %m", __func__);
306 exit (EX_UNAVAILABLE);
307 }
308}
Note: See TracBrowser for help on using the repository browser.