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
#define SNDBUF_SIZE (8*1024*1024)
35
/* We open a single fd, and we'll share it with the current process,
36
* all its threads, and all its subprocesses. This means we need to
37
* initialize it atomically, and need to operate on it atomically
38
* never assuming we are the only user */
40
static int journal_fd(void) {
42
static int fd_plus_one = 0;
46
return fd_plus_one - 1;
48
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
52
fd_inc_sndbuf(fd, SNDBUF_SIZE);
54
if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
55
close_nointr_nofail(fd);
62
_public_ int sd_journal_print(int priority, const char *format, ...) {
67
r = sd_journal_printv(priority, format, ap);
73
_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
74
char buffer[8 + LINE_MAX], p[11];
77
if (priority < 0 || priority > 7)
83
snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
86
memcpy(buffer, "MESSAGE=", 8);
87
vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
91
IOVEC_SET_STRING(iov[0], buffer);
92
IOVEC_SET_STRING(iov[1], p);
94
return sd_journal_sendv(iov, 2);
97
_public_ int sd_journal_send(const char *format, ...) {
98
int r, n = 0, i = 0, j;
100
struct iovec *iov = NULL;
102
va_start(ap, format);
109
c = realloc(iov, n * sizeof(struct iovec));
118
if (vasprintf(&buffer, format, ap) < 0) {
123
IOVEC_SET_STRING(iov[i++], buffer);
125
format = va_arg(ap, char *);
129
r = sd_journal_sendv(iov, i);
132
for (j = 0; j < i; j++)
133
free(iov[j].iov_base);
140
_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
146
struct sockaddr_un sa;
149
struct cmsghdr cmsghdr;
150
uint8_t buf[CMSG_SPACE(sizeof(int))];
152
struct cmsghdr *cmsg;
153
/* We use /dev/shm instead of /tmp here, since we want this to
154
* be a tmpfs, and one that is available from early boot on
155
* and where unprivileged users can create files. */
156
char path[] = "/dev/shm/journal.XXXXXX";
161
w = alloca(sizeof(struct iovec) * n * 5);
162
l = alloca(sizeof(uint64_t) * n);
164
for (i = 0; i < n; i++) {
167
if (!iov[i].iov_base ||
171
c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
172
if (!c || c == iov[i].iov_base)
175
nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
180
/* Already includes a newline? Bummer, then
181
* let's write the variable name, then a
182
* newline, then the size (64bit LE), followed
183
* by the data and a final newline */
185
w[j].iov_base = iov[i].iov_base;
186
w[j].iov_len = c - (char*) iov[i].iov_base;
189
IOVEC_SET_STRING(w[j++], "\n");
191
l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
192
w[j].iov_base = &l[i];
193
w[j].iov_len = sizeof(uint64_t);
196
w[j].iov_base = c + 1;
197
w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
201
/* Nothing special? Then just add the line and
202
* append a newline */
205
IOVEC_SET_STRING(w[j++], "\n");
213
sa.sun_family = AF_UNIX;
214
strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
218
mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
222
k = sendmsg(fd, &mh, MSG_NOSIGNAL);
226
if (errno != EMSGSIZE && errno != ENOBUFS)
229
/* Message doesn't fit... Let's dump the data in a temporary
230
* file and just pass a file descriptor of it to the other
233
buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
237
if (unlink(path) < 0) {
238
close_nointr_nofail(buffer_fd);
242
n = writev(buffer_fd, w, j);
244
close_nointr_nofail(buffer_fd);
252
mh.msg_control = &control;
253
mh.msg_controllen = sizeof(control);
255
cmsg = CMSG_FIRSTHDR(&mh);
256
cmsg->cmsg_level = SOL_SOCKET;
257
cmsg->cmsg_type = SCM_RIGHTS;
258
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
259
memcpy(CMSG_DATA(cmsg), &buffer_fd, sizeof(int));
261
mh.msg_controllen = cmsg->cmsg_len;
263
k = sendmsg(fd, &mh, MSG_NOSIGNAL);
264
close_nointr_nofail(buffer_fd);
272
_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
273
union sockaddr_union sa;
279
if (priority < 0 || priority > 7)
282
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
287
sa.un.sun_family = AF_UNIX;
288
strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
290
r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
292
close_nointr_nofail(fd);
296
if (shutdown(fd, SHUT_RD) < 0) {
297
close_nointr_nofail(fd);
301
fd_inc_sndbuf(fd, SNDBUF_SIZE);
306
l = strlen(identifier);
307
header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
309
memcpy(header, identifier, l);
311
header[l++] = '0' + priority;
313
header[l++] = '0' + !!level_prefix;
322
r = loop_write(fd, header, l, false);
324
close_nointr_nofail(fd);
328
if ((size_t) r != l) {
329
close_nointr_nofail(fd);