1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
29 #include "sd-journal.h"
31 #include "socket-util.h"
33 /* We open a single fd, and we'll share it with the current process,
34 * all its threads, and all its subprocesses. This means we need to
35 * initialize it atomically, and need to operate on it atomically
36 * never assuming we are the only user */
38 static int journal_fd(void) {
40 static int fd_plus_one = 0;
44 return fd_plus_one - 1;
46 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
50 if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
51 close_nointr_nofail(fd);
58 _public_ int sd_journal_print(int priority, const char *format, ...) {
63 r = sd_journal_printv(priority, format, ap);
69 _public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
70 char buffer[8 + LINE_MAX], p[11];
73 if (priority < 0 || priority > 7)
79 snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
82 memcpy(buffer, "MESSAGE=", 8);
83 vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
87 IOVEC_SET_STRING(iov[0], buffer);
88 IOVEC_SET_STRING(iov[1], p);
90 return sd_journal_sendv(iov, 2);
93 _public_ int sd_journal_send(const char *format, ...) {
94 int r, n = 0, i = 0, j;
96 struct iovec *iov = NULL;
105 c = realloc(iov, n * sizeof(struct iovec));
114 if (vasprintf(&buffer, format, ap) < 0) {
119 IOVEC_SET_STRING(iov[i++], buffer);
121 format = va_arg(ap, char *);
125 r = sd_journal_sendv(iov, i);
128 for (j = 0; j < i; j++)
129 free(iov[j].iov_base);
136 _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
142 struct sockaddr_un sa;
145 struct cmsghdr cmsghdr;
146 uint8_t buf[CMSG_SPACE(sizeof(int))];
148 struct cmsghdr *cmsg;
149 /* We use /dev/shm instead of /tmp here, since we want this to
150 * be a tmpfs, and one that is available from early boot on
151 * and where unprivileged users can create files. */
152 char path[] = "/dev/shm/journal.XXXXXX";
157 w = alloca(sizeof(struct iovec) * n * 5);
158 l = alloca(sizeof(uint64_t) * n);
160 for (i = 0; i < n; i++) {
163 if (!iov[i].iov_base ||
167 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
168 if (!c || c == iov[i].iov_base)
171 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
176 /* Already includes a newline? Bummer, then
177 * let's write the variable name, then a
178 * newline, then the size (64bit LE), followed
179 * by the data and a final newline */
181 w[j].iov_base = iov[i].iov_base;
182 w[j].iov_len = c - (char*) iov[i].iov_base;
185 IOVEC_SET_STRING(w[j++], "\n");
187 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
188 w[j].iov_base = &l[i];
189 w[j].iov_len = sizeof(uint64_t);
192 w[j].iov_base = c + 1;
193 w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
197 /* Nothing special? Then just add the line and
198 * append a newline */
201 IOVEC_SET_STRING(w[j++], "\n");
209 sa.sun_family = AF_UNIX;
210 strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
214 mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
218 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
222 if (errno != EMSGSIZE)
225 /* Message doesn't fit... Let's dump the data in a temporary
226 * file and just pass a file descriptor of it to the other
229 buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
233 if (unlink(path) < 0) {
234 close_nointr_nofail(buffer_fd);
238 n = writev(buffer_fd, w, j);
240 close_nointr_nofail(buffer_fd);
248 mh.msg_control = &control;
249 mh.msg_controllen = sizeof(control);
251 cmsg = CMSG_FIRSTHDR(&mh);
252 cmsg->cmsg_level = SOL_SOCKET;
253 cmsg->cmsg_type = SCM_RIGHTS;
254 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
255 memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
257 mh.msg_controllen = cmsg->cmsg_len;
259 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
260 close_nointr_nofail(buffer_fd);
268 _public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
269 union sockaddr_union sa;
275 if (priority < 0 || priority > 7)
278 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
283 sa.un.sun_family = AF_UNIX;
284 strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
286 r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
288 close_nointr_nofail(fd);
292 if (shutdown(fd, SHUT_RD) < 0) {
293 close_nointr_nofail(fd);
300 l = strlen(identifier);
301 header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
303 memcpy(header, identifier, l);
305 header[l++] = '0' + priority;
307 header[l++] = '0' + !!level_prefix;
316 r = loop_write(fd, header, l, false);
318 close_nointr_nofail(fd);
322 if ((size_t) r != l) {
323 close_nointr_nofail(fd);