~ubuntu-branches/debian/jessie/systemd/jessie

« back to all changes in this revision

Viewing changes to src/journal/journal-send.c

  • Committer: Package Import Robot
  • Author(s): Tollef Fog Heen, Tollef Fog Heen, Michael Biebl
  • Date: 2012-04-03 19:59:17 UTC
  • mfrom: (1.1.10) (6.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20120403195917-l532urrbg4pkreas
Tags: 44-1
[ Tollef Fog Heen ]
* New upstream version.
  - Backport 3492207: journal: PAGE_SIZE is not known on ppc and other
    archs
  - Backport 5a2a2a1: journal: react with immediate rotation to a couple
    of more errors
  - Backport 693ce21: util: never follow symlinks in rm_rf_children()
    Fixes CVE-2012-1174, closes: #664364
* Drop output message from init-functions hook, it's pointless.
* Only rmdir /lib/init/rw if it exists.
* Explicitly order debian-fixup before sysinit.target to prevent a
  possible race condition with the creation of sockets.  Thanks to
  Michael Biebl for debugging this.
* Always restart the initctl socket on upgrades, to mask sysvinit
  removing it.

[ Michael Biebl ]
* Remove workaround for non-interactive sessions from pam config again.
* Create compat /dev/initctl symlink in case we are upgrading from a system
  running a newer version of sysvinit (using /run/initctl) and sysvinit is
  replaced with systemd-sysv during the upgrade. Closes: #663219
* Install new man pages.
* Build-Depend on valac (>= 0.12) instead of valac-0.12. Closes: #663323

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#define SNDBUF_SIZE (8*1024*1024)
 
34
 
 
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 */
 
39
 
 
40
static int journal_fd(void) {
 
41
        int fd;
 
42
        static int fd_plus_one = 0;
 
43
 
 
44
retry:
 
45
        if (fd_plus_one > 0)
 
46
                return fd_plus_one - 1;
 
47
 
 
48
        fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
 
49
        if (fd < 0)
 
50
                return -errno;
 
51
 
 
52
        fd_inc_sndbuf(fd, SNDBUF_SIZE);
 
53
 
 
54
        if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
 
55
                close_nointr_nofail(fd);
 
56
                goto retry;
 
57
        }
 
58
 
 
59
        return fd;
 
60
}
 
61
 
 
62
_public_ int sd_journal_print(int priority, const char *format, ...) {
 
63
        int r;
 
64
        va_list ap;
 
65
 
 
66
        va_start(ap, format);
 
67
        r = sd_journal_printv(priority, format, ap);
 
68
        va_end(ap);
 
69
 
 
70
        return r;
 
71
}
 
72
 
 
73
_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
 
74
        char buffer[8 + LINE_MAX], p[11];
 
75
        struct iovec iov[2];
 
76
 
 
77
        if (priority < 0 || priority > 7)
 
78
                return -EINVAL;
 
79
 
 
80
        if (!format)
 
81
                return -EINVAL;
 
82
 
 
83
        snprintf(p, sizeof(p), "PRIORITY=%i", priority & LOG_PRIMASK);
 
84
        char_array_0(p);
 
85
 
 
86
        memcpy(buffer, "MESSAGE=", 8);
 
87
        vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
 
88
        char_array_0(buffer);
 
89
 
 
90
        zero(iov);
 
91
        IOVEC_SET_STRING(iov[0], buffer);
 
92
        IOVEC_SET_STRING(iov[1], p);
 
93
 
 
94
        return sd_journal_sendv(iov, 2);
 
95
}
 
96
 
 
97
_public_ int sd_journal_send(const char *format, ...) {
 
98
        int r, n = 0, i = 0, j;
 
99
        va_list ap;
 
100
        struct iovec *iov = NULL;
 
101
 
 
102
        va_start(ap, format);
 
103
        while (format) {
 
104
                struct iovec *c;
 
105
                char *buffer;
 
106
 
 
107
                if (i >= n) {
 
108
                        n = MAX(i*2, 4);
 
109
                        c = realloc(iov, n * sizeof(struct iovec));
 
110
                        if (!c) {
 
111
                                r = -ENOMEM;
 
112
                                goto fail;
 
113
                        }
 
114
 
 
115
                        iov = c;
 
116
                }
 
117
 
 
118
                if (vasprintf(&buffer, format, ap) < 0) {
 
119
                        r = -ENOMEM;
 
120
                        goto fail;
 
121
                }
 
122
 
 
123
                IOVEC_SET_STRING(iov[i++], buffer);
 
124
 
 
125
                format = va_arg(ap, char *);
 
126
        }
 
127
        va_end(ap);
 
128
 
 
129
        r = sd_journal_sendv(iov, i);
 
130
 
 
131
fail:
 
132
        for (j = 0; j < i; j++)
 
133
                free(iov[j].iov_base);
 
134
 
 
135
        free(iov);
 
136
 
 
137
        return r;
 
138
}
 
139
 
 
140
_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
 
141
        int fd, buffer_fd;
 
142
        struct iovec *w;
 
143
        uint64_t *l;
 
144
        int i, j = 0;
 
145
        struct msghdr mh;
 
146
        struct sockaddr_un sa;
 
147
        ssize_t k;
 
148
        union {
 
149
                struct cmsghdr cmsghdr;
 
150
                uint8_t buf[CMSG_SPACE(sizeof(int))];
 
151
        } control;
 
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";
 
157
 
 
158
        if (!iov || n <= 0)
 
159
                return -EINVAL;
 
160
 
 
161
        w = alloca(sizeof(struct iovec) * n * 5);
 
