chiark / gitweb /
journal: if the data to be sent is larger than the maximum datagram size resort to...
[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         char path[] = "/tmp/journal.XXXXXX";
144         ssize_t k;
145         union {
146                 struct cmsghdr cmsghdr;
147                 uint8_t buf[CMSG_SPACE(sizeof(int))];
148         } control;
149         struct cmsghdr *cmsg;
150
151         if (!iov || n <= 0)
152                 return -EINVAL;
153
154         w = alloca(sizeof(struct iovec) * n * 5);
155         l = alloca(sizeof(uint64_t) * n);
156
157         for (i = 0; i < n; i++) {
158                 char *c, *nl;
159
160                 if (!iov[i].iov_base ||
161                     iov[i].iov_len <= 1)
162                         return -EINVAL;
163
164                 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
165                 if (!c || c == iov[i].iov_base)
166                         return -EINVAL;
167
168                 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
169                 if (nl) {
170                         if (nl < c)
171                                 return -EINVAL;
172
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 */
177
178                         w[j].iov_base = iov[i].iov_base;
179                         w[j].iov_len = c - (char*) iov[i].iov_base;
180                         j++;
181
182                         IOVEC_SET_STRING(w[j++], "\n");
183
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);
187                         j++;
188
189                         w[j].iov_base = c + 1;
190                         w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
191                         j++;
192
193                 } else
194                         /* Nothing special? Then just add the line and
195                          * append a newline */
196                         w[j++] = iov[i];
197
198                 IOVEC_SET_STRING(w[j++], "\n");
199         }
200
201         fd = journal_fd();
202         if (fd < 0)
203                 return fd;
204
205         zero(sa);
206         sa.sun_family = AF_UNIX;
207         strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
208
209         zero(mh);
210         mh.msg_name = &sa;
211         mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
212         mh.msg_iov = w;
213         mh.msg_iovlen = j;
214
215         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
216         if (k >= 0)
217                 return 0;
218
219         if (errno != EMSGSIZE)
220                 return -errno;
221
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
224          * side */
225
226         buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
227         if (buffer_fd < 0)
228                 return -errno;
229
230         if (unlink(path) < 0) {
231                 close_nointr_nofail(buffer_fd);
232                 return -errno;
233         }
234
235         n = writev(buffer_fd, w, j);
236         if (n < 0)  {
237                 close_nointr_nofail(buffer_fd);
238                 return -errno;
239         }
240
241         mh.msg_iov = NULL;
242         mh.msg_iovlen = 0;
243
244         zero(control);
245         mh.msg_control = &control;
246         mh.msg_controllen = sizeof(control);
247
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));
253
254         mh.msg_controllen = cmsg->cmsg_len;
255
256         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
257         close_nointr_nofail(buffer_fd);
258
259         if (k < 0)
260                 return -errno;
261
262         return 0;
263 }
264
265 _public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
266         union sockaddr_union sa;
267         int fd;
268         char *header;
269         size_t l;
270         ssize_t r;
271
272         if (priority < 0 || priority > 7)
273                 return -EINVAL;
274
275         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
276         if (fd < 0)
277                 return -errno;
278
279         zero(sa);
280         sa.un.sun_family = AF_UNIX;
281         strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
282
283         r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
284         if (r < 0) {
285                 close_nointr_nofail(fd);
286                 return -errno;
287         }
288
289         if (shutdown(fd, SHUT_RD) < 0) {
290                 close_nointr_nofail(fd);
291                 return -errno;
292         }
293
294         if (!identifier)
295                 identifier = "";
296
297         l = strlen(identifier);
298         header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
299
300         memcpy(header, identifier, l);
301         header[l++] = '\n';
302         header[l++] = '0' + priority;
303         header[l++] = '\n';
304         header[l++] = '0' + !!level_prefix;
305         header[l++] = '\n';
306         header[l++] = '0';
307         header[l++] = '\n';
308         header[l++] = '0';
309         header[l++] = '\n';
310         header[l++] = '0';
311         header[l++] = '\n';
312
313         r = loop_write(fd, header, l, false);
314         if (r < 0) {
315                 close_nointr_nofail(fd);
316                 return (int) r;
317         }
318
319         if ((size_t) r != l) {
320                 close_nointr_nofail(fd);
321                 return -errno;
322         }
323
324         return fd;
325 }