1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* rc-logger.c
* Spawns a logging daemon to capture stdout and stderr so we can log
* them to a buffer and/or files.
*/
/*
* Copyright (c) 2007-2015 The OpenRC Authors.
* See the Authors file at the top-level directory of this distribution and
* https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
*
* This file is part of OpenRC. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file.
*/
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#if defined(__linux__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__)) \
|| defined(__GNU__)
# include <pty.h>
#elif defined(__NetBSD__) || defined(__OpenBSD__)
# include <util.h>
#else
# include <libutil.h>
#endif
#include "einfo.h"
#include "rc-logger.h"
#include "rc.h"
#include "misc.h"
#include "helpers.h"
#define TMPLOG RC_SVCDIR "/rc.log"
#define DEFAULTLOG "/var/log/rc.log"
static int signal_pipe[2] = { -1, -1 };
static int fd_stdout = -1;
static int fd_stderr = -1;
static const char *runlevel = NULL;
static bool in_escape = false;
static bool in_term = false;
static char *logbuf = NULL;
static size_t logbuf_size = 0;
static size_t logbuf_len = 0;
pid_t rc_logger_pid = -1;
int rc_logger_tty = -1;
bool rc_in_logger = false;
static void
write_log(int logfd, const char *buffer, size_t bytes)
{
const char *p = buffer;
while ((size_t)(p - buffer) < bytes) {
switch (*p) {
case '\r':
goto cont;
case '\033':
in_escape = true;
in_term = false;
goto cont;
case '\n':
in_escape = in_term = false;
break;
case '[':
if (in_escape)
in_term = true;
break;
}
if (!in_escape) {
if (!isprint((int) *p) && *p != '\n')
goto cont;
if (write(logfd, p++, 1) == -1)
eerror("write: %s", strerror(errno));
continue;
}
if (!in_term || isalpha((unsigned char)*p))
in_escape = in_term = false;
cont:
p++;
}
}
static void
write_time(FILE *f, const char *s)
{
time_t now = time(NULL);
struct tm *tm = localtime(&now);
fprintf(f, "\nrc %s logging %s at %s\n", runlevel, s, asctime(tm));
fflush(f);
}
void
rc_logger_close(void)
{
int sig = SIGTERM;
if (signal_pipe[1] > -1) {
if (write(signal_pipe[1], &sig, sizeof(sig)) == -1)
eerror("write: %s", strerror(errno));
close(signal_pipe[1]);
signal_pipe[1] = -1;
}
if (rc_logger_pid > 0)
waitpid(rc_logger_pid, 0, 0);
if (fd_stdout > -1)
dup2(fd_stdout, STDOUT_FILENO);
if (fd_stderr > -1)
dup2(fd_stderr, STDERR_FILENO);
}
void
rc_logger_open(const char *level)
{
int slave_tty;
struct termios tt;
struct winsize ws;
char buffer[BUFSIZ];
struct pollfd fd[2];
int s = 0;
size_t bytes;
int i;
FILE *log = NULL;
FILE *plog = NULL;
const char *logfile;
int log_error = 0;
if (!rc_conf_yesno("rc_logger"))
return;
if (pipe(signal_pipe) == -1)
eerrorx("pipe: %s", strerror(errno));
for (i = 0; i < 2; i++)
if ((s = fcntl (signal_pipe[i], F_GETFD, 0) == -1 ||
fcntl (signal_pipe[i], F_SETFD, s | FD_CLOEXEC) == -1))
eerrorx("fcntl: %s", strerror (errno));
if (isatty(STDOUT_FILENO)) {
tcgetattr(STDOUT_FILENO, &tt);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
if (openpty(&rc_logger_tty, &slave_tty, NULL, &tt, &ws))
return;
} else
if (openpty(&rc_logger_tty, &slave_tty, NULL, NULL, NULL))
return;
if ((s = fcntl(rc_logger_tty, F_GETFD, 0)) == 0)
fcntl(rc_logger_tty, F_SETFD, s | FD_CLOEXEC);
if ((s = fcntl(slave_tty, F_GETFD, 0)) == 0)
fcntl(slave_tty, F_SETFD, s | FD_CLOEXEC);
rc_logger_pid = fork();
switch (rc_logger_pid) {
case -1:
eerror("fork: %s", strerror(errno));
break;
case 0:
rc_in_logger = true;
close(signal_pipe[1]);
signal_pipe[1] = -1;
runlevel = level;
if ((log = fopen(TMPLOG, "ae")))
write_time(log, "started");
else {
free(logbuf);
logbuf_size = BUFSIZ * 10;
logbuf = xmalloc(sizeof (char) * logbuf_size);
logbuf_len = 0;
}
fd[0].fd = signal_pipe[0];
fd[0].events = fd[1].events = POLLIN;
fd[0].revents = fd[1].revents = 0;
if (rc_logger_tty >= 0)
fd[1].fd = rc_logger_tty;
for (;;) {
if ((s = poll(fd,
rc_logger_tty >= 0 ? 2 : 1, -1)) == -1)
{
eerror("poll: %s", strerror(errno));
break;
} else if (s == 0)
continue;
if (fd[1].revents & (POLLIN | POLLHUP)) {
memset(buffer, 0, BUFSIZ);
bytes = read(rc_logger_tty, buffer, BUFSIZ);
if (write(STDOUT_FILENO, buffer, bytes) == -1)
eerror("write: %s", strerror(errno));
if (log)
write_log(fileno (log), buffer, bytes);
else {
if (logbuf_size - logbuf_len < bytes) {
logbuf_size += BUFSIZ * 10;
logbuf = xrealloc(logbuf,
sizeof(char ) *
logbuf_size);
}
memcpy(logbuf + logbuf_len,
buffer, bytes);
logbuf_len += bytes;
}
}
/* Only SIGTERMS signals come down this pipe */
if (fd[0].revents & (POLLIN | POLLHUP))
break;
}
if (logbuf) {
if ((log = fopen(TMPLOG, "ae"))) {
write_time(log, "started");
write_log(fileno(log), logbuf, logbuf_len);
}
free(logbuf);
}
if (log) {
write_time(log, "stopped");
fclose(log);
}
/* Append the temporary log to the real log */
logfile = rc_conf_value("rc_log_path");
if (logfile == NULL)
logfile = DEFAULTLOG;
if (!strcmp(logfile, TMPLOG)) {
eerror("Cowardly refusing to concatenate a logfile into itself.");
eerrorx("Please change rc_log_path to something other than %s to get rid of this message", TMPLOG);
}
if ((plog = fopen(logfile, "ae"))) {
if ((log = fopen(TMPLOG, "re"))) {
while ((bytes = fread(buffer, sizeof(*buffer), BUFSIZ, log)) > 0) {
if (fwrite(buffer, sizeof(*buffer), bytes, plog) < bytes) {
log_error = 1;
eerror("Error: write(%s) failed: %s", logfile, strerror(errno));
break;
}
}
fclose(log);
} else {
log_error = 1;
eerror("Error: fopen(%s) failed: %s", TMPLOG, strerror(errno));
}
fclose(plog);
} else {
/*
* logfile or its basedir may be read-only during sysinit and
* shutdown so skip the error in this case
*/
if (errno != EROFS && ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && (strcmp(level, RC_LEVEL_SYSINIT) != 0))) {
log_error = 1;
eerror("Error: fopen(%s) failed: %s", logfile, strerror(errno));
}
}
/* Try to keep the temporary log in case of errors */
if (!log_error) {
if (errno != EROFS && ((strcmp(level, RC_LEVEL_SHUTDOWN) != 0) && (strcmp(level, RC_LEVEL_SYSINIT) != 0)))
if (unlink(TMPLOG) == -1)
eerror("Error: unlink(%s) failed: %s", TMPLOG, strerror(errno));
} else if (exists(TMPLOG))
eerrorx("Warning: temporary logfile left behind: %s", TMPLOG);
exit(0);
/* NOTREACHED */
default:
setpgid(rc_logger_pid, 0);
fd_stdout = dup(STDOUT_FILENO);
fd_stderr = dup(STDERR_FILENO);
if ((s = fcntl(fd_stdout, F_GETFD, 0)) == 0)
fcntl(fd_stdout, F_SETFD, s | FD_CLOEXEC);
if ((s = fcntl(fd_stderr, F_GETFD, 0)) == 0)
fcntl(fd_stderr, F_SETFD, s | FD_CLOEXEC);
dup2(slave_tty, STDOUT_FILENO);
dup2(slave_tty, STDERR_FILENO);
if (slave_tty != STDIN_FILENO &&
slave_tty != STDOUT_FILENO &&
slave_tty != STDERR_FILENO)
close(slave_tty);
close(signal_pipe[0]);
signal_pipe[0] = -1;
break;
}
}
|