journal-send.c revision ee55db41442ad8055f5a84a339b1e0e22bc037c4
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char **_f = &(f); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } while(false)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/* We open a single fd, and we'll share it with the current process,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * all its threads, and all its subprocesses. This means we need to
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * initialize it atomically, and need to operate on it atomically
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * never assuming we are the only user */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int journal_fd(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen_public_ int sd_journal_print(int priority, const char *format, ...) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering r = sd_journal_printv(priority, format, ap);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering /* FIXME: Instead of limiting things to LINE_MAX we could do a
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering C99 variable-length array on the stack here in a loop. */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering char buffer[8 + LINE_MAX], p[11]; struct iovec iov[2];
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering int r, n = 0, i = 0, j;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering c = realloc(iov, n * sizeof(struct iovec));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (j = 0; j < i; j++)
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen_public_ int sd_journal_send(const char *format, ...) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering for (j = 0; j < i; j++)
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering int r, i, j = 0;
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering /* We use /dev/shm instead of /tmp here, since we want this to
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering * be a tmpfs, and one that is available from early boot on
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering * and where unprivileged users can create files. */
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering w = alloca(sizeof(struct iovec) * n * 5 + 3);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1)) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(!c || c == iov[i].iov_base)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering have_syslog_identifier || (c == iov[i].iov_base + 17 && memcmp(iov[i].iov_base, "SYSLOG_IDENTIFIER", 17) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Already includes a newline? Bummer, then
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * let's write the variable name, then a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * newline, then the size (64bit LE), followed
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * by the data and a final newline */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Nothing special? Then just add the line and
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * append a newline */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering string_is_safe(program_invocation_short_name)) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* Implicitly add program_invocation_short_name, if it
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * is not set explicitly. We only do this for
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * program_invocation_short_name, and nothing else
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering * since everything else is much nicer to retrieve
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * from the outside. */
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering IOVEC_SET_STRING(w[j++], "SYSLOG_IDENTIFIER=");
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering IOVEC_SET_STRING(w[j++], program_invocation_short_name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (errno != EMSGSIZE && errno != ENOBUFS) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Message doesn't fit... Let's dump the data in a temporary
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * file and just pass a file descriptor of it to the other
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poetteringstatic int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering k = isempty(message) ? 0 : strlen(message) + 2;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering j = strerror_r(saved_errno, buffer + 8 + k, n - 8 - k);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char error[6 + 10 + 1]; /* for a 32bit value */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering snprintf(error, sizeof(error), "ERRNO=%u", saved_errno);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering_public_ int sd_journal_perror(const char *message) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return fill_iovec_perror_and_send(message, 0, iovec);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return (int) r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if ((size_t) r != l) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart 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) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
if (_unlikely_(i < 0)) {
goto finish;
const char *func,
return -EINVAL;
if (_unlikely_(n <= 0))
return -EINVAL;
const char *func,
const char *message) {