init: make sure fd 0,1,2 are not closed, + related optimizations.

init_main                                            929     920      -9
bb_daemonize_or_rexec                                145     127     -18
This commit is contained in:
Denis Vlasenko 2007-06-14 07:53:06 +00:00
parent 1adf681e87
commit d8540f71ac
3 changed files with 19 additions and 20 deletions

View File

@ -273,6 +273,9 @@ static void console_init(void)
while (fd > 2) close(fd--); while (fd > 2) close(fd--);
} }
messageD(L_LOG, "console='%s'", s); messageD(L_LOG, "console='%s'", s);
} else {
/* Make sure fd 0,1,2 are not closed */
bb_sanitize_stdio();
} }
s = getenv("TERM"); s = getenv("TERM");
@ -288,20 +291,14 @@ static void console_init(void)
putenv((char*)"TERM=linux"); putenv((char*)"TERM=linux");
} }
static void fixup_argv(int argc, char **argv, const char *new_argv0) static void fixup_argv(char **argv)
{ {
int len;
/* Fix up argv[0] to be certain we claim to be init */ /* Fix up argv[0] to be certain we claim to be init */
len = strlen(argv[0]); strncpy(argv[0], "init", strlen(argv[0]));
strncpy(argv[0], new_argv0, len);
/* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
len = 1; while (*++argv)
while (argc > len) { memset(*argv, 0, strlen(*argv));
memset(argv[len], 0, strlen(argv[len]));
len++;
}
} }
/* Open the new terminal device */ /* Open the new terminal device */
@ -919,6 +916,7 @@ int init_main(int argc, char **argv)
init_reboot(RB_DISABLE_CAD); init_reboot(RB_DISABLE_CAD);
#endif #endif
/* Figure out where the default console should be */ /* Figure out where the default console should be */
console_init(); console_init();
set_sane_term(); set_sane_term();
@ -972,7 +970,7 @@ int init_main(int argc, char **argv)
if (getenv("SELINUX_INIT") == NULL) { if (getenv("SELINUX_INIT") == NULL) {
int enforce = 0; int enforce = 0;
putenv("SELINUX_INIT=YES"); putenv((char*)"SELINUX_INIT=YES");
if (selinux_init_load_policy(&enforce) == 0) { if (selinux_init_load_policy(&enforce) == 0) {
BB_EXECVP(argv[0], argv); BB_EXECVP(argv[0], argv);
} else if (enforce > 0) { } else if (enforce > 0) {
@ -986,7 +984,7 @@ int init_main(int argc, char **argv)
#endif /* CONFIG_SELINUX */ #endif /* CONFIG_SELINUX */
/* Make the command line just say "init" -- thats all, nothing else */ /* Make the command line just say "init" -- thats all, nothing else */
fixup_argv(argc, argv, "init"); fixup_argv(argv);
/* Now run everything that needs to be run */ /* Now run everything that needs to be run */

View File

@ -202,7 +202,6 @@ int spawn_and_wait(char **argv)
return wait4pid(rc); return wait4pid(rc);
} }
#if !BB_MMU #if !BB_MMU
void forkexit_or_rexec(char **argv) void forkexit_or_rexec(char **argv)
{ {
@ -261,17 +260,18 @@ void bb_daemonize_or_rexec(int flags, char **argv)
if (!(flags & DAEMON_ONLY_SANITIZE)) { if (!(flags & DAEMON_ONLY_SANITIZE)) {
forkexit_or_rexec(argv); forkexit_or_rexec(argv);
/* if daemonizing, make sure we detach from stdio */ /* if daemonizing, make sure we detach from stdio & ctty */
setsid(); setsid();
dup2(fd, 0); dup2(fd, 0);
dup2(fd, 1); dup2(fd, 1);
dup2(fd, 2); dup2(fd, 2);
} }
if (fd > 2) while (fd > 2) {
close(fd--); close(fd--);
if (flags & DAEMON_CLOSE_EXTRA_FDS) if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
while (fd > 2) return;
close(fd--); /* close everything after fd#2 */ /* else close everything after fd#2 */
}
} }
void bb_sanitize_stdio(void) void bb_sanitize_stdio(void)

View File

@ -160,10 +160,11 @@ static ALWAYS_INLINE int check_securetty(void) { return 1; }
static void get_username_or_die(char *buf, int size_buf) static void get_username_or_die(char *buf, int size_buf)
{ {
int c, cntdown; int c, cntdown;
cntdown = EMPTY_USERNAME_COUNT; cntdown = EMPTY_USERNAME_COUNT;
prompt: prompt:
/* skip whitespace */
print_login_prompt(); print_login_prompt();
/* skip whitespace */
do { do {
c = getchar(); c = getchar();
if (c == EOF) exit(1); if (c == EOF) exit(1);