source: code/logger.c

Last change on this file was 4a74b5b, checked in by Izuru Yakumo <eternal-servant@…>, 7 weeks ago

Nitori Engineering

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