journal-send.c revision fe6521272ba203ec8f0d5a94f0729960b3f90525
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
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 General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen/* We open a single fd, and we'll share it with the current process,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * all its threads, and all its subprocesses. This means we need to
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * initialize it atomically, and need to operate on it atomically
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * never assuming we are the only user */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int journal_fd(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint sd_journal_print(int priority, const char *format, ...) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_journal_printv(priority, format, ap);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenint sd_journal_printv(int priority, const char *format, va_list ap) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int r, n = 0, i = 0, j;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (i >= n) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering for (j = 0; j < i; j++)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poetteringint sd_journal_sendv(const struct iovec *iov, int n) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (!iov || n <= 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering for (i = 0; i < n; i++) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Already includes a newline? Bummer, then
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * let's write the variable name, then a
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * newline, then the size (64bit LE), followed
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * by the data and a final newline */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Nothing special? Then just add the line and
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * append a newline */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringint sd_journal_stream_fd(const char *tag, int priority, int priority_prefix) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return (int) r;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if ((size_t) r != l) {