journal-send.c revision 87b0284327e34a4b96c22085fa2cdb3219294991
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright 2011 Lennart Poettering
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (at your option) any later version.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#define SD_JOURNAL_SUPPRESS_LOCATION
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#define SNDBUF_SIZE (8*1024*1024)
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek#define ALLOCA_CODE_FUNC(f, func) \
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek char **_f = &(f); \
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek memcpy(*_f, "CODE_FUNC=", 10); \
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek/* We open a single fd, and we'll share it with the current process,
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek * all its threads, and all its subprocesses. This means we need to
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek * initialize it atomically, and need to operate on it atomically
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek * never assuming we are the only user */
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_print(int priority, const char *format, ...) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = sd_journal_printv(priority, format, ap);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek /* FIXME: Instead of limiting things to LINE_MAX we could do a
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt C99 variable-length array on the stack here in a loop. */
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_return(priority >= 0, -EINVAL);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_return(priority <= 7, -EINVAL);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek return sd_journal_sendv(iov, 2);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek int r, n = 0, i = 0, j;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek iov = malloc0(n * sizeof(struct iovec));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek c = realloc(iov, n * sizeof(struct iovec));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if (vasprintf(&buffer, format, aq) < 0) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iov[i++], buffer);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt for (j = 0; j < i; j++)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek_public_ int sd_journal_send(const char *format, ...) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek i = fill_iovec_sprintf(format, ap, 0, &iov);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek for (j = 0; j < i; j++)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek .sun_path = "/run/systemd/journal/socket",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek .msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path),
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek uint8_t buf[CMSG_SPACE(sizeof(int))];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek bool have_syslog_identifier = false;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek w = alloca(sizeof(struct iovec) * n * 5 + 3);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek l = alloca(sizeof(uint64_t) * n);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek for (i = 0; i < n; i++) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (_unlikely_(!c || c == iov[i].iov_base))
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek have_syslog_identifier = have_syslog_identifier ||
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (c == (char *) iov[i].iov_base + 17 &&
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* Already includes a newline? Bummer, then
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * let's write the variable name, then a
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek * newline, then the size (64bit LE), followed
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek * by the data and a final newline */
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek w[j].iov_base = iov[i].iov_base;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek w[j].iov_len = c - (char*) iov[i].iov_base;
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek w[j].iov_len = sizeof(uint64_t);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek /* Nothing special? Then just add the line and
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek * append a newline */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek string_is_safe(program_invocation_short_name)) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* Implicitly add program_invocation_short_name, if it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * is not set explicitly. We only do this for
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * program_invocation_short_name, and nothing else
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * since everything else is much nicer to retrieve
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * from the outside. */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(w[j++], "SYSLOG_IDENTIFIER=");
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(w[j++], program_invocation_short_name);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek k = sendmsg(fd, &mh, MSG_NOSIGNAL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* Fail silently if the journal is not available */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (errno != EMSGSIZE && errno != ENOBUFS)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* Message doesn't fit... Let's dump the data in a temporary
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * file and just pass a file descriptor of it to the other
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * We use /dev/shm instead of /tmp here, since we want this to
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt * be a tmpfs, and one that is available from early boot on
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * and where unprivileged users can create files. */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek buffer_fd = open_tmpfile("/dev/shm", O_RDWR | O_CLOEXEC);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek mh.msg_controllen = sizeof(control);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek cmsg->cmsg_len = CMSG_LEN(sizeof(int));
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek mh.msg_controllen = cmsg->cmsg_len;
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek k = sendmsg(fd, &mh, MSG_NOSIGNAL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek k = isempty(message) ? 0 : strlen(message) + 2;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt char error[6 + 10 + 1]; /* for a 32bit value */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek memmove(buffer + 8 + k, j, strlen(j)+1);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek memcpy(buffer + 8, message, k - 2);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek memcpy(buffer + 8 + k - 2, ": ", 2);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt snprintf(error, sizeof(error), "ERRNO=%u", _saved_errno_);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iov[skip+1], buffer);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iov[skip+2], error);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return sd_journal_sendv(iov, skip + 3);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_perror(const char *message) {
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek return fill_iovec_perror_and_send(message, 0, iovec);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek .un.sun_path = "/run/systemd/journal/stdout",
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek assert_return(priority >= 0, -EINVAL);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek assert_return(priority <= 7, -EINVAL);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek if (shutdown(fd, SHUT_RD) < 0) {
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek header[l++] = '\n'; /* unit id */
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek header[l++] = '0' + !!level_prefix;
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek r = loop_write(fd, header, l, false);
a3152e7655231b94fa7b9582906fb86ab00b9c99Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek char buffer[8 + LINE_MAX], p[11];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_return(priority >= 0, -EINVAL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_return(priority <= 7, -EINVAL);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek /* func is initialized from __func__ which is not a macro, but
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek * a static const char[], hence cannot easily be prefixed with
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek * CODE_FUNC=, hence let's do it manually here. */
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iov[0], buffer);
30776485c5bc2d9c356e875f2aee874d22c393b7Zbigniew Jędrzejewski-Szmek return sd_journal_sendv(iov, ELEMENTSOF(iov));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek i = fill_iovec_sprintf(format, ap, 3, &iov);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek for (j = 3; j < i; j++)
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek_public_ int sd_journal_sendv_with_location(
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek const char *file, const char *line,
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek const struct iovec *iov, int n) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek niov = alloca(sizeof(struct iovec) * (n + 3));
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering memcpy(niov, iov, sizeof(struct iovec) * n);
1af719edc5958c01c19204fb68d6fc45c9eea85cZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(niov[n++], file);
1af719edc5958c01c19204fb68d6fc45c9eea85cZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(niov[n++], line);
1af719edc5958c01c19204fb68d6fc45c9eea85cZbigniew Jędrzejewski-Szmek return sd_journal_sendv(niov, n);
dad29dff1925a114e20d4eb7b47fca23c4f25fd7Lennart Poettering_public_ int sd_journal_perror_with_location(