![如何使用 busybox ash shell 執行修改後的 argv[0] 程式?](https://rvso.com/image/110476/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20busybox%20ash%20shell%20%E5%9F%B7%E8%A1%8C%E4%BF%AE%E6%94%B9%E5%BE%8C%E7%9A%84%20argv%5B0%5D%20%E7%A8%8B%E5%BC%8F%EF%BC%9F.png)
在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 個參數?了解如何以其他方式實現這一目標。