aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-14 07:53:06 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-14 07:53:06 +0000
commitd8540f71ac8d17ef461e2d52b3f63bd78b3c2c7e (patch)
tree2ab2ac263ef155ec9ea142ff297f89e634297bcf
parent1adf681e87f5cd81841f8a3d84a8d9bdf83c7406 (diff)
downloadbusybox-w32-d8540f71ac8d17ef461e2d52b3f63bd78b3c2c7e.tar.gz
busybox-w32-d8540f71ac8d17ef461e2d52b3f63bd78b3c2c7e.tar.bz2
busybox-w32-d8540f71ac8d17ef461e2d52b3f63bd78b3c2c7e.zip
init: make sure fd 0,1,2 are not closed, + related optimizations.
init_main 929 920 -9 bb_daemonize_or_rexec 145 127 -18
-rw-r--r--init/init.c22
-rw-r--r--libbb/vfork_daemon_rexec.c12
-rw-r--r--loginutils/login.c5
3 files changed, 19 insertions, 20 deletions
diff --git a/init/init.c b/init/init.c
index da79d101d..5c7efe210 100644
--- a/init/init.c
+++ b/init/init.c
@@ -273,6 +273,9 @@ static void console_init(void)
273 while (fd > 2) close(fd--); 273 while (fd > 2) close(fd--);
274 } 274 }
275 messageD(L_LOG, "console='%s'", s); 275 messageD(L_LOG, "console='%s'", s);
276 } else {
277 /* Make sure fd 0,1,2 are not closed */
278 bb_sanitize_stdio();
276 } 279 }
277 280
278 s = getenv("TERM"); 281 s = getenv("TERM");
@@ -288,20 +291,14 @@ static void console_init(void)
288 putenv((char*)"TERM=linux"); 291 putenv((char*)"TERM=linux");
289} 292}
290 293
291static void fixup_argv(int argc, char **argv, const char *new_argv0) 294static void fixup_argv(char **argv)
292{ 295{
293 int len;
294
295 /* Fix up argv[0] to be certain we claim to be init */ 296 /* Fix up argv[0] to be certain we claim to be init */
296 len = strlen(argv[0]); 297 strncpy(argv[0], "init", strlen(argv[0]));
297 strncpy(argv[0], new_argv0, len);
298 298
299 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ 299 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
300 len = 1; 300 while (*++argv)
301 while (argc > len) { 301 memset(*argv, 0, strlen(*argv));
302 memset(argv[len], 0, strlen(argv[len]));
303 len++;
304 }
305} 302}
306 303
307/* Open the new terminal device */ 304/* Open the new terminal device */
@@ -919,6 +916,7 @@ int init_main(int argc, char **argv)
919 init_reboot(RB_DISABLE_CAD); 916 init_reboot(RB_DISABLE_CAD);
920#endif 917#endif
921 918
919
922 /* Figure out where the default console should be */ 920 /* Figure out where the default console should be */
923 console_init(); 921 console_init();
924 set_sane_term(); 922 set_sane_term();
@@ -972,7 +970,7 @@ int init_main(int argc, char **argv)
972 if (getenv("SELINUX_INIT") == NULL) { 970 if (getenv("SELINUX_INIT") == NULL) {
973 int enforce = 0; 971 int enforce = 0;
974 972
975 putenv("SELINUX_INIT=YES"); 973 putenv((char*)"SELINUX_INIT=YES");
976 if (selinux_init_load_policy(&enforce) == 0) { 974 if (selinux_init_load_policy(&enforce) == 0) {
977 BB_EXECVP(argv[0], argv); 975 BB_EXECVP(argv[0], argv);
978 } else if (enforce > 0) { 976 } else if (enforce > 0) {
@@ -986,7 +984,7 @@ int init_main(int argc, char **argv)
986#endif /* CONFIG_SELINUX */ 984#endif /* CONFIG_SELINUX */
987 985
988 /* Make the command line just say "init" -- thats all, nothing else */ 986 /* Make the command line just say "init" -- thats all, nothing else */
989 fixup_argv(argc, argv, "init"); 987 fixup_argv(argv);
990 988
991 /* Now run everything that needs to be run */ 989 /* Now run everything that needs to be run */
992 990
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index cb4dee799..ea7b475eb 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -202,7 +202,6 @@ int spawn_and_wait(char **argv)
202 return wait4pid(rc); 202 return wait4pid(rc);
203} 203}
204 204
205
206#if !BB_MMU 205#if !BB_MMU
207void forkexit_or_rexec(char **argv) 206void forkexit_or_rexec(char **argv)
208{ 207{
@@ -261,17 +260,18 @@ void bb_daemonize_or_rexec(int flags, char **argv)
261 260
262 if (!(flags & DAEMON_ONLY_SANITIZE)) { 261 if (!(flags & DAEMON_ONLY_SANITIZE)) {
263 forkexit_or_rexec(argv); 262 forkexit_or_rexec(argv);
264 /* if daemonizing, make sure we detach from stdio */ 263 /* if daemonizing, make sure we detach from stdio & ctty */
265 setsid(); 264 setsid();
266 dup2(fd, 0); 265 dup2(fd, 0);
267 dup2(fd, 1); 266 dup2(fd, 1);
268 dup2(fd, 2); 267 dup2(fd, 2);
269 } 268 }
270 if (fd > 2) 269 while (fd > 2) {
271 close(fd--); 270 close(fd--);
272 if (flags & DAEMON_CLOSE_EXTRA_FDS) 271 if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
273 while (fd > 2) 272 return;
274 close(fd--); /* close everything after fd#2 */ 273 /* else close everything after fd#2 */
274 }
275} 275}
276 276
277void bb_sanitize_stdio(void) 277void bb_sanitize_stdio(void)
diff --git a/loginutils/login.c b/loginutils/login.c
index 0f71a2aa9..d69e3ce51 100644
--- a/loginutils/login.c
+++ b/loginutils/login.c
@@ -160,10 +160,11 @@ static ALWAYS_INLINE int check_securetty(void) { return 1; }
160static void get_username_or_die(char *buf, int size_buf) 160static void get_username_or_die(char *buf, int size_buf)
161{ 161{
162 int c, cntdown; 162 int c, cntdown;
163
163 cntdown = EMPTY_USERNAME_COUNT; 164 cntdown = EMPTY_USERNAME_COUNT;
164prompt: 165 prompt:
165 /* skip whitespace */
166 print_login_prompt(); 166 print_login_prompt();
167 /* skip whitespace */
167 do { 168 do {
168 c = getchar(); 169 c = getchar();
169 if (c == EOF) exit(1); 170 if (c == EOF) exit(1);