journal-send.c revision d0bbc21caa6e68693a47db60c93e99422bf2a858
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 General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2 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 General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
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);
19b50b5ba7ee8c1bfb330377309e4bab7a7531d8Lennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint sd_journal_print(int priority, const char *format, ...) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_journal_printv(priority, format, ap);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint sd_journal_printv(int priority, const char *format, va_list ap) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint sd_journal_send(const char *format, ...) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int r, n = 0, i = 0, j;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (vasprintf(&buffer, format, ap) < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt for (j = 0; j < i; j++)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint sd_journal_sendv(const struct iovec *iov, int n) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!iov || n <= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < n; i++) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen * by the data and a final newline */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
d6731e4c7964ee2860d4f5abdb0b52acd7a66960Tom Gundersen l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering 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
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen * append a newline */
6f4dedb250f2d607eceefaa491f338becbeee7c0Tom Gundersen w[j++] = iov[i];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));