如何使用 busybox ash shell 執行修改後的 argv[0] 程式?

如何使用 busybox ash shell 執行修改後的 argv[0] 程式?

bash我會簡單地使用exec -a.我怎麼能在 busybox 中做到這一點?是否有可能,或者我必須編寫自己的 C 程式才能exec(3)直接呼叫?

答案1

你有什麼版本的busybox?根據https://git.busybox.net/busybox/tree/shell/ash.c如果一個人深入研究exec可能會在第 9352 行左右遇到以下程式碼,它似乎支持exec [-a customname] ...

execcmd(int argc UNUSED_PARAM, char **argv)
{
    optionarg = NULL;
    while (nextopt("a:") != '\0')
        /* nextopt() sets optionarg to "-a ARGV0" */;

    argv = argptr;
    if (argv[0]) {
        char *prog;

        iflag = 0;              /* exit on error */
        mflag = 0;
        optschanged();
        /* We should set up signals for "exec CMD"
         * the same way as for "CMD" without "exec".
         * But optschanged->setinteractive->setsignal
         * still thought we are a root shell. Therefore, for example,
         * SIGQUIT is still set to IGN. Fix it:
         */
        shlvl++;
        setsignal(SIGQUIT);
        /*setsignal(SIGTERM); - unnecessary because of iflag=0 */
        /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
        /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */

        prog = argv[0];
        if (optionarg)
            argv[0] = optionarg;
        shellexec(prog, argv, pathval(), 0);

答案2

exec -a從 Busybox 1.27 開始支持,另請參閱以下問題的答案是否有 POSIX 方法來設定目標應用程式的第 0 個參數?了解如何以其他方式實現這一目標。

相關內容