]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journal-send.c
journal: add native protocol to journald, and client side API to send journal messages
[thirdparty/systemd.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
27 #include "sd-journal.h"
28 #include "util.h"
29
30 /* We open a single fd, and we'll share it with the current process,
31 * all its threads, and all its subprocesses. This means we need to
32 * initialize it atomically, and need to operate on it atomically
33 * never assuming we are the only user */
34
35 static int journal_fd(void) {
36 int fd;
37 static int fd_plus_one = 0;
38
39 retry:
40 if (fd_plus_one > 0)
41 return fd_plus_one - 1;
42
43 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
44 if (fd < 0)
45 return -errno;
46
47 if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
48 close_nointr_nofail(fd);
49 goto retry;
50 }
51
52 return fd;
53 }
54
55 int sd_journal_print(const char *format, ...) {
56 int r;
57 va_list ap;
58
59 va_start(ap, format);
60 r = sd_journal_printv(format, ap);
61 va_end(ap);
62
63 return r;
64 }
65
66 int sd_journal_printv(const char *format, va_list ap) {
67 char buffer[8 + LINE_MAX];
68 struct iovec iov;
69
70 memcpy(buffer, "MESSAGE=", 8);
71 vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
72
73 char_array_0(buffer);
74
75 zero(iov);
76 IOVEC_SET_STRING(iov, buffer);
77
78 return sd_journal_sendv(&iov, 1);
79 }
80
81 int sd_journal_send(const char *format, ...) {
82 int r, n = 0, i = 0, j;
83 va_list ap;
84 struct iovec *iov = NULL;
85
86 va_start(ap, format);
87 while (format) {
88 struct iovec *c;
89 char *buffer;
90
91 if (i >= n) {
92 n = MAX(i*2, 4);
93 c = realloc(iov, n * sizeof(struct iovec));
94 if (!c) {
95 r = -ENOMEM;
96 goto fail;
97 }
98
99 iov = c;
100 }
101
102 if (vasprintf(&buffer, format, ap) < 0) {
103 r = -ENOMEM;
104 goto fail;
105 }
106
107 IOVEC_SET_STRING(iov[i++], buffer);
108
109 format = va_arg(ap, char *);
110 }
111 va_end(ap);
112
113 r = sd_journal_sendv(iov, i);
114
115 fail:
116 for (j = 0; j < i; j++)
117 free(iov[j].iov_base);
118
119 free(iov);
120
121 return r;
122 }
123
124 int sd_journal_sendv(const struct iovec *iov, int n) {
125 int fd;
126 struct iovec *w;
127 uint64_t *l;
128 int i, j = 0;
129 struct msghdr mh;
130 struct sockaddr_un sa;
131
132 if (!iov || n <= 0)
133 return -EINVAL;
134
135 w = alloca(sizeof(struct iovec) * n * 5);
136 l = alloca(sizeof(uint64_t) * n);
137
138 for (i = 0; i < n; i++) {
139 char *c, *nl;
140
141 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
142 if (!c)
143 return -EINVAL;
144
145 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
146 if (nl) {
147 if (nl < c)
148 return -EINVAL;
149
150 /* Already includes a newline? Bummer, then
151 * let's write the variable name, then a
152 * newline, then the size (64bit LE), followed
153 * by the data and a final newline */
154
155 w[j].iov_base = iov[i].iov_base;
156 w[j].iov_len = c - (char*) iov[i].iov_base;
157 j++;
158
159 IOVEC_SET_STRING(w[j++], "\n");
160
161 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
162 w[j].iov_base = &l[i];
163 w[j].iov_len = sizeof(uint64_t);
164 j++;
165
166 w[j].iov_base = c + 1;
167 w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
168 j++;
169
170 } else
171 /* Nothing special? Then just add the line and
172 * append a newline */
173 w[j++] = iov[i];
174
175 IOVEC_SET_STRING(w[j++], "\n");
176 }
177
178 fd = journal_fd();
179 if (fd < 0)
180 return fd;
181
182 zero(sa);
183 sa.sun_family = AF_UNIX;
184 strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));
185
186 zero(mh);
187 mh.msg_name = &sa;
188 mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
189 mh.msg_iov = w;
190 mh.msg_iovlen = j;
191
192 if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
193 return -errno;
194
195 return 0;
196 }