aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-05-23 10:57:56 +0100
committerRon Yorston <rmy@pobox.com>2023-05-23 10:57:56 +0100
commitc2eb45989fcd59617884dc853743b5cc94b15935 (patch)
tree6a74f1a55758e74d77fb8ec705a930a4f2010c4e
parent20b6a57af64506a4152b145b031aad2946a37ca8 (diff)
downloadbusybox-w32-c2eb45989fcd59617884dc853743b5cc94b15935.tar.gz
busybox-w32-c2eb45989fcd59617884dc853743b5cc94b15935.tar.bz2
busybox-w32-c2eb45989fcd59617884dc853743b5cc94b15935.zip
drop: changes to ctrl-c handling
Use the new kill_child_ctrl_handler() function: - Ctrl-C can be used to terminate non-console applications. - Ctrl-C in an interactive cmd.exe or PowerShell session won't cause drop to terminate. Also: - Use getopt32() to process options. - Update usage messages. Adds 96-160 bytes.
-rw-r--r--miscutils/drop.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/miscutils/drop.c b/miscutils/drop.c
index 342de41d6..6b8e0f183 100644
--- a/miscutils/drop.c
+++ b/miscutils/drop.c
@@ -36,21 +36,21 @@
36//kbuild:lib-$(CONFIG_PDROP) += drop.o 36//kbuild:lib-$(CONFIG_PDROP) += drop.o
37 37
38//usage:#define drop_trivial_usage 38//usage:#define drop_trivial_usage
39//usage: "[COMMAND | -c [ARG...]]" 39//usage: "[COMMAND [ARG...] | -c CMD_STRING [ARG...]]"
40//usage:#define drop_full_usage "\n\n" 40//usage:#define drop_full_usage "\n\n"
41//usage: "Drop elevated privileges and run a command. If no COMMAND\n" 41//usage: "Drop elevated privileges and run a command. If no command\n"
42//usage: "is provided run the BusyBox shell.\n" 42//usage: "is provided run the BusyBox shell.\n"
43 43
44//usage:#define cdrop_trivial_usage 44//usage:#define cdrop_trivial_usage
45//usage: "[COMMAND | /c [ARG...]]" 45//usage: "[COMMAND [ARG...] | -c CMD_STRING [ARG...]]"
46//usage:#define cdrop_full_usage "\n\n" 46//usage:#define cdrop_full_usage "\n\n"
47//usage: "Drop elevated privileges and run a command. If no COMMAND\n" 47//usage: "Drop elevated privileges and run a command. If no command\n"
48//usage: "is provided run cmd.exe.\n" 48//usage: "is provided run cmd.exe.\n"
49 49
50//usage:#define pdrop_trivial_usage 50//usage:#define pdrop_trivial_usage
51//usage: "[COMMAND | -c [ARG...]]" 51//usage: "[COMMAND [ARG...] | -c CMD_STRING [ARG...]]"
52//usage:#define pdrop_full_usage "\n\n" 52//usage:#define pdrop_full_usage "\n\n"
53//usage: "Drop elevated privileges and run a command. If no COMMAND\n" 53//usage: "Drop elevated privileges and run a command. If no command\n"
54//usage: "is provided run PowerShell.\n" 54//usage: "is provided run PowerShell.\n"
55 55
56#include "libbb.h" 56#include "libbb.h"
@@ -108,12 +108,16 @@ int drop_main(int argc, char **argv)
108 TIL.Label.Attributes = SE_GROUP_INTEGRITY; 108 TIL.Label.Attributes = SE_GROUP_INTEGRITY;
109 if (SetTokenInformation(token, TokenIntegrityLevel, &TIL, 109 if (SetTokenInformation(token, TokenIntegrityLevel, &TIL,
110 sizeof(TOKEN_MANDATORY_LABEL))) { 110 sizeof(TOKEN_MANDATORY_LABEL))) {
111 char **a = argv + 1; 111 char *opt_command = NULL;
112 const char *arg; 112 char **a;
113 char *exe, *cmd; 113 const char *opt, *arg;
114 char *exe, *cmd, *q;
114 115
115 if (argc == 1 || strcmp(argv[1], "-c") == 0 116 getopt32(argv, "c:", &opt_command);
116 IF_CDROP(|| strcmp(argv[1], "/c") == 0)) { 117 a = argv + optind;
118 opt = "-c";
119
120 if (argc == 1 || opt_command) {
117 switch (*applet_name) { 121 switch (*applet_name) {
118#if ENABLE_PDROP 122#if ENABLE_PDROP
119 case 'p': 123 case 'p':
@@ -123,6 +127,7 @@ int drop_main(int argc, char **argv)
123#endif 127#endif
124#if ENABLE_CDROP 128#if ENABLE_CDROP
125 case 'c': 129 case 'c':
130 opt = "/c";
126 arg = "cmd.exe"; 131 arg = "cmd.exe";
127 exe = find_first_executable(arg); 132 exe = find_first_executable(arg);
128 break; 133 break;
@@ -161,10 +166,16 @@ int drop_main(int argc, char **argv)
161 166
162 slash_to_bs(exe); 167 slash_to_bs(exe);
163 cmd = quote_arg(arg); 168 cmd = quote_arg(arg);
169 if (opt_command) {
170 cmd = xappendword(cmd, opt);
171 q = quote_arg(opt_command);
172 cmd = xappendword(cmd, q);
173 free(q);
174 }
164 175
165 // Build the command line 176 // Build the command line
166 while (*a) { 177 while (*a) {
167 char *q = quote_arg(*a++); 178 q = quote_arg(*a++);
168 cmd = xappendword(cmd, q); 179 cmd = xappendword(cmd, q);
169 free(q); 180 free(q);
170 } 181 }
@@ -186,6 +197,8 @@ int drop_main(int argc, char **argv)
186 bb_error_msg_and_die("can't execute '%s'", exe); 197 bb_error_msg_and_die("can't execute '%s'", exe);
187 } 198 }
188 199
200 kill_child_ctrl_handler(pi.dwProcessId);
201 SetConsoleCtrlHandler(kill_child_ctrl_handler, TRUE);
189 WaitForSingleObject(pi.hProcess, INFINITE); 202 WaitForSingleObject(pi.hProcess, INFINITE);
190 if (GetExitCodeProcess(pi.hProcess, &code)) { 203 if (GetExitCodeProcess(pi.hProcess, &code)) {
191 return (int)code; 204 return (int)code;