-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminit.c
98 lines (73 loc) · 2.25 KB
/
minit.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <termios.h>
#include "tools.h"
#include "respawn.h"
#include "config.h"
int ueld_main(int argc, char* argv[]);
#ifndef CONFIG_NO_BUILD_IN_MINIT
int timeout = 0;
void sig_alarm(int signo) {
timeout = 1;
}
int main(int argc, char* argv[]) {
if (argc > 0 && (strcmp(argv[0], "-ueld") == 0)) {
return ueld_main(argc, argv);
}
if (ueld_readconfiglong("ueld_enable_multi_init", -1) != 1) {
return ueld_main(argc, argv);
}
/* then, use multi init... */
if (getpid() != 1) {
char* args[argc + 1];
for (int i = 0; i < argc; i++) {
args[i] = argv[i];
}
args[argc] = NULL;
char* other_init_telinit = ueld_readconfig("ueld_multi_init_other_init_telinit");
if (other_init_telinit)
execvp(other_init_telinit, args);
return ueld_main(argc, argv);
}
ueld_signal(SIGALRM, sig_alarm, 0);
printf("%s\n", "=============================\n"
"[Ueld Multi Init]\n"
"Press the keys(default s) to load the init which you configed, "
"the others or timeout to load ueld.");
struct termios ots, ts;
int s;
if ((s = tcgetattr(STDIN_FILENO, &ts)) == 0) {
ots = ts;
ts.c_lflag &= ~(ECHO | ECHONL | ICANON);
ts.c_cc[VMIN] = 1;
ts.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &ts) < 0)
ueld_echo("Warning: could not change the terminal characteristics.");
} else {
ueld_echo("Warning: could not get the terminal characteristics.");
}
alarm(ueld_readconfiglong("ueld_multi_init_choose_time_out", 3));
char c = 0;
read(STDIN_FILENO, &c, 1);
ueld_signal(SIGALRM, SIG_IGN, 0);
if (s == 0){
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &ots) < 0)
ueld_echo("Warning: could not restore the terminal characteristics.");
}
char* press_keys = ueld_readconfig("ueld_multi_init_press_keys");
if (!press_keys) press_keys = "sS";
if ((c != '\0') && strchr(press_keys, c) && !timeout) {
char* other_init = ueld_readconfig("ueld_multi_init_other_init");
if (other_init)
execl(other_init, argv[0], NULL);
ueld_echo("Can not load other init, load ueld!!!");
}
return ueld_main(argc, argv);
}
#else
int main(int argc, char* argv[]) { return ueld_main(argc, argv); }
#endif /* CONFIG_NO_BUILD_IN_MINIT */