aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-01-17 22:32:22 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-01-17 22:32:22 +0100
commit33f9dc08e55b8938452fce20e7c5e4138255edc9 (patch)
tree5475c1f9264fb5701a6c6acdd25c9930c318b93f
parent96a6bdcb7764a5616f2b9759226d1097ab489615 (diff)
downloadbusybox-w32-33f9dc08e55b8938452fce20e7c5e4138255edc9.tar.gz
busybox-w32-33f9dc08e55b8938452fce20e7c5e4138255edc9.tar.bz2
busybox-w32-33f9dc08e55b8938452fce20e7c5e4138255edc9.zip
ftpd: code shrink
function old new delta MMU: handle_dir_common 354 338 -16 NOMMU: ftpd_main 2437 2442 +5 popen_ls 201 174 -27 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/ftpd.c51
1 files changed, 30 insertions, 21 deletions
diff --git a/networking/ftpd.c b/networking/ftpd.c
index df8188cba..4e9f65ca3 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -616,18 +616,22 @@ handle_retr(void)
616static int 616static int
617popen_ls(const char *opt) 617popen_ls(const char *opt)
618{ 618{
619 char *cwd; 619 const char *argv[5];
620 const char *argv[] = {
621 "ftpd",
622 opt,
623 BB_MMU ? "--" : NULL,
624 G.ftp_arg,
625 NULL
626 };
627 struct fd_pair outfd; 620 struct fd_pair outfd;
628 pid_t pid; 621 pid_t pid;
629 622
630 cwd = xrealloc_getcwd_or_warn(NULL); 623 argv[0] = "ftpd";
624 argv[1] = opt; /* "-l" or "-1" */
625#if BB_MMU
626 argv[2] = "--";
627#else
628 /* NOMMU ftpd ls helper chdirs to argv[2],
629 * preventing peer from seeing real root. */
630 argv[2] = xrealloc_getcwd_or_warn(NULL);
631#endif
632 argv[3] = G.ftp_arg;
633 argv[4] = NULL;
634
631 xpiped_pair(outfd); 635 xpiped_pair(outfd);
632 636
633 /*fflush_all(); - so far we dont use stdio on output */ 637 /*fflush_all(); - so far we dont use stdio on output */
@@ -638,9 +642,14 @@ popen_ls(const char *opt)
638 if (pid == 0) { 642 if (pid == 0) {
639 /* child */ 643 /* child */
640#if !BB_MMU 644#if !BB_MMU
645 /* On NOMMU, we want to execute a child - copy of ourself.
646 * In chroot we usually can't do it. Thus we chdir
647 * out of the chroot back to original root,
648 * and (see later below) execute bb_busybox_exec_path
649 * relative to current directory */
641 if (fchdir(G.root_fd) != 0) 650 if (fchdir(G.root_fd) != 0)
642 _exit(127); 651 _exit(127);
643 close(G.root_fd); 652 /*close(G.root_fd); - close_on_exec_on() took care of this */
644#endif 653#endif
645 /* NB: close _first_, then move fd! */ 654 /* NB: close _first_, then move fd! */
646 close(outfd.rd); 655 close(outfd.rd);
@@ -651,25 +660,23 @@ popen_ls(const char *opt)
651 * ls won't read it anyway */ 660 * ls won't read it anyway */
652 close(STDIN_FILENO); 661 close(STDIN_FILENO);
653 dup(STDOUT_FILENO); /* copy will become STDIN_FILENO */ 662 dup(STDOUT_FILENO); /* copy will become STDIN_FILENO */
654#if !BB_MMU 663#if BB_MMU
655 /* ftpd ls helper chdirs to argv[2], 664 /* memset(&G, 0, sizeof(G)); - ls_main does it */
656 * preventing peer from seeing real root we are in now 665 exit(ls_main(ARRAY_SIZE(argv) - 1, (char**) argv));
657 */ 666#else
658 argv[2] = cwd;
659 /* + 1: we must use relative path here if in chroot. 667 /* + 1: we must use relative path here if in chroot.
660 * For example, execv("/proc/self/exe") will fail, since 668 * For example, execv("/proc/self/exe") will fail, since
661 * it looks for "/proc/self/exe" _relative to chroot!_ */ 669 * it looks for "/proc/self/exe" _relative to chroot!_ */
662 execv(bb_busybox_exec_path + 1, (char**) argv); 670 execv(bb_busybox_exec_path + 1, (char**) argv);
663 _exit(127); 671 _exit(127);
664#else
665 /* memset(&G, 0, sizeof(G)); - ls_main does it */
666 exit(ls_main(ARRAY_SIZE(argv) - 1, (char**) argv));
667#endif 672#endif
668 } 673 }
669 674
670 /* parent */ 675 /* parent */
671 close(outfd.wr); 676 close(outfd.wr);
672 free(cwd); 677#if !BB_MMU
678 free((char*)argv[2]);
679#endif
673 return outfd.rd; 680 return outfd.rd;
674} 681}
675 682
@@ -1108,8 +1115,9 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv)
1108 opts = getopt32(argv, "l1vS" IF_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose, &verbose_S); 1115 opts = getopt32(argv, "l1vS" IF_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose, &verbose_S);
1109 if (opts & (OPT_l|OPT_1)) { 1116 if (opts & (OPT_l|OPT_1)) {
1110 /* Our secret backdoor to ls */ 1117 /* Our secret backdoor to ls */
1111/* TODO: pass -n too? */ 1118/* TODO: pass -n? It prevents user/group resolution, whicj may not work in chroot anyway */
1112/* --group-directories-first would be nice, but ls don't do that yet */ 1119/* TODO: pass -A? It shows dot files */
1120/* TODO: pass --group-directories-first? would be nice, but ls don't do that yet */
1113 xchdir(argv[2]); 1121 xchdir(argv[2]);
1114 argv[2] = (char*)"--"; 1122 argv[2] = (char*)"--";
1115 /* memset(&G, 0, sizeof(G)); - ls_main does it */ 1123 /* memset(&G, 0, sizeof(G)); - ls_main does it */
@@ -1151,6 +1159,7 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv)
1151 1159
1152#if !BB_MMU 1160#if !BB_MMU
1153 G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY); 1161 G.root_fd = xopen("/", O_RDONLY | O_DIRECTORY);
1162 close_on_exec_on(G.root_fd);
1154#endif 1163#endif
1155 1164
1156 if (argv[optind]) { 1165 if (argv[optind]) {