X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=logger.c;h=ba325c696c80c71ceeb9feeb36987e844a0a83c1;hp=cd2970002c5cf61e4e38e78b005a06444250ef0f;hb=eced69b3016f32bda93030f31c0e334cc1e043f2;hpb=4901f972785c4cd3a2cc10a8f4b6ce4e2ab3a568 diff --git a/logger.c b/logger.c index cd2970002..ba325c696 100644 --- a/logger.c +++ b/logger.c @@ -1,5 +1,24 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include #include @@ -43,11 +62,6 @@ typedef enum StreamState { STREAM_RUNNING } StreamState; -typedef enum LogTarget { - LOG_TARGET_SYSLOG, - LOG_TARGET_KMSG -} LogTarget; - struct Stream { Server *server; @@ -67,16 +81,9 @@ struct Stream { LIST_FIELDS(Stream, stream); }; -#define IOVEC_SET_STRING(iovec, s) \ - do { \ - (iovec).iov_base = s; \ - (iovec).iov_len = strlen(s); \ - } while(false); - static int stream_log(Stream *s, char *p, usec_t timestamp) { char header_priority[16], header_time[64], header_pid[16]; - struct msghdr msghdr; struct iovec iovec[5]; assert(s); @@ -120,6 +127,8 @@ static int stream_log(Stream *s, char *p, usec_t timestamp) { IOVEC_SET_STRING(iovec[0], header_priority); if (s->target == LOG_TARGET_SYSLOG) { + struct msghdr msghdr; + IOVEC_SET_STRING(iovec[1], header_time); IOVEC_SET_STRING(iovec[2], s->process); IOVEC_SET_STRING(iovec[3], header_pid); @@ -136,7 +145,7 @@ static int stream_log(Stream *s, char *p, usec_t timestamp) { IOVEC_SET_STRING(iovec[1], s->process); IOVEC_SET_STRING(iovec[2], header_pid); IOVEC_SET_STRING(iovec[3], p); - IOVEC_SET_STRING(iovec[4], "\n"); + IOVEC_SET_STRING(iovec[4], (char*) "\n"); if (writev(s->server->kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) return -errno; @@ -274,7 +283,7 @@ static void stream_free(Stream *s) { if (s->server) epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL); - assert_se(close_nointr(s->fd) == 0); + close_nointr_nofail(s->fd); } free(s->process); @@ -296,12 +305,12 @@ static int stream_new(Server *s, int server_fd) { if (s->n_streams >= STREAMS_MAX) { log_warning("Too many connections, refusing connection."); - assert_se(close_nointr(fd) == 0); + close_nointr_nofail(fd); return 0; } if (!(stream = new0(Stream, 1))) { - assert_se(close_nointr(fd) == 0); + close_nointr_nofail(fd); return -ENOMEM; } @@ -319,7 +328,7 @@ static int stream_new(Server *s, int server_fd) { zero(ev); ev.data.ptr = stream; - ev.events = POLLIN; + ev.events = EPOLLIN; if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { r = -errno; goto fail; @@ -390,16 +399,16 @@ static void server_done(Server *s) { stream_free(s->streams); for (i = 0; i < s->n_server_fd; i++) - assert_se(close_nointr(SERVER_FD_START+i) == 0); + close_nointr_nofail(SERVER_FD_START+i); if (s->syslog_fd >= 0) - assert_se(close_nointr(s->syslog_fd) == 0); + close_nointr_nofail(s->syslog_fd); if (s->epoll_fd >= 0) - assert_se(close_nointr(s->epoll_fd) == 0); + close_nointr_nofail(s->epoll_fd); if (s->kmsg_fd >= 0) - assert_se(close_nointr(s->kmsg_fd) == 0); + close_nointr_nofail(s->kmsg_fd); } static int server_init(Server *s, unsigned n_sockets) { @@ -429,7 +438,7 @@ static int server_init(Server *s, unsigned n_sockets) { struct epoll_event ev; zero(ev); - ev.events = POLLIN; + ev.events = EPOLLIN; ev.data.ptr = UINT_TO_PTR(SERVER_FD_START+i); if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SERVER_FD_START+i, &ev) < 0) { r = -errno; @@ -479,7 +488,7 @@ static int process_event(Server *s, struct epoll_event *ev) { if (PTR_TO_UINT(ev->data.ptr) >= SERVER_FD_START && PTR_TO_UINT(ev->data.ptr) < SERVER_FD_START+s->n_server_fd) { - if (ev->events != POLLIN) { + if (ev->events != EPOLLIN) { log_info("Got invalid event from epoll. (1)"); return -EIO; } @@ -495,7 +504,7 @@ static int process_event(Server *s, struct epoll_event *ev) { timestamp = now(CLOCK_REALTIME); - if (!(ev->events & POLLIN)) { + if (!(ev->events & EPOLLIN)) { log_info("Got invalid event from epoll. (3)"); stream_free(stream); return 0; @@ -529,9 +538,9 @@ int main(int argc, char *argv[]) { for (;;) { struct epoll_event event; - int n; + int k; - if ((n = epoll_wait(server.epoll_fd, + if ((k = epoll_wait(server.epoll_fd, &event, 1, server.n_streams <= 0 ? TIMEOUT : -1)) < 0) { @@ -542,10 +551,10 @@ int main(int argc, char *argv[]) { goto fail; } - if (n <= 0) + if (k <= 0) break; - if ((r = process_event(&server, &event)) < 0) + if ((k = process_event(&server, &event)) < 0) goto fail; } r = 0;