source: code/logger.c@ 61fdba7

Last change on this file since 61fdba7 was d27dc1e, checked in by Mike Belopuhov <mike@…>, 10 years ago

fixup signal and MAXPATHLEN include fallout

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