journal-send.c revision 61c024b328d5493a334242a4d01ba923582093fa
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/socket.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/un.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stddef.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <fcntl.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <printf.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define SD_JOURNAL_SUPPRESS_LOCATION
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-journal.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "socket-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define SNDBUF_SIZE (8*1024*1024)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define ALLOCA_CODE_FUNC(f, func) \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering do { \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size_t _fl; \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *_func = (func); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char **_f = &(f); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _fl = strlen(_func) + 1; \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *_f = alloca(_fl + 10); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(*_f, "CODE_FUNC=", 10); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(*_f + 10, _func, _fl); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } while(false)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/* We open a single fd, and we'll share it with the current process,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * all its threads, and all its subprocesses. This means we need to
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * initialize it atomically, and need to operate on it atomically
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * never assuming we are the only user */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int journal_fd(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering static int fd_plus_one = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringretry:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd_plus_one > 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return fd_plus_one - 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_inc_sndbuf(fd, SNDBUF_SIZE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering safe_close(fd);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering goto retry;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen return fd;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering va_list ap;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering va_start(ap, format);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering r = sd_journal_printv(priority, format, ap);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering va_end(ap);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return r;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* FIXME: Instead of limiting things to LINE_MAX we could do a
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering C99 variable-length array on the stack here in a loop. */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_return(priority >= 0, -EINVAL);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_return(priority <= 7, -EINVAL);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_return(format, -EINVAL);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering char_array_0(p);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering memcpy(buffer, "MESSAGE=", 8);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering char_array_0(buffer);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering zero(iov);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering IOVEC_SET_STRING(iov[0], buffer);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering IOVEC_SET_STRING(iov[1], p);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return sd_journal_sendv(iov, 2);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering}
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering PROTECT_ERRNO;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int r, n = 0, i = 0, j;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct iovec *iov = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(_iov);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (extra > 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = MAX(extra * 2, extra + 4);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering iov = malloc0(n * sizeof(struct iovec));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!iov) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = -ENOMEM;
c5ed93163e6ef51a7462aa558a7e0912b17c4951Lennart Poettering goto fail;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering i = extra;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering }
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering while (format) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering struct iovec *c;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *buffer;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_list aq;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen if (i >= n) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = MAX(i*2, 4);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering c = realloc(iov, n * sizeof(struct iovec));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!c) {
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering r = -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen iov = c;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering va_copy(aq, ap);
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering if (vasprintf(&buffer, format, aq) < 0) {
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering va_end(aq);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = -ENOMEM;
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering goto fail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_end(aq);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering VA_FORMAT_ADVANCE(format, ap);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(iov[i++], buffer);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering format = va_arg(ap, char *);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *_iov = iov;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return i;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poetteringfail:
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering for (j = 0; j < i; j++)
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering free(iov[j].iov_base);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a2ba62c719224a4b47751623ca5e8b0333f49721Lennart Poettering free(iov);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen_public_ int sd_journal_send(const char *format, ...) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, i, j;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_list ap;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct iovec *iov = NULL;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_start(ap, format);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_end(ap);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(i < 0)) {
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen r = i;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_journal_sendv(iov, i);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringfinish:
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen for (j = 0; j < i; j++)
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen free(iov[j].iov_base);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(iov);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen PROTECT_ERRNO;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int fd;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_ int buffer_fd = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct iovec *w;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint64_t *l;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int i, j = 0;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering struct sockaddr_un sa = {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering .sun_family = AF_UNIX,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .sun_path = "/run/systemd/journal/socket",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct msghdr mh = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .msg_name = &sa,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ssize_t k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering union {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct cmsghdr cmsghdr;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(int))];
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering } control;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering struct cmsghdr *cmsg;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool have_syslog_identifier = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_return(iov, -EINVAL);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering assert_return(n > 0, -EINVAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5 + 3);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l = alloca(sizeof(uint64_t) * n);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering char *c, *nl;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (_unlikely_(!c || c == iov[i].iov_base))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering have_syslog_identifier = have_syslog_identifier ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (c == (char *) iov[i].iov_base + 17 &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (nl) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(nl < c))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* Already includes a newline? Bummer, then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * let's write the variable name, then a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * newline, then the size (64bit LE), followed
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * by the data and a final newline */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_base = iov[i].iov_base;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering j++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(w[j++], "\n");
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_base = &l[i];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_len = sizeof(uint64_t);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering j++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_base = c + 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering j++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen /* Nothing special? Then just add the line and
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * append a newline */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j++] = iov[i];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(w[j++], "\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!have_syslog_identifier &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering string_is_safe(program_invocation_short_name)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Implicitly add program_invocation_short_name, if it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * is not set explicitly. We only do this for
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * program_invocation_short_name, and nothing else
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen * since everything else is much nicer to retrieve
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * from the outside. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering IOVEC_SET_STRING(w[j++], "SYSLOG_IDENTIFIER=");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(w[j++], program_invocation_short_name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(w[j++], "\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = journal_fd();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(fd < 0))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return fd;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_iov = w;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_iovlen = j;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = sendmsg(fd, &mh, MSG_NOSIGNAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (k >= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Fail silently if the journal is not available */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (errno == ENOENT)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (errno != EMSGSIZE && errno != ENOBUFS)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Message doesn't fit... Let's dump the data in a temporary
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * file and just pass a file descriptor of it to the other
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * side.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * We use /dev/shm instead of /tmp here, since we want this to
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * be a tmpfs, and one that is available from early boot on
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * and where unprivileged users can create files. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering buffer_fd = open_tmpfile("/dev/shm", O_RDWR | O_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (buffer_fd < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return buffer_fd;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = writev(buffer_fd, w, j);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (n < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_iov = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_iovlen = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering zero(control);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering mh.msg_control = &control;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_controllen = sizeof(control);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cmsg = CMSG_FIRSTHDR(&mh);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cmsg->cmsg_level = SOL_SOCKET;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cmsg->cmsg_type = SCM_RIGHTS;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(int));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_controllen = cmsg->cmsg_len;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = sendmsg(fd, &mh, MSG_NOSIGNAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (k < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
static int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
PROTECT_ERRNO;
size_t n, k;
k = isempty(message) ? 0 : strlen(message) + 2;
n = 8 + k + 256 + 1;
for (;;) {
char buffer[n];
char* j;
errno = 0;
j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
if (errno == 0) {
char error[6 + 10 + 1]; /* for a 32bit value */
if (j != buffer + 8 + k)
memmove(buffer + 8 + k, j, strlen(j)+1);
memcpy(buffer, "MESSAGE=", 8);
if (k > 0) {
memcpy(buffer + 8, message, k - 2);
memcpy(buffer + 8 + k - 2, ": ", 2);
}
snprintf(error, sizeof(error), "ERRNO=%u", _saved_errno_);
char_array_0(error);
IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
IOVEC_SET_STRING(iov[skip+1], buffer);
IOVEC_SET_STRING(iov[skip+2], error);
return sd_journal_sendv(iov, skip + 3);
}
if (errno != ERANGE)
return -errno;
n *= 2;
}
}
_public_ int sd_journal_perror(const char *message) {
struct iovec iovec[3];
return fill_iovec_perror_and_send(message, 0, iovec);
}
_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
union sockaddr_union sa = {
.un.sun_family = AF_UNIX,
.un.sun_path = "/run/systemd/journal/stdout",
};
_cleanup_close_ int fd = -1;
char *header;
size_t l;
int r;
assert_return(priority >= 0, -EINVAL);
assert_return(priority <= 7, -EINVAL);
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd < 0)
return -errno;
r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0)
return -errno;
if (shutdown(fd, SHUT_RD) < 0)
return -errno;
fd_inc_sndbuf(fd, SNDBUF_SIZE);
if (!identifier)
identifier = "";
l = strlen(identifier);
header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
memcpy(header, identifier, l);
header[l++] = '\n';
header[l++] = '\n'; /* unit id */
header[l++] = '0' + priority;
header[l++] = '\n';
header[l++] = '0' + !!level_prefix;
header[l++] = '\n';
header[l++] = '0';
header[l++] = '\n';
header[l++] = '0';
header[l++] = '\n';
header[l++] = '0';
header[l++] = '\n';
r = (int) loop_write(fd, header, l, false);
if (r < 0)
return r;
if ((size_t) r != l)
return -errno;
r = fd;
fd = -1;
return r;
}
_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
int r;
va_list ap;
va_start(ap, format);
r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
va_end(ap);
return r;
}
_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
char buffer[8 + LINE_MAX], p[11];
struct iovec iov[5];
char *f;
assert_return(priority >= 0, -EINVAL);
assert_return(priority <= 7, -EINVAL);
assert_return(format, -EINVAL);
snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
char_array_0(p);
memcpy(buffer, "MESSAGE=", 8);
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
char_array_0(buffer);
/* func is initialized from __func__ which is not a macro, but
* a static const char[], hence cannot easily be prefixed with
* CODE_FUNC=, hence let's do it manually here. */
ALLOCA_CODE_FUNC(f, func);
zero(iov);
IOVEC_SET_STRING(iov[0], buffer);
IOVEC_SET_STRING(iov[1], p);
IOVEC_SET_STRING(iov[2], file);
IOVEC_SET_STRING(iov[3], line);
IOVEC_SET_STRING(iov[4], f);
return sd_journal_sendv(iov, ELEMENTSOF(iov));
}
_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
int r, i, j;
va_list ap;
struct iovec *iov = NULL;
char *f;
va_start(ap, format);
i = fill_iovec_sprintf(format, ap, 3, &iov);
va_end(ap);
if (_unlikely_(i < 0)) {
r = i;
goto finish;
}
ALLOCA_CODE_FUNC(f, func);
IOVEC_SET_STRING(iov[0], file);
IOVEC_SET_STRING(iov[1], line);
IOVEC_SET_STRING(iov[2], f);
r = sd_journal_sendv(iov, i);
finish:
for (j = 3; j < i; j++)
free(iov[j].iov_base);
free(iov);
return r;
}
_public_ int sd_journal_sendv_with_location(
const char *file, const char *line,
const char *func,
const struct iovec *iov, int n) {
struct iovec *niov;
char *f;
assert_return(iov, -EINVAL);
assert_return(n > 0, -EINVAL);
niov = alloca(sizeof(struct iovec) * (n + 3));
memcpy(niov, iov, sizeof(struct iovec) * n);
ALLOCA_CODE_FUNC(f, func);
IOVEC_SET_STRING(niov[n++], file);
IOVEC_SET_STRING(niov[n++], line);
IOVEC_SET_STRING(niov[n++], f);
return sd_journal_sendv(niov, n);
}
_public_ int sd_journal_perror_with_location(
const char *file, const char *line,
const char *func,
const char *message) {
struct iovec iov[6];
char *f;
ALLOCA_CODE_FUNC(f, func);
IOVEC_SET_STRING(iov[0], file);
IOVEC_SET_STRING(iov[1], line);
IOVEC_SET_STRING(iov[2], f);
return fill_iovec_perror_and_send(message, 3, iov);
}