forked from bitstreamout/showconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogd.c
565 lines (486 loc) · 12.5 KB
/
blogd.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* blogd.c
*
* Copyright 2000,2015 Werner Fink, 2000 SuSE GmbH Nuernberg, Germany.
* Copyright 2015 SuSE Linux GmbH.
*
* This source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <sys/time.h>
#include <sys/types.h> /* Defines the macros major and minor */
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/vt.h>
#include <sys/kd.h>
#include <time.h>
#include <pty.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stropts.h>
#include <dirent.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <paths.h>
#include <linux/major.h>
#include "libconsole.h"
#ifndef _POSIX_MAX_CANON
# define _POSIX_MAX_CANON 255
#endif
extern volatile sig_atomic_t nsigsys;
extern volatile sig_atomic_t signaled;
extern int final;
static int show_status;
static const char console[] = "/dev/console";
static const char *varrun = _PATH_VARRUN;
static void _initialize(void) __attribute__((__constructor__));
static void _initialize(void)
{
const char *run;
run = realpath(varrun, NULL);
if (run && *run)
varrun = run;
}
/*
* Cry and exit.
*/
void error (const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(EXIT_FAILURE, fmt, ap);
va_end(ap);
}
/*
* Remove pidfile
*/
static const char *myname;
static char *pidfile;
static char *plymouth;
static void rmfpid()
{
if (!pidfile || *pidfile == 0)
return;
unlink(pidfile);
if (plymouth && *plymouth)
unlink(plymouth);
}
/*
* Write pidfile
*/
static void attribute((noinline)) dopidfile()
{
FILE * fpid;
int ret;
ret = asprintf(&pidfile, "%s/%s.pid", varrun, myname);
if (ret < 0)
error("can not allocate memory for pid file");
if ((fpid = fopen (pidfile, "w")) == NULL) {
warn("can not open %s", pidfile);
goto out;
}
fprintf(fpid, "%d\n", getpid());
fclose(fpid);
ret = mkdir("/run/plymouth", 0755);
if (ret < 0) {
if (errno != EEXIST)
warn("can not make directory /run/plymouth");
else
ret = 0;
}
if (ret == 0) {
plymouth = "/run/plymouth/pid";
ret = symlink(pidfile, plymouth);
do {
int serr = errno;
int fd;
if (ret >= 0)
break;
fd = open(plymouth, O_RDONLY|O_NOFOLLOW);
if (fd < 0 && errno == ELOOP) {
unlink(plymouth);
ret = symlink(pidfile, plymouth);
break;
}
if (fd >= 0) {
char bpid[128];
ssize_t len;
len = read(fd, &bpid[0], sizeof(bpid)-1);
close(fd);
if (len > 0) {
pid_t pid;
char *exe;
pid = (pid_t) strtol(&bpid[0], &exe, 10);
if (pid > 1 && (!exe || !*exe || *exe == '\n')) {
exe = proc2exe(pid);
if (exe) {
errno = EACCES;
error("plymouth is active %ld", (long int)pid);
}
}
unlink(plymouth);
ret = symlink(pidfile, plymouth);
break;
}
}
errno = serr;
warn("can not create %s", plymouth);
plymouth = NULL;
} while(0);
}
atexit(rmfpid);
out:
return;
}
/*
* Signal handler
*/
static struct sigaction saved_sigttin;
static struct sigaction saved_sigttou;
static struct sigaction saved_sigtstp;
static struct sigaction saved_sighup;
static struct sigaction saved_sigint;
static struct sigaction saved_sigquit;
static struct sigaction saved_sigterm;
static struct sigaction saved_sigsys;
static struct sigaction saved_sigpipe;
static void sighandle(int sig)
{
if (nsigsys && (sig == SIGTERM))
return;
signaled = (volatile sig_atomic_t)sig;
}
/*
* Stop writing logs to disk, only repeat messages
*/
static void sigsys(int sig)
{
nsigsys = (volatile sig_atomic_t)sig;
}
/*
* To be able to reconnect to real tty on EIO
*/
static void reconnect(int fd)
{
struct console * c;
list_for_each_entry(c, &cons->node, node) {
int newfd;
if (!c->tty) continue;
if (c->fd != fd) continue;
switch (c->fd) {
case 0:
case -1: /* Weired */
break;
default: /* IO of system consoles */
if ((newfd = open(c->tty, O_WRONLY|O_NONBLOCK|O_NOCTTY)) < 0)
error("can not open %s", c->tty);
dup2(newfd, c->fd);
if (newfd != c->fd)
close(newfd);
break;
}
}
}
static volatile pid_t pid = -1;
static void flush_handler (void) attribute((noinline));
static void exit_handler (void) attribute((noinline));
/*
* Now do the job
*/
int main(int argc, char *argv[])
{
volatile char *arg0 = argv[0];
char ptsname[NAME_MAX+1];
const char *tty, *stt;
struct console *c;
struct termios o;
struct winsize w;
struct stat st;
time_t tt;
int ptm, pts, fd, arg;
int listen, pipefd[2];
listen = open_un_socket_and_listen();
if (listen < -1)
_exit(EXIT_SUCCESS);
if (stat("/run/systemd/show-status", &st) == 0)
show_status = 1;
if (kill (1, SIGRTMIN+20) < 0)
warn("could not tell system to show its status");
while ((arg = getopt(argc, argv, "f")) != -1) {
switch (arg) {
case 'f':
final = 1;
break;
case '?':
default:
return 1;
}
}
argv += optind;
argc -= optind;
myname = program_invocation_short_name;
close(0);
close(1);
close(2);
if ((fd = open(console, O_RDWR|O_NONBLOCK|O_NOCTTY)) < 0)
error("Can not open system console %s", console);
if (fd > 0) {
dup2(fd, 0);
close(fd);
}
dup2(0, 1);
dup2(0, 2);
tty = console;
(void)ioctl(0, TIOCCONS, NULL); /* Undo any current map if any */
getconsoles(&cons, 1);
list_for_each_entry(c, &cons->node, node) {
speed_t ospeed;
speed_t ispeed;
int flags;
ispeed = B38400;
ospeed = B38400;
if (ioctl(c->fd, TIOCMGET, &flags) == 0) { /* serial line */
ispeed = cfgetispeed(&c->otio);
ospeed = cfgetospeed(&c->otio);
c->flags |= CON_SERIAL;
}
cfsetispeed(&c->otio, ispeed);
cfsetospeed(&c->otio, ospeed);
if (c->flags & CON_CONSDEV) {
tty = c->tty;
w.ws_row = 0;
w.ws_col = 0;
if (ioctl(c->fd, TIOCGWINSZ, &w) < 0)
error("can not get window size of %s", c->tty);
if (!w.ws_row)
w.ws_row = 24;
if (!w.ws_col)
w.ws_col = 80;
memcpy(&o, &c->otio, sizeof(o));
cfmakeraw(&o);
cfsetispeed(&o, ispeed);
cfsetospeed(&o, ospeed);
o.c_lflag &= ~ECHO;
o.c_lflag |= ISIG;
o.c_cc[VTIME] = 0;
o.c_cc[VMIN] = CMIN;
}
}
if (openpty(&ptm, &pts, ptsname, &o, &w) < 0)
error("can not open pty/tty pair");
if (fstat(pts, &st) < 0)
error("can not stat slave pty");
else {
struct termios lock;
memset(&lock, 0xff, sizeof(lock));
(void)ioctl(pts, TIOCSLCKTRMIOS, &lock);
}
if (ioctl(pts, TIOCCONS, NULL) < 0)
error("can not set console device to %s", ptsname);
dup2(pts, 1);
dup2(pts, 2); /* Now we are blind upto safeIO() loop */
if (pts > 2)
close(pts);
signaled = nsigsys = 0;
set_signal(SIGTTIN, &saved_sigttin, SIG_IGN);
set_signal(SIGTTOU, &saved_sigttou, SIG_IGN);
set_signal(SIGTSTP, &saved_sigtstp, SIG_IGN);
set_signal(SIGHUP, &saved_sighup, SIG_IGN);
set_signal(SIGPIPE, &saved_sigpipe, SIG_IGN);
set_signal(SIGINT, &saved_sigint, sighandle);
set_signal(SIGQUIT, &saved_sigquit, sighandle);
set_signal(SIGTERM, &saved_sigterm, sighandle);
set_signal(SIGSYS, &saved_sigsys, sigsys);
(void)siginterrupt(SIGINT, 0);
(void)siginterrupt(SIGQUIT, 0);
(void)siginterrupt(SIGTERM, 0);
(void)siginterrupt(SIGSYS, 0);
list_for_each_entry(c, &cons->node, node) {
struct termios oldtio;
speed_t ospeed;
speed_t ispeed;
int flags;
(void)ioctl(fd, TIOCNXCL); /* Avoid EBUSY */
#ifdef _PC_MAX_CANON
if ((c->max_canon = (ssize_t)fpathconf(c->fd, _PC_MAX_CANON)) <= 0)
#endif
c->max_canon = _POSIX_MAX_CANON;
c->tlock = 0;
if (tcgetattr(c->fd, &c->otio) < 0)
continue;
c->tlock = 1;
if (ioctl(c->fd, TIOCGLCKTRMIOS, &c->ltio) == 0) {
/*
* Remove any lock as e.g. startpar(8) will use it
*/
struct termios lock;
memset(&lock, 0x00, sizeof(lock));
if (ioctl(c->fd, TIOCSLCKTRMIOS, &lock) == 0)
c->tlock = 2;
}
oldtio = c->otio; /* Remember old tty I/O flags */
if (ioctl(c->fd, TIOCMGET, &flags) == 0) { /* serial line */
ispeed = cfgetispeed(&c->otio);
ospeed = cfgetospeed(&c->otio);
c->otio.c_iflag = c->otio.c_lflag = 0;
c->otio.c_oflag = (ONLCR | OPOST);
c->otio.c_cflag = CREAD | CS8 | HUPCL | (c->otio.c_cflag & CLOCAL);
cfsetispeed(&c->otio, ispeed);
cfsetospeed(&c->otio, ospeed);
} else {
ioctl(fd, KDSETMODE, KD_TEXT); /* Enforce text mode */
c->otio.c_iflag |= (ICRNL | IGNBRK);
c->otio.c_iflag &= ~(INLCR | IGNCR | BRKINT);
c->otio.c_oflag |= (ONLCR | OPOST);
c->otio.c_oflag &= ~(OCRNL | ONLRET);
}
(void)tcsetattr(c->fd, TCSADRAIN, &c->otio);
c->ctio = c->otio; /* Current setting */
c->otio = oldtio; /* To be able to restore */
}
atexit(exit_handler); /* Register main exit handler */
/* Make the first byte in argv be '@' so that we can survive systemd's killing
* spree when going from initrd to /, and so we stay alive all the way until
* the power is killed at shutdown.
* http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons
*/
if (access("/etc/initrd-release", F_OK) >= 0 || final)
arg0[0] = '@';
if (pipe2(pipefd, O_CLOEXEC) < 0)
pipefd[0] = pipefd[1] = -1;
switch ((pid = fork())) {
case 0:
/* Write pid file */
dopidfile();
/* Get our own session */
setsid();
/* Reconnect our own terminal I/O */
dup2(ptm, 0);
{
const char *msg = "can not get terminal flags of stdin\n";
int flags;
if ((flags = fcntl(0, F_GETFL)) < 0)
list_for_each_entry(c, &cons->node, node) {
if (c->fd < 0)
continue;
(void)write(c->fd, msg, strlen(msg));
}
else {
flags &= ~(O_NONBLOCK);
flags |= O_NOCTTY;
if (fcntl(0, F_SETFL, flags) < 0)
list_for_each_entry(c, &cons->node, node) {
if (c->fd < 0)
continue;
(void)write(c->fd, msg, strlen(msg));
}
}
}
if (ptm > 0)
close(ptm);
if (pipefd[0] >= 0)
close(pipefd[0]);
break;
case -1:
if (ptm > 0)
close(ptm);
if (pipefd[0] >= 0)
close(pipefd[0]);
if (pipefd[1] >= 0)
close(pipefd[1]);
list_for_each_entry(c, &cons->node, node) {
const char *msg = "blogd: can not fork to become daemon: ";
const char *err = strerror(errno);
if (c->fd < 0)
continue;
(void)write(c->fd, msg, strlen(msg));
(void)write(c->fd, err, strlen(err));
(void)write(c->fd, "\n", 1);
}
exit(EXIT_FAILURE);
default:
time(&tt);
stt = ctime(&tt);
close(ptm);
if (pipefd[1] >= 0)
close(pipefd[1]);
list_for_each_entry(c, &cons->node, node) {
if (c->fd > 0) {
close(c->fd);
c->fd = -1;
}
}
fflush(stdout);
fprintf(stdout, "\rBoot logging started on %s(%s) at %.24s\n", tty, console, stt);
fflush(stdout);
if (pipefd[0] > 0) {
int dummy;
read(pipefd[0], &dummy, 1);
close(pipefd[0]);
}
_exit(EXIT_SUCCESS); /* NEVER rise exit handlers here */
}
atexit(flush_handler); /* Register flush exit handler */
prepareIO(reconnect, listen, 0);
if (pipefd[1] >= 0)
close(pipefd[1]);
while (!signaled)
safeIO();
exit(EXIT_SUCCESS); /* Raise exit handlers */
}
static void flush_handler (void)
{
(void)tcdrain(1);
(void)tcdrain(2);
closeIO();
}
static void exit_handler (void)
{
struct console *c;
int fd;
if (show_status == 0 && kill (1, SIGRTMIN+20) < 0)
warn("could not tell system to hode its status");
list_for_each_entry(c, &cons->node, node) {
if (c->fd > 0) {
if (c->tlock) /* write back old setup */
tcsetattr(c->fd, TCSADRAIN, &c->otio);
if (c->tlock > 1) /* write back lock if any */
(void)ioctl(c->fd, TIOCSLCKTRMIOS, &c->ltio);
c->tlock = 0;
if (c->fd > 1)
close(c->fd);
c->fd = -1;
}
}
errno = 0;
if ((fd = open(console, O_RDWR|O_NOCTTY)) >= 0) {
(void)ioctl(fd, TIOCCONS, NULL); /* Restore old console mapping */
if (fd > 0)
close(fd);
}
close(1);
close(2);
(void)tcflush(0, TCIFLUSH);
close(0);
reset_signal(SIGTTIN, &saved_sigttin);
reset_signal(SIGTTOU, &saved_sigttou);
reset_signal(SIGTSTP, &saved_sigtstp);
reset_signal(SIGHUP, &saved_sighup);
reset_signal(SIGPIPE, &saved_sigpipe);
reset_signal(SIGINT, &saved_sigint);
reset_signal(SIGQUIT, &saved_sigquit);
reset_signal(SIGTERM, &saved_sigterm);
reset_signal(SIGSYS, &saved_sigsys);
}