简体   繁体   English

执行与执行

[英]Execlp vs Execl

Is there any occasion in which is better to use execl instead of execlp ? 有什么情况下最好使用execl而不是execlp吗? I think that maybe when a program is in two different folders using execlp could lead to confusion but I don't know if it is the only case. 我认为,当程序位于两个不同的文件夹中时,使用execlp可能会引起混乱,但我不知道这是否是唯一的情况。 I ask because one could think that writing execlp("ls", ...) is easier than writing execl("/bin/ls", ...) . 我问,是因为有人认为写execlp("ls", ...)比写execl("/bin/ls", ...)容易。

Security 安全

Looking programs up via PATH is convenient, but it can also be insecure. 通过PATH查找程序很方便,但是它也不安全。 If a directory in a user's PATH is world writable, it's possible to inject a malicious program into the PATH lookup. 如果用户PATH的目录是可写的,则有可能将恶意程序注入PATH查找中。 This would affect execlp but not execl . 这将影响execlp但不会影响execl

For example, if you had a PATH like /foo/bar/bin:/home/you/bin:/usr/bin:/bin and /foo/bar/bin was world writable, someone with access to that machine could copy a malicious program to /foo/bar/bin/ls . 例如,如果您具有/foo/bar/bin:/home/you/bin:/usr/bin:/bin这样的PATH并且/foo/bar/bin是世界可写的,则有权访问该计算机的人可以复制恶意程序/foo/bar/bin/ls Then executing ls would run /foo/bar/bin/ls rather than /bin/ls . 然后执行ls将运行/foo/bar/bin/ls而不是/bin/ls They'd be able to execute commands as you and gain greater access. 他们将能够在您执行命令时获得更大的访问权限。

For this reason, it's often a good idea to refer to specific executables in known locations. 因此,在已知位置引用特定的可执行文件通常是一个好主意。 Or to hard wire a secure PATH in the executable. 或在可执行文件中硬连接安全PATH

Compatibility 兼容性

While there is a common set of Unix commands and features specified by POSIX , many programs rely on extensions. 尽管POSIX指定了一组通用的Unix命令和功能 ,但是许多程序都依赖扩展。 If your program uses those extensions, grabbing the first one in the PATH might not be a good idea. 如果您的程序使用这些扩展名,那么抓住PATH的第一个扩展名可能不是一个好主意。

For example, here on OS X the installed utilities in /bin and /usr/bin are BSD-flavored. 例如,在OS X上, /bin/usr/bin中安装的实用程序都是BSD风格的。 But I have GNU versions installed earlier in my PATH . 但是我在PATH早期安装了GNU版本。 A program designed to run on OS X would want to explicitly use, for example, /bin/ls or /usr/bin/tar to be sure they get a known version of those utilities. 设计为在OS X上运行的程序希望明确使用/bin/ls/usr/bin/tar ,以确保它们获得这些实用程序的已知版本。

$ /usr/bin/tar --version
bsdtar 2.8.3 - libarchive 2.8.3
$ tar --version
tar (GNU tar) 1.29

Both execl() and execlp() work fine and similarly if your executables are in different folders or in the same folder, but you need to set the $PATH if different folders. 如果您的可执行文件位于不同的文件夹或相同的文件夹中,则execl()execlp()可以正常工作,但如果不同的文件夹,则需要设置$ PATH。

execl() is needed for executing executables (like "ls") from command line as you can't go with execlp() in that case. execl()从命令行执行可执行文件(如“ ls” execl()所必需的,因为在这种情况下,您不能使用execlp() I added a snapshot below. 我在下面添加了快照。

#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
        if(argc!=2)
        {
                printf("Usage Msg: ./a.out userdefined_executable \n");
                return;
        }
        //execl(argv[1],argv[1],NULL);//**it works** 
        execlp(argv[1],argv[1],NULL);//**it doesn't work** 
        return 0;
}

//Input will be like this, here "p1" is an user-defined executable.
//xyz@xyz:~/stack_overflow$ ./a.out p1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM