]>
Commit | Line | Data |
---|---|---|
3b7124a8 LP |
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 Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 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 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <fcntl.h> | |
23 | #include <unistd.h> | |
4871690d | 24 | #include <sys/socket.h> |
3b7124a8 LP |
25 | |
26 | #include "journald.h" | |
27 | #include "journald-console.h" | |
28 | ||
29 | void server_forward_console( | |
30 | Server *s, | |
31 | int priority, | |
32 | const char *identifier, | |
33 | const char *message, | |
34 | struct ucred *ucred) { | |
35 | ||
36 | struct iovec iovec[4]; | |
37 | char header_pid[16]; | |
38 | int n = 0, fd; | |
39 | char *ident_buf = NULL; | |
40 | const char *tty; | |
41 | ||
42 | assert(s); | |
43 | assert(message); | |
44 | ||
45 | if (LOG_PRI(priority) > s->max_level_console) | |
46 | return; | |
47 | ||
48 | /* First: identifier and PID */ | |
49 | if (ucred) { | |
50 | if (!identifier) { | |
51 | get_process_comm(ucred->pid, &ident_buf); | |
52 | identifier = ident_buf; | |
53 | } | |
54 | ||
55 | snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid); | |
56 | char_array_0(header_pid); | |
57 | ||
58 | if (identifier) | |
59 | IOVEC_SET_STRING(iovec[n++], identifier); | |
60 | ||
61 | IOVEC_SET_STRING(iovec[n++], header_pid); | |
62 | } else if (identifier) { | |
63 | IOVEC_SET_STRING(iovec[n++], identifier); | |
64 | IOVEC_SET_STRING(iovec[n++], ": "); | |
65 | } | |
66 | ||
67 | /* Third: message */ | |
68 | IOVEC_SET_STRING(iovec[n++], message); | |
69 | IOVEC_SET_STRING(iovec[n++], "\n"); | |
70 | ||
71 | tty = s->tty_path ? s->tty_path : "/dev/console"; | |
72 | ||
73 | fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC); | |
74 | if (fd < 0) { | |
75 | log_debug("Failed to open %s for logging: %s", tty, strerror(errno)); | |
76 | goto finish; | |
77 | } | |
78 | ||
79 | if (writev(fd, iovec, n) < 0) | |
80 | log_debug("Failed to write to %s for logging: %s", tty, strerror(errno)); | |
81 | ||
82 | close_nointr_nofail(fd); | |
83 | ||
84 | finish: | |
85 | free(ident_buf); | |
86 | } |