chiark / gitweb /
journal: when sending journal data via file, place it in /dev/shm, to allow early...
[elogind.git] / src / journal / journal-send.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <errno.h>
25 #include <stddef.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "sd-journal.h"
30 #include "util.h"
31 #include "socket-util.h"
32
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 */
37
38 static int journal_fd(void) {
39         int fd;
40         static int fd_plus_one = 0;
41
42 retry:
43         if (fd_plus_one > 0)
44                 return fd_plus_one - 1;
45
46         fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
47         if (fd < 0)
48                 return -errno;
49
50         if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
51                 close_nointr_nofail(fd);
52                 goto retry;
53         }
54
55         return fd;
56 }
57
58 _public_ int sd_journal_print(int priority, const char *format, ...) {
59         int r;
60         va_list ap;
61
62         va_start(ap, format);
63         r = sd_journal_printv(priority, format, ap);
64         va_end(ap);
65
66         return r;
67 }
68
69 _public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
70         char buffer[8 + LINE_MAX], p[11];
71         struct iovec iov[2];
72
73         if (priority < 0 || priority > 7)
74                 return -EINVAL;
75
76         if (!format)
77                 return -EINVAL;
78
79         snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
80         char_array_0(p);
81
82         memcpy(buffer, "MESSAGE=", 8);
83         vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
84         char_array_0(buffer);
85
86         zero(iov);
87         IOVEC_SET_STRING(iov[0], buffer);
88         IOVEC_SET_STRING(iov[1], p);
89
90         return sd_journal_sendv(iov, 2);
91 }
92
93 _public_ int sd_journal_send(const char *format, ...) {
94         int r, n = 0, i = 0, j;
95         va_list ap;
96         struct iovec *iov = NULL;
97
98         va_start(ap, format);
99         while (format) {
100                 struct iovec *c;
101                 char *buffer;
102
103                 if (i >= n) {
104                         n = MAX(i*2, 4);
105                         c = realloc(iov, n * sizeof(struct iovec));
106                         if (!c) {
107                                 r = -ENOMEM;
108                                 goto fail;
109                         }
110
111                         iov = c;
112                 }
113
114                 if (vasprintf(&buffer, format, ap) < 0) {
115                         r = -ENOMEM;
116                         goto fail;
117                 }
118
119                 IOVEC_SET_STRING(iov[i++], buffer);
120
121                 format = va_arg(ap, char *);
122         }
123         va_end(ap);
124
125         r = sd_journal_sendv(iov, i);
126
127 fail:
128         for (j = 0; j < i; j++)
129                 free(iov[j].iov_base);
130
131         free(iov);
132
133         return r;
134 }
135
136 _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
137         int fd, buffer_fd;
138         struct iovec *w;
139         uint64_t *l;
140         int i, j = 0;
141         struct msghdr mh;
142         struct sockaddr_un sa;
143         ssize_t k;
144         union {
145                 struct cmsghdr cmsghdr;
146                 uint8_t buf[CMSG_SPACE(sizeof(int))];
147         } control;
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";
153
154         if (!iov || n <= 0)
155                 return -EINVAL;
156
157         w = alloca(sizeof(struct iovec) * n * 5);
158         l = alloca(sizeof(uint64_t) * n);
159
160         for (i = 0; i < n; i++) {
161                 char *c, *nl;
162
163                 if (!iov[i].iov_base ||
164                     iov[i].iov_len <= 1)
165                         return -EINVAL;
166
167                 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
168                 if (!c || c == iov[i].iov_base)
169                         return -EINVAL;
170
171                 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
172                 if (nl) {
173                         if (nl < c)
174                                 return -EINVAL;
175
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 */
180
181                         w[j].iov_base = iov[i].iov_base;
182                         w[j].iov_len = c - (char*) iov[i].iov_base;
183                         j++;
184
185                         IOVEC_SET_STRING(w[j++], "\n");
186
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);
190                         j++;
191
192                         w[j].iov_base = c + 1;
193                         w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
194                         j++;
195
196                 } else
197                         /* Nothing special? Then just add the line and
198                          * append a newline */
199                         w[j++] = iov[i];
200
201                 IOVEC_SET_STRING(w[j++], "\n");
202         }
203
204         fd = journal_fd();
205         if (fd < 0)
206                 return fd;
207
208         zero(sa);
209         sa.sun_family = AF_UNIX;
210         strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
211
212         zero(mh);
213         mh.msg_name = &sa;
214         mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
215         mh.msg_iov = w;
216         mh.msg_iovlen = j;
217
218         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
219         if (k >= 0)
220                 return 0;
221
222         if (errno != EMSGSIZE)
223                 return -errno;
224
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
227          * side */
228
229         buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
230         if (buffer_fd < 0)
231                 return -errno;
232
233         if (unlink(path) < 0) {
234                 close_nointr_nofail(buffer_fd);
235                 return -errno;
236         }
237
238         n = writev(buffer_fd, w, j);
239         if (n < 0)  {
240                 close_nointr_nofail(buffer_fd);
241                 return -errno;
242         }
243
244         mh.msg_iov = NULL;
245         mh.msg_iovlen = 0;
246
247         zero(control);
248         mh.msg_control = &control;
249         mh.msg_controllen = sizeof(control);
250
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));
256
257         mh.msg_controllen = cmsg->cmsg_len;
258
259         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
260         close_nointr_nofail(buffer_fd);
261
262         if (k < 0)
263                 return -errno;
264
265         return 0;
266 }
267
268 _public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
269         union sockaddr_union sa;
270         int fd;
271         char *header;
272         size_t l;
273         ssize_t r;
274
275         if (priority < 0 || priority > 7)
276                 return -EINVAL;
277
278         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
279         if (fd < 0)
280                 return -errno;
281
282         zero(sa);
283         sa.un.sun_family = AF_UNIX;
284         strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
285
286         r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
287         if (r < 0) {
288                 close_nointr_nofail(fd);
289                 return -errno;
290         }
291
292         if (shutdown(fd, SHUT_RD) < 0) {
293                 close_nointr_nofail(fd);
294                 return -errno;
295         }
296
297         if (!identifier)
298                 identifier = "";
299
300         l = strlen(identifier);
301         header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
302
303         memcpy(header, identifier, l);
304         header[l++] = '\n';
305         header[l++] = '0' + priority;
306         header[l++] = '\n';
307         header[l++] = '0' + !!level_prefix;
308         header[l++] = '\n';
309         header[l++] = '0';
310         header[l++] = '\n';
311         header[l++] = '0';
312         header[l++] = '\n';
313         header[l++] = '0';
314         header[l++] = '\n';
315
316         r = loop_write(fd, header, l, false);
317         if (r < 0) {
318                 close_nointr_nofail(fd);
319                 return (int) r;
320         }
321
322         if ((size_t) r != l) {
323                 close_nointr_nofail(fd);
324                 return -errno;
325         }
326
327         return fd;
328 }