chiark / gitweb /
journal: implement inotify-based live logging logic
[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
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(int priority, const char *format, ...) {
56         int r;
57         va_list ap;
58
59         va_start(ap, format);
60         r = sd_journal_printv(priority, format, ap);
61         va_end(ap);
62
63         return r;
64 }
65
66 int sd_journal_printv(int priority, const char *format, va_list ap) {
67         char buffer[8 + LINE_MAX], p[11];
68         struct iovec iov[2];
69
70         snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
71         char_array_0(p);
72
73         memcpy(buffer, "MESSAGE=", 8);
74         vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
75         char_array_0(buffer);
76
77         zero(iov);
78         IOVEC_SET_STRING(iov[0], buffer);
79         IOVEC_SET_STRING(iov[1], p);
80
81         return sd_journal_sendv(iov, 2);
82 }
83
84 int sd_journal_send(const char *format, ...) {
85         int r, n = 0, i = 0, j;
86         va_list ap;
87         struct iovec *iov = NULL;
88
89         va_start(ap, format);
90         while (format) {
91                 struct iovec *c;
92                 char *buffer;
93
94                 if (i >= n) {
95                         n = MAX(i*2, 4);
96                         c = realloc(iov, n * sizeof(struct iovec));
97                         if (!c) {
98                                 r = -ENOMEM;
99                                 goto fail;
100                         }
101
102                         iov = c;
103                 }
104
105                 if (vasprintf(&buffer, format, ap) < 0) {
106                         r = -ENOMEM;
107                         goto fail;
108                 }
109
110                 IOVEC_SET_STRING(iov[i++], buffer);
111
112                 format = va_arg(ap, char *);
113         }
114         va_end(ap);
115
116         r = sd_journal_sendv(iov, i);
117
118 fail:
119         for (j = 0; j < i; j++)
120                 free(iov[j].iov_base);
121
122         free(iov);
123
124         return r;
125 }
126
127 int sd_journal_sendv(const struct iovec *iov, int n) {
128         int fd;
129         struct iovec *w;
130         uint64_t *l;
131         int i, j = 0;
132         struct msghdr mh;
133         struct sockaddr_un sa;
134
135         if (!iov || n <= 0)
136                 return -EINVAL;
137
138         w = alloca(sizeof(struct iovec) * n * 5);
139         l = alloca(sizeof(uint64_t) * n);
140
141         for (i = 0; i < n; i++) {
142                 char *c, *nl;
143
144                 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
145                 if (!c)
146                         return -EINVAL;
147
148                 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
149                 if (nl) {
150                         if (nl < c)
151                                 return -EINVAL;
152
153                         /* Already includes a newline? Bummer, then
154                          * let's write the variable name, then a
155                          * newline, then the size (64bit LE), followed
156                          * by the data and a final newline */
157
158                         w[j].iov_base = iov[i].iov_base;
159                         w[j].iov_len = c - (char*) iov[i].iov_base;
160                         j++;
161
162                         IOVEC_SET_STRING(w[j++], "\n");
163
164                         l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
165                         w[j].iov_base = &l[i];
166                         w[j].iov_len = sizeof(uint64_t);
167                         j++;
168
169                         w[j].iov_base = c + 1;
170                         w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
171                         j++;
172
173                 } else
174                         /* Nothing special? Then just add the line and
175                          * append a newline */
176                         w[j++] = iov[i];
177
178                 IOVEC_SET_STRING(w[j++], "\n");
179         }
180
181         fd = journal_fd();
182         if (fd < 0)
183                 return fd;
184
185         zero(sa);
186         sa.sun_family = AF_UNIX;
187         strncpy(sa.sun_path,"/run/systemd/journal", sizeof(sa.sun_path));
188
189         zero(mh);
190         mh.msg_name = &sa;
191         mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
192         mh.msg_iov = w;
193         mh.msg_iovlen = j;
194
195         if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
196                 return -errno;
197
198         return 0;
199 }