journal-send.c revision b070e7f3c9ed680c821bd89d42506695f2438506
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/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/* 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
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart 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) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart 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 Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = sd_journal_printv(priority, format, ap);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering int r, n = 0, i, j;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering c = realloc(iov, n * sizeof(struct iovec));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (j = 0; j < i; j++)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering_public_ int sd_journal_send(const char *format, ...) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering for (j = 0; j < i; j++)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering int r, i, j = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* We use /dev/shm instead of /tmp here, since we want this to
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * be a tmpfs, and one that is available from early boot on
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * and where unprivileged users can create files. */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering w = alloca(sizeof(struct iovec) * n * 5);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering for (i = 0; i < n; i++) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1)) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (_unlikely_(!c || c == iov[i].iov_base)) {
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);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart 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);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (errno != EMSGSIZE && errno != ENOBUFS) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering /* Message doesn't fit... Let's dump the data in a temporary
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering * file and just pass a file descriptor of it to the other
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart 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) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return (int) r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if ((size_t) r != l) {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart 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) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering /* func is initialized from __func__ which is not a macro, but
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * a static const char[], hence cannot easily be prefixed with
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * CODE_FUNC=, hence let's do it manually here. */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_journal_sendv(iov, ELEMENTSOF(iov));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering i = fill_iovec_sprintf(format, ap, 3, &iov);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering for (j = 3; j < i; j++)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering_public_ int sd_journal_sendv_with_location(const char *file, const char *line, const char *func, const struct iovec *iov, int n) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering niov = alloca(sizeof(struct iovec) * (n + 3));
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering memcpy(niov, iov, sizeof(struct iovec) * n);