diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2011-01-04 19:56:15 +0700 |
---|---|---|
committer | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2011-01-04 19:56:15 +0700 |
commit | 5f6f2162512106adf120d4b528bb125e93e34429 (patch) | |
tree | 7d7449f755633c263be7125ad58d21cc3ca5b8a7 /libbb/appletlib.c | |
parent | 9db69882bee2d528d706d61d34ef7741122330be (diff) | |
parent | a116552869db5e7793ae10968eb3c962c69b3d8c (diff) | |
download | busybox-w32-5f6f2162512106adf120d4b528bb125e93e34429.tar.gz busybox-w32-5f6f2162512106adf120d4b528bb125e93e34429.tar.bz2 busybox-w32-5f6f2162512106adf120d4b528bb125e93e34429.zip |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r-- | libbb/appletlib.c | 79 |
1 files changed, 43 insertions, 36 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 3b691f945..b1c772cf3 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -28,7 +28,11 @@ | |||
28 | */ | 28 | */ |
29 | #include "busybox.h" | 29 | #include "busybox.h" |
30 | #include <assert.h> | 30 | #include <assert.h> |
31 | #include <malloc.h> | 31 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
32 | || defined(__APPLE__) \ | ||
33 | ) | ||
34 | # include <malloc.h> /* for mallopt */ | ||
35 | #endif | ||
32 | /* Try to pull in PAGE_SIZE */ | 36 | /* Try to pull in PAGE_SIZE */ |
33 | #ifdef __linux__ | 37 | #ifdef __linux__ |
34 | # include <sys/user.h> | 38 | # include <sys/user.h> |
@@ -66,7 +70,7 @@ static const char usage_messages[] ALIGN1 = UNPACKED_USAGE; | |||
66 | #if ENABLE_FEATURE_COMPRESS_USAGE | 70 | #if ENABLE_FEATURE_COMPRESS_USAGE |
67 | 71 | ||
68 | static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; | 72 | static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; |
69 | # include "unarchive.h" | 73 | # include "archive.h" |
70 | static const char *unpack_usage_messages(void) | 74 | static const char *unpack_usage_messages(void) |
71 | { | 75 | { |
72 | char *outbuf = NULL; | 76 | char *outbuf = NULL; |
@@ -75,7 +79,7 @@ static const char *unpack_usage_messages(void) | |||
75 | 79 | ||
76 | i = start_bunzip(&bd, | 80 | i = start_bunzip(&bd, |
77 | /* src_fd: */ -1, | 81 | /* src_fd: */ -1, |
78 | /* inbuf: */ (void *)packed_usage, | 82 | /* inbuf: */ packed_usage, |
79 | /* len: */ sizeof(packed_usage)); | 83 | /* len: */ sizeof(packed_usage)); |
80 | /* read_bunzip can longjmp to start_bunzip, and ultimately | 84 | /* read_bunzip can longjmp to start_bunzip, and ultimately |
81 | * end up here with i != 0 on read data errors! Not trivial */ | 85 | * end up here with i != 0 on read data errors! Not trivial */ |
@@ -103,14 +107,13 @@ void FAST_FUNC bb_show_usage(void) | |||
103 | if (ENABLE_SHOW_USAGE) { | 107 | if (ENABLE_SHOW_USAGE) { |
104 | #ifdef SINGLE_APPLET_STR | 108 | #ifdef SINGLE_APPLET_STR |
105 | /* Imagine that this applet is "true". Dont suck in printf! */ | 109 | /* Imagine that this applet is "true". Dont suck in printf! */ |
106 | const char *p; | 110 | const char *usage_string = unpack_usage_messages(); |
107 | const char *usage_string = p = unpack_usage_messages(); | ||
108 | 111 | ||
109 | if (*p == '\b') { | 112 | if (*usage_string == '\b') { |
110 | full_write2_str("No help available.\n\n"); | 113 | full_write2_str("No help available.\n\n"); |
111 | } else { | 114 | } else { |
112 | full_write2_str("Usage: "SINGLE_APPLET_STR" "); | 115 | full_write2_str("Usage: "SINGLE_APPLET_STR" "); |
113 | full_write2_str(p); | 116 | full_write2_str(usage_string); |
114 | full_write2_str("\n\n"); | 117 | full_write2_str("\n\n"); |
115 | } | 118 | } |
116 | if (ENABLE_FEATURE_CLEAN_UP) | 119 | if (ENABLE_FEATURE_CLEAN_UP) |
@@ -228,7 +231,7 @@ bool re_execed; | |||
228 | 231 | ||
229 | IF_FEATURE_SUID(static uid_t ruid;) /* real uid */ | 232 | IF_FEATURE_SUID(static uid_t ruid;) /* real uid */ |
230 | 233 | ||
231 | #if ENABLE_FEATURE_SUID_CONFIG | 234 | # if ENABLE_FEATURE_SUID_CONFIG |
232 | 235 | ||
233 | /* applets[] is const, so we have to define this "override" structure */ | 236 | /* applets[] is const, so we have to define this "override" structure */ |
234 | static struct BB_suid_config { | 237 | static struct BB_suid_config { |
@@ -499,15 +502,15 @@ static void parse_config_file(void) | |||
499 | sct_head = sct; | 502 | sct_head = sct; |
500 | } | 503 | } |
501 | } | 504 | } |
502 | #else | 505 | # else |
503 | static inline void parse_config_file(void) | 506 | static inline void parse_config_file(void) |
504 | { | 507 | { |
505 | IF_FEATURE_SUID(ruid = getuid();) | 508 | IF_FEATURE_SUID(ruid = getuid();) |
506 | } | 509 | } |
507 | #endif /* FEATURE_SUID_CONFIG */ | 510 | # endif /* FEATURE_SUID_CONFIG */ |
508 | 511 | ||
509 | 512 | ||
510 | #if ENABLE_FEATURE_SUID | 513 | # if ENABLE_FEATURE_SUID |
511 | static void check_suid(int applet_no) | 514 | static void check_suid(int applet_no) |
512 | { | 515 | { |
513 | gid_t rgid; /* real gid */ | 516 | gid_t rgid; /* real gid */ |
@@ -516,7 +519,7 @@ static void check_suid(int applet_no) | |||
516 | return; /* run by root - no need to check more */ | 519 | return; /* run by root - no need to check more */ |
517 | rgid = getgid(); | 520 | rgid = getgid(); |
518 | 521 | ||
519 | #if ENABLE_FEATURE_SUID_CONFIG | 522 | # if ENABLE_FEATURE_SUID_CONFIG |
520 | if (suid_cfg_readable) { | 523 | if (suid_cfg_readable) { |
521 | uid_t uid; | 524 | uid_t uid; |
522 | struct BB_suid_config *sct; | 525 | struct BB_suid_config *sct; |
@@ -559,7 +562,7 @@ static void check_suid(int applet_no) | |||
559 | bb_perror_msg_and_die("setresuid"); | 562 | bb_perror_msg_and_die("setresuid"); |
560 | return; | 563 | return; |
561 | } | 564 | } |
562 | #if !ENABLE_FEATURE_SUID_CONFIG_QUIET | 565 | # if !ENABLE_FEATURE_SUID_CONFIG_QUIET |
563 | { | 566 | { |
564 | static bool onetime = 0; | 567 | static bool onetime = 0; |
565 | 568 | ||
@@ -568,9 +571,9 @@ static void check_suid(int applet_no) | |||
568 | fprintf(stderr, "Using fallback suid method\n"); | 571 | fprintf(stderr, "Using fallback suid method\n"); |
569 | } | 572 | } |
570 | } | 573 | } |
571 | #endif | 574 | # endif |
572 | check_need_suid: | 575 | check_need_suid: |
573 | #endif | 576 | # endif |
574 | if (APPLET_SUID(applet_no) == _BB_SUID_REQUIRE) { | 577 | if (APPLET_SUID(applet_no) == _BB_SUID_REQUIRE) { |
575 | /* Real uid is not 0. If euid isn't 0 too, suid bit | 578 | /* Real uid is not 0. If euid isn't 0 too, suid bit |
576 | * is most probably not set on our executable */ | 579 | * is most probably not set on our executable */ |
@@ -581,25 +584,24 @@ static void check_suid(int applet_no) | |||
581 | xsetuid(ruid); | 584 | xsetuid(ruid); |
582 | } | 585 | } |
583 | } | 586 | } |
584 | #else | 587 | # else |
585 | #define check_suid(x) ((void)0) | 588 | # define check_suid(x) ((void)0) |
586 | #endif /* FEATURE_SUID */ | 589 | # endif /* FEATURE_SUID */ |
587 | 590 | ||
588 | 591 | ||
589 | #if ENABLE_FEATURE_INSTALLER | 592 | # if ENABLE_FEATURE_INSTALLER |
590 | static const char usr_bin [] ALIGN1 = "/usr/bin/"; | 593 | static const char usr_bin [] ALIGN1 = "/usr/bin/"; |
591 | static const char usr_sbin[] ALIGN1 = "/usr/sbin/"; | 594 | static const char usr_sbin[] ALIGN1 = "/usr/sbin/"; |
592 | static const char *const install_dir[] = { | 595 | static const char *const install_dir[] = { |
593 | &usr_bin [8], /* "/" */ | 596 | &usr_bin [8], /* "/" */ |
594 | &usr_bin [4], /* "/bin/" */ | 597 | &usr_bin [4], /* "/bin/" */ |
595 | &usr_sbin[4] /* "/sbin/" */ | 598 | &usr_sbin[4] /* "/sbin/" */ |
596 | # if !ENABLE_INSTALL_NO_USR | 599 | # if !ENABLE_INSTALL_NO_USR |
597 | ,usr_bin | 600 | ,usr_bin |
598 | ,usr_sbin | 601 | ,usr_sbin |
599 | # endif | 602 | # endif |
600 | }; | 603 | }; |
601 | 604 | ||
602 | |||
603 | /* create (sym)links for each applet */ | 605 | /* create (sym)links for each applet */ |
604 | static void install_links(const char *busybox, int use_symbolic_links, | 606 | static void install_links(const char *busybox, int use_symbolic_links, |
605 | char *custom_install_dir) | 607 | char *custom_install_dir) |
@@ -629,9 +631,9 @@ static void install_links(const char *busybox, int use_symbolic_links, | |||
629 | free(fpc); | 631 | free(fpc); |
630 | } | 632 | } |
631 | } | 633 | } |
632 | #else | 634 | # else |
633 | # define install_links(x,y,z) ((void)0) | 635 | # define install_links(x,y,z) ((void)0) |
634 | #endif | 636 | # endif |
635 | 637 | ||
636 | /* If we were called as "busybox..." */ | 638 | /* If we were called as "busybox..." */ |
637 | static int busybox_main(char **argv) | 639 | static int busybox_main(char **argv) |
@@ -696,10 +698,10 @@ static int busybox_main(char **argv) | |||
696 | const char *a = applet_names; | 698 | const char *a = applet_names; |
697 | dup2(1, 2); | 699 | dup2(1, 2); |
698 | while (*a) { | 700 | while (*a) { |
699 | #if ENABLE_FEATURE_INSTALLER | 701 | # if ENABLE_FEATURE_INSTALLER |
700 | if (argv[1][6]) /* --list-path? */ | 702 | if (argv[1][6]) /* --list-path? */ |
701 | full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1); | 703 | full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1); |
702 | #endif | 704 | # endif |
703 | full_write2_str(a); | 705 | full_write2_str(a); |
704 | full_write2_str("\n"); | 706 | full_write2_str("\n"); |
705 | i++; | 707 | i++; |
@@ -772,7 +774,7 @@ void FAST_FUNC run_applet_and_exit(const char *name, char **argv) | |||
772 | int applet = find_applet_by_name(name); | 774 | int applet = find_applet_by_name(name); |
773 | if (applet >= 0) | 775 | if (applet >= 0) |
774 | run_applet_no_and_exit(applet, argv); | 776 | run_applet_no_and_exit(applet, argv); |
775 | if (!strncmp(name, "busybox", 7)) | 777 | if (strncmp(name, "busybox", 7) == 0) |
776 | exit(busybox_main(argv)); | 778 | exit(busybox_main(argv)); |
777 | } | 779 | } |
778 | 780 | ||
@@ -804,14 +806,6 @@ int main(int argc UNUSED_PARAM, char **argv) | |||
804 | mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256); | 806 | mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256); |
805 | #endif | 807 | #endif |
806 | 808 | ||
807 | #if defined(SINGLE_APPLET_MAIN) | ||
808 | /* Only one applet is selected by the user! */ | ||
809 | /* applet_names in this case is just "applet\0\0" */ | ||
810 | lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv)); | ||
811 | return SINGLE_APPLET_MAIN(argc, argv); | ||
812 | #else | ||
813 | lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv)); | ||
814 | |||
815 | #if !BB_MMU | 809 | #if !BB_MMU |
816 | /* NOMMU re-exec trick sets high-order bit in first byte of name */ | 810 | /* NOMMU re-exec trick sets high-order bit in first byte of name */ |
817 | if (argv[0][0] & 0x80) { | 811 | if (argv[0][0] & 0x80) { |
@@ -819,6 +813,19 @@ int main(int argc UNUSED_PARAM, char **argv) | |||
819 | argv[0][0] &= 0x7f; | 813 | argv[0][0] &= 0x7f; |
820 | } | 814 | } |
821 | #endif | 815 | #endif |
816 | |||
817 | #if defined(SINGLE_APPLET_MAIN) | ||
818 | /* Only one applet is selected in .config */ | ||
819 | if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) { | ||
820 | /* "busybox <applet> <params>" should still work as expected */ | ||
821 | argv++; | ||
822 | } | ||
823 | /* applet_names in this case is just "applet\0\0" */ | ||
824 | lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv)); | ||
825 | return SINGLE_APPLET_MAIN(argc, argv); | ||
826 | #else | ||
827 | lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv)); | ||
828 | |||
822 | applet_name = argv[0]; | 829 | applet_name = argv[0]; |
823 | if (applet_name[0] == '-') | 830 | if (applet_name[0] == '-') |
824 | applet_name++; | 831 | applet_name++; |