source: code/logger.c@ e13307d

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

Rewrite logger_dispatch to use bufferevents to simplify error processing

This also ensures that we're dealing with partial messages correctly and
don't get out of sync with the main process.

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