diff options
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r-- | libbb/appletlib.c | 184 |
1 files changed, 179 insertions, 5 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c index c15014a34..8efb1754a 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c | |||
@@ -48,6 +48,15 @@ | |||
48 | # define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__ | 48 | # define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__ |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | #if (ENABLE_FEATURE_INSTALLER && !ENABLE_PLATFORM_MINGW32) || \ | ||
52 | (ENABLE_PLATFORM_MINGW32 && (ENABLE_FEATURE_PREFER_APPLETS \ | ||
53 | || ENABLE_FEATURE_SH_STANDALONE \ | ||
54 | || ENABLE_FEATURE_SH_NOFORK)) | ||
55 | # define IF_FULL_LIST_OPTION(...) __VA_ARGS__ | ||
56 | #else | ||
57 | # define IF_FULL_LIST_OPTION(...) | ||
58 | #endif | ||
59 | |||
51 | #include "usage_compressed.h" | 60 | #include "usage_compressed.h" |
52 | 61 | ||
53 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS | 62 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS |
@@ -57,6 +66,7 @@ | |||
57 | # define NUM_SCRIPTS 0 | 66 | # define NUM_SCRIPTS 0 |
58 | #endif | 67 | #endif |
59 | #if NUM_SCRIPTS > 0 | 68 | #if NUM_SCRIPTS > 0 |
69 | # define BB_ARCHIVE_PUBLIC | ||
60 | # include "bb_archive.h" | 70 | # include "bb_archive.h" |
61 | static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS }; | 71 | static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS }; |
62 | #endif | 72 | #endif |
@@ -106,6 +116,7 @@ static const char usage_messages[] ALIGN1 = UNPACKED_USAGE; | |||
106 | #if ENABLE_FEATURE_COMPRESS_USAGE | 116 | #if ENABLE_FEATURE_COMPRESS_USAGE |
107 | 117 | ||
108 | static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; | 118 | static const char packed_usage[] ALIGN1 = { PACKED_USAGE }; |
119 | # define BB_ARCHIVE_PUBLIC | ||
109 | # include "bb_archive.h" | 120 | # include "bb_archive.h" |
110 | # define unpack_usage_messages() \ | 121 | # define unpack_usage_messages() \ |
111 | unpack_bz2_data(packed_usage, sizeof(packed_usage), sizeof(UNPACKED_USAGE)) | 122 | unpack_bz2_data(packed_usage, sizeof(packed_usage), sizeof(UNPACKED_USAGE)) |
@@ -149,7 +160,7 @@ void FAST_FUNC bb_show_usage(void) | |||
149 | ap--; | 160 | ap--; |
150 | } | 161 | } |
151 | full_write2_str(bb_banner); | 162 | full_write2_str(bb_banner); |
152 | full_write2_str(" multi-call binary.\n"); /* common string */ | 163 | full_write2_str(" multi-call binary\n"); |
153 | if (*p == '\b') | 164 | if (*p == '\b') |
154 | full_write2_str("\nNo help available\n"); | 165 | full_write2_str("\nNo help available\n"); |
155 | else { | 166 | else { |
@@ -312,6 +323,10 @@ void lbb_prepare(const char *applet | |||
312 | if (ENABLE_LOCALE_SUPPORT) | 323 | if (ENABLE_LOCALE_SUPPORT) |
313 | setlocale(LC_ALL, ""); | 324 | setlocale(LC_ALL, ""); |
314 | 325 | ||
326 | #if ENABLE_PLATFORM_MINGW32 | ||
327 | init_winsock(); | ||
328 | #endif | ||
329 | |||
315 | #if ENABLE_FEATURE_INDIVIDUAL | 330 | #if ENABLE_FEATURE_INDIVIDUAL |
316 | /* Redundant for busybox (run_applet_and_exit covers that case) | 331 | /* Redundant for busybox (run_applet_and_exit covers that case) |
317 | * but needed for "individual applet" mode */ | 332 | * but needed for "individual applet" mode */ |
@@ -702,6 +717,7 @@ static void check_suid(int applet_no) | |||
702 | 717 | ||
703 | 718 | ||
704 | # if ENABLE_FEATURE_INSTALLER | 719 | # if ENABLE_FEATURE_INSTALLER |
720 | # if !ENABLE_PLATFORM_MINGW32 | ||
705 | static const char usr_bin [] ALIGN1 = "/usr/bin/"; | 721 | static const char usr_bin [] ALIGN1 = "/usr/bin/"; |
706 | static const char usr_sbin[] ALIGN1 = "/usr/sbin/"; | 722 | static const char usr_sbin[] ALIGN1 = "/usr/sbin/"; |
707 | static const char *const install_dir[] = { | 723 | static const char *const install_dir[] = { |
@@ -746,6 +762,30 @@ static void install_links(const char *busybox, int use_symbolic_links, | |||
746 | continue; | 762 | continue; |
747 | } | 763 | } |
748 | } | 764 | } |
765 | # else /* ENABLE_PLATFORM_MINGW32 */ | ||
766 | static void install_links(const char *busybox, | ||
767 | int use_symbolic_links UNUSED_PARAM, char *custom_install_dir) | ||
768 | { | ||
769 | char *fpc; | ||
770 | const char *appname = applet_names; | ||
771 | int rc; | ||
772 | |||
773 | if (!is_directory(custom_install_dir, FALSE)) | ||
774 | bb_error_msg_and_die("'%s' is not a directory", custom_install_dir); | ||
775 | |||
776 | while (*appname) { | ||
777 | fpc = xasprintf("%s/%s.exe", custom_install_dir, appname); | ||
778 | rc = link(busybox, fpc); | ||
779 | if (rc != 0 && (errno != EEXIST || | ||
780 | strcmp("busybox.exe", bb_basename(fpc)) != 0)) { | ||
781 | bb_simple_perror_msg(fpc); | ||
782 | } | ||
783 | free(fpc); | ||
784 | while (*appname++ != '\0') | ||
785 | continue; | ||
786 | } | ||
787 | } | ||
788 | # endif | ||
749 | # elif ENABLE_BUSYBOX | 789 | # elif ENABLE_BUSYBOX |
750 | static void install_links(const char *busybox UNUSED_PARAM, | 790 | static void install_links(const char *busybox UNUSED_PARAM, |
751 | int use_symbolic_links UNUSED_PARAM, | 791 | int use_symbolic_links UNUSED_PARAM, |
@@ -829,20 +869,31 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
829 | 869 | ||
830 | dup2(1, 2); | 870 | dup2(1, 2); |
831 | full_write2_str(bb_banner); /* reuse const string */ | 871 | full_write2_str(bb_banner); /* reuse const string */ |
832 | full_write2_str(" multi-call binary.\n"); /* reuse */ | 872 | full_write2_str(" multi-call binary\n"); /* reuse */ |
873 | #if defined(MINGW_VER) | ||
874 | if (sizeof(MINGW_VER) > 5) { | ||
875 | full_write2_str(MINGW_VER "\n\n"); | ||
876 | } | ||
877 | #endif | ||
833 | full_write2_str( | 878 | full_write2_str( |
834 | "BusyBox is copyrighted by many authors between 1998-2015.\n" | 879 | "BusyBox is copyrighted by many authors between 1998-2018.\n" |
835 | "Licensed under GPLv2. See source distribution for detailed\n" | 880 | "Licensed under GPLv2. See source distribution for detailed\n" |
836 | "copyright notices.\n" | 881 | "copyright notices.\n" |
837 | "\n" | 882 | "\n" |
838 | "Usage: busybox [function [arguments]...]\n" | 883 | "Usage: busybox [function [arguments]...]\n" |
839 | " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n" | 884 | " or: busybox --list"IF_FULL_LIST_OPTION("[-full]")"\n" |
840 | # if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0 | 885 | # if ENABLE_FEATURE_SHOW_SCRIPT && NUM_SCRIPTS > 0 |
841 | " or: busybox --show SCRIPT\n" | 886 | " or: busybox --show SCRIPT\n" |
842 | # endif | 887 | # endif |
843 | IF_FEATURE_INSTALLER( | 888 | IF_FEATURE_INSTALLER( |
889 | IF_NOT_PLATFORM_MINGW32( | ||
844 | " or: busybox --install [-s] [DIR]\n" | 890 | " or: busybox --install [-s] [DIR]\n" |
845 | ) | 891 | ) |
892 | IF_PLATFORM_MINGW32( | ||
893 | " or: busybox --install [DIR]\n" | ||
894 | " or: busybox --uninstall [-n] file\n" | ||
895 | ) | ||
896 | ) | ||
846 | " or: function [arguments]...\n" | 897 | " or: function [arguments]...\n" |
847 | "\n" | 898 | "\n" |
848 | IF_NOT_FEATURE_SH_STANDALONE( | 899 | IF_NOT_FEATURE_SH_STANDALONE( |
@@ -859,6 +910,11 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
859 | "\tTo run external program, use full path (/sbin/ip instead of ip).\n" | 910 | "\tTo run external program, use full path (/sbin/ip instead of ip).\n" |
860 | ) | 911 | ) |
861 | "\n" | 912 | "\n" |
913 | #if ENABLE_GLOBBING | ||
914 | "\tSupport for native Windows wildcards is enabled. In some\n" | ||
915 | "\tcases this may result in wildcards being processed twice.\n" | ||
916 | "\n" | ||
917 | #endif | ||
862 | "Currently defined functions:\n" | 918 | "Currently defined functions:\n" |
863 | ); | 919 | ); |
864 | col = 0; | 920 | col = 0; |
@@ -903,9 +959,27 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
903 | const char *a = applet_names; | 959 | const char *a = applet_names; |
904 | dup2(1, 2); | 960 | dup2(1, 2); |
905 | while (*a) { | 961 | while (*a) { |
906 | # if ENABLE_FEATURE_INSTALLER | 962 | # if ENABLE_FEATURE_INSTALLER && !ENABLE_PLATFORM_MINGW32 |
907 | if (argv[1][6]) /* --list-full? */ | 963 | if (argv[1][6]) /* --list-full? */ |
908 | full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1); | 964 | full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1); |
965 | # elif ENABLE_PLATFORM_MINGW32 && (ENABLE_FEATURE_PREFER_APPLETS \ | ||
966 | || ENABLE_FEATURE_SH_STANDALONE \ | ||
967 | || ENABLE_FEATURE_SH_NOFORK) | ||
968 | if (argv[1][6]) { /* --list-full? */ | ||
969 | const char *str; | ||
970 | |||
971 | if (APPLET_IS_NOFORK(i)) | ||
972 | str = "NOFORK "; | ||
973 | else if (APPLET_IS_NOEXEC(i)) | ||
974 | str = "noexec "; | ||
975 | # if NUM_SCRIPTS > 0 | ||
976 | else if (applet_main[i] == scripted_main) | ||
977 | str = "script "; | ||
978 | # endif | ||
979 | else | ||
980 | str = " "; | ||
981 | full_write2_str(str); | ||
982 | } | ||
909 | # endif | 983 | # endif |
910 | full_write2_str(a); | 984 | full_write2_str(a); |
911 | full_write2_str("\n"); | 985 | full_write2_str("\n"); |
@@ -917,6 +991,7 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
917 | } | 991 | } |
918 | 992 | ||
919 | if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) { | 993 | if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) { |
994 | #if !ENABLE_PLATFORM_MINGW32 | ||
920 | int use_symbolic_links; | 995 | int use_symbolic_links; |
921 | const char *busybox; | 996 | const char *busybox; |
922 | 997 | ||
@@ -937,9 +1012,40 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
937 | */ | 1012 | */ |
938 | use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && ++argv); | 1013 | use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && ++argv); |
939 | install_links(busybox, use_symbolic_links, argv[2]); | 1014 | install_links(busybox, use_symbolic_links, argv[2]); |
1015 | #else | ||
1016 | /* busybox --install [DIR] | ||
1017 | * where DIR is the directory to install to. If DIR is not | ||
1018 | * provided put the links in the same directory as busybox. | ||
1019 | */ | ||
1020 | install_links(bb_busybox_exec_path, FALSE, argv[2] ? argv[2] : | ||
1021 | dirname(xstrdup(bb_busybox_exec_path))); | ||
1022 | #endif | ||
940 | return 0; | 1023 | return 0; |
941 | } | 1024 | } |
942 | 1025 | ||
1026 | #if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_INSTALLER | ||
1027 | if (strcmp(argv[1], "--uninstall") == 0) { | ||
1028 | char name[PATH_MAX]; | ||
1029 | int dry_run = (argv[2] && strcmp(argv[2], "-n") == 0 && ++argv); | ||
1030 | const char *file = argv[2]; | ||
1031 | |||
1032 | if (!argv[2]) | ||
1033 | bb_error_msg_and_die(bb_msg_requires_arg, "--uninstall"); | ||
1034 | |||
1035 | while (enumerate_links(file, name)) { | ||
1036 | if (dry_run) { | ||
1037 | full_write1_str(name); | ||
1038 | full_write1_str("\n"); | ||
1039 | } | ||
1040 | else if (unlink(name) != 0) { | ||
1041 | bb_simple_perror_msg(name); | ||
1042 | } | ||
1043 | file = NULL; | ||
1044 | } | ||
1045 | return 0; | ||
1046 | } | ||
1047 | #endif | ||
1048 | |||
943 | if (strcmp(argv[1], "--help") == 0) { | 1049 | if (strcmp(argv[1], "--help") == 0) { |
944 | /* "busybox --help [<applet>]" */ | 1050 | /* "busybox --help [<applet>]" */ |
945 | if (!argv[2]) | 1051 | if (!argv[2]) |
@@ -959,15 +1065,42 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) | |||
959 | # endif | 1065 | # endif |
960 | 1066 | ||
961 | # if NUM_APPLETS > 0 | 1067 | # if NUM_APPLETS > 0 |
1068 | |||
1069 | # if ENABLE_PLATFORM_MINGW32 | ||
1070 | static int interp = 0; | ||
1071 | char bb_comm[COMM_LEN]; | ||
1072 | char bb_command_line[128]; | ||
1073 | # endif | ||
1074 | |||
962 | void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **argv) | 1075 | void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **argv) |
963 | { | 1076 | { |
964 | int argc = string_array_len(argv); | 1077 | int argc = string_array_len(argv); |
1078 | # if ENABLE_PLATFORM_MINGW32 | ||
1079 | int i; | ||
1080 | const char *vmask; | ||
1081 | unsigned int mask; | ||
1082 | # endif | ||
965 | 1083 | ||
966 | /* | 1084 | /* |
967 | * We do not use argv[0]: do not want to repeat massaging of | 1085 | * We do not use argv[0]: do not want to repeat massaging of |
968 | * "-/sbin/halt" -> "halt", for example. | 1086 | * "-/sbin/halt" -> "halt", for example. |
969 | */ | 1087 | */ |
970 | applet_name = name; | 1088 | applet_name = name; |
1089 | # if ENABLE_PLATFORM_MINGW32 | ||
1090 | safe_strncpy(bb_comm, | ||
1091 | interp ? bb_basename(argv[interp]) : applet_name, | ||
1092 | sizeof(bb_comm)); | ||
1093 | |||
1094 | safe_strncpy(bb_command_line, applet_name, sizeof(bb_command_line)); | ||
1095 | for (i=1; i < argc && argv[i] && | ||
1096 | strlen(bb_command_line) + strlen(argv[i]) + 2 < 128; ++i) { | ||
1097 | strcat(strcat(bb_command_line, " "), argv[i]); | ||
1098 | } | ||
1099 | |||
1100 | vmask = getenv("BB_UMASK"); | ||
1101 | if (vmask && sscanf(vmask, "%o", &mask) == 1) | ||
1102 | umask((mode_t)(mask&0777)); | ||
1103 | # endif | ||
971 | 1104 | ||
972 | /* Special case. POSIX says "test --help" | 1105 | /* Special case. POSIX says "test --help" |
973 | * should be no different from e.g. "test --foo". | 1106 | * should be no different from e.g. "test --foo". |
@@ -993,6 +1126,7 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar | |||
993 | } | 1126 | } |
994 | if (ENABLE_FEATURE_SUID) | 1127 | if (ENABLE_FEATURE_SUID) |
995 | check_suid(applet_no); | 1128 | check_suid(applet_no); |
1129 | |||
996 | xfunc_error_retval = applet_main[applet_no](argc, argv); | 1130 | xfunc_error_retval = applet_main[applet_no](argc, argv); |
997 | /* Note: applet_main() may also not return (die on a xfunc or such) */ | 1131 | /* Note: applet_main() may also not return (die on a xfunc or such) */ |
998 | xfunc_die(); | 1132 | xfunc_die(); |
@@ -1075,6 +1209,31 @@ int main(int argc UNUSED_PARAM, char **argv) | |||
1075 | argv[0][0] &= 0x7f; | 1209 | argv[0][0] &= 0x7f; |
1076 | } | 1210 | } |
1077 | #endif | 1211 | #endif |
1212 | #if ENABLE_PLATFORM_MINGW32 | ||
1213 | /* detect if we're running an interpreted script */ | ||
1214 | if (argv[0][1] == ':' && argv[0][2] == '/') { | ||
1215 | switch (argv[0][0]) { | ||
1216 | case '1': | ||
1217 | interp = 1; | ||
1218 | break; | ||
1219 | case'2': | ||
1220 | interp = 2; | ||
1221 | break; | ||
1222 | } | ||
1223 | } | ||
1224 | #endif | ||
1225 | |||
1226 | #if defined(__MINGW64_VERSION_MAJOR) | ||
1227 | if ( stdin ) { | ||
1228 | _setmode(fileno(stdin), _O_BINARY); | ||
1229 | } | ||
1230 | if ( stdout ) { | ||
1231 | _setmode(fileno(stdout), _O_BINARY); | ||
1232 | } | ||
1233 | if ( stderr ) { | ||
1234 | _setmode(fileno(stderr), _O_BINARY); | ||
1235 | } | ||
1236 | #endif | ||
1078 | 1237 | ||
1079 | #if defined(SINGLE_APPLET_MAIN) | 1238 | #if defined(SINGLE_APPLET_MAIN) |
1080 | 1239 | ||
@@ -1107,6 +1266,21 @@ int main(int argc UNUSED_PARAM, char **argv) | |||
1107 | applet_name = argv[0]; | 1266 | applet_name = argv[0]; |
1108 | if (applet_name[0] == '-') | 1267 | if (applet_name[0] == '-') |
1109 | applet_name++; | 1268 | applet_name++; |
1269 | # if ENABLE_PLATFORM_MINGW32 | ||
1270 | if ( argv[1] && argv[2] && strcmp(argv[1], "--busybox") == 0 ) { | ||
1271 | argv += 2; | ||
1272 | applet_name = argv[0]; | ||
1273 | } | ||
1274 | else { | ||
1275 | char *s; | ||
1276 | |||
1277 | str_tolower(argv[0]); | ||
1278 | convert_slashes(argv[0]); | ||
1279 | if (has_exe_suffix_or_dot(argv[0]) && (s=strrchr(argv[0], '.'))) { | ||
1280 | *s = '\0'; | ||
1281 | } | ||
1282 | } | ||
1283 | # endif | ||
1110 | applet_name = bb_basename(applet_name); | 1284 | applet_name = bb_basename(applet_name); |
1111 | 1285 | ||
1112 | /* If we are a result of execv("/proc/self/exe"), fix ugly comm of "exe" */ | 1286 | /* If we are a result of execv("/proc/self/exe"), fix ugly comm of "exe" */ |