kmsg-syslogd.c revision 1a2801529e916ec31d2a8cc66cd5c3b8d9ad9caa
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signalfd.h>
#include "util.h"
#include "log.h"
#include "sd-daemon.h"
#include "fdset.h"
#define SERVER_FD_MAX 16
typedef struct Server {
int kmsg_fd;
int epoll_fd;
int signal_fd;
} Server;
static void server_done(Server *s) {
assert(s);
if (s->epoll_fd >= 0)
if (s->kmsg_fd >= 0)
if (s->signal_fd >= 0)
if (s->syslog_fds)
fdset_free(s->syslog_fds);
}
int r;
unsigned i;
struct epoll_event ev;
assert(s);
zero(*s);
r = -errno;
goto fail;
}
if (!(s->syslog_fds = fdset_new())) {
r = -ENOMEM;
goto fail;
}
for (i = 0; i < n_sockets; i++) {
int fd;
fd = SD_LISTEN_FDS_START+i;
goto fail;
}
if (!r) {
log_error("Wrong file descriptor type.");
r = -EINVAL;
goto fail;
}
r = -errno;
goto fail;
}
goto fail;
}
}
return -errno;
}
log_error("signalfd(): %m");
return -errno;
}
log_error("epoll_ctl(): %m");
return -errno;
}
return 0;
fail:
server_done(s);
return r;
}
enum {
} sequence[] = {
};
const char *p;
unsigned i;
p = *buf;
for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
if (!*p)
return;
switch (sequence[i]) {
case SPACE:
if (*p != ' ')
return;
break;
case SPACE_OR_NUMBER:
if (*p == ' ')
break;
/* fall through */
case NUMBER:
if (*p < '0' || *p > '9')
return;
break;
case LETTER:
if (!(*p >= 'A' && *p <= 'Z') &&
!(*p >= 'a' && *p <= 'z'))
return;
break;
case COLON:
if (*p != ':')
return;
break;
}
}
*buf = p;
}
const char *p;
size_t l;
p = *buf;
p += strspn(p, WHITESPACE);
l = strcspn(p, WHITESPACE);
if (l <= 0 ||
p[l-1] != ':')
return 0;
l--;
if (p[l-1] == ']') {
size_t k = l-1;
for (;;) {
if (p[k] == '[') {
l = k;
break;
}
if (k == 0)
break;
k--;
}
}
*buf = p + l;
return 1;
}
const char *p;
p = *buf;
if (*p != '[')
return;
p++;
p += strspn(p, "0123456789");
if (*p != ']')
return;
p++;
*buf = p;
}
ssize_t k;
unsigned i = 0;
int r = 0;
assert(s);
if (*buf == 0)
return 0;
if ((prio & LOG_FACMASK) == 0)
/* First, set priority field */
/* Second, skip date */
/* Then, add process if set */
i++;
else if (ucred &&
/* Skip the stored PID if we have a better one */
if (ucred) {
if (*buf == ':')
buf++;
}
/* Is the remaining message empty? */
if (*buf) {
/* And the rest is the message */
}
}
return r;
}
assert(s);
log_info("Got invalid event from epoll.");
return -EIO;
}
struct signalfd_siginfo sfsi;
ssize_t n;
if (n >= 0)
return -EIO;
return 0;
return -errno;
}
return 0;
} else {
for (;;) {
union {
} control;
ssize_t n;
int k;
char *e;
return 1;
log_error("recvmsg() failed: %m");
return -errno;
}
else
*e = 0;
else
buf[n] = 0;
return k;
}
}
return 1;
}
int r = EXIT_FAILURE, n;
if (getppid() != 1) {
log_error("This program should be invoked by init only.");
return EXIT_FAILURE;
}
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
if ((n = sd_listen_fds(true)) < 0) {
return EXIT_FAILURE;
}
if (n <= 0 || n > SERVER_FD_MAX) {
log_error("No or too many file descriptors passed.");
return EXIT_FAILURE;
}
if (server_init(&server, (unsigned) n) < 0)
return EXIT_FAILURE;
sd_notify(false,
"READY=1\n"
"STATUS=Processing messages...");
for (;;) {
struct epoll_event event;
int k;
continue;
log_error("epoll_wait() failed: %m");
goto fail;
}
if (k <= 0)
break;
goto fail;
if (k == 0)
break;
}
r = EXIT_SUCCESS;
fail:
sd_notify(false,
"STATUS=Shutting down...");
return r;
}