noexec: consolidate code

function                                             old     new   delta
run_noexec_applet_and_exit                             -      61     +61
find_applet_by_name                                  128     124      -4
run_applet_no_and_exit                               441     434      -7
tryexec                                              169     152     -17
pseudo_exec_argv                                     338     321     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/6 up/down: 61/-48)             Total: 13 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-08-07 19:24:57 +02:00
parent c9c1ccc4ed
commit 80e8e3cc05
5 changed files with 26 additions and 34 deletions

View File

@ -1112,9 +1112,10 @@ int wait_for_exitstatus(pid_t pid) FAST_FUNC;
int spawn_and_wait(char **argv) FAST_FUNC; int spawn_and_wait(char **argv) FAST_FUNC;
/* Does NOT check that applet is NOFORK, just blindly runs it */ /* Does NOT check that applet is NOFORK, just blindly runs it */
int run_nofork_applet(int applet_no, char **argv) FAST_FUNC; int run_nofork_applet(int applet_no, char **argv) FAST_FUNC;
void run_noexec_applet_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC;
#ifndef BUILD_INDIVIDUAL #ifndef BUILD_INDIVIDUAL
extern int find_applet_by_name(const char *name) FAST_FUNC; int find_applet_by_name(const char *name) FAST_FUNC;
extern void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC;
#endif #endif
#if defined(__linux__) #if defined(__linux__)
void set_task_comm(const char *comm) FAST_FUNC; void set_task_comm(const char *comm) FAST_FUNC;

View File

@ -924,8 +924,6 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
{ {
int argc = string_array_len(argv); int argc = string_array_len(argv);
/* Reinit some shared global data */
xfunc_error_retval = EXIT_FAILURE;
/* /*
* We do not use argv[0]: do not want to repeat massaging of * We do not use argv[0]: do not want to repeat massaging of
* "-/sbin/halt" -> "halt", for example. * "-/sbin/halt" -> "halt", for example.

View File

@ -158,6 +158,26 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
} }
#endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */ #endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */
#if (NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE)
void FAST_FUNC run_noexec_applet_and_exit(int a, const char *name, char **argv)
{
/* reset some state and run without execing */
/* msg_eol = "\n"; - no caller needs this reinited yet */
logmode = LOGMODE_STDIO;
xfunc_error_retval = EXIT_FAILURE;
die_func = NULL;
GETOPT_RESET();
//TODO: think pidof, pgrep, pkill!
//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
//but one from procps-ng-3.3.10 needs more!
//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
set_task_comm(name);
/* xfunc_error_retval and applet_name are init by: */
run_applet_no_and_exit(a, name, argv);
}
#endif
int FAST_FUNC spawn_and_wait(char **argv) int FAST_FUNC spawn_and_wait(char **argv)
{ {
int rc; int rc;
@ -175,22 +195,7 @@ int FAST_FUNC spawn_and_wait(char **argv)
return wait4pid(rc); return wait4pid(rc);
/* child */ /* child */
/* reset some state and run without execing */ run_noexec_applet_and_exit(a, argv[0], argv);
GETOPT_RESET();
/* msg_eol = "\n"; - no caller needs this reinited yet */
logmode = LOGMODE_STDIO;
/* die_func = NULL; - needed if the caller is a shell,
* init, or a NOFORK applet. But none of those call us
* as of yet (and that should probably always stay true).
*/
//TODO: think pidof, pgrep, pkill!
//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
//but one from procps-ng-3.3.10 needs more!
//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
set_task_comm(argv[0]);
/* xfunc_error_retval and applet_name are init by: */
run_applet_no_and_exit(a, argv[0], argv);
} }
# endif # endif
} }

View File

@ -7803,13 +7803,7 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c
while (*envp) while (*envp)
putenv(*envp++); putenv(*envp++);
popredir(/*drop:*/ 1); popredir(/*drop:*/ 1);
GETOPT_RESET(); run_noexec_applet_and_exit(applet_no, cmd, argv);
//TODO: think pidof, pgrep, pkill!
//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
//but one from procps-ng-3.3.10 needs more!
//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
set_task_comm(argv[0]);
run_applet_no_and_exit(applet_no, cmd, argv);
} }
/* re-exec ourselves with the new arguments */ /* re-exec ourselves with the new arguments */
execve(bb_busybox_exec_path, argv, envp); execve(bb_busybox_exec_path, argv, envp);

View File

@ -7386,14 +7386,8 @@ static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
//FIXME: should also close saved redir fds //FIXME: should also close saved redir fds
/* Without this, "rm -i FILE" can't be ^C'ed: */ /* Without this, "rm -i FILE" can't be ^C'ed: */
switch_off_special_sigs(G.special_sig_mask); switch_off_special_sigs(G.special_sig_mask);
GETOPT_RESET();
//TODO: think pidof, pgrep, pkill!
//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
//but one from procps-ng-3.3.10 needs more!
//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
set_task_comm(argv[0]);
debug_printf_exec("running applet '%s'\n", argv[0]); debug_printf_exec("running applet '%s'\n", argv[0]);
run_applet_no_and_exit(a, argv[0], argv); run_noexec_applet_and_exit(a, argv[0], argv);
} }
# endif # endif
/* Re-exec ourselves */ /* Re-exec ourselves */