summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-19 21:19:35 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-19 21:19:35 +0000
commit9af7c9d6b62ceb07a9ba24cee0cf4a08c689235e (patch)
tree69a650a8cd686f21087fc572a995420b6ac632cc
parentf8c11aa65df2af4ab20c0effc42997bbd7357cc3 (diff)
downloadbusybox-w32-9af7c9d6b62ceb07a9ba24cee0cf4a08c689235e.tar.gz
busybox-w32-9af7c9d6b62ceb07a9ba24cee0cf4a08c689235e.tar.bz2
busybox-w32-9af7c9d6b62ceb07a9ba24cee0cf4a08c689235e.zip
openvt,getty,vfork_daemon_rexec,mount: tighten up fd cleanup code
(will close all fd's > 2 on daemonization now) getty: fix "getty -" support, and also do not try to chown/chmod "-" telnetd: fix "lost ctty" bug Yet another attempt on saner function names: bb_sanitize_server_stdio(0/1) -> bb_sanitize_stdio() + bb_daemonize();
-rw-r--r--console-tools/openvt.c15
-rw-r--r--debianutils/start_stop_daemon.c2
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/vfork_daemon_rexec.c4
-rw-r--r--libbb/xfuncs.c12
-rw-r--r--loginutils/getty.c32
-rw-r--r--miscutils/setsid.c7
-rw-r--r--networking/fakeidentd.c2
-rw-r--r--networking/inetd.c4
-rw-r--r--networking/isrv_identd.c2
-rw-r--r--networking/telnetd.c12
-rw-r--r--networking/zcip.c5
-rw-r--r--shell/hush.c6
-rw-r--r--shell/lash.c2
-rw-r--r--util-linux/mount.c2
15 files changed, 57 insertions, 54 deletions
diff --git a/console-tools/openvt.c b/console-tools/openvt.c
index f1cf5645b..c7b3e4fa4 100644
--- a/console-tools/openvt.c
+++ b/console-tools/openvt.c
@@ -17,7 +17,6 @@ int openvt_main(int argc, char **argv)
17 int fd; 17 int fd;
18 char vtname[sizeof(VC_FORMAT) + 2]; 18 char vtname[sizeof(VC_FORMAT) + 2];
19 19
20
21 if (argc < 3) { 20 if (argc < 3) {
22 bb_show_usage(); 21 bb_show_usage();
23 } 22 }
@@ -25,18 +24,16 @@ int openvt_main(int argc, char **argv)
25 sprintf(vtname, VC_FORMAT, (int)xatoul_range(argv[1], 1, 63)); 24 sprintf(vtname, VC_FORMAT, (int)xatoul_range(argv[1], 1, 63));
26 25
27 if (fork() == 0) { 26 if (fork() == 0) {
28 /* leave current vt */ 27 /* child */
29 if (setsid() < 0) { 28 /* leave current vt (controlling tty) */
30 bb_perror_msg_and_die("setsid"); 29 setsid();
31 }
32 close(0); /* so that new vt becomes stdin */
33
34 /* and grab new one */ 30 /* and grab new one */
35 fd = xopen(vtname, O_RDWR); 31 fd = xopen(vtname, O_RDWR);
36 32 /* Reassign stdin, stdout and sterr */
37 /* Reassign stdout and sterr */ 33 dup2(fd, STDIN_FILENO);
38 dup2(fd, STDOUT_FILENO); 34 dup2(fd, STDOUT_FILENO);
39 dup2(fd, STDERR_FILENO); 35 dup2(fd, STDERR_FILENO);
36 while (fd > 2) close(fd--);
40 37
41 execvp(argv[2], &argv[2]); 38 execvp(argv[2], &argv[2]);
42 _exit(1); 39 _exit(1);
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 521b43d04..9a865f6c6 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -291,8 +291,8 @@ int start_stop_daemon_main(int argc, char **argv)
291 } 291 }
292 *--argv = startas; 292 *--argv = startas;
293 if (opt & OPT_BACKGROUND) { 293 if (opt & OPT_BACKGROUND) {
294 xdaemon(0, 0);
295 setsid(); 294 setsid();
295 bb_daemonize();
296 } 296 }
297 if (opt & OPT_MAKEPID) { 297 if (opt & OPT_MAKEPID) {
298 /* user wants _us_ to make the pidfile */ 298 /* user wants _us_ to make the pidfile */
diff --git a/include/libbb.h b/include/libbb.h
index 2089d2322..7721cbf82 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -268,7 +268,9 @@ extern void xsetgid(gid_t gid);
268extern void xsetuid(uid_t uid); 268extern void xsetuid(uid_t uid);
269extern void xdaemon(int nochdir, int noclose); 269extern void xdaemon(int nochdir, int noclose);
270/* More clever/thorough xdaemon */ 270/* More clever/thorough xdaemon */
271extern void bb_sanitize_server_stdio(int daemonize); 271extern void bb_sanitize_stdio_maybe_daemonize(int daemonize);
272extern void bb_sanitize_stdio(void);
273extern void bb_daemonize(void);
272extern void xchdir(const char *path); 274extern void xchdir(const char *path);
273extern void xsetenv(const char *key, const char *value); 275extern void xsetenv(const char *key, const char *value);
274extern int xopen(const char *pathname, int flags); 276extern int xopen(const char *pathname, int flags);
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index 81ae12687..26d1826e0 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -35,8 +35,8 @@ void vfork_daemon_rexec(int nochdir, int noclose,
35 dup2(fd, STDIN_FILENO); 35 dup2(fd, STDIN_FILENO);
36 dup2(fd, STDOUT_FILENO); 36 dup2(fd, STDOUT_FILENO);
37 dup2(fd, STDERR_FILENO); 37 dup2(fd, STDERR_FILENO);
38 if (fd > 2) 38 while (fd > 2)
39 close(fd); 39 close(fd--);
40 } 40 }
41 41
42 vfork_args = xzalloc(sizeof(char *) * (argc + 3)); 42 vfork_args = xzalloc(sizeof(char *) * (argc + 3));
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 84d47414a..dc160bf5c 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -509,7 +509,7 @@ void xdaemon(int nochdir, int noclose)
509} 509}
510#endif 510#endif
511 511
512void bb_sanitize_server_stdio(int daemonize) 512void bb_sanitize_stdio_maybe_daemonize(int daemonize)
513{ 513{
514 int fd; 514 int fd;
515 /* Mega-paranoid */ 515 /* Mega-paranoid */
@@ -523,8 +523,8 @@ void bb_sanitize_server_stdio(int daemonize)
523 if (pid) /* parent */ 523 if (pid) /* parent */
524 exit(0); 524 exit(0);
525 /* child */ 525 /* child */
526 setsid();
527 /* if daemonizing, make sure we detach from stdio */ 526 /* if daemonizing, make sure we detach from stdio */
527 setsid();
528 dup2(fd, 0); 528 dup2(fd, 0);
529 dup2(fd, 1); 529 dup2(fd, 1);
530 dup2(fd, 2); 530 dup2(fd, 2);
@@ -532,6 +532,14 @@ void bb_sanitize_server_stdio(int daemonize)
532 while (fd > 2) 532 while (fd > 2)
533 close(fd--); /* close everything after fd#2 */ 533 close(fd--); /* close everything after fd#2 */
534} 534}
535void bb_sanitize_stdio(void)
536{
537 bb_sanitize_stdio_maybe_daemonize(0);
538}
539void bb_daemonize(void)
540{
541 bb_sanitize_stdio_maybe_daemonize(1);
542}
535 543
536// Die with an error message if we can't open a new socket. 544// Die with an error message if we can't open a new socket.
537int xsocket(int domain, int type, int protocol) 545int xsocket(int domain, int type, int protocol)
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 5ceaefcac..be4938972 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -211,7 +211,7 @@ static void parse_args(int argc, char **argv, struct options *op)
211 bb_show_usage(); 211 bb_show_usage();
212 212
213 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ 213 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
214 if ('0' <= argv[0][0] && argv[0][0] <= '9') { 214 if (isdigit(argv[0][0])) {
215 /* a number first, assume it's a speed (BSD style) */ 215 /* a number first, assume it's a speed (BSD style) */
216 parse_speeds(op, argv[0]); /* baud rate(s) */ 216 parse_speeds(op, argv[0]); /* baud rate(s) */
217 op->tty = argv[1]; /* tty name */ 217 op->tty = argv[1]; /* tty name */
@@ -255,10 +255,8 @@ static void open_tty(char *tty, struct termios *tp, int local)
255 255
256 debug("open(2)\n"); 256 debug("open(2)\n");
257 fd = xopen(tty, O_RDWR | O_NONBLOCK); 257 fd = xopen(tty, O_RDWR | O_NONBLOCK);
258 if (fd) { 258 xdup2(fd, 0, tty);
259 xdup2(fd, 0, tty); 259 while (fd > 2) close(fd--);
260 close(fd);
261 }
262 } else { 260 } else {
263 /* 261 /*
264 * Standard input should already be connected to an open port. Make 262 * Standard input should already be connected to an open port. Make
@@ -327,8 +325,10 @@ static void open_tty(char *tty, struct termios *tp, int local)
327 } 325 }
328 } 326 }
329#else 327#else
330 chown(tty, 0, 0); /* root, sys */ 328 if (NOT_LONE_DASH(tty)) {
331 chmod(tty, 0622); /* crw--w--w- */ 329 chown(tty, 0, 0); /* 0:0 */
330 chmod(tty, 0622); /* crw--w--w- */
331 }
332#endif 332#endif
333 if (chdir_to_root) 333 if (chdir_to_root)
334 xchdir("/"); 334 xchdir("/");
@@ -736,22 +736,14 @@ int getty_main(int argc, char **argv)
736 /* Already too late because of theoretical 736 /* Already too late because of theoretical
737 * possibility of getty --help somehow triggered 737 * possibility of getty --help somehow triggered
738 * inadvertently before we reach this. Oh well. */ 738 * inadvertently before we reach this. Oh well. */
739 close(0);
740 close(1);
741 close(2);
742 logmode = LOGMODE_NONE; 739 logmode = LOGMODE_NONE;
743#ifdef __linux__
744 setsid(); 740 setsid();
745#endif
746 /* Was "/dev/console". Why should we spam *system console*
747 * if there is a problem with getty on /dev/ttyS15?... */
748 nullfd = xopen(bb_dev_null, O_RDWR); 741 nullfd = xopen(bb_dev_null, O_RDWR);
749 if (nullfd) { 742 /* dup2(nullfd, 0); - no, because of possible "getty - 9600" */
750 dup2(nullfd, 0); 743 /* open_tty() will take care of fd# 0 anyway */
751 close(nullfd); 744 dup2(nullfd, 1);
752 } 745 dup2(nullfd, 2);
753 dup2(0, 1); 746 while (nullfd > 2) close(nullfd--);
754 dup2(0, 2);
755 /* We want special flavor of error_msg_and_die */ 747 /* We want special flavor of error_msg_and_die */
756 die_sleep = 10; 748 die_sleep = 10;
757 msg_eol = "\r\n"; 749 msg_eol = "\r\n";
diff --git a/miscutils/setsid.c b/miscutils/setsid.c
index 347b2babd..47c44d2cc 100644
--- a/miscutils/setsid.c
+++ b/miscutils/setsid.c
@@ -15,9 +15,6 @@
15 */ 15 */
16 16
17#include "busybox.h" 17#include "busybox.h"
18#include <stdio.h>
19#include <unistd.h>
20#include <stdlib.h>
21 18
22int setsid_main(int argc, char *argv[]) 19int setsid_main(int argc, char *argv[])
23{ 20{
@@ -25,7 +22,7 @@ int setsid_main(int argc, char *argv[])
25 bb_show_usage(); 22 bb_show_usage();
26 23
27 if (getpgrp() == getpid()) { 24 if (getpgrp() == getpid()) {
28 switch (fork()){ 25 switch (fork()) {
29 case -1: 26 case -1:
30 bb_perror_msg_and_die("fork"); 27 bb_perror_msg_and_die("fork");
31 case 0: 28 case 0:
@@ -33,8 +30,8 @@ int setsid_main(int argc, char *argv[])
33 default: /* parent */ 30 default: /* parent */
34 exit(0); 31 exit(0);
35 } 32 }
36 /* child falls through */
37 } 33 }
34 /* child */
38 35
39 setsid(); /* no error possible */ 36 setsid(); /* no error possible */
40 37
diff --git a/networking/fakeidentd.c b/networking/fakeidentd.c
index 8c07082fc..6f766a827 100644
--- a/networking/fakeidentd.c
+++ b/networking/fakeidentd.c
@@ -1,3 +1,5 @@
1/* NB: this file is to be removed soon. See isrv_identd.c */
2
1/* vi: set sw=4 ts=4: */ 3/* vi: set sw=4 ts=4: */
2/* 4/*
3 * A fake identd server 5 * A fake identd server
diff --git a/networking/inetd.c b/networking/inetd.c
index 370dcbbe0..218f85e53 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1292,9 +1292,9 @@ inetd_main(int argc, char *argv[])
1292 /* reexec for vfork() do continue parent */ 1292 /* reexec for vfork() do continue parent */
1293 vfork_daemon_rexec(0, 0, argc, argv, "-f"); 1293 vfork_daemon_rexec(0, 0, argc, argv, "-f");
1294 } 1294 }
1295 bb_sanitize_server_stdio(0); 1295 bb_sanitize_stdio();
1296#else 1296#else
1297 bb_sanitize_server_stdio(!(opt & 2)); 1297 bb_sanitize_stdio_maybe_daemonize(!(opt & 2));
1298#endif 1298#endif
1299 openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 1299 openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
1300 logmode = LOGMODE_SYSLOG; 1300 logmode = LOGMODE_SYSLOG;
diff --git a/networking/isrv_identd.c b/networking/isrv_identd.c
index e757d7c29..2d4399cea 100644
--- a/networking/isrv_identd.c
+++ b/networking/isrv_identd.c
@@ -111,7 +111,7 @@ int fakeidentd_main(int argc, char **argv)
111 bogouser = argv[optind]; 111 bogouser = argv[optind];
112 112
113 /* Daemonize if no -f and no -i and no -w */ 113 /* Daemonize if no -f and no -i and no -w */
114 bb_sanitize_server_stdio(!(opt & OPT_fiw)); 114 bb_sanitize_stdio_maybe_daemonize(!(opt & OPT_fiw));
115 /* Where to log in inetd modes? "Classic" inetd 115 /* Where to log in inetd modes? "Classic" inetd
116 * probably has its stderr /dev/null'ed (we need log to syslog?), 116 * probably has its stderr /dev/null'ed (we need log to syslog?),
117 * but daemontools-like utilities usually expect that children 117 * but daemontools-like utilities usually expect that children
diff --git a/networking/telnetd.c b/networking/telnetd.c
index 51bd0c012..25cba3eb0 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -283,15 +283,19 @@ make_new_session(
283 283
284 /* child */ 284 /* child */
285 285
286 /* make new process group */
287 setsid();
288 tcsetpgrp(0, getpid());
289 /* ^^^ strace says: "ioctl(0, TIOCSPGRP, [pid]) = -1 ENOTTY" -- ??! */
290
286 /* open the child's side of the tty. */ 291 /* open the child's side of the tty. */
287 fd = xopen(tty_name, O_RDWR /*| O_NOCTTY*/); 292 /* NB: setsid() disconnects from any previous ctty's. Therefore
293 * we must open child's side of the tty AFTER setsid! */
294 fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
288 dup2(fd, 0); 295 dup2(fd, 0);
289 dup2(fd, 1); 296 dup2(fd, 1);
290 dup2(fd, 2); 297 dup2(fd, 2);
291 while (fd > 2) close(fd--); 298 while (fd > 2) close(fd--);
292 /* make new process group */
293 setsid();
294 tcsetpgrp(0, getpid());
295 299
296 /* The pseudo-terminal allocated to the client is configured to operate in 300 /* The pseudo-terminal allocated to the client is configured to operate in
297 * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */ 301 * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
diff --git a/networking/zcip.c b/networking/zcip.c
index 27e281c93..5d57c4287 100644
--- a/networking/zcip.c
+++ b/networking/zcip.c
@@ -221,7 +221,8 @@ int zcip_main(int argc, char *argv[])
221 } 221 }
222 if (opts & 4) { // -r n.n.n.n 222 if (opts & 4) { // -r n.n.n.n
223 if (inet_aton(r_opt, &ip) == 0 223 if (inet_aton(r_opt, &ip) == 0
224 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) { 224 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR
225 ) {
225 bb_error_msg_and_die("invalid link address"); 226 bb_error_msg_and_die("invalid link address");
226 } 227 }
227 } 228 }
@@ -270,7 +271,7 @@ int zcip_main(int argc, char *argv[])
270 // daemonize now; don't delay system startup 271 // daemonize now; don't delay system startup
271 if (!FOREGROUND) { 272 if (!FOREGROUND) {
272 setsid(); 273 setsid();
273 xdaemon(0, 0); 274 bb_daemonize();
274 bb_info_msg("start, interface %s", intf); 275 bb_info_msg("start, interface %s", intf);
275 } 276 }
276 277
diff --git a/shell/hush.c b/shell/hush.c
index 9bc0013d7..8f2dc80f2 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2634,8 +2634,8 @@ static void setup_job_control(void)
2634 2634
2635 /* Put ourselves in our own process group. */ 2635 /* Put ourselves in our own process group. */
2636 setsid(); 2636 setsid();
2637 shell_pgrp = getpid (); 2637 shell_pgrp = getpid();
2638 setpgid (shell_pgrp, shell_pgrp); 2638 setpgid(shell_pgrp, shell_pgrp);
2639 2639
2640 /* Grab control of the terminal. */ 2640 /* Grab control of the terminal. */
2641 tcsetpgrp(shell_terminal, shell_pgrp); 2641 tcsetpgrp(shell_terminal, shell_pgrp);
@@ -2665,7 +2665,7 @@ int hush_main(int argc, char **argv)
2665 2665
2666 /* Initialize some more globals to non-zero values */ 2666 /* Initialize some more globals to non-zero values */
2667 set_cwd(); 2667 set_cwd();
2668 if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt(); 2668 if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt();
2669 else PS1 = NULL; 2669 else PS1 = NULL;
2670 PS2 = "> "; 2670 PS2 = "> ";
2671 2671
diff --git a/shell/lash.c b/shell/lash.c
index 52b117431..b2ccaf0a1 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -1486,7 +1486,7 @@ static void setup_job_control(void)
1486 1486
1487 /* Put ourselves in our own process group. */ 1487 /* Put ourselves in our own process group. */
1488 setsid(); 1488 setsid();
1489 shell_pgrp = getpid (); 1489 shell_pgrp = getpid();
1490 setpgid(shell_pgrp, shell_pgrp); 1490 setpgid(shell_pgrp, shell_pgrp);
1491 1491
1492 /* Grab control of the terminal. */ 1492 /* Grab control of the terminal. */
diff --git a/util-linux/mount.c b/util-linux/mount.c
index b3e8c478c..77382ff51 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -737,7 +737,7 @@ static int daemonize(void)
737 dup2(fd, 0); 737 dup2(fd, 0);
738 dup2(fd, 1); 738 dup2(fd, 1);
739 dup2(fd, 2); 739 dup2(fd, 2);
740 if (fd > 2) close(fd); 740 while (fd > 2) close(fd--);
741 setsid(); 741 setsid();
742 openlog(applet_name, LOG_PID, LOG_DAEMON); 742 openlog(applet_name, LOG_PID, LOG_DAEMON);
743 logmode = LOGMODE_SYSLOG; 743 logmode = LOGMODE_SYSLOG;