]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
journal: replace linked list by hashmap when merging files
[thirdparty/systemd.git] / src / journal / journald.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/epoll.h>
23 #include <sys/socket.h>
24 #include <errno.h>
25 #include <sys/signalfd.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "hashmap.h"
30 #include "journal-private.h"
31 #include "sd-daemon.h"
32 #include "socket-util.h"
33
34 typedef struct Server {
35 int syslog_fd;
36 int epoll_fd;
37 int signal_fd;
38
39 JournalFile *system_journal;
40 Hashmap *user_journals;
41 } Server;
42
43 static void process_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
44 char *message = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
45 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
46 *comm = NULL, *cmdline = NULL, *hostname = NULL,
47 *audit_session = NULL, *audit_loginuid = NULL,
48 *syslog_priority = NULL, *syslog_facility = NULL,
49 *exe = NULL;
50 dual_timestamp ts;
51 struct iovec iovec[15];
52 unsigned n = 0;
53 char idbuf[33];
54 sd_id128_t id;
55 int r;
56 char *t;
57 int priority = LOG_USER | LOG_INFO;
58
59 dual_timestamp_get(&ts);
60
61 parse_syslog_priority((char**) &buf, &priority);
62 skip_syslog_date((char**) &buf);
63
64 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
65 IOVEC_SET_STRING(iovec[n++], syslog_priority);
66
67 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
68 IOVEC_SET_STRING(iovec[n++], syslog_facility);
69
70 message = strappend("MESSAGE=", buf);
71 if (message)
72 IOVEC_SET_STRING(iovec[n++], message);
73
74 if (ucred) {
75 uint32_t session;
76 uid_t loginuid;
77
78 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
79 IOVEC_SET_STRING(iovec[n++], pid);
80
81 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
82 IOVEC_SET_STRING(iovec[n++], uid);
83
84 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
85 IOVEC_SET_STRING(iovec[n++], gid);
86
87 r = get_process_comm(ucred->pid, &t);
88 if (r >= 0) {
89 comm = strappend("COMM=", t);
90 if (comm)
91 IOVEC_SET_STRING(iovec[n++], comm);
92 free(t);
93 }
94
95 r = get_process_exe(ucred->pid, &t);
96 if (r >= 0) {
97 exe = strappend("EXE=", t);
98 if (comm)
99 IOVEC_SET_STRING(iovec[n++], exe);
100 free(t);
101 }
102
103 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
104 if (r >= 0) {
105 cmdline = strappend("CMDLINE=", t);
106 if (cmdline)
107 IOVEC_SET_STRING(iovec[n++], cmdline);
108 free(t);
109 }
110
111 r = audit_session_from_pid(ucred->pid, &session);
112 if (r >= 0)
113 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
114 IOVEC_SET_STRING(iovec[n++], audit_session);
115
116 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
117 if (r >= 0)
118 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
119 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
120 }
121
122 if (tv) {
123 if (asprintf(&source_time, "SOURCE_REALTIME_TIMESTAMP=%llu",
124 (unsigned long long) timeval_load(tv)) >= 0)
125 IOVEC_SET_STRING(iovec[n++], source_time);
126 }
127
128 r = sd_id128_get_boot(&id);
129 if (r >= 0)
130 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
131 IOVEC_SET_STRING(iovec[n++], boot_id);
132
133 r = sd_id128_get_machine(&id);
134 if (r >= 0)
135 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
136 IOVEC_SET_STRING(iovec[n++], machine_id);
137
138 t = gethostname_malloc();
139 if (t) {
140 hostname = strappend("HOSTNAME=", t);
141 if (hostname)
142 IOVEC_SET_STRING(iovec[n++], hostname);
143 free(t);
144 }
145
146 r = journal_file_append_entry(s->system_journal, &ts, iovec, n, NULL, NULL);
147 if (r < 0)
148 log_error("Failed to write entry: %s", strerror(-r));
149
150
151 free(message);
152 free(pid);
153 free(uid);
154 free(gid);
155 free(comm);
156 free(cmdline);
157 free(source_time);
158 free(boot_id);
159 free(machine_id);
160 free(hostname);
161 free(audit_session);
162 free(audit_loginuid);
163 free(syslog_facility);
164 free(syslog_priority);
165 }
166
167 static int process_event(Server *s, struct epoll_event *ev) {
168 assert(s);
169
170 if (ev->events != EPOLLIN) {
171 log_info("Got invalid event from epoll.");
172 return -EIO;
173 }
174
175 if (ev->data.fd == s->signal_fd) {
176 struct signalfd_siginfo sfsi;
177 ssize_t n;
178
179 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
180 if (n != sizeof(sfsi)) {
181
182 if (n >= 0)
183 return -EIO;
184
185 if (errno == EINTR || errno == EAGAIN)
186 return 0;
187
188 return -errno;
189 }
190
191 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
192 return 0;
193
194 } else {
195 for (;;) {
196 char buf[LINE_MAX+1];
197 struct msghdr msghdr;
198 struct iovec iovec;
199 struct ucred *ucred = NULL;
200 struct timeval *tv = NULL;
201 struct cmsghdr *cmsg;
202 union {
203 struct cmsghdr cmsghdr;
204 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
205 CMSG_SPACE(sizeof(struct timeval))];
206 } control;
207 ssize_t n;
208 char *e;
209
210 zero(iovec);
211 iovec.iov_base = buf;
212 iovec.iov_len = sizeof(buf)-1;
213
214 zero(control);
215 zero(msghdr);
216 msghdr.msg_iov = &iovec;
217 msghdr.msg_iovlen = 1;
218 msghdr.msg_control = &control;
219 msghdr.msg_controllen = sizeof(control);
220
221 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
222 if (n < 0) {
223
224 if (errno == EINTR || errno == EAGAIN)
225 return 1;
226
227 log_error("recvmsg() failed: %m");
228 return -errno;
229 }
230
231 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
232
233 if (cmsg->cmsg_level == SOL_SOCKET &&
234 cmsg->cmsg_type == SCM_CREDENTIALS &&
235 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
236 ucred = (struct ucred*) CMSG_DATA(cmsg);
237 else if (cmsg->cmsg_level == SOL_SOCKET &&
238 cmsg->cmsg_type == SO_TIMESTAMP &&
239 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
240 tv = (struct timeval*) CMSG_DATA(cmsg);
241 }
242
243 e = memchr(buf, '\n', n);
244 if (e)
245 *e = 0;
246 else
247 buf[n] = 0;
248
249 process_message(s, strstrip(buf), ucred, tv);
250 }
251 }
252
253 return 1;
254 }
255
256
257 static int open_system_journal(JournalFile **f) {
258 int r;
259
260 r = journal_file_open("/var/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
261 if (r == -ENOENT) {
262 mkdir_p("/run/log/journal", 0755);
263
264 r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
265 }
266
267 return r;
268 }
269
270 static int server_init(Server *s) {
271 int n, one, r;
272 struct epoll_event ev;
273 sigset_t mask;
274
275 assert(s);
276
277 zero(*s);
278 s->syslog_fd = s->signal_fd = -1;
279
280 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
281 if (s->epoll_fd < 0) {
282 log_error("Failed to create epoll object: %m");
283 return -errno;
284 }
285
286 n = sd_listen_fds(true);
287 if (n < 0) {
288 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
289 return n;
290 }
291
292 if (n > 1) {
293 log_error("Too many file descriptors passed.");
294 return -EINVAL;
295 }
296
297 if (n == 1)
298 s->syslog_fd = SD_LISTEN_FDS_START;
299 else {
300 union sockaddr_union sa;
301
302 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
303 if (s->syslog_fd < 0) {
304 log_error("socket() failed: %m");
305 return -errno;
306 }
307
308 zero(sa);
309 sa.un.sun_family = AF_UNIX;
310 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
311
312 unlink(sa.un.sun_path);
313
314 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
315 if (r < 0) {
316 log_error("bind() failed: %m");
317 return -errno;
318 }
319
320 chmod(sa.un.sun_path, 0666);
321 }
322
323 one = 1;
324 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
325 if (r < 0) {
326 log_error("SO_PASSCRED failed: %m");
327 return -errno;
328 }
329
330 one = 1;
331 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
332 if (r < 0) {
333 log_error("SO_TIMESTAMP failed: %m");
334 return -errno;
335 }
336
337 zero(ev);
338 ev.events = EPOLLIN;
339 ev.data.fd = s->syslog_fd;
340 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
341 log_error("Failed to add server fd to epoll object: %m");
342 return -errno;
343 }
344
345 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
346 if (!s->user_journals) {
347 log_error("Out of memory.");
348 return -ENOMEM;
349 }
350
351 r = open_system_journal(&s->system_journal);
352 if (r < 0) {
353 log_error("Failed to open journal: %s", strerror(-r));
354 return r;
355 }
356
357 assert_se(sigemptyset(&mask) == 0);
358 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
359 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
360
361 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
362 if (s->signal_fd < 0) {
363 log_error("signalfd(): %m");
364 return -errno;
365 }
366
367 zero(ev);
368 ev.events = EPOLLIN;
369 ev.data.fd = s->signal_fd;
370
371 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
372 log_error("epoll_ctl(): %m");
373 return -errno;
374 }
375
376 return 0;
377 }
378
379 static void server_done(Server *s) {
380 JournalFile *f;
381 assert(s);
382
383 if (s->system_journal)
384 journal_file_close(s->system_journal);
385
386 while ((f = hashmap_steal_first(s->user_journals)))
387 journal_file_close(f);
388
389 hashmap_free(s->user_journals);
390
391 if (s->epoll_fd >= 0)
392 close_nointr_nofail(s->epoll_fd);
393
394 if (s->signal_fd >= 0)
395 close_nointr_nofail(s->signal_fd);
396
397 if (s->syslog_fd >= 0)
398 close_nointr_nofail(s->syslog_fd);
399 }
400
401 int main(int argc, char *argv[]) {
402 Server server;
403 int r;
404
405 /* if (getppid() != 1) { */
406 /* log_error("This program should be invoked by init only."); */
407 /* return EXIT_FAILURE; */
408 /* } */
409
410 if (argc > 1) {
411 log_error("This program does not take arguments.");
412 return EXIT_FAILURE;
413 }
414
415 log_set_target(LOG_TARGET_AUTO);
416 log_parse_environment();
417 log_open();
418
419 umask(0022);
420
421 r = server_init(&server);
422 if (r < 0)
423 goto finish;
424
425 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
426
427 sd_notify(false,
428 "READY=1\n"
429 "STATUS=Processing messages...");
430
431 for (;;) {
432 struct epoll_event event;
433
434 r = epoll_wait(server.epoll_fd, &event, 1, -1);
435 if (r < 0) {
436
437 if (errno == EINTR)
438 continue;
439
440 log_error("epoll_wait() failed: %m");
441 r = -errno;
442 goto finish;
443 } else if (r == 0)
444 break;
445
446 r = process_event(&server, &event);
447 if (r < 0)
448 goto finish;
449 else if (r == 0)
450 break;
451 }
452
453 finish:
454 sd_notify(false,
455 "STATUS=Shutting down...");
456
457 server_done(&server);
458
459 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
460 }