aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-09-30 23:50:48 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-09-30 23:50:48 +0000
commit96e1b38586e80a0f014038bf4fdf4689c668fbd6 (patch)
treed7f6a7866700601598cfcc006b7dcb6cb4a7c07e
parentdeabacdf91c6d1c3cfcdb4cd06780807193de81d (diff)
downloadbusybox-w32-96e1b38586e80a0f014038bf4fdf4689c668fbd6.tar.gz
busybox-w32-96e1b38586e80a0f014038bf4fdf4689c668fbd6.tar.bz2
busybox-w32-96e1b38586e80a0f014038bf4fdf4689c668fbd6.zip
introduce and use close_on_exec_on(fd). -50 bytes.
-rw-r--r--include/libbb.h1
-rw-r--r--init/init.c2
-rw-r--r--libbb/xfuncs.c5
-rw-r--r--miscutils/devfsd.c5
-rw-r--r--networking/nc.c2
-rw-r--r--networking/udhcp/signalpipe.c8
-rw-r--r--runit/runit_lib.c5
-rw-r--r--runit/runit_lib.h2
-rw-r--r--runit/runsv.c24
-rw-r--r--runit/runsvdir.c6
-rw-r--r--runit/svlogd.c10
-rw-r--r--shell/ash.c4
-rw-r--r--shell/hush.c8
-rw-r--r--shell/lash.c2
14 files changed, 41 insertions, 43 deletions
diff --git a/include/libbb.h b/include/libbb.h
index e3f9c4957..a8463ae49 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -248,6 +248,7 @@ extern char *bb_get_last_path_component_nostrip(const char *path);
248 248
249int ndelay_on(int fd); 249int ndelay_on(int fd);
250int ndelay_off(int fd); 250int ndelay_off(int fd);
251int close_on_exec_on(int fd);
251void xdup2(int, int); 252void xdup2(int, int);
252void xmove_fd(int, int); 253void xmove_fd(int, int);
253 254
diff --git a/init/init.c b/init/init.c
index 543ec2ea8..5e8401163 100644
--- a/init/init.c
+++ b/init/init.c
@@ -179,7 +179,7 @@ static void message(int device, const char *fmt, ...)
179 bb_error_msg("can't log to %s", log_console); 179 bb_error_msg("can't log to %s", log_console);
180 device = L_CONSOLE; 180 device = L_CONSOLE;
181 } else { 181 } else {
182 fcntl(log_fd, F_SETFD, FD_CLOEXEC); 182 close_on_exec_on(log_fd);
183 } 183 }
184 } 184 }
185 } 185 }
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index eb1633be2..2bf20f3a0 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -164,6 +164,11 @@ int ndelay_on(int fd)
164 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) | O_NONBLOCK); 164 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) | O_NONBLOCK);
165} 165}
166 166
167int close_on_exec_on(int fd)
168{
169 return fcntl(fd, F_SETFD, FD_CLOEXEC);
170}
171
167int ndelay_off(int fd) 172int ndelay_off(int fd)
168{ 173{
169 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) & ~O_NONBLOCK); 174 return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) & ~O_NONBLOCK);
diff --git a/miscutils/devfsd.c b/miscutils/devfsd.c
index bfa5c1727..734d4a7d9 100644
--- a/miscutils/devfsd.c
+++ b/miscutils/devfsd.c
@@ -371,10 +371,7 @@ int devfsd_main(int argc, char **argv)
371 xchdir(mount_point); 371 xchdir(mount_point);
372 372
373 fd = xopen(".devfsd", O_RDONLY); 373 fd = xopen(".devfsd", O_RDONLY);
374 374 close_on_exec_on(fd);
375 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
376 bb_perror_msg_and_die("FD_CLOEXEC");
377
378 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); 375 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
379 376
380 /*setup initial entries */ 377 /*setup initial entries */
diff --git a/networking/nc.c b/networking/nc.c
index e7bd519e0..19750e59e 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -113,7 +113,7 @@ int nc_main(int argc, char **argv)
113 lport = get_nport(&lsa->sa); 113 lport = get_nport(&lsa->sa);
114 fdprintf(2, "%d\n", ntohs(lport)); 114 fdprintf(2, "%d\n", ntohs(lport));
115 } 115 }
116 fcntl(sfd, F_SETFD, FD_CLOEXEC); 116 close_on_exec_on(sfd);
117 accept_again: 117 accept_again:
118 cfd = accept(sfd, NULL, 0); 118 cfd = accept(sfd, NULL, 0);
119 if (cfd < 0) 119 if (cfd < 0)
diff --git a/networking/udhcp/signalpipe.c b/networking/udhcp/signalpipe.c
index 9c7ead965..fafd2082a 100644
--- a/networking/udhcp/signalpipe.c
+++ b/networking/udhcp/signalpipe.c
@@ -39,9 +39,9 @@ void udhcp_sp_setup(void)
39{ 39{
40 /* was socketpair, but it needs AF_UNIX in kernel */ 40 /* was socketpair, but it needs AF_UNIX in kernel */
41 xpipe(signal_pipe); 41 xpipe(signal_pipe);
42 fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC); 42 close_on_exec_on(signal_pipe[0]);
43 fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC); 43 close_on_exec_on(signal_pipe[1]);
44 fcntl(signal_pipe[1], F_SETFL, O_NONBLOCK); 44 ndelay_on(signal_pipe[1]);
45 signal(SIGUSR1, signal_handler); 45 signal(SIGUSR1, signal_handler);
46 signal(SIGUSR2, signal_handler); 46 signal(SIGUSR2, signal_handler);
47 signal(SIGTERM, signal_handler); 47 signal(SIGTERM, signal_handler);
@@ -56,7 +56,7 @@ int udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
56 FD_ZERO(rfds); 56 FD_ZERO(rfds);
57 FD_SET(signal_pipe[0], rfds); 57 FD_SET(signal_pipe[0], rfds);
58 if (extra_fd >= 0) { 58 if (extra_fd >= 0) {
59 fcntl(extra_fd, F_SETFD, FD_CLOEXEC); 59 close_on_exec_on(extra_fd);
60 FD_SET(extra_fd, rfds); 60 FD_SET(extra_fd, rfds);
61 } 61 }
62 return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd; 62 return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
diff --git a/runit/runit_lib.c b/runit/runit_lib.c
index 4b7950ca3..2ed9054fd 100644
--- a/runit/runit_lib.c
+++ b/runit/runit_lib.c
@@ -50,11 +50,6 @@ unsigned byte_chr(char *s,unsigned n,int c)
50 return t - s; 50 return t - s;
51} 51}
52 52
53int coe(int fd)
54{
55 return fcntl(fd, F_SETFD, FD_CLOEXEC);
56}
57
58#ifdef UNUSED 53#ifdef UNUSED
59static /* as it isn't used anywhere else */ 54static /* as it isn't used anywhere else */
60void tai_pack(char *s, const struct tai *t) 55void tai_pack(char *s, const struct tai *t)
diff --git a/runit/runit_lib.h b/runit/runit_lib.h
index c644f5b9d..4b9482024 100644
--- a/runit/runit_lib.h
+++ b/runit/runit_lib.h
@@ -27,8 +27,6 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28extern unsigned byte_chr(char *s,unsigned n,int c); 28extern unsigned byte_chr(char *s,unsigned n,int c);
29 29
30extern int coe(int);
31
32#define direntry struct dirent 30#define direntry struct dirent
33 31
34//struct tai { 32//struct tai {
diff --git a/runit/runsv.c b/runit/runsv.c
index 1ee3dda01..7cf142d8a 100644
--- a/runit/runsv.c
+++ b/runit/runsv.c
@@ -454,8 +454,8 @@ int runsv_main(int argc, char **argv)
454 dir = argv[1]; 454 dir = argv[1];
455 455
456 xpipe(selfpipe); 456 xpipe(selfpipe);
457 coe(selfpipe[0]); 457 close_on_exec_on(selfpipe[0]);
458 coe(selfpipe[1]); 458 close_on_exec_on(selfpipe[1]);
459 ndelay_on(selfpipe[0]); 459 ndelay_on(selfpipe[0]);
460 ndelay_on(selfpipe[1]); 460 ndelay_on(selfpipe[1]);
461 461
@@ -491,8 +491,8 @@ int runsv_main(int argc, char **argv)
491 if (stat("log/down", &s) != -1) 491 if (stat("log/down", &s) != -1)
492 svd[1].want = W_DOWN; 492 svd[1].want = W_DOWN;
493 xpipe(logpipe); 493 xpipe(logpipe);
494 coe(logpipe[0]); 494 close_on_exec_on(logpipe[0]);
495 coe(logpipe[1]); 495 close_on_exec_on(logpipe[1]);
496 } 496 }
497 } 497 }
498 498
@@ -512,7 +512,7 @@ int runsv_main(int argc, char **argv)
512 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); 512 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
513 if (lock_exnb(svd[0].fdlock) == -1) 513 if (lock_exnb(svd[0].fdlock) == -1)
514 fatal_cannot("lock supervise/lock"); 514 fatal_cannot("lock supervise/lock");
515 coe(svd[0].fdlock); 515 close_on_exec_on(svd[0].fdlock);
516 if (haslog) { 516 if (haslog) {
517 if (mkdir("log/supervise", 0700) == -1) { 517 if (mkdir("log/supervise", 0700) == -1) {
518 r = readlink("log/supervise", buf, 256); 518 r = readlink("log/supervise", buf, 256);
@@ -536,30 +536,30 @@ int runsv_main(int argc, char **argv)
536 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); 536 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
537 if (lock_ex(svd[1].fdlock) == -1) 537 if (lock_ex(svd[1].fdlock) == -1)
538 fatal_cannot("lock log/supervise/lock"); 538 fatal_cannot("lock log/supervise/lock");
539 coe(svd[1].fdlock); 539 close_on_exec_on(svd[1].fdlock);
540 } 540 }
541 541
542 mkfifo("log/supervise/control"+4, 0600); 542 mkfifo("log/supervise/control"+4, 0600);
543 svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY); 543 svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
544 coe(svd[0].fdcontrol); 544 close_on_exec_on(svd[0].fdcontrol);
545 svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY); 545 svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
546 coe(svd[0].fdcontrolwrite); 546 close_on_exec_on(svd[0].fdcontrolwrite);
547 update_status(&svd[0]); 547 update_status(&svd[0]);
548 if (haslog) { 548 if (haslog) {
549 mkfifo("log/supervise/control", 0600); 549 mkfifo("log/supervise/control", 0600);
550 svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY); 550 svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
551 coe(svd[1].fdcontrol); 551 close_on_exec_on(svd[1].fdcontrol);
552 svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY); 552 svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
553 coe(svd[1].fdcontrolwrite); 553 close_on_exec_on(svd[1].fdcontrolwrite);
554 update_status(&svd[1]); 554 update_status(&svd[1]);
555 } 555 }
556 mkfifo("log/supervise/ok"+4, 0600); 556 mkfifo("log/supervise/ok"+4, 0600);
557 fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY); 557 fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
558 coe(fd); 558 close_on_exec_on(fd);
559 if (haslog) { 559 if (haslog) {
560 mkfifo("log/supervise/ok", 0600); 560 mkfifo("log/supervise/ok", 0600);
561 fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY); 561 fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
562 coe(fd); 562 close_on_exec_on(fd);
563 } 563 }
564 for (;;) { 564 for (;;) {
565 struct pollfd x[3]; 565 struct pollfd x[3];
diff --git a/runit/runsvdir.c b/runit/runsvdir.c
index 8d25923d4..924f771b7 100644
--- a/runit/runsvdir.c
+++ b/runit/runsvdir.c
@@ -190,8 +190,8 @@ static int setup_log(void)
190 warnx("cannot create pipe for log"); 190 warnx("cannot create pipe for log");
191 return -1; 191 return -1;
192 } 192 }
193 coe(logpipe[1]); 193 close_on_exec_on(logpipe[1]);
194 coe(logpipe[0]); 194 close_on_exec_on(logpipe[0]);
195 ndelay_on(logpipe[0]); 195 ndelay_on(logpipe[0]);
196 ndelay_on(logpipe[1]); 196 ndelay_on(logpipe[1]);
197 if (dup2(logpipe[1], 2) == -1) { 197 if (dup2(logpipe[1], 2) == -1) {
@@ -245,7 +245,7 @@ int runsvdir_main(int argc, char **argv)
245 curdir = open_read("."); 245 curdir = open_read(".");
246 if (curdir == -1) 246 if (curdir == -1)
247 fatal2_cannot("open current directory", ""); 247 fatal2_cannot("open current directory", "");
248 coe(curdir); 248 close_on_exec_on(curdir);
249 249
250 stampcheck = monotonic_sec(); 250 stampcheck = monotonic_sec();
251 251
diff --git a/runit/svlogd.c b/runit/svlogd.c
index b5eed15ab..cdf4e424b 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -361,7 +361,7 @@ static unsigned rotate(struct logdir *ld)
361 /* we presume this cannot fail */ 361 /* we presume this cannot fail */
362 ld->filecur = fdopen(ld->fdcur, "a"); //// 362 ld->filecur = fdopen(ld->fdcur, "a"); ////
363 setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// 363 setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
364 coe(ld->fdcur); 364 close_on_exec_on(ld->fdcur);
365 ld->size = 0; 365 ld->size = 0;
366 while (fchmod(ld->fdcur, 0644) == -1) 366 while (fchmod(ld->fdcur, 0644) == -1)
367 pause2cannot("set mode of current", ld->name); 367 pause2cannot("set mode of current", ld->name);
@@ -482,7 +482,7 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
482 warn2("cannot open log directory", (char*)fn); 482 warn2("cannot open log directory", (char*)fn);
483 return 0; 483 return 0;
484 } 484 }
485 coe(ld->fddir); 485 close_on_exec_on(ld->fddir);
486 if (fchdir(ld->fddir) == -1) { 486 if (fchdir(ld->fddir) == -1) {
487 logdir_close(ld); 487 logdir_close(ld);
488 warn2("cannot change directory", (char*)fn); 488 warn2("cannot change directory", (char*)fn);
@@ -498,7 +498,7 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
498 pause1cannot("change to initial working directory"); 498 pause1cannot("change to initial working directory");
499 return 0; 499 return 0;
500 } 500 }
501 coe(ld->fdlock); 501 close_on_exec_on(ld->fdlock);
502 502
503 ld->size = 0; 503 ld->size = 0;
504 ld->sizemax = 1000000; 504 ld->sizemax = 1000000;
@@ -624,7 +624,7 @@ static unsigned logdir_open(struct logdir *ld, const char *fn)
624 ld->filecur = fdopen(ld->fdcur, "a"); //// 624 ld->filecur = fdopen(ld->fdcur, "a"); ////
625 setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// 625 setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
626 626
627 coe(ld->fdcur); 627 close_on_exec_on(ld->fdcur);
628 while (fchmod(ld->fdcur, 0644) == -1) 628 while (fchmod(ld->fdcur, 0644) == -1)
629 pause2cannot("set mode of current", ld->name); 629 pause2cannot("set mode of current", ld->name);
630 630
@@ -851,7 +851,7 @@ int svlogd_main(int argc, char **argv)
851 if (dirn <= 0) usage(); 851 if (dirn <= 0) usage();
852 ////if (buflen <= linemax) usage(); 852 ////if (buflen <= linemax) usage();
853 fdwdir = xopen(".", O_RDONLY|O_NDELAY); 853 fdwdir = xopen(".", O_RDONLY|O_NDELAY);
854 coe(fdwdir); 854 close_on_exec_on(fdwdir);
855 dir = xzalloc(dirn * sizeof(struct logdir)); 855 dir = xzalloc(dirn * sizeof(struct logdir));
856 for (i = 0; i < dirn; ++i) { 856 for (i = 0; i < dirn; ++i) {
857 dir[i].fddir = -1; 857 dir[i].fddir = -1;
diff --git a/shell/ash.c b/shell/ash.c
index 183911ccc..9d8b83c18 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3469,7 +3469,7 @@ setjobctl(int on)
3469 close(ofd); 3469 close(ofd);
3470 if (fd < 0) 3470 if (fd < 0)
3471 goto out; 3471 goto out;
3472 fcntl(fd, F_SETFD, FD_CLOEXEC); 3472 close_on_exec_on(fd);
3473 do { /* while we are in the background */ 3473 do { /* while we are in the background */
3474 pgrp = tcgetpgrp(fd); 3474 pgrp = tcgetpgrp(fd);
3475 if (pgrp < 0) { 3475 if (pgrp < 0) {
@@ -8830,7 +8830,7 @@ closescript(void)
8830static void 8830static void
8831setinputfd(int fd, int push) 8831setinputfd(int fd, int push)
8832{ 8832{
8833 fcntl(fd, F_SETFD, FD_CLOEXEC); 8833 close_on_exec_on(fd);
8834 if (push) { 8834 if (push) {
8835 pushfile(); 8835 pushfile();
8836 parsefile->buf = 0; 8836 parsefile->buf = 0;
diff --git a/shell/hush.c b/shell/hush.c
index 5f9f2c5c1..9c705568d 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -760,9 +760,11 @@ static int builtin_eval(char **argv)
760static int builtin_cd(char **argv) 760static int builtin_cd(char **argv)
761{ 761{
762 const char *newdir; 762 const char *newdir;
763 if (argv[1] == NULL) 763 if (argv[1] == NULL) {
764 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
765 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
764 newdir = getenv("HOME") ? : "/"; 766 newdir = getenv("HOME") ? : "/";
765 else 767 } else
766 newdir = argv[1]; 768 newdir = argv[1];
767 if (chdir(newdir)) { 769 if (chdir(newdir)) {
768 printf("cd: %s: %s\n", newdir, strerror(errno)); 770 printf("cd: %s: %s\n", newdir, strerror(errno));
@@ -3629,7 +3631,7 @@ static void setup_job_control(void)
3629 3631
3630 saved_task_pgrp = shell_pgrp = getpgrp(); 3632 saved_task_pgrp = shell_pgrp = getpgrp();
3631 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp); 3633 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
3632 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC); 3634 close_on_exec_on(interactive_fd);
3633 3635
3634 /* If we were ran as 'hush &', 3636 /* If we were ran as 'hush &',
3635 * sleep until we are in the foreground. */ 3637 * sleep until we are in the foreground. */
diff --git a/shell/lash.c b/shell/lash.c
index 5ba490f2a..af3a8f80b 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -576,7 +576,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
576 if (openfd != redir->fd) { 576 if (openfd != redir->fd) {
577 if (squirrel && redir->fd < 3) { 577 if (squirrel && redir->fd < 3) {
578 squirrel[redir->fd] = dup(redir->fd); 578 squirrel[redir->fd] = dup(redir->fd);
579 fcntl(squirrel[redir->fd], F_SETFD, FD_CLOEXEC); 579 close_on_exec_on(squirrel[redir->fd]);
580 } 580 }
581 dup2(openfd, redir->fd); 581 dup2(openfd, redir->fd);
582 close(openfd); 582 close(openfd);