journal-send.c revision 2a0e0692565f0435657c93498e09cbb2d3517152
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/***
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering This file is part of systemd.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright 2011 Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (at your option) any later version.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
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
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart 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***/
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <sys/socket.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <sys/un.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <errno.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <stddef.h>
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering#include <unistd.h>
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering#include <fcntl.h>
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering#include <printf.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering#define SD_JOURNAL_SUPPRESS_LOCATION
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include "sd-journal.h"
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include "util.h"
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering#include "socket-util.h"
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering#define SNDBUF_SIZE (8*1024*1024)
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering#define ALLOCA_CODE_FUNC(f, func) \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering do { \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering size_t _fl; \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering const char *_func = (func); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering char **_f = &(f); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering _fl = strlen(_func) + 1; \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering *_f = alloca(_fl + 10); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering memcpy(*_f, "CODE_FUNC=", 10); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering memcpy(*_f + 10, _func, _fl); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering } while(false)
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering
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 Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int journal_fd(void) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering static int fd_plus_one = 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringretry:
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (fd_plus_one > 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return fd_plus_one - 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (fd < 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering fd_inc_sndbuf(fd, SNDBUF_SIZE);
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering close_nointr_nofail(fd);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering goto retry;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering va_list ap;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering va_start(ap, format);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering r = sd_journal_printv(priority, format, ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering va_end(ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* FIXME: Instead of limiting things to LINE_MAX we could do a
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering C99 variable-length array on the stack here in a loop. */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (priority < 0 || priority > 7)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (!format)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering char_array_0(p);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(buffer, "MESSAGE=", 8);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char_array_0(buffer);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering zero(iov);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering IOVEC_SET_STRING(iov[0], buffer);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering IOVEC_SET_STRING(iov[1], p);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering return sd_journal_sendv(iov, 2);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
44a6b1b68029833893f6e9cee35aa27a974038f6Zbigniew Jędrzejewski-Szmek_printf_attr_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering PROTECT_ERRNO;
25d042e81516246b1ebf706a57c47ac19abb0b8aLennart Poettering int r, n = 0, i = 0, j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct iovec *iov = NULL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering assert(_iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (extra > 0) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering n = MAX(extra * 2, extra + 4);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering iov = malloc0(n * sizeof(struct iovec));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (!iov) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = -ENOMEM;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering goto fail;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering }
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering i = extra;
25d042e81516246b1ebf706a57c47ac19abb0b8aLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering while (format) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct iovec *c;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *buffer;
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering va_list aq;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (i >= n) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering n = MAX(i*2, 4);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = realloc(iov, n * sizeof(struct iovec));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!c) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = -ENOMEM;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering goto fail;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering iov = c;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering va_copy(aq, ap);
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering if (vasprintf(&buffer, format, aq) < 0) {
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering va_end(aq);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = -ENOMEM;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering goto fail;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering va_end(aq);
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering VA_FORMAT_ADVANCE(format, ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iov[i++], buffer);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering format = va_arg(ap, char *);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering *_iov = iov;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return i;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringfail:
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering for (j = 0; j < i; j++)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering free(iov[j].iov_base);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering free(iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return r;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering}
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_send(const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering int r, i, j;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_list ap;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering struct iovec *iov = NULL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_start(ap, format);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering va_end(ap);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(i < 0)) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = i;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering goto finish;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering }
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = sd_journal_sendv(iov, i);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringfinish:
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < i; j++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering free(iov[j].iov_base);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering free(iov);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering PROTECT_ERRNO;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering int fd, buffer_fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct iovec *w;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering uint64_t *l;
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering int i, j = 0;
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek struct sockaddr_un sa = {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .sun_family = AF_UNIX,
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .sun_path = "/run/systemd/journal/socket",
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek };
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek struct msghdr mh = {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .msg_name = &sa,
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path),
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek };
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering ssize_t k;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering union {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering struct cmsghdr cmsghdr;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(int))];
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering } control;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering struct cmsghdr *cmsg;
9058851be7821edac08c1fa7ecafe5cba9ab9022Lennart Poettering /* We use /dev/shm instead of /tmp here, since we want this to
9058851be7821edac08c1fa7ecafe5cba9ab9022Lennart Poettering * be a tmpfs, and one that is available from early boot on
9058851be7821edac08c1fa7ecafe5cba9ab9022Lennart Poettering * and where unprivileged users can create files. */
9058851be7821edac08c1fa7ecafe5cba9ab9022Lennart Poettering char path[] = "/dev/shm/journal.XXXXXX";
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering bool have_syslog_identifier = false;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(!iov))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return -EINVAL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(n <= 0))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5 + 3);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l = alloca(sizeof(uint64_t) * n);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (i = 0; i < n; i++) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *c, *nl;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -EINVAL;
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(!c || c == iov[i].iov_base))
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
29abad107f8610e73b2fc091216040b579c75453Zbigniew Jędrzejewski-Szmek have_syslog_identifier = have_syslog_identifier ||
29abad107f8610e73b2fc091216040b579c75453Zbigniew Jędrzejewski-Szmek (c == (char *) iov[i].iov_base + 17 &&
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (nl) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(nl < c))
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Already includes a newline? Bummer, then
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * let's write the variable name, then a
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * newline, then the size (64bit LE), followed
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * by the data and a final newline */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_base = iov[i].iov_base;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering j++;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(w[j++], "\n");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_base = &l[i];
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_len = sizeof(uint64_t);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering j++;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_base = c + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering j++;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering } else
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Nothing special? Then just add the line and
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * append a newline */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j++] = iov[i];
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(w[j++], "\n");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering if (!have_syslog_identifier &&
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering string_is_safe(program_invocation_short_name)) {
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering /* Implicitly add program_invocation_short_name, if it
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * is not set explicitly. We only do this for
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * program_invocation_short_name, and nothing else
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * since everything else is much nicer to retrieve
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * from the outside. */
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering IOVEC_SET_STRING(w[j++], "SYSLOG_IDENTIFIER=");
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering IOVEC_SET_STRING(w[j++], program_invocation_short_name);
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering IOVEC_SET_STRING(w[j++], "\n");
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering }
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering fd = journal_fd();
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(fd < 0))
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering mh.msg_iov = w;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering mh.msg_iovlen = j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering k = sendmsg(fd, &mh, MSG_NOSIGNAL);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (k >= 0)
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return 0;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (errno != EMSGSIZE && errno != ENOBUFS)
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering /* Message doesn't fit... Let's dump the data in a temporary
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering * file and just pass a file descriptor of it to the other
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering * side */
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (buffer_fd < 0)
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (unlink(path) < 0) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering close_nointr_nofail(buffer_fd);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering }
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering n = writev(buffer_fd, w, j);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (n < 0) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering close_nointr_nofail(buffer_fd);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering }
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mh.msg_iov = NULL;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mh.msg_iovlen = 0;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering zero(control);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mh.msg_control = &control;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mh.msg_controllen = sizeof(control);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering cmsg = CMSG_FIRSTHDR(&mh);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering cmsg->cmsg_level = SOL_SOCKET;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering cmsg->cmsg_type = SCM_RIGHTS;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(int));
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mh.msg_controllen = cmsg->cmsg_len;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering k = sendmsg(fd, &mh, MSG_NOSIGNAL);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering close_nointr_nofail(buffer_fd);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (k < 0)
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poetteringstatic int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering PROTECT_ERRNO;
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering size_t n, k;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering k = isempty(message) ? 0 : strlen(message) + 2;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering n = 8 + k + 256 + 1;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering for (;;) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char buffer[n];
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char* j;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering errno = 0;
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (errno == 0) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char error[6 + 10 + 1]; /* for a 32bit value */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (j != buffer + 8 + k)
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering memmove(buffer + 8 + k, j, strlen(j)+1);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering memcpy(buffer, "MESSAGE=", 8);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (k > 0) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering memcpy(buffer + 8, message, k - 2);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering memcpy(buffer + 8 + k - 2, ": ", 2);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering }
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering snprintf(error, sizeof(error), "ERRNO=%u", _saved_errno_);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char_array_0(error);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[skip+1], buffer);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[skip+2], error);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return sd_journal_sendv(iov, skip + 3);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering }
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (errno != ERANGE)
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering return -errno;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering n *= 2;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering }
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering}
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_perror(const char *message) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering struct iovec iovec[3];
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering return fill_iovec_perror_and_send(message, 0, iovec);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering}
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek union sockaddr_union sa = {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .un.sun_family = AF_UNIX,
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .un.sun_path = "/run/systemd/journal/stdout",
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek };
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char *header;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering size_t l;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ssize_t r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (priority < 0 || priority > 7)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (fd < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering if (shutdown(fd, SHUT_RD) < 0) {
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering close_nointr_nofail(fd);
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering return -errno;
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering }
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering fd_inc_sndbuf(fd, SNDBUF_SIZE);
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering if (!identifier)
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering identifier = "";
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering l = strlen(identifier);
8b38f3cc3eb73adf9536cb73d0f319e60d42ea0cLennart Poettering header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering memcpy(header, identifier, l);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '\n';
a6e87e90ede66815989ba2db92a07102a69906feLennart Poettering header[l++] = '\n'; /* unit id */
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '0' + priority;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '\n';
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering header[l++] = '0' + !!level_prefix;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '\n';
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '0';
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering header[l++] = '\n';
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering header[l++] = '0';
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering header[l++] = '\n';
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering header[l++] = '0';
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering header[l++] = '\n';
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = loop_write(fd, header, l, false);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return (int) r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if ((size_t) r != l) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering int r;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_list ap;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_start(ap, format);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_end(ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return r;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering}
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char buffer[8 + LINE_MAX], p[11];
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering struct iovec iov[5];
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char *f;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (priority < 0 || priority > 7)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return -EINVAL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(!format))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return -EINVAL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char_array_0(p);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering memcpy(buffer, "MESSAGE=", 8);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char_array_0(buffer);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* func is initialized from __func__ which is not a macro, but
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering * a static const char[], hence cannot easily be prefixed with
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering * CODE_FUNC=, hence let's do it manually here. */
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering ALLOCA_CODE_FUNC(f, func);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering zero(iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[0], buffer);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[1], p);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[2], file);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[3], line);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[4], f);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return sd_journal_sendv(iov, ELEMENTSOF(iov));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering}
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering int r, i, j;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_list ap;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering struct iovec *iov = NULL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char *f;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_start(ap, format);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering i = fill_iovec_sprintf(format, ap, 3, &iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering va_end(ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(i < 0)) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = i;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering goto finish;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering }
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering ALLOCA_CODE_FUNC(f, func);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[0], file);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[1], line);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(iov[2], f);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = sd_journal_sendv(iov, i);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringfinish:
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering for (j = 3; j < i; j++)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering free(iov[j].iov_base);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering free(iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return r;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering}
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_sendv_with_location(
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const char *file, const char *line,
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const char *func,
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const struct iovec *iov, int n) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering struct iovec *niov;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering char *f;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(!iov))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return -EINVAL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (_unlikely_(n <= 0))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return -EINVAL;
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering niov = alloca(sizeof(struct iovec) * (n + 3));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering memcpy(niov, iov, sizeof(struct iovec) * n);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering ALLOCA_CODE_FUNC(f, func);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(niov[n++], file);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(niov[n++], line);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering IOVEC_SET_STRING(niov[n++], f);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return sd_journal_sendv(niov, n);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering}
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_perror_with_location(
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const char *file, const char *line,
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const char *func,
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering const char *message) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering struct iovec iov[6];
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering char *f;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering ALLOCA_CODE_FUNC(f, func);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[0], file);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[1], line);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[2], f);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering return fill_iovec_perror_and_send(message, 3, iov);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering}