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

« back to all changes in this revision

Viewing changes to src/kmsg-syslogd.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 2010 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/types.h>
24
 
#include <assert.h>
25
 
#include <time.h>
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <errno.h>
29
 
#include <unistd.h>
30
 
#include <sys/poll.h>
31
 
#include <sys/epoll.h>
32
 
#include <sys/un.h>
33
 
#include <fcntl.h>
34
 
#include <sys/signalfd.h>
35
 
 
36
 
#include "util.h"
37
 
#include "log.h"
38
 
#include "sd-daemon.h"
39
 
#include "fdset.h"
40
 
 
41
 
#define SERVER_FD_MAX 16
42
 
 
43
 
typedef struct Stream Stream;
44
 
 
45
 
typedef struct Server {
46
 
        FDSet *syslog_fds;
47
 
        int kmsg_fd;
48
 
        int epoll_fd;
49
 
        int signal_fd;
50
 
} Server;
51
 
 
52
 
static void server_done(Server *s) {
53
 
        assert(s);
54
 
 
55
 
        if (s->epoll_fd >= 0)
56
 
                close_nointr_nofail(s->epoll_fd);
57
 
 
58
 
        if (s->kmsg_fd >= 0)
59
 
                close_nointr_nofail(s->kmsg_fd);
60
 
 
61
 
        if (s->signal_fd >= 0)
62
 
                close_nointr_nofail(s->signal_fd);
63
 
 
64
 
        if (s->syslog_fds)
65
 
                fdset_free(s->syslog_fds);
66
 
}
67
 
 
68
 
static int server_init(Server *s, unsigned n_sockets) {
69
 
        int r;
70
 
        unsigned i;
71
 
        struct epoll_event ev;
72
 
        sigset_t mask;
73
 
 
74
 
        assert(s);
75
 
        assert(n_sockets > 0);
76
 
 
77
 
        zero(*s);
78
 
 
79
 
        s->kmsg_fd = s->signal_fd = -1;
80
 
 
81
 
        if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
82
 
                r = -errno;
83
 
                log_error("Failed to create epoll object: %s", strerror(errno));
84
 
                goto fail;
85
 
        }
86
 
 
87
 
        if (!(s->syslog_fds = fdset_new())) {
88
 
                r = -ENOMEM;
89
 
                log_error("Failed to allocate file descriptor set: %s", strerror(errno));
90
 
                goto fail;
91
 
        }
92
 
 
93
 
        for (i = 0; i < n_sockets; i++) {
94
 
                int fd, one = 1;
95
 
 
96
 
                fd = SD_LISTEN_FDS_START+i;
97
 
 
98
 
                if ((r = sd_is_socket(fd, AF_UNSPEC, SOCK_DGRAM, -1)) < 0) {
99
 
                        log_error("Failed to determine file descriptor type: %s", strerror(-r));
100
 
                        goto fail;
101
 
                }
102
 
 
103
 
                if (!r) {
104
 
                        log_error("Wrong file descriptor type.");
105
 
                        r = -EINVAL;
106
 
                        goto fail;
107
 
                }
108
 
 
109
 
                if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
110
 
                        log_error("SO_PASSCRED failed: %m");
111
 
 
112
 
                zero(ev);
113
 
                ev.events = EPOLLIN;
114
 
                ev.data.fd = fd;
115
 
                if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
116
 
                        r = -errno;
117
 
                        log_error("Failed to add server fd to epoll object: %s", strerror(errno));
118
 
                        goto fail;
119
 
                }
120
 
 
121
 
                if ((r = fdset_put(s->syslog_fds, fd)) < 0) {
122
 
                        log_error("Failed to store file descriptor in set: %s", strerror(-r));
123
 
                        goto fail;
124
 
                }
125
 
        }
126
 
 
127
 
        if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
128
 
                log_error("Failed to open /dev/kmsg for logging: %m");
129
 
                return -errno;
130
 
        }
131
 
 
132
 
        assert_se(sigemptyset(&mask) == 0);
133
 
        sigset_add_many(&mask, SIGINT, SIGTERM, -1);
134
 
        assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
135
 
 
136
 
        if ((s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
137
 
                log_error("signalfd(): %m");
138
 
                return -errno;
139
 
        }
140
 
 
141
 
        zero(ev);
142
 
        ev.events = EPOLLIN;
143
 
        ev.data.fd = s->signal_fd;
144
 
 
145
 
        if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
146
 
                log_error("epoll_ctl(): %m");
147
 
                return -errno;
148
 
        }
149
 
 
150
 
        return 0;
151
 
 
152
 
fail:
153
 
        server_done(s);
154
 
        return r;
155
 
}
156
 
 
157
 
static void skip_date(const char **buf) {
158
 
        enum {
159
 
                LETTER,
160
 
                SPACE,
161
 
                NUMBER,
162
 
                SPACE_OR_NUMBER,
163
 
                COLON
164
 
        } sequence[] = {
165
 
                LETTER, LETTER, LETTER,
166
 
                SPACE,
167
 
                SPACE_OR_NUMBER, NUMBER,
168
 
                SPACE,
169
 
                SPACE_OR_NUMBER, NUMBER,
170
 
                COLON,
171
 
                SPACE_OR_NUMBER, NUMBER,
172
 
                COLON,
173
 
                SPACE_OR_NUMBER, NUMBER,
174
 
                SPACE
175
 
        };
176
 
 
177
 
        const char *p;
178
 
        unsigned i;
179
 
 
180
 
        assert(buf);
181
 
        assert(*buf);
182
 
 
183
 
        p = *buf;
184
 
 
185
 
        for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
186
 
 
187
 
                if (!*p)
188
 
                        return;
189
 
 
190
 
                switch (sequence[i]) {
191
 
 
192
 
                case SPACE:
193
 
                        if (*p != ' ')
194
 
                                return;
195
 
                        break;
196
 
 
197
 
                case SPACE_OR_NUMBER:
198
 
                        if (*p == ' ')
199
 
                                break;
200
 
 
201
 
                        /* fall through */
202
 
 
203
 
                case NUMBER:
204
 
                        if (*p < '0' || *p > '9')
205
 
                                return;
206
 
 
207
 
                        break;
208
 
 
209
 
                case LETTER:
210
 
                        if (!(*p >= 'A' && *p <= 'Z') &&
211
 
                            !(*p >= 'a' && *p <= 'z'))
212
 
                                return;
213
 
 
214
 
                        break;
215
 
 
216
 
                case COLON:
217
 
                        if (*p != ':')
218
 
                                return;
219
 
                        break;
220
 
 
221
 
                }
222
 
        }
223
 
 
224
 
        *buf = p;
225
 
}
226
 
 
227
 
static int read_process(const char **buf, struct iovec *iovec) {
228
 
        const char *p;
229
 
        size_t l;
230
 
 
231
 
        assert(buf);
232
 
        assert(*buf);
233
 
        assert(iovec);
234
 
 
235
 
        p = *buf;
236
 
 
237
 
        p += strspn(p, WHITESPACE);
238
 
        l = strcspn(p, WHITESPACE);
239
 
 
240
 
        if (l <= 0 ||
241
 
            p[l-1] != ':')
242
 
                return 0;
243
 
 
244
 
        l--;
245
 
 
246
 
        if (p[l-1] == ']') {
247
 
                size_t k = l-1;
248
 
 
249
 
                for (;;) {
250
 
 
251
 
                        if (p[k] == '[') {
252
 
                                l = k;
253
 
                                break;
254
 
                        }
255
 
 
256
 
                        if (k == 0)
257
 
                                break;
258
 
 
259
 
                        k--;
260
 
                }
261
 
        }
262
 
 
263
 
        iovec->iov_base = (char*) p;
264
 
        iovec->iov_len = l;
265
 
        *buf = p + l;
266
 
        return 1;
267
 
}
268
 
 
269
 
static void skip_pid(const char **buf) {
270
 
        const char *p;
271
 
 
272
 
        assert(buf);
273
 
        assert(*buf);
274
 
 
275
 
        p = *buf;
276
 
 
277
 
        if (*p != '[')
278
 
                return;
279
 
 
280
 
        p++;
281
 
        p += strspn(p, "0123456789");
282
 
 
283
 
        if (*p != ']')
284
 
                return;
285
 
 
286
 
        p++;
287
 
 
288
 
        *buf = p;
289
 
}
290
 
 
291
 
static int write_message(Server *s, const char *buf, struct ucred *ucred) {
292
 
        ssize_t k;
293
 
        char priority[6], pid[16];
294
 
        struct iovec iovec[5];
295
 
        unsigned i = 0;
296
 
        char *process = NULL;
297
 
        int r = 0;
298
 
        int prio = LOG_USER | LOG_INFO;
299
 
 
300
 
        assert(s);
301
 
        assert(buf);
302
 
 
303
 
        parse_syslog_priority((char**) &buf, &prio);
304
 
 
305
 
        if (*buf == 0)
306
 
                return 0;
307
 
 
308
 
        if ((prio & LOG_FACMASK) == 0)
309
 
                prio = LOG_USER | LOG_PRI(prio);
310
 
 
311
 
        /* First, set priority field */
312
 
        snprintf(priority, sizeof(priority), "<%i>", prio);
313
 
        char_array_0(priority);
314
 
        IOVEC_SET_STRING(iovec[i++], priority);
315
 
 
316
 
        /* Second, skip date */
317
 
        skip_date(&buf);
318
 
 
319
 
        /* Then, add process if set */
320
 
        if (read_process(&buf, &iovec[i]) > 0)
321
 
                i++;
322
 
        else if (ucred &&
323
 
                 ucred->pid > 0 &&
324
 
                 get_process_name(ucred->pid, &process) >= 0)
325
 
                IOVEC_SET_STRING(iovec[i++], process);
326
 
 
327
 
        /* Skip the stored PID if we have a better one */
328
 
        if (ucred) {
329
 
                snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
330
 
                char_array_0(pid);
331
 
                IOVEC_SET_STRING(iovec[i++], pid);
332
 
 
333
 
                skip_pid(&buf);
334
 
 
335
 
                if (*buf == ':')
336
 
                        buf++;
337
 
 
338
 
                buf += strspn(buf, WHITESPACE);
339
 
        }
340
 
 
341
 
        /* Is the remaining message empty? */
342
 
        if (*buf) {
343
 
 
344
 
                /* And the rest is the message */
345
 
                IOVEC_SET_STRING(iovec[i++], buf);
346
 
                IOVEC_SET_STRING(iovec[i++], "\n");
347
 
 
348
 
                if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
349
 
                        log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
350
 
                        r = k < 0 ? -errno : -EIO;
351
 
                }
352
 
        }
353
 
 
354
 
        free(process);
355
 
 
356
 
        return r;
357
 
}
358
 
 
359
 
