forked from slicer69/sysvinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
3191 lines (2867 loc) · 69.5 KB
/
init.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Init A System-V Init Clone.
*
* Usage: /sbin/init
* init [0123456SsQqAaBbCc]
* telinit [0123456SsQqAaBbCc]
*
* Version: @(#)init.c 2.86 30-Jul-2004 [email protected]
* Version: init.c 2.90 18-Jun-2018 [email protected]
*/
/*
Version information is not placed in the top-level Makefile by default
*/
#ifndef VERSION
#define VERSION "3.01"
#endif
/*
* This file is part of the sysvinit suite,
* Copyright (C) 1991-2004 Miquel van Smoorenburg.
* Copyright (C) 2017-2019 Jesse Smith
*
* This program 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#ifdef __linux__
#include <sys/kd.h>
#endif
#include <sys/resource.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#ifdef __FreeBSD__
#include <utmpx.h>
#else
#include <utmp.h>
#endif
#include <ctype.h>
#include <stdarg.h>
#include <sys/ttydefaults.h>
#include <sys/syslog.h>
#include <sys/time.h>
/*
* inittab.d
*/
#include <sys/types.h>
#include <dirent.h>
#ifdef WITH_SELINUX
# include <selinux/selinux.h>
#endif
#ifdef __FreeBSD__
extern char **environ;
#endif
#ifdef __i386__
# ifdef __GLIBC__
/* GNU libc 2.x */
# define STACK_DEBUG 1
# if (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
/* Only glibc 2.0 needs this */
# include <sigcontext.h>
# elif ( __GLIBC__ > 2) && ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 1))
# include <bits/sigcontext.h>
# endif
# endif
#endif
#include "init.h"
#include "initreq.h"
#include "paths.h"
#include "reboot.h"
#include "runlevellog.h"
#include "set.h"
#ifndef SIGPWR
# define SIGPWR SIGUSR2
#endif
#ifndef CBAUD
# define CBAUD 0
#endif
#ifndef CBAUDEX
# define CBAUDEX 0
#endif
/* Set a signal handler. */
#define SETSIG(sa, sig, fun, flags) \
do { \
memset(&sa, 0, sizeof(sa)); \
sa.sa_handler = fun; \
sa.sa_flags = flags; \
sigemptyset(&sa.sa_mask); \
sigaction(sig, &sa, NULL); \
} while(0)
/* Version information */
char *Version = "@(#) init " VERSION " [email protected]";
char *bootmsg = "version " VERSION " %s";
#define E_VERSION "INIT_VERSION=sysvinit-" VERSION
CHILD *family = NULL; /* The linked list of all entries */
CHILD *newFamily = NULL; /* The list after inittab re-read */
CHILD ch_emerg = { /* Emergency shell */
WAITING, 0, 0, 0, 0,
"~~",
"S",
3,
"/sbin/sulogin",
NULL,
NULL
};
char runlevel = 'S'; /* The current run level */
char thislevel = 'S'; /* The current runlevel */
char prevlevel = 'N'; /* Previous runlevel */
int dfl_level = 0; /* Default runlevel */
sig_atomic_t got_cont = 0; /* Set if we received the SIGCONT signal */
sig_atomic_t got_signals; /* Set if we received a signal. */
int emerg_shell = 0; /* Start emergency shell? */
int wrote_wtmp_reboot = 1; /* Set when we wrote the reboot record */
int wrote_utmp_reboot = 1; /* Set when we wrote the reboot record */
int wrote_wtmp_rlevel = 1; /* Set when we wrote the runlevel record */
int wrote_utmp_rlevel = 1; /* Set when we wrote the runlevel record */
int sleep_time = WAIT_BETWEEN_SIGNALS; /* Sleep time between TERM and KILL */
char *argv0; /* First arguments; show up in ps listing */
int maxproclen; /* Maximal length of argv[0] with \0 */
struct utmp utproto; /* Only used for sizeof(utproto.ut_id) */
char *console_dev; /* Console device. */
int pipe_fd = -1; /* /run/initctl */
int did_boot = 0; /* Did we already do BOOT* stuff? */
int main(int, char **);
/* Used by re-exec part */
int reload = 0; /* Should we do initialization stuff? */
char *myname=INIT; /* What should we exec */
int oops_error; /* Used by some of the re-exec code. */
const char *Signature = "12567362"; /* Signature for re-exec fd */
/* Macro to see if this is a special action */
#define ISPOWER(i) ((i) == POWERWAIT || (i) == POWERFAIL || \
(i) == POWEROKWAIT || (i) == POWERFAILNOW || \
(i) == CTRLALTDEL)
/* ascii values for the `action' field. */
struct actions {
char *name;
int act;
} actions[] = {
{ "respawn", RESPAWN },
{ "wait", WAIT },
{ "once", ONCE },
{ "boot", BOOT },
{ "bootwait", BOOTWAIT },
{ "powerfail", POWERFAIL },
{ "powerfailnow",POWERFAILNOW },
{ "powerwait", POWERWAIT },
{ "powerokwait", POWEROKWAIT },
{ "ctrlaltdel", CTRLALTDEL },
{ "off", OFF },
{ "ondemand", ONDEMAND },
{ "initdefault", INITDEFAULT },
{ "sysinit", SYSINIT },
{ "kbrequest", KBREQUEST },
{ NULL, 0 },
};
/*
* State parser token table (see receive_state)
*/
struct {
char name[4];
int cmd;
} cmds[] = {
{ "VER", C_VER },
{ "END", C_END },
{ "REC", C_REC },
{ "EOR", C_EOR },
{ "LEV", C_LEV },
{ "FL ", C_FLAG },
{ "AC ", C_ACTION },
{ "CMD", C_PROCESS },
{ "PID", C_PID },
{ "EXS", C_EXS },
{ "-RL", D_RUNLEVEL },
{ "-TL", D_THISLEVEL },
{ "-PL", D_PREVLEVEL },
{ "-SI", D_GOTSIGN },
{ "-WR", D_WROTE_WTMP_REBOOT},
{ "-WU", D_WROTE_UTMP_REBOOT},
{ "-ST", D_SLTIME },
{ "-DB", D_DIDBOOT },
{ "-LW", D_WROTE_WTMP_RLEVEL},
{ "-LU", D_WROTE_UTMP_RLEVEL},
{ "", 0 }
};
struct {
char *name;
int mask;
} flags[]={
{"RU",RUNNING},
{"DE",DEMAND},
{"XD",XECUTED},
{"WT",WAITING},
{NULL,0}
};
#define NR_EXTRA_ENV 16
char *extra_env[NR_EXTRA_ENV];
#define MINI_SLEEP 10 /* ten milliseconds */
#define SHORT_SLEEP 5000 /* five seconds */
#define LONG_SLEEP 30000 /* 30 seconds sleep to deal with memory issues*/
/*
* Sleep a number of milliseconds.
*
* This only works correctly because Linux select updates
* the elapsed time in the struct timeval passed to select!
*/
static
void do_msleep(int msec)
{
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
while(select(0, NULL, NULL, NULL, &tv) < 0 && errno == EINTR)
;
}
/*
* Non-failing allocation routines (init cannot fail).
*/
static
void *imalloc(size_t size)
{
void *m;
while ((m = malloc(size)) == NULL) {
initlog(L_VB, "out of memory");
do_msleep(SHORT_SLEEP);
}
memset(m, 0, size);
return m;
}
static
char *istrdup(const char *s)
{
char *m;
int l;
l = strlen(s) + 1;
m = imalloc(l);
memcpy(m, s, l);
return m;
}
/*
* Send the state info of the previous running init to
* the new one, in a version-independant way.
*/
static
void send_state(int fd)
{
FILE *fp;
CHILD *p;
int i,val;
fp = fdopen(fd,"w");
fprintf(fp, "VER%s\n", Version);
fprintf(fp, "-RL%c\n", runlevel);
fprintf(fp, "-TL%c\n", thislevel);
fprintf(fp, "-PL%c\n", prevlevel);
fprintf(fp, "-SI%u\n", got_signals);
fprintf(fp, "-WR%d\n", wrote_wtmp_reboot);
fprintf(fp, "-WU%d\n", wrote_utmp_reboot);
fprintf(fp, "-ST%d\n", sleep_time);
fprintf(fp, "-DB%d\n", did_boot);
for (p = family; p; p = p->next) {
fprintf(fp, "REC%s\n", p->id);
fprintf(fp, "LEV%s\n", p->rlevel);
for (i = 0, val = p->flags; flags[i].mask; i++)
if (val & flags[i].mask) {
val &= ~flags[i].mask;
fprintf(fp, "FL %s\n",flags[i].name);
}
fprintf(fp, "PID%d\n",p->pid);
fprintf(fp, "EXS%u\n",p->exstat);
for(i = 0; actions[i].act; i++)
if (actions[i].act == p->action) {
fprintf(fp, "AC %s\n", actions[i].name);
break;
}
fprintf(fp, "CMD%s\n", p->process);
fprintf(fp, "EOR\n");
}
fprintf(fp, "END\n");
fclose(fp);
}
/*
* Read a string from a file descriptor.
* Q: why not use fgets() ?
* A: Answer: looked into this. Turns out after all the checks
* required in the fgets() approach (removing newline, read errors, etc)
* the function is longer and takes approximately the same amount of
* time to do one big fgets and checks as it does to do a pile of getcs.
* We don't benefit from switching.
* - Jesse
*/
static int get_string(char *p, int size, FILE *f)
{
int c;
while ((c = getc(f)) != EOF && c != '\n') {
if (--size > 0)
*p++ = c;
}
*p = '\0';
return (c != EOF) && (size > 0);
}
/*
* Read trailing data from the state pipe until we see a newline.
*/
static int get_void(FILE *f)
{
int c;
while ((c = getc(f)) != EOF && c != '\n')
;
return (c != EOF);
}
/*
* Read the next "command" from the state pipe.
*/
static int get_cmd(FILE *f)
{
char cmd[4] = " ";
int i;
if (fread(cmd, 1, sizeof(cmd) - 1, f) != sizeof(cmd) - 1)
return C_EOF;
for(i = 0; cmds[i].cmd && strcmp(cmds[i].name, cmd) != 0; i++)
;
return cmds[i].cmd;
}
/*
* Read a CHILD * from the state pipe.
*/
static CHILD *get_record(FILE *f)
{
int cmd;
char s[32];
int i;
CHILD *p;
do {
errno = 0;
switch (cmd = get_cmd(f)) {
case C_END:
get_void(f);
return NULL;
case 0:
get_void(f);
break;
case C_REC:
break;
case D_RUNLEVEL:
if (fscanf(f, "%c\n", &runlevel) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_THISLEVEL:
if (fscanf(f, "%c\n", &thislevel) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_PREVLEVEL:
if (fscanf(f, "%c\n", &prevlevel) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_GOTSIGN:
if (fscanf(f, "%u\n", &got_signals) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_WROTE_WTMP_REBOOT:
if (fscanf(f, "%d\n", &wrote_wtmp_reboot) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_WROTE_UTMP_REBOOT:
if (fscanf(f, "%d\n", &wrote_utmp_reboot) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_SLTIME:
if (fscanf(f, "%d\n", &sleep_time) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_DIDBOOT:
if (fscanf(f, "%d\n", &did_boot) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_WROTE_WTMP_RLEVEL:
if (fscanf(f, "%d\n", &wrote_wtmp_rlevel) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case D_WROTE_UTMP_RLEVEL:
if (fscanf(f, "%d\n", &wrote_utmp_rlevel) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
default:
if (cmd > 0 || cmd == C_EOF) {
oops_error = -1;
return NULL;
}
}
} while (cmd != C_REC);
p = imalloc(sizeof(CHILD));
get_string(p->id, sizeof(p->id), f);
do switch(cmd = get_cmd(f)) {
case 0:
case C_EOR:
get_void(f);
break;
case C_PID:
if (fscanf(f, "%d\n", &(p->pid)) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case C_EXS:
if (fscanf(f, "%u\n", &(p->exstat)) == EOF && errno != 0) {
fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
}
break;
case C_LEV:
get_string(p->rlevel, sizeof(p->rlevel), f);
break;
case C_PROCESS:
get_string(p->process, sizeof(p->process), f);
break;
case C_FLAG:
get_string(s, sizeof(s), f);
for(i = 0; flags[i].name; i++) {
if (strcmp(flags[i].name,s) == 0)
break;
}
p->flags |= flags[i].mask;
break;
case C_ACTION:
get_string(s, sizeof(s), f);
for(i = 0; actions[i].name; i++) {
if (strcmp(actions[i].name, s) == 0)
break;
}
p->action = actions[i].act ? actions[i].act : OFF;
break;
default:
free(p);
oops_error = -1;
return NULL;
} while( cmd != C_EOR);
return p;
}
/*
* Read the complete state info from the state pipe.
* Returns 0 on success
*/
static
int receive_state(int fd)
{
FILE *f;
char old_version[256];
CHILD **pp;
f = fdopen(fd, "r");
if (get_cmd(f) != C_VER) {
fclose(f);
return -1;
}
get_string(old_version, sizeof(old_version), f);
oops_error = 0;
for (pp = &family; (*pp = get_record(f)) != NULL; pp = &((*pp)->next))
;
fclose(f);
return oops_error;
}
/*
* Set the process title.
*/
#ifdef __GNUC__
#ifndef __FreeBSD__
__attribute__ ((format (printf, 1, 2)))
#endif
#endif
/* This function already exists on FreeBSD. No need to declare it. */
#ifndef __FreeBSD__
static int setproctitle(char *fmt, ...)
{
va_list ap;
int len;
char buf[256];
buf[0] = 0;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (maxproclen > 1) {
memset(argv0, 0, maxproclen);
strncpy(argv0, buf, maxproclen - 1);
}
return len;
}
#endif
/*
* Set console_dev to a working console.
*/
static
void console_init(void)
{
int fd;
int tried_devcons = 0;
int tried_vtmaster = 0;
char *s;
if ((s = getenv("CONSOLE")) != NULL)
console_dev = s;
else {
console_dev = CONSOLE;
tried_devcons++;
}
while ((fd = open(console_dev, O_RDONLY|O_NONBLOCK)) < 0) {
if (!tried_devcons) {
tried_devcons++;
console_dev = CONSOLE;
continue;
}
if (!tried_vtmaster) {
tried_vtmaster++;
console_dev = VT_MASTER;
continue;
}
break;
}
if (fd < 0)
console_dev = "/dev/null";
else
close(fd);
}
/*
* Open the console with retries.
*/
static
int console_open(int mode)
{
int f, fd = -1;
int m;
/*
* Open device in nonblocking mode.
*/
m = mode | O_NONBLOCK;
/*
* Retry the open five times.
*/
for(f = 0; f < 5; f++) {
if ((fd = open(console_dev, m)) >= 0) break;
usleep(10000);
}
if (fd < 0) return fd;
/*
* Set original flags.
*/
if (m != mode)
fcntl(fd, F_SETFL, mode);
return fd;
}
/*
* We got a signal (HUP PWR WINCH ALRM INT)
*/
static
void signal_handler(int sig)
{
ADDSET(got_signals, sig);
}
/*
* SIGCHLD: one of our children has died.
*/
static
# ifdef __GNUC__
void chld_handler(int sig __attribute__((unused)))
# else
void chld_handler(int sig)
# endif
{
CHILD *ch;
int pid, st;
int saved_errno = errno;
/*
* Find out which process(es) this was (were)
*/
while((pid = waitpid(-1, &st, WNOHANG)) != 0) {
if (errno == ECHILD) break;
for( ch = family; ch; ch = ch->next )
if ( ch->pid == pid && (ch->flags & RUNNING) ) {
INITDBG(L_VB,
"chld_handler: marked %d as zombie",
ch->pid);
ADDSET(got_signals, SIGCHLD);
ch->exstat = st;
ch->flags |= ZOMBIE;
if (ch->new) {
ch->new->exstat = st;
ch->new->flags |= ZOMBIE;
}
break;
}
if (ch == NULL) {
INITDBG(L_VB, "chld_handler: unknown child %d exited.",
pid);
}
}
errno = saved_errno;
}
/*
* Linux ignores all signals sent to init when the
* SIG_DFL handler is installed. Therefore we must catch SIGTSTP
* and SIGCONT, or else they won't work....
*
* The SIGCONT handler
*/
static
# ifdef __GNUC__
void cont_handler(int sig __attribute__((unused)))
# else
void cont_handler(int sig)
# endif
{
got_cont = 1;
}
/*
* Fork and dump core in /.
*/
static
void coredump(void)
{
static int dumped = 0;
struct rlimit rlim;
sigset_t mask;
if (dumped) return;
dumped = 1;
if (fork() != 0) return;
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &rlim);
if (0 != chdir("/"))
initlog(L_VB, "unable to chdir to /: %s",
strerror(errno));
signal(SIGSEGV, SIG_DFL);
raise(SIGSEGV);
sigdelset(&mask, SIGSEGV);
sigprocmask(SIG_SETMASK, &mask, NULL);
do_msleep(SHORT_SLEEP);
exit(0);
}
/*
* OOPS: segmentation violation!
* If we have the info, print where it occurred.
* Then sleep 30 seconds and try to continue.
*/
static
#if defined(STACK_DEBUG) && defined(__linux__)
# ifdef __GNUC__
void segv_handler(int sig __attribute__((unused)), struct sigcontext ctx)
# else
void segv_handler(int sig, struct sigcontext ctx)
# endif
{
char *p = "";
int saved_errno = errno;
if ((void *)ctx.eip >= (void *)do_msleep &&
(void *)ctx.eip < (void *)main)
p = " (code)";
initlog(L_VB, "PANIC: segmentation violation at %p%s! "
"sleeping for 30 seconds.", (void *)ctx.eip, p);
coredump();
do_msleep(LONG_SLEEP);
errno = saved_errno;
}
#else
# ifdef __GNUC__
void segv_handler(int sig __attribute__((unused)))
# else
void segv_handler(int sig)
# endif
{
int saved_errno = errno;
initlog(L_VB,
"PANIC: segmentation violation! sleeping for 30 seconds.");
coredump();
do_msleep(LONG_SLEEP);
errno = saved_errno;
}
#endif
/*
* The SIGSTOP & SIGTSTP handler
*/
static
# ifdef __GNUC__
void stop_handler(int sig __attribute__((unused)))
# else
void stop_handler(int sig)
# endif
{
int saved_errno = errno;
got_cont = 0;
while(!got_cont) pause();
got_cont = 0;
errno = saved_errno;
}
/*
* Set terminal settings to reasonable defaults
*/
static
void console_stty(void)
{
struct termios tty;
int fd;
if ((fd = console_open(O_RDWR|O_NOCTTY)) < 0) {
initlog(L_VB, "can't open %s", console_dev);
return;
}
#ifdef __FreeBSD_kernel__
/*
* The kernel of FreeBSD expects userland to set TERM. Usually, we want
* "xterm". Later, gettys might disagree on this (i.e. we're not using
* syscons) but some boot scripts, like /etc/init.d/xserver-xorg, still
* need a non-dumb terminal.
*/
putenv ("TERM=xterm");
#endif
(void) tcgetattr(fd, &tty);
tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
tty.c_cflag |= HUPCL|CLOCAL|CREAD;
tty.c_cc[VINTR] = CINTR;
tty.c_cc[VQUIT] = CQUIT;
tty.c_cc[VERASE] = CERASE; /* ASCII DEL (0177) */
tty.c_cc[VKILL] = CKILL;
tty.c_cc[VEOF] = CEOF;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
#ifdef VSWTC /* not defined on FreeBSD */
tty.c_cc[VSWTC] = _POSIX_VDISABLE;
#endif /* VSWTC */
tty.c_cc[VSTART] = CSTART;
tty.c_cc[VSTOP] = CSTOP;
tty.c_cc[VSUSP] = CSUSP;
tty.c_cc[VEOL] = _POSIX_VDISABLE;
tty.c_cc[VREPRINT] = CREPRINT;
tty.c_cc[VDISCARD] = CDISCARD;
tty.c_cc[VWERASE] = CWERASE;
tty.c_cc[VLNEXT] = CLNEXT;
tty.c_cc[VEOL2] = _POSIX_VDISABLE;
/*
* Set pre and post processing
*/
tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY
#ifdef IUTF8 /* Not defined on FreeBSD */
| (tty.c_iflag & IUTF8)
#endif /* IUTF8 */
;
tty.c_oflag = OPOST|ONLCR;
tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOE|ECHOKE;
#if defined(SANE_TIO) && (SANE_TIO == 1)
/*
* Disable flow control (-ixon), ignore break (ignbrk),
* and make nl/cr more usable (sane).
*/
tty.c_iflag |= IGNBRK;
tty.c_iflag &= ~(BRKINT|INLCR|IGNCR|IXON);
tty.c_oflag &= ~(OCRNL|ONLRET);
#endif
/*
* Now set the terminal line.
* We don't care about non-transmitted output data
* and non-read input data.
*/
(void) tcsetattr(fd, TCSANOW, &tty);
(void) tcflush(fd, TCIOFLUSH);
(void) close(fd);
}
static ssize_t
safe_write(int fd, const char *buffer, size_t count)
{
ssize_t offset = 0;
while (count > 0) {
ssize_t block = write(fd, &buffer[offset], count);
if (block < 0 && errno == EINTR)
continue;
if (block <= 0)
return offset ? offset : block;
offset += block;
count -= block;
}
return offset;
}
/*
* Print to the system console
*/
void print(char *s)
{
int fd;
if ((fd = console_open(O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
safe_write(fd, s, strlen(s));
close(fd);
}
}
/*
* Log something to a logfile and the console.
*/
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif
void initlog(int loglevel, char *s, ...)
{
va_list va_alist;
char buf[256];
sigset_t nmask, omask;
va_start(va_alist, s);
vsnprintf(buf, sizeof(buf), s, va_alist);
va_end(va_alist);
if (loglevel & L_SY) {
/*
* Re-establish connection with syslogd every time.
* Block signals while talking to syslog.
*/
sigfillset(&nmask);
sigprocmask(SIG_BLOCK, &nmask, &omask);
openlog("init", 0, LOG_DAEMON);
syslog(LOG_INFO, "%s", buf);
closelog();
sigprocmask(SIG_SETMASK, &omask, NULL);
}
/*
* And log to the console.
*/
if (loglevel & L_CO) {
print("\rINIT: ");
print(buf);
print("\r\n");
}
}
/*
* Add or replace specific environment value
*/
int addnewenv(const char *new, char **curr, int n)
{
size_t nlen = strcspn(new, "=");
int i;
for (i = 0; i < n; i++) {
if (nlen != strcspn(curr[i], "="))
continue;
if (strncmp (new, curr[i], nlen) == 0)
break;
}
if (i >= n)
curr[n++] = istrdup(new);
else {
free(curr[i]);
curr[i] = istrdup(new);
}
return n;
}
/*
* Build a new environment for execve().
*/
char **init_buildenv(int child)
{
char i_lvl[] = "RUNLEVEL=x";
char i_prev[] = "PREVLEVEL=x";
char i_cons[128];
char i_shell[] = "SHELL=" SHELL;
char **e;
int n, i;
for (n = 0; environ[n]; n++)
;
n += NR_EXTRA_ENV + 1; /* Also room for last NULL */
if (child)
n += 8;
while ((e = (char**)calloc(n, sizeof(char *))) == NULL) {
initlog(L_VB, "out of memory");
do_msleep(SHORT_SLEEP);
}
for (n = 0; environ[n]; n++)
e[n] = istrdup(environ[n]);
for (i = 0; i < NR_EXTRA_ENV; i++) {
if (extra_env[i] == NULL || *extra_env[i] == '\0')
continue;
n = addnewenv(extra_env[i], e, n);
}