journald-syslog.c revision de0671ee7fe465e108f62dcbbbe9366f81dd9e9a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <stddef.h>
#include "systemd/sd-messages.h"
#include "socket-util.h"
#include "selinux-util.h"
#include "journald-server.h"
#include "journald-syslog.h"
#include "journald-kmsg.h"
#include "journald-console.h"
#include "journald-wall.h"
/* Warn once every 30s if we missed syslog message */
static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
union sockaddr_union sa = {
};
.msg_iovlen = n_iovec,
};
union {
} control;
assert(s);
if (ucred) {
}
* the SO_TIMESTAMP auxiliary data, and hence we don't. */
return;
/* The socket is full? I guess the syslog implementation is
* too slow, and we shouldn't wait for that... */
s->n_forward_syslog_missed++;
return;
}
struct ucred u;
/* Hmm, presumably the sender process vanished
* by now, so let's fix it as good as we
* can, and retry */
u = *ucred;
return;
s->n_forward_syslog_missed++;
return;
}
}
log_debug("Failed to forward syslog message: %m");
}
static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
assert(s);
return;
}
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
int n = 0;
time_t t;
assert(s);
return;
/* First: priority field */
/* Second: timestamp */
if (!tm)
return;
return;
/* Third: identifier and PID */
if (ucred) {
if (!identifier) {
}
if (identifier)
} else if (identifier) {
}
/* Fourth: message */
}
int syslog_fixup_facility(int priority) {
if ((priority & LOG_FACMASK) == 0)
return priority;
}
const char *p;
char *t;
size_t l, e;
p = *buf;
p += strspn(p, WHITESPACE);
l = strcspn(p, WHITESPACE);
if (l <= 0 ||
p[l-1] != ':')
return 0;
e = l;
l--;
if (p[l-1] == ']') {
size_t k = l-1;
for (;;) {
if (p[k] == '[') {
if (t)
*pid = t;
l = k;
break;
}
if (k == 0)
break;
k--;
}
}
t = strndup(p, l);
if (t)
*identifier = t;
e += strspn(p + e, WHITESPACE);
*buf = p + e;
return e;
}
int a = 0, b = 0, c = 0;
int k;
assert(p);
assert(*p);
if ((*p)[0] != '<')
return;
if (!strchr(*p, '>'))
return;
if ((*p)[2] == '>') {
c = undecchar((*p)[1]);
k = 3;
} else if ((*p)[3] == '>') {
b = undecchar((*p)[1]);
c = undecchar((*p)[2]);
k = 4;
} else if ((*p)[4] == '>') {
a = undecchar((*p)[1]);
b = undecchar((*p)[2]);
c = undecchar((*p)[3]);
k = 5;
} else
return;
if (a < 0 || b < 0 || c < 0 ||
(!with_facility && (a || b || c > 7)))
return;
if (with_facility)
else
*p += k;
}
static void syslog_skip_date(char **buf) {
enum {
} sequence[] = {
};
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;
}
Server *s,
const char *buf,
const char *label,
char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
unsigned n = 0;
const char *orig;
assert(s);
if (s->forward_to_syslog)
syslog_skip_date((char**) &buf);
if (s->forward_to_kmsg)
if (s->forward_to_console)
if (s->forward_to_wall)
if (priority & LOG_FACMASK)
if (identifier) {
if (syslog_identifier)
}
if (pid) {
if (syslog_pid)
}
if (message)
server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority, 0);
}
int server_open_syslog_socket(Server *s) {
int one, r;
assert(s);
if (s->syslog_fd < 0) {
union sockaddr_union sa = {
};
if (s->syslog_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
} else
one = 1;
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
#ifdef HAVE_SELINUX
if (use_selinux()) {
one = 1;
if (r < 0)
log_warning("SO_PASSSEC failed: %m");
}
#endif
one = 1;
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
if (r < 0) {
return r;
}
return 0;
}
void server_maybe_warn_forward_syslog_missed(Server *s) {
usec_t n;
assert(s);
if (s->n_forward_syslog_missed <= 0)
return;
n = now(CLOCK_MONOTONIC);
return;
server_driver_message(s, SD_MESSAGE_FORWARD_SYSLOG_MISSED, "Forwarding to syslog missed %u messages.", s->n_forward_syslog_missed);
s->n_forward_syslog_missed = 0;
s->last_warn_forward_syslog_missed = n;
}