]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journal-send.c
Merge branch 'journal'
[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 #include "socket-util.h"
30
31 /* We open a single fd, and we'll share it with the current process,
32 * all its threads, and all its subprocesses. This means we need to
33 * initialize it atomically, and need to operate on it atomically
34 * never assuming we are the only user */
35
36 static int journal_fd(void) {
37 int fd;
38 static int fd_plus_one = 0;
39
40 retry:
41 if (fd_plus_one > 0)
42 return fd_plus_one - 1;
43
44 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
45 if (fd < 0)
46 return -errno;
47
48 if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
49 close_nointr_nofail(fd);
50 goto retry;
51 }
52
53 return fd;
54 }
55
56 int sd_journal_print(int priority, const char *format, ...) {
57 int r;
58 va_list ap;
59
60 va_start(ap, format);
61 r = sd_journal_printv(priority, format, ap);
62 va_end(ap);
63
64 return r;
65 }
66
67 int sd_journal_printv(int priority, const char *format, va_list ap) {
68 char buffer[8 + LINE_MAX], p[11];
69 struct iovec iov[2];
70
71 if (priority < 0 || priority > 7)
72 return -EINVAL;
73
74 if (!format)
75 return -EINVAL;
76
77 snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
78 char_array_0(p);
79
80 memcpy(buffer, "MESSAGE=", 8);
81 vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
82 char_array_0(buffer);
83
84 zero(iov);
85 IOVEC_SET_STRING(iov[0], buffer);
86 IOVEC_SET_STRING(iov[1], p);
87
88 return sd_journal_sendv(iov, 2);
89 }
90
91 int sd_journal_send(const char *format, ...) {
92 int r, n = 0, i = 0, j;
93 va_list ap;
94 struct iovec *iov = NULL;
95
96 va_start(ap, format);
97 while (format) {
98 struct iovec *c;
99 char *buffer;
100
101 if (i >= n) {
102 n = MAX(i*2, 4);
103 c = realloc(iov, n * sizeof(struct iovec));
104 if (!c) {
105 r = -ENOMEM;
106 goto fail;
107 }
108
109 iov = c;
110 }
111
112 if (vasprintf(&buffer, format, ap) < 0) {
113 r = -ENOMEM;
114 goto fail;
115 }
116
117 IOVEC_SET_STRING(iov[i++], buffer);
118
119 format = va_arg(ap, char *);
120 }
121 va_end(ap);
122
123 r = sd_journal_sendv(iov, i);
124
125 fail:
126 for (j = 0; j < i; j++)
127 free(iov[j].iov_base);
128
129 free(iov);
130
131 return r;
132 }
133
134 int sd_journal_sendv(const struct iovec *iov, int n) {
135 int fd;
136 struct iovec *w;
137 uint64_t *l;
138 int i, j = 0;
139 struct msghdr mh;
140 struct sockaddr_un sa;
141
142 if (!iov || n <= 0)
143 return -EINVAL;
144
145 w = alloca(sizeof(struct iovec) * n * 5);
146 l = alloca(sizeof(uint64_t) * n);
147
148 for (i = 0; i < n; i++) {
149 char *c, *nl;
150
151 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
152 if (!c)
153 return -EINVAL;
154
155 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
156 if (nl) {
157 if (nl < c)
158 return -EINVAL;
159
160 /* Already includes a newline? Bummer, then
161 * let's write the variable name, then a
162 * newline, then the size (64bit LE), followed
163 * by the data and a final newline */
164
165 w[j].iov_base = iov[i].iov_base;
166 w[j].iov_len = c - (char*) iov[i].iov_base;
167 j++;
168
169 IOVEC_SET_STRING(w[j++], "\n");
170
171 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
172 w[j].iov_base = &l[i];
173 w[j].iov_len = sizeof(uint64_t);
174 j++;
175
176 w[j].iov_base = c + 1;
177 w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
178 j++;
179
180 } else
181 /* Nothing special? Then just add the line and
182 * append a newline */
183 w[j++] = iov[i];
184
185 IOVEC_SET_STRING(w[j++], "\n");
186 }
187
188 fd = journal_fd();
189 if (fd < 0)
190 return fd;
191
192 zero(sa);
193 sa.sun_family = AF_UNIX;
194 strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));
195
196 zero(mh);
197 mh.msg_name = &sa;
198 mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
199 mh.msg_iov = w;
200 mh.msg_iovlen = j;
201
202 if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
203 return -errno;
204
205 return 0;
206 }
207
208 int sd_journal_stream_fd(const char *tag, int priority, int priority_prefix) {
209 union sockaddr_union sa;
210 int fd;
211 char *header;
212 size_t l;
213 ssize_t r;
214
215 if (priority < 0 || priority > 7)
216 return -EINVAL;
217
218 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
219 if (fd < 0)
220 return -errno;
221
222 zero(sa);
223 sa.un.sun_family = AF_UNIX;
224 strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
225
226 r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
227 if (r < 0) {
228 close_nointr_nofail(fd);
229 return -errno;
230 }
231
232 if (!tag)
233 tag = "";
234
235 l = strlen(tag);
236 header = alloca(l + 1 + 2 + 2 + 2);
237
238 memcpy(header, tag, l);
239 header[l++] = '\n';
240 header[l++] = '0' + priority;
241 header[l++] = '\n';
242 header[l++] = '0' + !!priority_prefix;
243 header[l++] = '\n';
244 header[l++] = '0';
245 header[l++] = '\n';
246
247 r = loop_write(fd, header, l, false);
248 if (r < 0) {
249 close_nointr_nofail(fd);
250 return (int) r;
251 }
252
253 if ((size_t) r != l) {
254 close_nointr_nofail(fd);
255 return -errno;
256 }
257
258 return fd;
259 }