简体   繁体   English

execve - 没有这样的文件或目录?

[英]execve - No such file or directory?

I'm having some problems with execve. 我在execve遇到了一些问题。 I'm trying to make a shell that can function just like the bash shell, but I have problems with the forked child executing a command. 我正在尝试创建一个可以像bash shell一样运行的shell,但是我遇到了forked子执行命令的问题。 Here is what I have for the child. 这就是我对孩子的看法。 cmd is a char * with the command that the user typed in. However, when I run this program, I get this error from perror: cmd是一个char *,其中包含用户输入的命令。但是,当我运行此程序时,我从perror中收到此错误:

execve error: No such file or directory.

I have tried the program with a simple ls, and it should make path="/bin/ls" and execute it (I have confirmed this is where my ls command is) but it still complains. 我用一个简单的ls尝试了程序,它应该使path =“/ bin / ls”并执行它(我已经确认这是我的ls命令所在的位置)但它仍然抱怨。 What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!

if(pid == 0)
{
    // Parse the command
    char * word = strtok(cmd, " ");
    char path[128] = "/bin/";
    strcat(path, word);

    // Execute the process
    char * newenvp[] = { NULL };
    char * newargv[] = { path, NULL };
    ret = execve(path, newargv, newenvp);

    if(ret == -1)
        perror("execve error");

    return EXIT_SUCCESS;
}

The first thing I would do would be to insert: 我要做的第一件事就是插入:

printf ("[%s]\n", path);

before the call to execve . 之前调用execve That should confirm that the executable is what you think it is. 这应该确认可执行文件是你认为的。

That code of yours looks okay as long as the input you're feeding into it is correct and the executable actually is available. 只要您输入的输入正确且可执行文件实际可用,您的代码看起来可以了。 For example, the following complete program works fine on my Debian box: 例如,以下完整程序在我的Debian框中正常工作:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main (int argc, char *argv[]) {
    if (argc > 1) {
        char * word = strtok (argv[1], " ");
        char path[128] = "/bin/";
        strcat (path, word);

        char * newenvp[] = { NULL };
        char * newargv[] = { path, NULL };
        printf ("[%s]\n", path);
        int ret = execve (path, newargv, newenvp);
        if (ret == -1) {
            perror("execve error");
        }
    }
    return 0;
}

outputting, when I run ./testprog ls , something along the lines of: 输出,当我运行./testprog ls ,有些东西:

[/bin/ls]
kidsshares.ods  paxwords    birthdays    homeloantracking.gnumeric
shares2011.ods  backup0.sh  development  lexar
accounts.ods    backup1.sh  photos       testprog.c
testprog

If you don't want to manually travel through the fileystem to find the correct binary, there is execlp (with an additional p ). 如果您不想手动遍历文件系统以找到正确的二进制文件,则有execlp (附加p )。 From the manpage: 从联机帮助页:

The execlp (), execvp (), and execvpe () functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. 如果指定的文件名不包含斜杠(/)字符,则execlp (), execvp ()和execvpe ()函数会复制shell在搜索可执行文件时的操作。 The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable. PATH环境变量中指定的以冒号分隔的目录路径名列表中查找该文件。 If this variable isn't defined, the path list defaults to the current directory followed by the list of directories returned by confstr(_CS_PATH). 如果未定义此变量,则路径列表默认为当前目录,后跟confstr(_CS_PATH)返回的目录列表。 (This confstr(3) call typically returns the value "/bin:/usr/bin".) [...] (此confstr(3)调用通常返回值“/ bin:/ usr / bin”。)[...]

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

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