1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
22 #include <sys/epoll.h>
23 #include <sys/socket.h>
25 #include <sys/signalfd.h>
30 #include "journal-private.h"
31 #include "sd-daemon.h"
32 #include "socket-util.h"
34 typedef struct Server {
39 JournalFile *system_journal;
40 Hashmap *user_journals;
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,
51 struct iovec iovec[15];
57 int priority = LOG_USER | LOG_INFO;
59 dual_timestamp_get(&ts);
61 parse_syslog_priority((char**) &buf, &priority);
62 skip_syslog_date((char**) &buf);
64 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
65 IOVEC_SET_STRING(iovec[n++], syslog_priority);
67 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
68 IOVEC_SET_STRING(iovec[n++], syslog_facility);
70 message = strappend("MESSAGE=", buf);
72 IOVEC_SET_STRING(iovec[n++], message);
78 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
79 IOVEC_SET_STRING(iovec[n++], pid);
81 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
82 IOVEC_SET_STRING(iovec[n++], uid);
84 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
85 IOVEC_SET_STRING(iovec[n++], gid);
87 r = get_process_comm(ucred->pid, &t);
89 comm = strappend("COMM=", t);
91 IOVEC_SET_STRING(iovec[n++], comm);
95 r = get_process_exe(ucred->pid, &t);
97 exe = strappend("EXE=", t);
99 IOVEC_SET_STRING(iovec[n++], exe);
103 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
105 cmdline = strappend("CMDLINE=", t);
107 IOVEC_SET_STRING(iovec[n++], cmdline);
111 r = audit_session_from_pid(ucred->pid, &session);
113 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
114 IOVEC_SET_STRING(iovec[n++], audit_session);
116 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
118 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
119 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
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);
128 r = sd_id128_get_boot(&id);
130 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
131 IOVEC_SET_STRING(iovec[n++], boot_id);
133 r = sd_id128_get_machine(&id);
135 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
136 IOVEC_SET_STRING(iovec[n++], machine_id);
138 t = gethostname_malloc();
140 hostname = strappend("HOSTNAME=", t);
142 IOVEC_SET_STRING(iovec[n++], hostname);
146 r = journal_file_append_entry(s->system_journal, &ts, iovec, n, NULL, NULL);
148 log_error("Failed to write entry: %s", strerror(-r));
162 free(audit_loginuid);
163 free(syslog_facility);
164 free(syslog_priority);
167 static int process_event(Server *s, struct epoll_event *ev) {
170 if (ev->events != EPOLLIN) {
171 log_info("Got invalid event from epoll.");
175 if (ev->data.fd == s->signal_fd) {
176 struct signalfd_siginfo sfsi;
179 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
180 if (n != sizeof(sfsi)) {
185 if (errno == EINTR || errno == EAGAIN)
191 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
196 char buf[LINE_MAX+1];
197 struct msghdr msghdr;
199 struct ucred *ucred = NULL;
200 struct timeval *tv = NULL;
201 struct cmsghdr *cmsg;
203 struct cmsghdr cmsghdr;
204 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
205 CMSG_SPACE(sizeof(struct timeval))];
211 iovec.iov_base = buf;
212 iovec.iov_len = sizeof(buf)-1;
216 msghdr.msg_iov = &iovec;
217 msghdr.msg_iovlen = 1;
218 msghdr.msg_control = &control;
219 msghdr.msg_controllen = sizeof(control);
221 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
224 if (errno == EINTR || errno == EAGAIN)
227 log_error("recvmsg() failed: %m");
231 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
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);
243 e = memchr(buf, '\n', n);
249 process_message(s, strstrip(buf), ucred, tv);
257 static int open_system_journal(JournalFile **f) {
260 r = journal_file_open("/var/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
262 mkdir_p("/run/log/journal", 0755);
264 r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0644, f);
270 static int server_init(Server *s) {
272 struct epoll_event ev;
278 s->syslog_fd = s->signal_fd = -1;
280 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
281 if (s->epoll_fd < 0) {
282 log_error("Failed to create epoll object: %m");
286 n = sd_listen_fds(true);
288 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
293 log_error("Too many file descriptors passed.");
298 s->syslog_fd = SD_LISTEN_FDS_START;
300 union sockaddr_union sa;
302 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
303 if (s->syslog_fd < 0) {
304 log_error("socket() failed: %m");
309 sa.un.sun_family = AF_UNIX;
310 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
312 unlink(sa.un.sun_path);
314 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
316 log_error("bind() failed: %m");
320 chmod(sa.un.sun_path, 0666);
324 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
326 log_error("SO_PASSCRED failed: %m");
331 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
333 log_error("SO_TIMESTAMP failed: %m");
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");
345 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
346 if (!s->user_journals) {
347 log_error("Out of memory.");
351 r = open_system_journal(&s->system_journal);
353 log_error("Failed to open journal: %s", strerror(-r));
357 assert_se(sigemptyset(&mask) == 0);
358 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
359 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
361 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
362 if (s->signal_fd < 0) {
363 log_error("signalfd(): %m");
369 ev.data.fd = s->signal_fd;
371 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
372 log_error("epoll_ctl(): %m");
379 static void server_done(Server *s) {
383 if (s->system_journal)
384 journal_file_close(s->system_journal);
386 while ((f = hashmap_steal_first(s->user_journals)))
387 journal_file_close(f);
389 hashmap_free(s->user_journals);
391 if (s->epoll_fd >= 0)
392 close_nointr_nofail(s->epoll_fd);
394 if (s->signal_fd >= 0)
395 close_nointr_nofail(s->signal_fd);
397 if (s->syslog_fd >= 0)
398 close_nointr_nofail(s->syslog_fd);
401 int main(int argc, char *argv[]) {
405 /* if (getppid() != 1) { */
406 /* log_error("This program should be invoked by init only."); */
407 /* return EXIT_FAILURE; */
411 log_error("This program does not take arguments.");
415 log_set_target(LOG_TARGET_AUTO);
416 log_parse_environment();
421 r = server_init(&server);
425 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
429 "STATUS=Processing messages...");
432 struct epoll_event event;
434 r = epoll_wait(server.epoll_fd, &event, 1, -1);
440 log_error("epoll_wait() failed: %m");
446 r = process_event(&server, &event);
455 "STATUS=Shutting down...");
457 server_done(&server);
459 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;