为什么要首先将execve()的arg设置为可执行文件的路径 [英] Why first arg to execve() must be path to executable

查看:46
本文介绍了为什么要首先将execve()的arg设置为可执行文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道execve()和family要求其参数数组的第一个参数与也由其第一个参数指向的可执行文件相同.也就是说,在此:

I understand that execve() and family require the first argument of its argument array to be the same as the executable that is also pointed to by its first argument. That is, in this:

execve(prog, args, env);

args [0]通常与prog相同.但是我似乎找不到有关为什么的信息.

args[0] will usually be the same as prog. But I can't seem to find information as to why this is.

我还了解到,可执行文件(至少是shell脚本)在运行时始终将其调用路径作为第一个参数,但是我认为shell会做一些工作来将其放在那里,而execve()只会使用其第一个参数(上面的"prog")中给出的路径调用可执行文件,然后像在命令行上那样传递参数数组(上面的"args")..即,我不调用脚本在命令行中,在args列表中有重复的可执行路径....

I also understand that executables (er, at least shell scripts) always have their calling path as the first argument when running, but I would think that the shell would do the work to put it there, and execve() would just call the executable using the path given in its first argument ("prog" from above), then passing the argument array ("args" from above) as one would on the command line.... i.e., I don't call scripts on the command line with a duplicate executable path in the args list....

/bin/ls /bin/ls /home/john

有人可以解释吗?

推荐答案

它允许您指定要加载的可执行文件的确切路径,但也可以在诸如之类的工具中显示美化"名称.> ps top .

It allows you to specify the exact path to the executable to be loaded, but also allows for a "beautified" name to be presented in tools such as ps or top.

execl("/bin/ls", "ls", "/home/john", (char *)0);

这篇关于为什么要首先将execve()的arg设置为可执行文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