static int process_event(Server *s, struct epoll_event *ev) {
360
 
        assert(s);
361
 
 
362
 
        if (ev->events != EPOLLIN) {
363
 
                log_info("Got invalid event from epoll.");
364
 
                return -EIO;
365
 
        }
366
 
 
367
 
        if (ev->data.fd == s->signal_fd) {
368
 
                struct signalfd_siginfo sfsi;
369
 
                ssize_t n;
370
 
 
371
 
                if ((n = read(s->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
372
 
 
373
 
                        if (n >= 0)
374
 
                                return -EIO;
375
 
 
376
 
                        if (errno == EINTR || errno == EAGAIN)
377
 
                                return 0;
378
 
 
379
 
                        return -errno;
380
 
                }
381
 
 
382
 
                log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
383
 
                return 0;
384
 
 
385
 
        } else {
386
 
                for (;;) {
387
 
                        char buf[LINE_MAX+1];
388
 
                        struct msghdr msghdr;
389
 
                        struct iovec iovec;
390
 
                        struct ucred *ucred;
391
 
                        union {
392
 
                                struct cmsghdr cmsghdr;
393
 
                                uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
394
 
                        } control;
395
 
                        ssize_t n;
396
 
                        int k;
397
 
                        char *e;
398
 
 
399
 
                        zero(iovec);
400
 
                        iovec.iov_base = buf;
401
 
                        iovec.iov_len = sizeof(buf)-1;
402
 
 
403
 
                        zero(control);
404
 
                        zero(msghdr);
405
 
                        msghdr.msg_iov = &iovec;
406
 
                        msghdr.msg_iovlen = 1;
407
 
                        msghdr.msg_control = &control;
408
 
                        msghdr.msg_controllen = sizeof(control);
409
 
 
410
 
                        if ((n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT)) < 0) {
411
 
 
412
 
                                if (errno == EINTR || errno == EAGAIN)
413
 
                                        return 1;
414
 
 
415
 
                                log_error("recvmsg() failed: %m");
416
 
                                return -errno;
417
 
                        }
418
 
 
419
 
                        if (msghdr.msg_controllen >= CMSG_LEN(sizeof(struct ucred)) &&
420
 
                            control.cmsghdr.cmsg_level == SOL_SOCKET &&
421
 
                            control.cmsghdr.cmsg_type == SCM_CREDENTIALS &&
422
 
                            control.cmsghdr.cmsg_len == CMSG_LEN(sizeof(struct ucred)))
423
 
                                ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
424
 
                        else
425
 
                                ucred = NULL;
426
 
 
427
 
                        if ((e = memchr(buf, '\n', n)))
428
 
                                *e = 0;
429
 
                        else
430
 
                                buf[n] = 0;
431
 
 
432
 
                        if ((k = write_message(s, strstrip(buf), ucred)) < 0)
433
 
                                return k;
434
 
                }
435
 
        }
436
 
 
437
 
        return 1;
438
 
}
439
 
 
440
 
int main(int argc, char *argv[]) {
441
 
        Server server;
442
 
        int r = EXIT_FAILURE, n;
443
 
 
444
 
        if (getppid() != 1) {
445
 
                log_error("This program should be invoked by init only.");
446
 
                return EXIT_FAILURE;
447
 
        }
448
 
 
449
 
        if (argc > 1) {
450
 
                log_error("This program does not take arguments.");
451
 
                return EXIT_FAILURE;
452
 
        }
453
 
 
454
 
        log_set_target(LOG_TARGET_KMSG);
455
 
        log_parse_environment();
456
 
        log_open();
457
 
 
458
 
        umask(0022);
459
 
 
460
 
        if ((n = sd_listen_fds(true)) < 0) {
461
 
                log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
462
 
                return EXIT_FAILURE;
463
 
        }
464
 
 
465
 
        if (n <= 0 || n > SERVER_FD_MAX) {
466
 
                log_error("No or too many file descriptors passed.");
467
 
                return EXIT_FAILURE;
468
 
        }
469
 
 
470
 
        if (server_init(&server, (unsigned) n) < 0)
471
 
                return EXIT_FAILURE;
472
 
 
473
 
        log_debug("systemd-kmsg-syslogd running as pid %lu", (unsigned long) getpid());
474
 
 
475
 
        sd_notify(false,
476
 
                  "READY=1\n"
477
 
                  "STATUS=Processing messages...");
478
 
 
479
 
        for (;;) {
480
 
                struct epoll_event event;
481
 
                int k;
482
 
 
483
 
                if ((k = epoll_wait(server.epoll_fd, &event, 1, -1)) < 0) {
484
 
 
485
 
                        if (errno == EINTR)
486
 
                                continue;
487
 
 
488
 
                        log_error("epoll_wait() failed: %m");
489
 
                        goto fail;
490
 
                }
491
 
 
492
 
                if (k <= 0)
493
 
                        break;
494
 
 
495
 
                if ((k = process_event(&server, &event)) < 0)
496
 
                        goto fail;
497
 
 
498
 
                if (k == 0)
499
 
                        break;
500
 
        }
501
 
 
502
 
        r = EXIT_SUCCESS;
503
 
 
504
 
        log_debug("systemd-kmsg-syslogd stopped as pid %lu", (unsigned long) getpid());
505
 
 
506
 
fail:
507
 
        sd_notify(false,
508
 
                  "STATUS=Shutting down...");
509
 
 
510
 
        server_done(&server);
511
 
 
512
 
        return r;
513
 
}