无法使用“execve()”成功地

发布于 2024-08-20 00:22:05 字数 1905 浏览 3 评论 0原文

该程序的目的是分叉一个新的子进程并执行一个也具有命令行参数的进程。如果我输入 /bin/ls --help,我会收到错误:

shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$ ./a.out
Enter the name of the executable(with full path)/bin/ls --help
Starting the executable as a new child process...
Binary file to be executed: /bin/ls
/bin/ls: unrecognized option '--help
'
Try `/bin/ls --help' for more information.
Status returned by Child process: 2
shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$

What would be the right argument to execve()?

#include<stdio.h>
#include<string.h>      //strcpy() used
#include<malloc.h>      //malloc() used
#include<unistd.h>      //fork() used
#include<stdlib.h>      //exit() function used
#include<sys/wait.h>    //waitpid() used

int main(int argc, char **argv)
{
    char command[256];
    char **args=NULL;
    char *arg;
    int count=0;
    char *binary;
    pid_t pid;
    printf("Enter the name of the executable(with full path)");
    fgets(command,256,stdin);
    binary=strtok(command," ");
    args=malloc(sizeof(char*)*10);
    args[0]=malloc(strlen(binary)+1);
    strcpy(args[0],binary);
    while ((arg=strtok(NULL," "))!=NULL)
    {
        if ( count%10 == 0) args=realloc(args,sizeof(char*)*10);
        count++;
        args[count]=malloc(strlen(arg));
        strcpy(args[count],arg);
    }
    args[++count]=NULL;
    if ((pid = fork()) == -1)
    {
        perror("Error forking...\n");
        exit(1);
    }
    if (pid == 0)
    {
        printf("Starting the executable as a new child process...\n");
        printf("Binary file to be executed: %s\n",binary);
        execve(args[0],args,NULL);
    }
    else
    {
        int status;
        waitpid(-1, &status, 0);
        printf("Status returned by Child process: %d\n",WEXITSTATUS(status));
    }
    return 0;
}

The aim of the program is to fork a new child process and execute a process which also has command line arguments. If I enter /bin/ls --help, I get the error:

shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$ ./a.out
Enter the name of the executable(with full path)/bin/ls --help
Starting the executable as a new child process...
Binary file to be executed: /bin/ls
/bin/ls: unrecognized option '--help
'
Try `/bin/ls --help' for more information.
Status returned by Child process: 2
shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$

What would be the right argument to execve()?

#include<stdio.h>
#include<string.h>      //strcpy() used
#include<malloc.h>      //malloc() used
#include<unistd.h>      //fork() used
#include<stdlib.h>      //exit() function used
#include<sys/wait.h>    //waitpid() used

int main(int argc, char **argv)
{
    char command[256];
    char **args=NULL;
    char *arg;
    int count=0;
    char *binary;
    pid_t pid;
    printf("Enter the name of the executable(with full path)");
    fgets(command,256,stdin);
    binary=strtok(command," ");
    args=malloc(sizeof(char*)*10);
    args[0]=malloc(strlen(binary)+1);
    strcpy(args[0],binary);
    while ((arg=strtok(NULL," "))!=NULL)
    {
        if ( count%10 == 0) args=realloc(args,sizeof(char*)*10);
        count++;
        args[count]=malloc(strlen(arg));
        strcpy(args[count],arg);
    }
    args[++count]=NULL;
    if ((pid = fork()) == -1)
    {
        perror("Error forking...\n");
        exit(1);
    }
    if (pid == 0)
    {
        printf("Starting the executable as a new child process...\n");
        printf("Binary file to be executed: %s\n",binary);
        execve(args[0],args,NULL);
    }
    else
    {
        int status;
        waitpid(-1, &status, 0);
        printf("Status returned by Child process: %d\n",WEXITSTATUS(status));
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一页 2024-08-27 00:22:05

args 数组中的第一个条目应该再次是程序名称。您的代码以 --help 作为进程名称来调用 /bin/ls

The first entry in the args array should be the program name again. Your code calls /bin/ls with --help as the process name.

誰ツ都不明白 2024-08-27 00:22:05

请检查以确保 args 没有被 realloc 调用破坏。请参阅此处关于 realloc

编辑:
而且循环看起来很有趣......
您像这样调用 strtok

binary=strtok(command," ");

将循环构造更改为使用 binary 代替,如图所示...

char *tmpPtr;
while (binary != NULL){
   if ( count%10 == 0) tmpPtr=realloc(args,sizeof(char)*10);
   if (tmpPtr != NULL) args = tmpPtr;
   count++;
   args[count-1]=malloc(strlen(binary)+1);
   strcpy(args[count-1],binary);  
   binary = strtok(command, " ");
}

并使用 binary 复制字符串。 ..

希望这有帮助,
此致,
汤姆.

Please check to make sure args is not getting clobbered by the realloc call. See here on SO regarding realloc

Edit:
Also the loop looks funny....
You called strtok like this:

binary=strtok(command," ");

Change the loop construct to use binary instead as shown...

char *tmpPtr;
while (binary != NULL){
   if ( count%10 == 0) tmpPtr=realloc(args,sizeof(char)*10);
   if (tmpPtr != NULL) args = tmpPtr;
   count++;
   args[count-1]=malloc(strlen(binary)+1);
   strcpy(args[count-1],binary);  
   binary = strtok(command, " ");
}

And use the binary for copying the string....

Hope this helps,
Best regards,
Tom.

心如荒岛 2024-08-27 00:22:05

你的程序有一些明显的错误。例如,声明 char **args=NULL; 然后声明 args=realloc(args,sizeof(char)*10); (因为它是 char**,你应该alloc-ing到char*,不是吗?..)。

由于 sizeof(char*) 通常为 4,而 sizeof(char) 通常为 1,因此您最终会遇到一些严重的内存管理问题(分配的内存少于使用的内存) ,你最终会写到不应该写的地方)。从那时起,一切都乱了套,你不能指望你的程序的行为有任何意义。

我建议您通过 Valgrind 之类的实用程序运行程序,以找出内存泄漏并适当纠正程序。一旦内存问题得到纠正,您的 execve 问题可能就会消失。

Your program has some obvious errors. For instance, declaring char **args=NULL; and then args=realloc(args,sizeof(char)*10); (since it's char**, you should be alloc-ing to char*, no?..).

Since sizeof(char*) is usually 4 while sizeof(char) is usually 1, you end up with some serious memory management problems around there (you alloc less than you use, and you end up writing where you shouldn't). From there on, all hell breaks loose and you can't expect your program's behavior to make any sense.

I'd suggest that you run your program through an util such as Valgrind to figure out memory leaks and correct the program appropriately. Probably your execve problems will disappear as soon as the memory problems are corrected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文