journal-send.c revision 44b601bc79e46722bc0f0862ee0ce34a2284ef11
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering This file is part of systemd.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright 2011 Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is free software; you can redistribute it and/or modify it
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering under the terms of the GNU Lesser General Public License as published by
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (at your option) any later version.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is distributed in the hope that it will be useful, but
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Lesser General Public License for more details.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering You should have received a copy of the GNU Lesser General Public License
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char **_f = &(f); \
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering } while(false)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* We open a single fd, and we'll share it with the current process,
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * all its threads, and all its subprocesses. This means we need to
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * initialize it atomically, and need to operate on it atomically
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * never assuming we are the only user */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int journal_fd(void) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = sd_journal_printv(priority, format, ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* FIXME: Instead of limiting things to LINE_MAX we could do a
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering C99 variable-length array on the stack here in a loop. */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int r, n = 0, i = 0, j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = realloc(iov, n * sizeof(struct iovec));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (vasprintf(&buffer, format, aq) < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < i; j++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering_public_ int sd_journal_send(const char *format, ...) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < i; j++)
struct iovec *w;
uint64_t *l;
ssize_t k;
} control;
bool have_syslog_identifier = false;
return -EINVAL;
if (_unlikely_(n <= 0))
return -EINVAL;
char *c, *nl;
return -EINVAL;
return -EINVAL;
if (nl) {
return -EINVAL;
w[j].iov_base = &l[i];
w[j++] = iov[i];
if (!have_syslog_identifier &&
return fd;
return -errno;
if (buffer_fd < 0)
return -errno;
return -errno;
return -errno;
return -errno;
size_t n, k;
char buffer[n];
errno = 0;
if (errno == 0) {
return -errno;
int fd;
char *header;
size_t l;
ssize_t r;
return -EINVAL;
if (fd < 0)
return -errno;
return -errno;
return -errno;
if (!identifier)
if ((size_t) r != l) {
return -errno;
return fd;
_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
return -EINVAL;
return -EINVAL;
char_array_0(p);
_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
if (_unlikely_(i < 0)) {
goto finish;
const char *func,
return -EINVAL;
if (_unlikely_(n <= 0))
return -EINVAL;
const char *func,
const char *message) {