162
        l = alloca(sizeof(uint64_t) * n);
 
163
 
 
164
        for (i = 0; i < n; i++) {
 
165
                char *c, *nl;
 
166
 
 
167
                if (!iov[i].iov_base ||
 
168
                    iov[i].iov_len <= 1)
 
169
                        return -EINVAL;
 
170
 
 
171
                c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
 
172
                if (!c || c == iov[i].iov_base)
 
173
                        return -EINVAL;
 
174
 
 
175
                nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
 
176
                if (nl) {
 
177
                        if (nl < c)
 
178
                                return -EINVAL;
 
179
 
 
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 */
 
184
 
 
185
                        w[j].iov_base = iov[i].iov_base;
 
186
                        w[j].iov_len = c - (char*) iov[i].iov_base;
 
187
                        j++;
 
188
 
 
189
                        IOVEC_SET_STRING(w[j++], "\n");
 
190
 
 
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);
 
194
                        j++;
 
195
 
 
196
                        w[j].iov_base = c + 1;
 
197
                        w[j].iov_len = iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1;
 
198
                        j++;
 
199
 
 
200
                } else
 
201
                        /* Nothing special? Then just add the line and
 
202
                         * append a newline */
 
203
                        w[j++] = iov[i];
 
204
 
 
205
                IOVEC_SET_STRING(w[j++], "\n");
 
206
        }
 
207
 
 
208
        fd = journal_fd();
 
209
        if (fd < 0)
 
210
                return fd;
 
211
 
 
212
        zero(sa);
 
213
        sa.sun_family = AF_UNIX;
 
214
        strncpy(sa.sun_path, "/run/systemd/journal/socket", sizeof(sa.sun_path));
 
215
 
 
216
        zero(mh);
 
217
        mh.msg_name = &sa;
 
218
        mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(sa.sun_path);
 
219
        mh.msg_iov = w;
 
220
        mh.msg_iovlen = j;
 
221
 
 
222
        k = sendmsg(fd, &mh, MSG_NOSIGNAL);
 
223
        if (k >= 0)
 
224
                return 0;
 
225
 
 
226
        if (errno != EMSGSIZE && errno != ENOBUFS)
 
227
                return -errno;
 
228
 
 
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
 
231
         * side */
 
232
 
 
233
        buffer_fd = mkostemp(path, O_CLOEXEC|O_RDWR);
 
234
        if (buffer_fd < 0)
 
235
                return -errno;
 
236
 
 
237
        if (unlink(path) < 0) {
 
238
                close_nointr_nofail(buffer_fd);
 
239
                return -errno;
 
240
        }
 
241
 
 
242
        n = writev(buffer_fd, w, j);
 
243
        if (n < 0)  {
 
244
                close_nointr_nofail(buffer_fd);
 
245
                return -errno;
 
246
        }
 
247
 
 
248
        mh.msg_iov = NULL;
 
249
        mh.msg_iovlen = 0;
 
250
 
 
251
        zero(control);
 
252
        mh.msg_control = &control;
 
253
        mh.msg_controllen = sizeof(control);
 
254
 
 
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));
 
260
 
 
261
        mh.msg_controllen = cmsg->cmsg_len;
 
262
 
 
263
        k = sendmsg(fd, &mh, MSG_NOSIGNAL);
 
264
        close_nointr_nofail(buffer_fd);
 
265
 
 
266
        if (k < 0)
 
267
                return -errno;
 
268
 
 
269
        return 0;
 
270
}
 
271
 
 
272
_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
 
273
        union sockaddr_union sa;
 
274
        int fd;
 
275
        char *header;
 
276
        size_t l;
 
277
        ssize_t r;
 
278
 
 
279
        if (priority < 0 || priority > 7)
 
280
                return -EINVAL;
 
281
 
 
282
        fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
 
283
        if (fd < 0)
 
284
                return -errno;
 
285
 
 
286
        zero(sa);
 
287
        sa.un.sun_family = AF_UNIX;
 
288
        strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
 
289
 
 
290
        r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
 
291
        if (r < 0) {
 
292
                close_nointr_nofail(fd);
 
293
                return -errno;
 
294
        }
 
295
 
 
296
        if (shutdown(fd, SHUT_RD) < 0) {
 
297
                close_nointr_nofail(fd);
 
298
                return -errno;
 
299
        }
 
300
 
 
301
        fd_inc_sndbuf(fd, SNDBUF_SIZE);
 
302
 
 
303
        if (!identifier)
 
304
                identifier = "";
 
305
 
 
306
        l = strlen(identifier);
 
307
        header = alloca(l + 1 + 2 + 2 + 2 + 2 + 2);
 
308
 
 
309
        memcpy(header, identifier, l);
 
310
        header[l++] = '\n';
 
311
        header[l++] = '0' + priority;
 
312
        header[l++] = '\n';
 
313
        header[l++] = '0' + !!level_prefix;
 
314
        header[l++] = '\n';
 
315
        header[l++] = '0';
 
316
        header[l++] = '\n';
 
317
        header[l++] = '0';
 
318
        header[l++] = '\n';
 
319
        header[l++] = '0';
 
320
        header[l++] = '\n';
 
321
 
 
322
        r = loop_write(fd, header, l, false);
 
323
        if (r < 0) {
 
324
                close_nointr_nofail(fd);
 
325
                return (int) r;
 
326
        }
 
327
 
 
328
        if ((size_t) r != l) {
 
329
                close_nointr_nofail(fd);
 
330
                return -errno;
 
331
        }
 
332
 
 
333
        return fd;
 
334
}