7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering This file is part of systemd.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright 2011 Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (at your option) any later version.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is distributed in the hope that it will be useful, but
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering You should have received a copy of the GNU Lesser General Public License
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering char **_f = &(f); \
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering } while(false)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* We open a single fd, and we'll share it with the current process,
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * all its threads, and all its subprocesses. This means we need to
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * initialize it atomically, and need to operate on it atomically
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * never assuming we are the only user */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_print(int priority, const char *format, ...) {
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering r = sd_journal_printv(priority, format, ap);
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* FIXME: Instead of limiting things to LINE_MAX we could do a
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering C99 variable-length array on the stack here in a loop. */
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek char buffer[8 + LINE_MAX], p[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority >= 0, -EINVAL);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority <= 7, -EINVAL);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(format, -EINVAL);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
44b601bc79e46722bc0f0862ee0ce34a2284ef11Lennart Poettering_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
25d042e81516246b1ebf706a57c47ac19abb0b8aLennart Poettering int r, n = 0, i = 0, j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = realloc(iov, n * sizeof(struct iovec));
72f1d5a2880d103dc1c1746f5c02e192e054705eLennart Poettering if (vasprintf(&buffer, format, aq) < 0) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering for (j = 0; j < i; j++)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_send(const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering i = fill_iovec_sprintf(format, ap, 0, &iov);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < i; j++)
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
61c024b328d5493a334242a4d01ba923582093faZbigniew Jędrzejewski-Szmek _cleanup_close_ int buffer_fd = -1;
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .sun_path = "/run/systemd/journal/socket",
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path),
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(n > 0, -EINVAL);
9a7800af088cc013573310504ae76e325d44d4b4Zbigniew Jędrzejewski-Szmek w = newa(struct iovec, n * 5 + 3);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (i = 0; i < n; i++) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (_unlikely_(!c || c == iov[i].iov_base))
29abad107f8610e73b2fc091216040b579c75453Zbigniew Jędrzejewski-Szmek have_syslog_identifier = have_syslog_identifier ||
29abad107f8610e73b2fc091216040b579c75453Zbigniew Jędrzejewski-Szmek (c == (char *) iov[i].iov_base + 17 &&
2a0e0692565f0435657c93498e09cbb2d3517152Shawn Landden startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Already includes a newline? Bummer, then
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * let's write the variable name, then a
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * newline, then the size (64bit LE), followed
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * by the data and a final newline */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_len = c - (char*) iov[i].iov_base;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Nothing special? Then just add the line and
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * append a newline */
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering string_is_safe(program_invocation_short_name)) {
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering /* Implicitly add program_invocation_short_name, if it
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * is not set explicitly. We only do this for
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * program_invocation_short_name, and nothing else
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * since everything else is much nicer to retrieve
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering * from the outside. */
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering IOVEC_SET_STRING(w[j++], "SYSLOG_IDENTIFIER=");
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering IOVEC_SET_STRING(w[j++], program_invocation_short_name);
6c045c0b4c49c88a1d3b9360c05efa5084796d2dZbigniew Jędrzejewski-Szmek /* Fail silently if the journal is not available */
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering if (errno != EMSGSIZE && errno != ENOBUFS)
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering /* Message doesn't fit... Let's dump the data in a memfd or
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering * temporary file and just pass a file descriptor of it to the
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering * For the temporary files we use /dev/shm instead of /tmp
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering * here, since we want this to be a tmpfs, and one that is
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering * available from early boot on and where unprivileged users
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering * can create files. */
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering buffer_fd = open_tmpfile("/dev/shm", O_RDWR | O_CLOEXEC);
4941e4aca907f26bf74aa9efe1c70ccad1d10a82Zbigniew Jędrzejewski-Szmek r = send_one_fd_sa(fd, buffer_fd, mh.msg_name, mh.msg_namelen, 0);
4941e4aca907f26bf74aa9efe1c70ccad1d10a82Zbigniew Jędrzejewski-Szmek /* Fail silently if the journal is not available */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poetteringstatic int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering k = isempty(message) ? 0 : strlen(message) + 2;
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek char error[sizeof("ERRNO=")-1 + DECIMAL_STR_MAX(int) + 1];
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek xsprintf(error, "ERRNO=%i", _saved_errno_);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering IOVEC_SET_STRING(iov[skip+0], "PRIORITY=3");
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_perror(const char *message) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering return fill_iovec_perror_and_send(message, 0, iovec);
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .un.sun_path = "/run/systemd/journal/stdout",
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority >= 0, -EINVAL);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority <= 7, -EINVAL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
8b38f3cc3eb73adf9536cb73d0f319e60d42ea0cLennart Poettering header = alloca(l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
553acb7b6b8d4f16a4747b1f978e8b7888fbfb2cZbigniew Jędrzejewski-Szmek r = loop_write(fd, header, l, false);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart 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) {
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek char buffer[8 + LINE_MAX], p[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority >= 0, -EINVAL);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(priority <= 7, -EINVAL);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(format, -EINVAL);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* func is initialized from __func__ which is not a macro, but
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering * a static const char[], hence cannot easily be prefixed with
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering * CODE_FUNC=, hence let's do it manually here. */
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return sd_journal_sendv(iov, ELEMENTSOF(iov));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering i = fill_iovec_sprintf(format, ap, 3, &iov);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering for (j = 3; j < i; j++)
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_sendv_with_location(
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen assert_return(n > 0, -EINVAL);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering niov = alloca(sizeof(struct iovec) * (n + 3));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering memcpy(niov, iov, sizeof(struct iovec) * n);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering_public_ int sd_journal_perror_with_location(