journal-send.c revision 0dad12c190b7493955cd60d2a1625199b1709f69
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/>.
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering/* We open a single fd, and we'll share it with the current process,
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart 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
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * never assuming we are the only user */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int journal_fd(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = sd_journal_printv(priority, format, ap);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen_public_ int sd_journal_send(const char *format, ...) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int r, n = 0, i = 0, j;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c = realloc(iov, n * sizeof(struct iovec));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (vasprintf(&buffer, format, ap) < 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering for (j = 0; j < i; j++)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!iov || n <= 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering for (i = 0; i < n; i++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Already includes a newline? Bummer, then
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * let's write the variable name, then a
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * newline, then the size (64bit LE), followed
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * by the data and a final newline */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Nothing special? Then just add the line and
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering * append a newline */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Message doesn't fit... Let's dump the data in a temporary
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering * file and just pass a file descriptor of it to the other
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return (int) r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if ((size_t) r != l) {