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