diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-12-11 20:13:44 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-12-11 20:55:09 +0100 |
commit | f9a810bedacf1da7c505c1786a2416d592665926 (patch) | |
tree | e4fdb88684e2bacb4567228aabd3448d75fb3ce0 /src/journal | |
parent | 419173e60a05424008fcd312f6df4b59b2ce8e62 (diff) |
journald: port to sd-event and enable watchdog support
Diffstat (limited to 'src/journal')
-rw-r--r-- | src/journal/journald-kmsg.c | 38 | ||||
-rw-r--r-- | src/journal/journald-kmsg.h | 1 | ||||
-rw-r--r-- | src/journal/journald-native.c | 11 | ||||
-rw-r--r-- | src/journal/journald-server.c | 432 | ||||
-rw-r--r-- | src/journal/journald-server.h | 18 | ||||
-rw-r--r-- | src/journal/journald-stream.c | 67 | ||||
-rw-r--r-- | src/journal/journald-stream.h | 2 | ||||
-rw-r--r-- | src/journal/journald-syslog.c | 11 | ||||
-rw-r--r-- | src/journal/journald.c | 27 |
9 files changed, 280 insertions, 327 deletions
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c index af49d8578a..bc42548cd1 100644 --- a/src/journal/journald-kmsg.c +++ b/src/journal/journald-kmsg.c @@ -320,7 +320,7 @@ finish: free(pid); } -int server_read_dev_kmsg(Server *s) { +static int server_read_dev_kmsg(Server *s) { char buffer[8192+1]; /* the kernel-side limit per record is 8K currently */ ssize_t l; @@ -335,7 +335,7 @@ int server_read_dev_kmsg(Server *s) { * return EINVAL when we try. So handle this cleanly, * but don' try to ever read from it again. */ if (errno == EINVAL) { - epoll_ctl(s->epoll_fd, EPOLL_CTL_DEL, s->dev_kmsg_fd, NULL); + s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); return 0; } @@ -375,8 +375,24 @@ int server_flush_dev_kmsg(Server *s) { return 0; } +static int dispatch_dev_kmsg(sd_event_source *es, int fd, uint32_t revents, void *userdata) { + Server *s = userdata; + + assert(es); + assert(fd == s->dev_kmsg_fd); + assert(s); + + if (revents & EPOLLERR) + log_warning("/dev/kmsg buffer overrun, some messages lost."); + + if (!(revents & EPOLLIN)) + log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32, revents); + + return server_read_dev_kmsg(s); +} + int server_open_dev_kmsg(Server *s) { - struct epoll_event ev; + int r; assert(s); @@ -386,17 +402,21 @@ int server_open_dev_kmsg(Server *s) { return 0; } - zero(ev); - ev.events = EPOLLIN; - ev.data.fd = s->dev_kmsg_fd; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->dev_kmsg_fd, &ev) < 0) { + r = sd_event_add_io(s->event, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s, &s->dev_kmsg_event_source); + if (r < 0) { /* This will fail with EPERM on older kernels where * /dev/kmsg is not readable. */ - if (errno == EPERM) + if (r == -EPERM) return 0; - log_error("Failed to add /dev/kmsg fd to epoll object: %m"); + log_error("Failed to add /dev/kmsg fd to event loop: %s", strerror(-r)); + return -errno; + } + + r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10); + if (r < 0) { + log_error("Failed to adjust priority of kmsg event source: %s", strerror(-r)); return -errno; } diff --git a/src/journal/journald-kmsg.h b/src/journal/journald-kmsg.h index f287161329..f60f605572 100644 --- a/src/journal/journald-kmsg.h +++ b/src/journal/journald-kmsg.h @@ -24,7 +24,6 @@ #include "journald-server.h" int server_open_dev_kmsg(Server *s); -int server_read_dev_kmsg(Server *s); int server_flush_dev_kmsg(Server *s); void server_forward_kmsg(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred); diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c index 2c91cba16d..5e19fa3f55 100644 --- a/src/journal/journald-native.c +++ b/src/journal/journald-native.c @@ -369,7 +369,6 @@ void server_process_native_file( int server_open_native_socket(Server*s) { union sockaddr_union sa; int one, r; - struct epoll_event ev; assert(s); @@ -420,12 +419,10 @@ int server_open_native_socket(Server*s) { return -errno; } - zero(ev); - ev.events = EPOLLIN; - ev.data.fd = s->native_fd; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) { - log_error("Failed to add native server fd to epoll object: %m"); - return -errno; + r = sd_event_add_io(s->event, s->native_fd, EPOLLIN, process_datagram, s, &s->native_event_source); + if (r < 0) { + log_error("Failed to add native server fd to event loop: %s", strerror(-r)); + return r; } return 0; diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index fc8115c31c..dd14493db5 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -333,7 +333,6 @@ void server_rotate(Server *s) { } void server_sync(Server *s) { - static const struct itimerspec sync_timer_disable = {}; JournalFile *f; void *k; Iterator i; @@ -351,9 +350,11 @@ void server_sync(Server *s) { log_error("Failed to sync user journal: %s", strerror(-r)); } - r = timerfd_settime(s->sync_timer_fd, 0, &sync_timer_disable, NULL); - if (r < 0) - log_error("Failed to disable max timer: %m"); + if (s->sync_event_source) { + r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_OFF); + if (r < 0) + log_error("Failed to disable sync timer source: %s", strerror(-r)); + } s->sync_scheduled = false; } @@ -1067,235 +1068,157 @@ finish: return r; } -int process_event(Server *s, struct epoll_event *ev) { +int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) { + Server *s = userdata; + assert(s); - assert(ev); + assert(fd == s->native_fd || fd == s->syslog_fd); + + if (revents != EPOLLIN) { + log_error("Got invalid event from epoll for datagram fd: %"PRIx32, revents); + return -EIO; + } + + for (;;) { + struct ucred *ucred = NULL; + struct timeval *tv = NULL; + struct cmsghdr *cmsg; + char *label = NULL; + size_t label_len = 0; + struct iovec iovec; + + union { + struct cmsghdr cmsghdr; + + /* We use NAME_MAX space for the + * SELinux label here. The kernel + * currently enforces no limit, but + * according to suggestions from the + * SELinux people this will change and + * it will probably be identical to + * NAME_MAX. For now we use that, but + * this should be updated one day when + * the final limit is known.*/ + uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) + + CMSG_SPACE(sizeof(struct timeval)) + + CMSG_SPACE(sizeof(int)) + /* fd */ + CMSG_SPACE(NAME_MAX)]; /* selinux label */ + } control = {}; + struct msghdr msghdr = { + .msg_iov = &iovec, + .msg_iovlen = 1, + .msg_control = &control, + .msg_controllen = sizeof(control), + }; - if (ev->data.fd == s->signal_fd) { - struct signalfd_siginfo sfsi; ssize_t n; + int v; + int *fds = NULL; + unsigned n_fds = 0; - if (ev->events != EPOLLIN) { - log_error("Got invalid event from epoll for %s: %"PRIx32, - "signal fd", ev->events); - return -EIO; + if (ioctl(fd, SIOCINQ, &v) < 0) { + log_error("SIOCINQ failed: %m"); + return -errno; } - n = read(s->signal_fd, &sfsi, sizeof(sfsi)); - if (n != sizeof(sfsi)) { + if (!GREEDY_REALLOC(s->buffer, s->buffer_size, LINE_MAX + (size_t) v)) + return log_oom(); - if (n >= 0) - return -EIO; + iovec.iov_base = s->buffer; + iovec.iov_len = s->buffer_size; + n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); + if (n < 0) { if (errno == EINTR || errno == EAGAIN) - return 1; + return 0; + log_error("recvmsg() failed: %m"); return -errno; } - if (sfsi.ssi_signo == SIGUSR1) { - log_info("Received request to flush runtime journal from PID %"PRIu32, - sfsi.ssi_pid); - touch("/run/systemd/journal/flushed"); - server_flush_to_var(s); - server_sync(s); - return 1; - } - - if (sfsi.ssi_signo == SIGUSR2) { - log_info("Received request to rotate journal from PID %"PRIu32, - sfsi.ssi_pid); - server_rotate(s); - server_vacuum(s); - return 1; + for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { + + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_CREDENTIALS && + cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) + ucred = (struct ucred*) CMSG_DATA(cmsg); + else if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_SECURITY) { + label = (char*) CMSG_DATA(cmsg); + label_len = cmsg->cmsg_len - CMSG_LEN(0); + } else if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SO_TIMESTAMP && + cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) + tv = (struct timeval*) CMSG_DATA(cmsg); + else if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_RIGHTS) { + fds = (int*) CMSG_DATA(cmsg); + n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); + } } - log_info("Received SIG%s", signal_to_string(sfsi.ssi_signo)); - - return 0; + if (fd == s->syslog_fd) { + if (n > 0 && n_fds == 0) { + s->buffer[n] = 0; + server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len); + } else if (n_fds > 0) + log_warning("Got file descriptors via syslog socket. Ignoring."); - } else if (ev->data.fd == s->sync_timer_fd) { - int r; - uint64_t t; - - log_debug("Got sync request from epoll."); - - r = read(ev->data.fd, (void *)&t, sizeof(t)); - if (r < 0) - return 0; - - server_sync(s); - return 1; - - } else if (ev->data.fd == s->dev_kmsg_fd) { - int r; - - if (ev->events & EPOLLERR) - log_warning("/dev/kmsg buffer overrun, some messages lost."); - - if (!(ev->events & EPOLLIN)) { - log_error("Got invalid event from epoll for %s: %"PRIx32, - "/dev/kmsg", ev->events); - return -EIO; - } - - r = server_read_dev_kmsg(s); - if (r < 0) - return r; - - return 1; - - } else if (ev->data.fd == s->native_fd || - ev->data.fd == s->syslog_fd) { - - if (ev->events != EPOLLIN) { - log_error("Got invalid event from epoll for %s: %"PRIx32, - ev->data.fd == s->native_fd ? "native fd" : "syslog fd", - ev->events); - return -EIO; + } else { + if (n > 0 && n_fds == 0) + server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len); + else if (n == 0 && n_fds == 1) + server_process_native_file(s, fds[0], ucred, tv, label, label_len); + else if (n_fds > 0) + log_warning("Got too many file descriptors via native socket. Ignoring."); } - for (;;) { - struct ucred *ucred = NULL; - struct timeval *tv = NULL; - struct cmsghdr *cmsg; - char *label = NULL; - size_t label_len = 0; - - struct iovec iovec; - union { - struct cmsghdr cmsghdr; - - /* We use NAME_MAX space for the - * SELinux label here. The kernel - * currently enforces no limit, but - * according to suggestions from the - * SELinux people this will change and - * it will probably be identical to - * NAME_MAX. For now we use that, but - * this should be updated one day when - * the final limit is known.*/ - uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) + - CMSG_SPACE(sizeof(struct timeval)) + - CMSG_SPACE(sizeof(int)) + /* fd */ - CMSG_SPACE(NAME_MAX)]; /* selinux label */ - } control = {}; - struct msghdr msghdr = { - .msg_iov = &iovec, - .msg_iovlen = 1, - .msg_control = &control, - .msg_controllen = sizeof(control), - }; - - ssize_t n; - int v; - int *fds = NULL; - unsigned n_fds = 0; - - if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) { - log_error("SIOCINQ failed: %m"); - return -errno; - } - - if (!GREEDY_REALLOC(s->buffer, s->buffer_size, LINE_MAX + (size_t) v)) - return log_oom(); - - iovec.iov_base = s->buffer; - iovec.iov_len = s->buffer_size; - - n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); - if (n < 0) { - if (errno == EINTR || errno == EAGAIN) - return 1; - - log_error("recvmsg() failed: %m"); - return -errno; - } - - for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { - - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_CREDENTIALS && - cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) - ucred = (struct ucred*) CMSG_DATA(cmsg); - else if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_SECURITY) { - label = (char*) CMSG_DATA(cmsg); - label_len = cmsg->cmsg_len - CMSG_LEN(0); - } else if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SO_TIMESTAMP && - cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval))) - tv = (struct timeval*) CMSG_DATA(cmsg); - else if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_RIGHTS) { - fds = (int*) CMSG_DATA(cmsg); - n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); - } - } + close_many(fds, n_fds); + } - if (ev->data.fd == s->syslog_fd) { - if (n > 0 && n_fds == 0) { - s->buffer[n] = 0; - server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len); - } else if (n_fds > 0) - log_warning("Got file descriptors via syslog socket. Ignoring."); + return 0; +} - } else { - if (n > 0 && n_fds == 0) - server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len); - else if (n == 0 && n_fds == 1) - server_process_native_file(s, fds[0], ucred, tv, label, label_len); - else if (n_fds > 0) - log_warning("Got too many file descriptors via native socket. Ignoring."); - } +static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { + Server *s = userdata; - close_many(fds, n_fds); - } + assert(s); - return 1; + log_info("Received request to flush runtime journal from PID %"PRIu32, si->ssi_pid); - } else if (ev->data.fd == s->stdout_fd) { + touch("/run/systemd/journal/flushed"); + server_flush_to_var(s); + server_sync(s); - if (ev->events != EPOLLIN) { - log_error("Got invalid event from epoll for %s: %"PRIx32, - "stdout fd", ev->events); - return -EIO; - } + return 0; +} - stdout_stream_new(s); - return 1; +static int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { + Server *s = userdata; - } else { - StdoutStream *stream; + assert(s); - if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) { - log_error("Got invalid event from epoll for %s: %"PRIx32, - "stdout stream", ev->events); - return -EIO; - } + log_info("Received request to rotate journal from PID %"PRIu32, si->ssi_pid); + server_rotate(s); + server_vacuum(s); - /* If it is none of the well-known fds, it must be an - * stdout stream fd. Note that this is a bit ugly here - * (since we rely that none of the well-known fds - * could be interpreted as pointer), but nonetheless - * safe, since the well-known fds would never get an - * fd > 4096, i.e. beyond the first memory page */ + return 0; +} - stream = ev->data.ptr; +static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) { + Server *s = userdata; - if (stdout_stream_process(stream) <= 0) - stdout_stream_free(stream); + assert(s); - return 1; - } + log_info("Received SIG%s", signal_to_string(si->ssi_signo)); - log_error("Unknown event."); + sd_event_request_quit(s->event); return 0; } -static int open_signalfd(Server *s) { +static int setup_signals(Server *s) { sigset_t mask; - struct epoll_event ev; + int r; assert(s); @@ -1303,20 +1226,21 @@ static int open_signalfd(Server *s) { sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1); assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); - s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); - if (s->signal_fd < 0) { - log_error("signalfd(): %m"); - return -errno; - } + r = sd_event_add_signal(s->event, SIGUSR1, dispatch_sigusr1, s, &s->sigusr1_event_source); + if (r < 0) + return r; + + r = sd_event_add_signal(s->event, SIGUSR2, dispatch_sigusr2, s, &s->sigusr2_event_source); + if (r < 0) + return r; - zero(ev); - ev.events = EPOLLIN; - ev.data.fd = s->signal_fd; + r = sd_event_add_signal(s->event, SIGTERM, dispatch_sigterm, s, &s->sigterm_event_source); + if (r < 0) + return r; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) { - log_error("epoll_ctl(): %m"); - return -errno; - } + r = sd_event_add_signal(s->event, SIGINT, dispatch_sigterm, s, &s->sigint_event_source); + if (r < 0) + return r; return 0; } @@ -1389,26 +1313,12 @@ static int server_parse_config_file(Server *s) { return r; } -static int server_open_sync_timer(Server *s) { - int r; - struct epoll_event ev; +static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) { + Server *s = userdata; assert(s); - s->sync_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); - if (s->sync_timer_fd < 0) - return -errno; - - zero(ev); - ev.events = EPOLLIN; - ev.data.fd = s->sync_timer_fd; - - r = epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->sync_timer_fd, &ev); - if (r < 0) { - log_error("Failed to add idle timer fd to epoll object: %m"); - return -errno; - } - + server_sync(s); return 0; } @@ -1426,17 +1336,33 @@ int server_schedule_sync(Server *s, int priority) { if (s->sync_scheduled) return 0; - if (s->sync_interval_usec) { - struct itimerspec sync_timer_enable = {}; + if (s->sync_interval_usec > 0) { + usec_t when; - timespec_store(&sync_timer_enable.it_value, s->sync_interval_usec); + r = sd_event_get_now_monotonic(s->event, &when); + if (r < 0) + return r; - r = timerfd_settime(s->sync_timer_fd, 0, &sync_timer_enable, NULL); + when += s->sync_interval_usec; + + if (!s->sync_event_source) { + r = sd_event_add_monotonic(s->event, when, 0, server_dispatch_sync, s, &s->sync_event_source); + if (r < 0) + return r; + + r = sd_event_source_set_priority(s->sync_event_source, SD_EVENT_PRIORITY_IMPORTANT); + } else { + r = sd_event_source_set_time(s->sync_event_source, when); + if (r < 0) + return r; + + r = sd_event_source_set_enabled(s->sync_event_source, SD_EVENT_ONESHOT); + } if (r < 0) - return -errno; - } + return r; - s->sync_scheduled = true; + s->sync_scheduled = true; + } return 0; } @@ -1447,8 +1373,7 @@ int server_init(Server *s) { assert(s); zero(*s); - s->sync_timer_fd = s->syslog_fd = s->native_fd = s->stdout_fd = - s->signal_fd = s->epoll_fd = s->dev_kmsg_fd = -1; + s->syslog_fd = s->native_fd = s->stdout_fd = s->dev_kmsg_fd = -1; s->compress = true; s->seal = true; @@ -1487,12 +1412,14 @@ int server_init(Server *s) { if (!s->mmap) return log_oom(); - s->epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if (s->epoll_fd < 0) { - log_error("Failed to create epoll object: %m"); - return -errno; + r = sd_event_default(&s->event); + if (r < 0) { + log_error("Failed to create event loop: %s", strerror(-r)); + return r; } + sd_event_set_watchdog(s->event, true); + n = sd_listen_fds(true); if (n < 0) { log_error("Failed to read listening file descriptors from environment: %s", strerror(-n)); @@ -1554,11 +1481,7 @@ int server_init(Server *s) { if (r < 0) return r; - r = server_open_sync_timer(s); - if (r < 0) - return r; - - r = open_signalfd(s); + r = setup_signals(s); if (r < 0) return r; @@ -1566,8 +1489,7 @@ int server_init(Server *s) { if (!s->udev) return -ENOMEM; - s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, - s->rate_limit_burst); + s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst); if (!s->rate_limit) return -ENOMEM; @@ -1612,11 +1534,16 @@ void server_done(Server *s) { hashmap_free(s->user_journals); - if (s->epoll_fd >= 0) - close_nointr_nofail(s->epoll_fd); - - if (s->signal_fd >= 0) - close_nointr_nofail(s->signal_fd); + sd_event_source_unref(s->syslog_event_source); + sd_event_source_unref(s->native_event_source); + sd_event_source_unref(s->stdout_event_source); + sd_event_source_unref(s->dev_kmsg_event_source); + sd_event_source_unref(s->sync_event_source); + sd_event_source_unref(s->sigusr1_event_source); + sd_event_source_unref(s->sigusr2_event_source); + sd_event_source_unref(s->sigterm_event_source); + sd_event_source_unref(s->sigint_event_source); + sd_event_unref(s->event); if (s->syslog_fd >= 0) close_nointr_nofail(s->syslog_fd); @@ -1630,9 +1557,6 @@ void server_done(Server *s) { if (s->dev_kmsg_fd >= 0) close_nointr_nofail(s->dev_kmsg_fd); - if (s->sync_timer_fd >= 0) - close_nointr_nofail(s->sync_timer_fd); - if (s->rate_limit) journal_rate_limit_free(s->rate_limit); diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h index db577b8913..2c3e2c239d 100644 --- a/src/journal/journald-server.h +++ b/src/journal/journald-server.h @@ -27,6 +27,7 @@ #include <sys/types.h> #include <sys/socket.h> +#include "sd-event.h" #include "journal-file.h" #include "hashmap.h" #include "util.h" @@ -54,13 +55,23 @@ typedef enum SplitMode { typedef struct StdoutStream StdoutStream; typedef struct Server { - int epoll_fd; - int signal_fd; int syslog_fd; int native_fd; int stdout_fd; int dev_kmsg_fd; + sd_event *event; + + sd_event_source *syslog_event_source; + sd_event_source *native_event_source; + sd_event_source *stdout_event_source; + sd_event_source *dev_kmsg_event_source; + sd_event_source *sync_event_source; + sd_event_source *sigusr1_event_source; + sd_event_source *sigusr2_event_source; + sd_event_source *sigterm_event_source; + sd_event_source *sigint_event_source; + JournalFile *runtime_journal; JournalFile *system_journal; Hashmap *user_journals; @@ -118,7 +129,6 @@ typedef struct Server { struct udev *udev; - int sync_timer_fd; bool sync_scheduled; } Server; @@ -152,5 +162,5 @@ void server_vacuum(Server *s); void server_rotate(Server *s); int server_schedule_sync(Server *s, int priority); int server_flush_to_var(Server *s); -int process_event(Server *s, struct epoll_event *ev); void server_maybe_append_tags(Server *s); +int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata); diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index 5c15a56bac..e887f01a49 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -22,12 +22,12 @@ #include <fcntl.h> #include <unistd.h> #include <stddef.h> -#include <sys/epoll.h> #ifdef HAVE_SELINUX #include <selinux/selinux.h> #endif +#include "sd-event.h" #include "socket-util.h" #include "selinux-util.h" #include "journald-server.h" @@ -71,6 +71,8 @@ struct StdoutStream { char buffer[LINE_MAX+1]; size_t length; + sd_event_source *event_source; + LIST_FIELDS(StdoutStream, stdout_stream); }; @@ -282,12 +284,18 @@ static int stdout_stream_scan(StdoutStream *s, bool force_flush) { return 0; } -int stdout_stream_process(StdoutStream *s) { +static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents, void *userdata) { + StdoutStream *s = userdata; ssize_t l; int r; assert(s); + if ((revents|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) { + log_error("Got invalid event from epoll for stdout stream: %"PRIx32, revents); + return -EIO; + } + l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length); if (l < 0) { @@ -295,13 +303,13 @@ int stdout_stream_process(StdoutStream *s) { return 0; log_warning("Failed to read from stream: %m"); - return -errno; + goto fail; } if (l == 0) { r = stdout_stream_scan(s, true); if (r < 0) - return r; + goto fail; return 0; } @@ -309,10 +317,13 @@ int stdout_stream_process(StdoutStream *s) { s->length += l; r = stdout_stream_scan(s, false); if (r < 0) - return r; + goto fail; return 1; +fail: + stdout_stream_free(s); + return 0; } void stdout_stream_free(StdoutStream *s) { @@ -324,12 +335,11 @@ void stdout_stream_free(StdoutStream *s) { LIST_REMOVE(stdout_stream, s->server->stdout_streams, s); } - if (s->fd >= 0) { - if (s->server) - epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL); + if (s->event_source) + s->event_source = sd_event_source_unref(s->event_source); + if (s->fd >= 0) close_nointr_nofail(s->fd); - } #ifdef HAVE_SELINUX if (s->security_context) @@ -341,14 +351,19 @@ void stdout_stream_free(StdoutStream *s) { free(s); } -int stdout_stream_new(Server *s) { +static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) { + Server *s = userdata; StdoutStream *stream; int fd, r; socklen_t len; - struct epoll_event ev; assert(s); + if (revents != EPOLLIN) { + log_error("Got invalid event from epoll for stdout server fd: %"PRIx32, revents); + return -EIO; + } + fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); if (fd < 0) { if (errno == EAGAIN) @@ -392,12 +407,15 @@ int stdout_stream_new(Server *s) { goto fail; } - zero(ev); - ev.data.ptr = stream; - ev.events = EPOLLIN; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { - log_error("Failed to add stream to event loop: %m"); - r = -errno; + r = sd_event_add_io(s->event, fd, EPOLLIN, stdout_stream_process, stream, &stream->event_source); + if (r < 0) { + log_error("Failed to add stream to event loop: %s", strerror(-r)); + goto fail; + } + + r = sd_event_source_set_priority(stream->event_source, SD_EVENT_PRIORITY_NORMAL+5); + if (r < 0) { + log_error("Failed to adjust stdout event source priority: %s", strerror(-r)); goto fail; } @@ -414,7 +432,6 @@ fail: int server_open_stdout_socket(Server *s) { int r; - struct epoll_event ev = { .events = EPOLLIN }; assert(s); @@ -447,10 +464,16 @@ int server_open_stdout_socket(Server *s) { } else fd_nonblock(s->stdout_fd, 1); - ev.data.fd = s->stdout_fd; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) { - log_error("Failed to add stdout server fd to epoll object: %m"); - return -errno; + r = sd_event_add_io(s->event, s->stdout_fd, EPOLLIN, stdout_stream_new, s, &s->stdout_event_source); + if (r < 0) { + log_error("Failed to add stdout server fd to event source: %s", strerror(-r)); + return r; + } + + r = sd_event_source_set_priority(s->stdout_event_source, SD_EVENT_PRIORITY_NORMAL+10); + if (r < 0) { + log_error("Failed to adjust priority of stdout server event source: %s", strerror(-r)); + return r; } return 0; diff --git a/src/journal/journald-stream.h b/src/journal/journald-stream.h index dfb6267bf6..8cad012967 100644 --- a/src/journal/journald-stream.h +++ b/src/journal/journald-stream.h @@ -25,6 +25,4 @@ int server_open_stdout_socket(Server *s); -int stdout_stream_new(Server *s); void stdout_stream_free(StdoutStream *s); -int stdout_stream_process(StdoutStream *s); diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c index 2caa1738d9..80eb9ff2db 100644 --- a/src/journal/journald-syslog.c +++ b/src/journal/journald-syslog.c @@ -418,7 +418,6 @@ void server_process_syslog_message( int server_open_syslog_socket(Server *s) { int one, r; - struct epoll_event ev; assert(s); @@ -469,12 +468,10 @@ int server_open_syslog_socket(Server *s) { return -errno; } - zero(ev); - ev.events = EPOLLIN; - ev.data.fd = s->syslog_fd; - if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) { - log_error("Failed to add syslog server fd to epoll object: %m"); - return -errno; + r = sd_event_add_io(s->event, s->syslog_fd, EPOLLIN, process_datagram, s, &s->syslog_event_source); + if (r < 0) { + log_error("Failed to add syslog server fd to event loop: %s", strerror(-r)); + return r; } return 0; diff --git a/src/journal/journald.c b/src/journal/journald.c index 5fb10b1d3f..aa12fa785d 100644 --- a/src/journal/journald.c +++ b/src/journal/journald.c @@ -70,9 +70,7 @@ int main(int argc, char *argv[]) { "STATUS=Processing requests..."); for (;;) { - struct epoll_event event; - int t = -1; - usec_t n; + usec_t t = (usec_t) -1, n; n = now(CLOCK_REALTIME); @@ -87,7 +85,7 @@ int main(int argc, char *argv[]) { } /* Calculate when to rotate the next time */ - t = (int) ((server.oldest_file_usec + server.max_retention_usec - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC); + t = server.oldest_file_usec + server.max_retention_usec - n; } #ifdef HAVE_GCRYPT @@ -98,30 +96,18 @@ int main(int argc, char *argv[]) { if (n >= u) t = 0; else - t = MIN(t, (int) ((u - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC)); + t = MIN(t, u - n); } } #endif - r = epoll_wait(server.epoll_fd, &event, 1, t); + r = sd_event_run(server.event, t); if (r < 0) { - - if (errno == EINTR) - continue; - - log_error("epoll_wait() failed: %m"); + log_error("Failed to run event loop: %s", strerror(-r)); r = -errno; goto finish; } - if (r > 0) { - r = process_event(&server, &event); - if (r < 0) - goto finish; - else if (r == 0) - break; - } - server_maybe_append_tags(&server); server_maybe_warn_forward_syslog_missed(&server); } @@ -130,8 +116,7 @@ int main(int argc, char *argv[]) { server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped"); finish: - sd_notify(false, - "STATUS=Shutting down..."); + sd_notify(false, "STATUS=Shutting down..."); server_done(&server); |