2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2012-01-14 01:53:20 +01:00
|
|
|
#include <fcntl.h>
|
2012-09-24 23:42:03 +02:00
|
|
|
#include <printf.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2012-04-02 19:24:30 +02:00
|
|
|
#define SD_JOURNAL_SUPPRESS_LOCATION
|
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
#include "sd-journal.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2019-03-14 12:24:39 +01:00
|
|
|
#include "errno-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "io-util.h"
|
2020-03-06 13:57:50 +01:00
|
|
|
#include "fileio.h"
|
2014-10-30 18:32:37 +01:00
|
|
|
#include "memfd-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "socket-util.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-11-30 21:05:27 +01:00
|
|
|
#include "tmpfile-util.h"
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2012-01-27 18:57:37 +01:00
|
|
|
#define SNDBUF_SIZE (8*1024*1024)
|
|
|
|
|
2012-11-20 21:22:18 +01:00
|
|
|
#define ALLOCA_CODE_FUNC(f, func) \
|
|
|
|
do { \
|
|
|
|
size_t _fl; \
|
|
|
|
const char *_func = (func); \
|
|
|
|
char **_f = &(f); \
|
|
|
|
_fl = strlen(_func) + 1; \
|
2019-01-26 15:52:18 +01:00
|
|
|
*_f = newa(char, _fl + 10); \
|
2012-11-20 21:22:18 +01:00
|
|
|
memcpy(*_f, "CODE_FUNC=", 10); \
|
|
|
|
memcpy(*_f + 10, _func, _fl); \
|
2016-02-23 18:52:52 +01:00
|
|
|
} while (false)
|
2012-11-20 21:22:18 +01:00
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
/* We open a single fd, and we'll share it with the current process,
|
|
|
|
* all its threads, and all its subprocesses. This means we need to
|
|
|
|
* initialize it atomically, and need to operate on it atomically
|
|
|
|
* never assuming we are the only user */
|
|
|
|
|
|
|
|
static int journal_fd(void) {
|
|
|
|
int fd;
|
|
|
|
static int fd_plus_one = 0;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (fd_plus_one > 0)
|
|
|
|
return fd_plus_one - 1;
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2012-01-27 18:57:37 +01:00
|
|
|
fd_inc_sndbuf(fd, SNDBUF_SIZE);
|
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(fd);
|
2011-12-17 00:56:34 +01:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_print(int priority, const char *format, ...) {
|
2011-12-17 00:56:34 +01:00
|
|
|
int r;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2011-12-17 01:32:49 +01:00
|
|
|
r = sd_journal_printv(priority, format, ap);
|
2011-12-17 00:56:34 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
|
2020-03-06 13:57:50 +01:00
|
|
|
char p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
|
|
|
|
char sbuf[LINE_MAX + 8] = "MESSAGE=";
|
2015-01-27 14:00:11 +01:00
|
|
|
struct iovec iov[2];
|
2020-03-06 13:57:50 +01:00
|
|
|
int len;
|
|
|
|
va_list aq;
|
|
|
|
char *buffer = sbuf;
|
2011-12-17 01:32:49 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(priority >= 0, -EINVAL);
|
|
|
|
assert_return(priority <= 7, -EINVAL);
|
|
|
|
assert_return(format, -EINVAL);
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2020-03-06 13:57:50 +01:00
|
|
|
va_copy(aq, ap);
|
|
|
|
len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
|
|
|
|
va_end(aq);
|
|
|
|
|
|
|
|
if (len >= (int)LONG_LINE_MAX - 8)
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
2020-05-24 23:00:13 +02:00
|
|
|
/* Allocate large buffer to accommodate big message */
|
2020-03-06 13:57:50 +01:00
|
|
|
if (len >= LINE_MAX) {
|
|
|
|
buffer = alloca(len + 9);
|
|
|
|
memcpy(buffer, "MESSAGE=", 8);
|
2020-06-10 19:45:33 +02:00
|
|
|
assert_se(vsnprintf(buffer + 8, len + 1, format, ap) == len);
|
2020-03-06 13:57:50 +01:00
|
|
|
}
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2016-07-01 02:37:21 +02:00
|
|
|
/* Strip trailing whitespace, keep prefix whitespace. */
|
|
|
|
(void) strstrip(buffer);
|
|
|
|
|
2016-07-19 14:27:05 +02:00
|
|
|
/* Suppress empty lines */
|
2020-03-06 13:57:50 +01:00
|
|
|
if (isempty(buffer + 8))
|
2016-07-19 14:27:05 +02:00
|
|
|
return 0;
|
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[0] = IOVEC_MAKE_STRING(buffer);
|
|
|
|
iov[1] = IOVEC_MAKE_STRING(p);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2011-12-17 01:32:49 +01:00
|
|
|
return sd_journal_sendv(iov, 2);
|
2011-12-17 00:56:34 +01:00
|
|
|
}
|
|
|
|
|
2013-10-16 03:17:09 +02:00
|
|
|
_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
|
2013-04-02 16:31:55 +02:00
|
|
|
PROTECT_ERRNO;
|
2012-04-12 17:43:33 +02:00
|
|
|
int r, n = 0, i = 0, j;
|
2011-12-17 00:56:34 +01:00
|
|
|
struct iovec *iov = NULL;
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
assert(_iov);
|
|
|
|
|
|
|
|
if (extra > 0) {
|
|
|
|
n = MAX(extra * 2, extra + 4);
|
|
|
|
iov = malloc0(n * sizeof(struct iovec));
|
|
|
|
if (!iov) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = extra;
|
2012-04-12 17:43:33 +02:00
|
|
|
}
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
while (format) {
|
|
|
|
struct iovec *c;
|
|
|
|
char *buffer;
|
2012-09-24 23:42:03 +02:00
|
|
|
va_list aq;
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
if (i >= n) {
|
|
|
|
n = MAX(i*2, 4);
|
2020-10-09 14:59:44 +02:00
|
|
|
c = reallocarray(iov, n, sizeof(struct iovec));
|
2011-12-17 00:56:34 +01:00
|
|
|
if (!c) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov = c;
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:42:03 +02:00
|
|
|
va_copy(aq, ap);
|
|
|
|
if (vasprintf(&buffer, format, aq) < 0) {
|
|
|
|
va_end(aq);
|
2011-12-17 00:56:34 +01:00
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-09-24 23:42:03 +02:00
|
|
|
va_end(aq);
|
|
|
|
|
|
|
|
VA_FORMAT_ADVANCE(format, ap);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2016-07-01 02:37:21 +02:00
|
|
|
(void) strstrip(buffer); /* strip trailing whitespace, keep prefixing whitespace */
|
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[i++] = IOVEC_MAKE_STRING(buffer);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
format = va_arg(ap, char *);
|
|
|
|
}
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
*_iov = iov;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
free(iov[j].iov_base);
|
|
|
|
|
|
|
|
free(iov);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_send(const char *format, ...) {
|
|
|
|
int r, i, j;
|
|
|
|
va_list ap;
|
|
|
|
struct iovec *iov = NULL;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
i = fill_iovec_sprintf(format, ap, 0, &iov);
|
2011-12-17 00:56:34 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2012-04-02 19:24:30 +02:00
|
|
|
if (_unlikely_(i < 0)) {
|
|
|
|
r = i;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
r = sd_journal_sendv(iov, i);
|
|
|
|
|
2012-04-02 19:24:30 +02:00
|
|
|
finish:
|
2011-12-17 00:56:34 +01:00
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
free(iov[j].iov_base);
|
|
|
|
|
|
|
|
free(iov);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
|
2013-04-02 16:31:55 +02:00
|
|
|
PROTECT_ERRNO;
|
2014-10-30 17:36:02 +01:00
|
|
|
int fd, r;
|
2014-06-18 23:56:03 +02:00
|
|
|
_cleanup_close_ int buffer_fd = -1;
|
2011-12-17 00:56:34 +01:00
|
|
|
struct iovec *w;
|
|
|
|
uint64_t *l;
|
2013-04-02 16:31:55 +02:00
|
|
|
int i, j = 0;
|
2016-05-05 22:24:36 +02:00
|
|
|
static const union sockaddr_union sa = {
|
|
|
|
.un.sun_family = AF_UNIX,
|
|
|
|
.un.sun_path = "/run/systemd/journal/socket",
|
2013-06-14 05:32:14 +02:00
|
|
|
};
|
|
|
|
struct msghdr mh = {
|
2016-05-05 22:24:36 +02:00
|
|
|
.msg_name = (struct sockaddr*) &sa.sa,
|
|
|
|
.msg_namelen = SOCKADDR_UN_LEN(sa.un),
|
2013-06-14 05:32:14 +02:00
|
|
|
};
|
2012-01-14 01:53:20 +01:00
|
|
|
ssize_t k;
|
2012-11-20 21:25:26 +01:00
|
|
|
bool have_syslog_identifier = false;
|
2014-10-30 17:36:02 +01:00
|
|
|
bool seal = true;
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(iov, -EINVAL);
|
|
|
|
assert_return(n > 0, -EINVAL);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2015-12-03 06:27:22 +01:00
|
|
|
w = newa(struct iovec, n * 5 + 3);
|
|
|
|
l = newa(uint64_t, n);
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
char *c, *nl;
|
|
|
|
|
2013-04-02 16:31:55 +02:00
|
|
|
if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
|
|
|
|
return -EINVAL;
|
2012-01-03 20:54:15 +01:00
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
|
2013-04-02 16:31:55 +02:00
|
|
|
if (_unlikely_(!c || c == iov[i].iov_base))
|
|
|
|
return -EINVAL;
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2012-11-25 21:34:10 +01:00
|
|
|
have_syslog_identifier = have_syslog_identifier ||
|
|
|
|
(c == (char *) iov[i].iov_base + 17 &&
|
2013-08-22 03:20:55 +02:00
|
|
|
startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
|
2012-11-20 21:25:26 +01:00
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
|
|
|
|
if (nl) {
|
2013-04-02 16:31:55 +02:00
|
|
|
if (_unlikely_(nl < c))
|
|
|
|
return -EINVAL;
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
/* Already includes a newline? Bummer, then
|
|
|
|
* let's write the variable name, then a
|
|
|
|
* newline, then the size (64bit LE), followed
|
|
|
|
* by the data and a final newline */
|
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
w[j++] = IOVEC_MAKE(iov[i].iov_base, c - (char*) iov[i].iov_base);
|
|
|
|
w[j++] = IOVEC_MAKE_STRING("\n");
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
|
2017-09-21 13:52:34 +02:00
|
|
|
w[j++] = IOVEC_MAKE(&l[i], sizeof(uint64_t));
|
2011-12-17 00:56:34 +01:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
w[j++] = IOVEC_MAKE(c + 1, iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
|
2011-12-17 00:56:34 +01:00
|
|
|
} else
|
|
|
|
/* Nothing special? Then just add the line and
|
|
|
|
* append a newline */
|
|
|
|
w[j++] = iov[i];
|
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
w[j++] = IOVEC_MAKE_STRING("\n");
|
2011-12-17 00:56:34 +01:00
|
|
|
}
|
|
|
|
|
2012-11-20 21:25:26 +01:00
|
|
|
if (!have_syslog_identifier &&
|
|
|
|
string_is_safe(program_invocation_short_name)) {
|
|
|
|
|
|
|
|
/* Implicitly add program_invocation_short_name, if it
|
|
|
|
* is not set explicitly. We only do this for
|
|
|
|
* program_invocation_short_name, and nothing else
|
|
|
|
* since everything else is much nicer to retrieve
|
|
|
|
* from the outside. */
|
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
w[j++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
|
|
|
|
w[j++] = IOVEC_MAKE_STRING(program_invocation_short_name);
|
|
|
|
w[j++] = IOVEC_MAKE_STRING("\n");
|
2012-11-20 21:25:26 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 00:56:34 +01:00
|
|
|
fd = journal_fd();
|
2013-04-02 16:31:55 +02:00
|
|
|
if (_unlikely_(fd < 0))
|
|
|
|
return fd;
|
2011-12-17 00:56:34 +01:00
|
|
|
|
|
|
|
mh.msg_iov = w;
|
|
|
|
mh.msg_iovlen = j;
|
|
|
|
|
2012-01-14 01:53:20 +01:00
|
|
|
k = sendmsg(fd, &mh, MSG_NOSIGNAL);
|
2013-04-02 16:31:55 +02:00
|
|
|
if (k >= 0)
|
|
|
|
return 0;
|
2012-01-14 01:53:20 +01:00
|
|
|
|
2013-12-03 14:07:32 +01:00
|
|
|
/* Fail silently if the journal is not available */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2017-09-29 09:58:22 +02:00
|
|
|
if (!IN_SET(errno, EMSGSIZE, ENOBUFS))
|
2013-04-02 16:31:55 +02:00
|
|
|
return -errno;
|
2012-01-14 01:53:20 +01:00
|
|
|
|
2014-10-30 17:36:02 +01:00
|
|
|
/* Message doesn't fit... Let's dump the data in a memfd or
|
|
|
|
* temporary file and just pass a file descriptor of it to the
|
|
|
|
* other side.
|
2014-01-26 02:48:01 +01:00
|
|
|
*
|
2014-10-30 17:36:02 +01:00
|
|
|
* For the temporary files we use /dev/shm instead of /tmp
|
|
|
|
* here, since we want this to be a tmpfs, and one that is
|
|
|
|
* available from early boot on and where unprivileged users
|
|
|
|
* can create files. */
|
2014-10-30 18:28:37 +01:00
|
|
|
buffer_fd = memfd_new(NULL);
|
2014-10-30 17:36:02 +01:00
|
|
|
if (buffer_fd < 0) {
|
2014-10-30 18:28:37 +01:00
|
|
|
if (buffer_fd == -ENOSYS) {
|
2016-04-20 19:27:32 +02:00
|
|
|
buffer_fd = open_tmpfile_unlinkable("/dev/shm", O_RDWR | O_CLOEXEC);
|
2014-10-30 17:36:02 +01:00
|
|
|
if (buffer_fd < 0)
|
|
|
|
return buffer_fd;
|
|
|
|
|
|
|
|
seal = false;
|
|
|
|
} else
|
2014-10-30 18:28:37 +01:00
|
|
|
return buffer_fd;
|
2014-10-30 17:36:02 +01:00
|
|
|
}
|
2012-01-14 01:53:20 +01:00
|
|
|
|
2014-01-29 00:23:38 +01:00
|
|
|
n = writev(buffer_fd, w, j);
|
2014-06-18 23:56:03 +02:00
|
|
|
if (n < 0)
|
2013-04-02 16:31:55 +02:00
|
|
|
return -errno;
|
2012-01-14 01:53:20 +01:00
|
|
|
|
2014-10-30 17:36:02 +01:00
|
|
|
if (seal) {
|
2014-10-30 18:28:37 +01:00
|
|
|
r = memfd_set_sealed(buffer_fd);
|
2014-10-30 17:36:02 +01:00
|
|
|
if (r < 0)
|
2014-10-30 18:28:37 +01:00
|
|
|
return r;
|
2014-10-30 17:36:02 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 08:44:50 +01:00
|
|
|
r = send_one_fd_sa(fd, buffer_fd, mh.msg_name, mh.msg_namelen, 0);
|
|
|
|
if (r == -ENOENT)
|
|
|
|
/* Fail silently if the journal is not available */
|
|
|
|
return 0;
|
|
|
|
return r;
|
2011-12-17 00:56:34 +01:00
|
|
|
}
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2012-07-31 16:09:01 +02:00
|
|
|
static int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
|
2013-04-02 16:31:55 +02:00
|
|
|
PROTECT_ERRNO;
|
|
|
|
size_t n, k;
|
2012-07-31 16:09:01 +02:00
|
|
|
|
|
|
|
k = isempty(message) ? 0 : strlen(message) + 2;
|
|
|
|
n = 8 + k + 256 + 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char buffer[n];
|
|
|
|
char* j;
|
|
|
|
|
|
|
|
errno = 0;
|
2013-04-02 16:31:55 +02:00
|
|
|
j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
|
2012-07-31 16:09:01 +02:00
|
|
|
if (errno == 0) {
|
2017-12-14 19:02:29 +01:00
|
|
|
char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
|
2012-07-31 16:09:01 +02:00
|
|
|
|
|
|
|
if (j != buffer + 8 + k)
|
|
|
|
memmove(buffer + 8 + k, j, strlen(j)+1);
|
|
|
|
|
|
|
|
memcpy(buffer, "MESSAGE=", 8);
|
|
|
|
|
|
|
|
if (k > 0) {
|
|
|
|
memcpy(buffer + 8, message, k - 2);
|
|
|
|
memcpy(buffer + 8 + k - 2, ": ", 2);
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(error, "ERRNO=%i", _saved_errno_);
|
2012-07-31 16:09:01 +02:00
|
|
|
|
2016-01-26 13:48:57 +01:00
|
|
|
assert_cc(3 == LOG_ERR);
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[skip+0] = IOVEC_MAKE_STRING("PRIORITY=3");
|
|
|
|
iov[skip+1] = IOVEC_MAKE_STRING(buffer);
|
|
|
|
iov[skip+2] = IOVEC_MAKE_STRING(error);
|
2012-07-31 16:09:01 +02:00
|
|
|
|
2013-04-02 16:31:55 +02:00
|
|
|
return sd_journal_sendv(iov, skip + 3);
|
2012-07-31 16:09:01 +02:00
|
|
|
}
|
|
|
|
|
2013-04-02 16:31:55 +02:00
|
|
|
if (errno != ERANGE)
|
|
|
|
return -errno;
|
2012-07-31 16:09:01 +02:00
|
|
|
|
|
|
|
n *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_perror(const char *message) {
|
|
|
|
struct iovec iovec[3];
|
|
|
|
|
|
|
|
return fill_iovec_perror_and_send(message, 0, iovec);
|
|
|
|
}
|
|
|
|
|
2012-01-05 21:43:49 +01:00
|
|
|
_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
|
2016-05-05 22:24:36 +02:00
|
|
|
static const union sockaddr_union sa = {
|
2013-06-14 05:32:14 +02:00
|
|
|
.un.sun_family = AF_UNIX,
|
|
|
|
.un.sun_path = "/run/systemd/journal/stdout",
|
|
|
|
};
|
2014-06-18 23:56:03 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
2011-12-23 20:50:48 +01:00
|
|
|
char *header;
|
|
|
|
size_t l;
|
2014-06-18 23:56:03 +02:00
|
|
|
int r;
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(priority >= 0, -EINVAL);
|
|
|
|
assert_return(priority <= 7, -EINVAL);
|
2011-12-23 20:50:48 +01:00
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2016-05-05 22:24:36 +02:00
|
|
|
r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
|
2014-06-18 23:56:03 +02:00
|
|
|
if (r < 0)
|
2011-12-23 20:50:48 +01:00
|
|
|
return -errno;
|
|
|
|
|
2014-06-18 23:56:03 +02:00
|
|
|
if (shutdown(fd, SHUT_RD) < 0)
|
2012-01-05 21:39:31 +01:00
|
|
|
return -errno;
|
|
|
|
|
2018-01-23 19:07:06 +01:00
|
|
|
(void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
|
2012-01-27 18:57:37 +01:00
|
|
|
|
2018-01-10 17:11:19 +01:00
|
|
|
identifier = strempty(identifier);
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2012-01-05 21:43:49 +01:00
|
|
|
l = strlen(identifier);
|
2019-01-26 15:52:18 +01:00
|
|
|
header = newa(char, l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2012-01-05 21:43:49 +01:00
|
|
|
memcpy(header, identifier, l);
|
2011-12-23 20:50:48 +01:00
|
|
|
header[l++] = '\n';
|
2012-09-06 00:25:32 +02:00
|
|
|
header[l++] = '\n'; /* unit id */
|
2011-12-23 20:50:48 +01:00
|
|
|
header[l++] = '0' + priority;
|
|
|
|
header[l++] = '\n';
|
2012-01-05 21:43:49 +01:00
|
|
|
header[l++] = '0' + !!level_prefix;
|
2011-12-23 20:50:48 +01:00
|
|
|
header[l++] = '\n';
|
|
|
|
header[l++] = '0';
|
|
|
|
header[l++] = '\n';
|
2012-01-05 15:34:13 +01:00
|
|
|
header[l++] = '0';
|
|
|
|
header[l++] = '\n';
|
|
|
|
header[l++] = '0';
|
|
|
|
header[l++] = '\n';
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2014-12-02 02:43:19 +01:00
|
|
|
r = loop_write(fd, header, l, false);
|
2014-06-18 23:56:03 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-12-23 20:50:48 +01:00
|
|
|
|
2018-03-22 17:04:29 +01:00
|
|
|
return TAKE_FD(fd);
|
2011-12-23 20:50:48 +01:00
|
|
|
}
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
|
|
|
|
int r;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
|
2020-03-06 13:57:50 +01:00
|
|
|
char p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
|
|
|
|
char sbuf[LINE_MAX + 8] = "MESSAGE=";
|
2012-04-02 19:24:30 +02:00
|
|
|
struct iovec iov[5];
|
|
|
|
char *f;
|
2020-03-06 13:57:50 +01:00
|
|
|
int len;
|
|
|
|
char *buffer = sbuf;
|
|
|
|
va_list aq;
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(priority >= 0, -EINVAL);
|
|
|
|
assert_return(priority <= 7, -EINVAL);
|
|
|
|
assert_return(format, -EINVAL);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2020-03-06 13:57:50 +01:00
|
|
|
va_copy(aq, ap);
|
|
|
|
len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
|
|
|
|
va_end(aq);
|
|
|
|
|
|
|
|
if (len >= (int)LONG_LINE_MAX - 8)
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
2020-05-24 23:00:13 +02:00
|
|
|
/* Allocate large buffer to accommodate big message */
|
2020-03-06 13:57:50 +01:00
|
|
|
if (len >= LINE_MAX) {
|
|
|
|
buffer = alloca(len + 9);
|
|
|
|
memcpy(buffer, "MESSAGE=", 8);
|
2020-06-10 19:45:33 +02:00
|
|
|
assert_se(vsnprintf(buffer + 8, len + 1, format, ap) == len);
|
2020-03-06 13:57:50 +01:00
|
|
|
}
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2016-07-19 14:27:05 +02:00
|
|
|
/* Strip trailing whitespace, keep prefixing whitespace */
|
|
|
|
(void) strstrip(buffer);
|
|
|
|
|
|
|
|
/* Suppress empty lines */
|
2020-03-06 13:57:50 +01:00
|
|
|
if (isempty(buffer + 8))
|
2016-07-19 14:27:05 +02:00
|
|
|
return 0;
|
2016-07-01 02:37:21 +02:00
|
|
|
|
2012-04-02 19:24:30 +02:00
|
|
|
/* func is initialized from __func__ which is not a macro, but
|
|
|
|
* a static const char[], hence cannot easily be prefixed with
|
|
|
|
* CODE_FUNC=, hence let's do it manually here. */
|
2012-11-20 21:22:18 +01:00
|
|
|
ALLOCA_CODE_FUNC(f, func);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[0] = IOVEC_MAKE_STRING(buffer);
|
|
|
|
iov[1] = IOVEC_MAKE_STRING(p);
|
|
|
|
iov[2] = IOVEC_MAKE_STRING(file);
|
|
|
|
iov[3] = IOVEC_MAKE_STRING(line);
|
|
|
|
iov[4] = IOVEC_MAKE_STRING(f);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
return sd_journal_sendv(iov, ELEMENTSOF(iov));
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
|
2017-09-21 13:52:34 +02:00
|
|
|
_cleanup_free_ struct iovec *iov = NULL;
|
2012-04-02 19:24:30 +02:00
|
|
|
int r, i, j;
|
|
|
|
va_list ap;
|
|
|
|
char *f;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
i = fill_iovec_sprintf(format, ap, 3, &iov);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (_unlikely_(i < 0)) {
|
|
|
|
r = i;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2012-11-20 21:22:18 +01:00
|
|
|
ALLOCA_CODE_FUNC(f, func);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[0] = IOVEC_MAKE_STRING(file);
|
|
|
|
iov[1] = IOVEC_MAKE_STRING(line);
|
|
|
|
iov[2] = IOVEC_MAKE_STRING(f);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
r = sd_journal_sendv(iov, i);
|
|
|
|
|
|
|
|
finish:
|
|
|
|
for (j = 3; j < i; j++)
|
|
|
|
free(iov[j].iov_base);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-07-31 16:09:01 +02:00
|
|
|
_public_ int sd_journal_sendv_with_location(
|
|
|
|
const char *file, const char *line,
|
|
|
|
const char *func,
|
|
|
|
const struct iovec *iov, int n) {
|
|
|
|
|
2012-04-02 19:24:30 +02:00
|
|
|
struct iovec *niov;
|
|
|
|
char *f;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(iov, -EINVAL);
|
|
|
|
assert_return(n > 0, -EINVAL);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2018-04-27 17:29:38 +02:00
|
|
|
niov = newa(struct iovec, n + 3);
|
2012-04-02 19:24:30 +02:00
|
|
|
memcpy(niov, iov, sizeof(struct iovec) * n);
|
|
|
|
|
2012-11-20 21:22:18 +01:00
|
|
|
ALLOCA_CODE_FUNC(f, func);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
niov[n++] = IOVEC_MAKE_STRING(file);
|
|
|
|
niov[n++] = IOVEC_MAKE_STRING(line);
|
|
|
|
niov[n++] = IOVEC_MAKE_STRING(f);
|
2012-04-02 19:24:30 +02:00
|
|
|
|
|
|
|
return sd_journal_sendv(niov, n);
|
|
|
|
}
|
2012-07-31 16:09:01 +02:00
|
|
|
|
|
|
|
_public_ int sd_journal_perror_with_location(
|
|
|
|
const char *file, const char *line,
|
|
|
|
const char *func,
|
|
|
|
const char *message) {
|
|
|
|
|
|
|
|
struct iovec iov[6];
|
|
|
|
char *f;
|
|
|
|
|
2012-11-20 21:22:18 +01:00
|
|
|
ALLOCA_CODE_FUNC(f, func);
|
2012-07-31 16:09:01 +02:00
|
|
|
|
2017-09-21 13:52:34 +02:00
|
|
|
iov[0] = IOVEC_MAKE_STRING(file);
|
|
|
|
iov[1] = IOVEC_MAKE_STRING(line);
|
|
|
|
iov[2] = IOVEC_MAKE_STRING(f);
|
2012-07-31 16:09:01 +02:00
|
|
|
|
|
|
|
return fill_iovec_perror_and_send(message, 3, iov);
|
|
|
|
}
|