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;
143 char path[] = "/tmp/journal.XXXXXX";
146 struct cmsghdr cmsghdr;
147 uint8_t buf[CMSG_SPACE(sizeof(int))];
149 struct cmsghdr *cmsg;
154 w = alloca(sizeof(struct iovec) * n * 5);
155 l = alloca(sizeof(uint64_t) * n);
157 for (i = 0; i < n; i++) {
160 if (!iov[i].iov_base ||
164 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
165 if (!c || c == iov[i].iov_base)
168 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
173 /* Already includes a newline? Bummer, then
174 * let's write the variable name, then a
175 * newline, then the size (64bit LE), followed
176 * by the data and a final newline */
178 w[j].iov_base = iov[i].iov_base;
179 w[j].iov_len = c - (char*) iov[i].iov_base;
182 IOVEC_SET_STRING(w[j++], "\n");
184 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
185 w[j].iov_base = &l[i];
186 w[j].iov_len = sizeof(uint64_t);
189 w[j].iov_base = c + 1;
190 w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
194 /* Nothing special? Then just add the line and
195 * append a newline */
198 IOVEC_SET_STRING(w[j++], "\n");
206 sa.sun_family = AF_UNIX;
207 strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
211 mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
215 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
219 if (errno != EMSGSIZE)
222 /* Message doesn't fit... Let's dump the data in a temporary
223 * file and just pass a file descriptor of it to the other
226 buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
230 if (unlink(path) < 0) {
231 close_nointr_nofail(buffer_fd);
235 n = writev(buffer_fd, w, j);
237 close_nointr_nofail(buffer_fd);
245 mh.msg_control = &control;
246 mh.msg_controllen = sizeof(control);
248 cmsg = CMSG_FIRSTHDR(&mh);
249 cmsg->cmsg_level = SOL_SOCKET;
250 cmsg->cmsg_type = SCM_RIGHTS;
251 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
252 memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
254 mh.msg_controllen = cmsg->cmsg_len;
256 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
257 close_nointr_nofail(buffer_fd);
265 _public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
266 union sockaddr_union sa;
272 if (priority < 0 || priority > 7)
275 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
280 sa.un.sun_family = AF_UNIX;
281 strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
283 r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
285 close_nointr_nofail(fd);
289 if (shutdown(fd, SHUT_RD) < 0) {
290 close_nointr_nofail(fd);
297 l = strlen(identifier);
298 header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
300 memcpy(header, identifier, l);
302 header[l++] = '0' + priority;
304 header[l++] = '0' + !!level_prefix;
313 r = loop_write(fd, header, l, false);
315 close_nointr_nofail(fd);
319 if ((size_t) r != l) {
320 close_nointr_nofail(fd);