journald-native.c revision 505b6a61c22d5565e9308045c7b9bf79f7d0517e
/*-*- 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 "socket-util.h"
#include "journald.h"
#include "journald-native.h"
#include "journald-kmsg.h"
#include "journald-console.h"
#include "journald-syslog.h"
static bool valid_user_field(const char *p, size_t l) {
const char *a;
/* We kinda enforce POSIX syntax recommendations for
environment variables here, but make a couple of additional
requirements.
/* No empty field names */
if (l <= 0)
return false;
/* Don't allow names longer than 64 chars */
if (l > 64)
return false;
/* Variables starting with an underscore are protected */
if (p[0] == '_')
return false;
/* Don't allow digits as first character */
if (p[0] >= '0' && p[0] <= '9')
return false;
/* Only allow A-Z0-9 and '_' */
for (a = p; a < p + l; a++)
if (!((*a >= 'A' && *a <= 'Z') ||
(*a >= '0' && *a <= '9') ||
*a == '_'))
return false;
return true;
}
Server *s,
unsigned n = 0, m = 0, j, tn = (unsigned) -1;
const char *p;
assert(s);
p = buffer;
while (remaining > 0) {
const char *e, *q;
if (!e) {
/* Trailing noise, let's ignore it, and flush what we collected */
log_debug("Received message with trailing noise, ignoring.");
break;
}
if (e == p) {
/* Entry separator */
n = 0;
p++;
remaining--;
continue;
}
if (*p == '.' || *p == '#') {
/* Ignore control commands for now, and
* comments too. */
remaining -= (e - p) + 1;
p = e + 1;
continue;
}
/* A property follows */
if (n+N_IOVEC_META_FIELDS >= m) {
struct iovec *c;
unsigned u;
if (!c) {
log_oom();
break;
}
iovec = c;
m = u;
}
q = memchr(p, '=', e - p);
if (q) {
if (valid_user_field(p, q - p)) {
size_t l;
l = e - p;
/* If the field name starts with an
* underscore, skip the variable,
* since that indidates a trusted
* field */
n++;
/* We need to determine the priority
* of this entry for the rate limiting
* logic */
if (l == 10 &&
p[9] >= '0' && p[9] <= '9')
else if (l == 17 &&
p[16] >= '0' && p[16] <= '9')
else if (l == 18 &&
p[16] >= '0' && p[16] <= '9' &&
p[17] >= '0' && p[17] <= '9')
else if (l >= 19 &&
char *t;
if (t) {
identifier = t;
}
} else if (l >= 8 &&
char *t;
if (t) {
message = t;
}
}
}
remaining -= (e - p) + 1;
p = e + 1;
continue;
} else {
uint64_t l;
char *k;
log_debug("Failed to parse message, ignoring.");
break;
}
if (l > DATA_SIZE_MAX) {
log_debug("Received binary data block too large, ignoring.");
break;
}
log_debug("Failed to parse message, ignoring.");
break;
}
k = malloc((e - p) + 1 + l);
if (!k) {
log_oom();
break;
}
memcpy(k, p, e - p);
k[e - p] = '=';
if (valid_user_field(p, e - p)) {
n++;
} else
free(k);
}
}
if (n <= 0)
goto finish;
tn = n++;
if (message) {
if (s->forward_to_syslog)
if (s->forward_to_kmsg)
if (s->forward_to_console)
}
for (j = 0; j < n; j++) {
if (j == tn)
continue;
}
}
Server *s,
int fd,
void *p;
ssize_t n;
assert(s);
/* Data is in the passed file, since it didn't fit in a
* datagram. We can't map the file here, since clients might
* then truncate it and trigger a SIGBUS for us. So let's
* stupidly read it */
log_error("Failed to stat passed file, ignoring: %m");
return;
}
log_error("File passed is not regular. Ignoring.");
return;
}
return;
log_error("File passed too large. Ignoring.");
return;
}
if (!p) {
log_oom();
return;
}
if (n < 0)
else if (n > 0)
free(p);
}
int server_open_native_socket(Server*s) {
union sockaddr_union sa;
int one, r;
struct epoll_event ev;
assert(s);
if (s->native_fd < 0) {
if (s->native_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->native_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
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;
}
log_error("Failed to add native server fd to epoll object: %m");
return -errno;
}
return 0;
}