首页 > 解决方案 > execve 失败 - 忽略退出语句

问题描述

fgets混合和execve在一起时我有一个奇怪的问题。

我有一个从文件中读取行并执行它们的函数,或者通过它自己的函数解析它们,或者使用execve. 为了简单起见,我的测试文件只有外部函数:

echo whoowhee
lx
ls
dir

第二个命令lx不存在,因此此时execve应该失败。

这是我从文件中读取的代码:

  while(fgets(line, 1024, testfile) != NULL){
    if(strlen(line) < 3){continue;}
    if(line[0] == '#'){continue;}

    printf("%s%s", value("PROMPT"), line); // Emulates the prompt

    //Terminates line at right place to simulate input
    line[strlen(line)-2] = '\0';

    execute(line);

    Var *prompt = retrieveVar("PROMPT");
    sprintf(prompt->value, "< Executing script... - [%s] > $ ", value("EXITCODE"));

    lineNo++; 
  }

这是我的 fork-exec 块:

  if(pid == 0){ // Child

    // For loop for using all paths
    for(int i = 0; i < pathn; i++){
      args[0] = paths[i];

      // Executes command path[i] with arguments args with environment envp
      execve(paths[i], args, envp);
    }

    perror("execve");
    exit(0);
  }
  else if(pid > 0){ //Parent
    current_pid = pid;

    if(setpgid(pid, pid) != 0) perror("setpid");

    // Waits if background flag not activated.
    if(BG == 0){
      // WUNTRACED used to stop waiting when suspended
      waitpid(current_pid, &status, WUNTRACED);

        if(WIFEXITED(status)){
          setExitcode(WEXITSTATUS(status));
        }
        else if(WIFSIGNALED(status)){
          printf("Process received SIGNAL %d\n", WTERMSIG(status));
        }
    }
  }
  else{
    perror("fork()");
  }

由于execve需要程序的绝对路径,paths是程序应该存在的可能路径的数组。BG说明进程是否应该在后台执行,value("EXITCODE")是进程的退出代码,并且execute是执行该行的函数.

现在,这是我运行测试文件时的输出,代码原样:

<Welcome to Eggshell> $ echo whoowhee
whoowhee
< Executing script... - [0] > $ lx
execve: No such file or directory
< Executing script... - [0] > $ ls
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt

如您所见,dir运行两次。

如果我在文本文件中添加另一个无意义的命令,问题并不止于此:

echo whoowhee
lx
onetimesoneistwo
ls
dir

如果我尝试运行该函数,它最终会陷入无限循环。不仅如此,这一次它还会一遍又一遍地运行整个文件!

但是,如果我exitfork-exec位中删除,然后重新运行该函数,就会出现以下结果:

<Welcome to Eggshell> $ echo whoowhee
whoowhee
< Executing script... - [0] > $ lx
execve: No such file or directory
< Executing script... - [0] > $ onetimesoneistwo
execve: No such file or directory
< Executing script... - [0] > $ ls
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ ls
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ onetimesoneistwo
execve: No such file or directory
< Executing script... - [0] > $ ls
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ ls
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt
< Executing script... - [0] > $ dir
add-on  codecov.yml documentation  eggshell.c  LICENSE  Makefile        README.md  switch.sh      val.log
ci  createfile.txt  eggshell       eggshell.h  main.c   Makefile-Clang  src        testinput.txt

令人惊讶的是,这不是一个无限循环!当然每个命令onetimesoneistwo运行 4 次后,onetimesoneistwo本身运行两次,但至少它是一些东西。

我猜想execve失败会导致分叉的孩子永远不会终止,但是为什么会exit导致无限循环而exit之后没有重复的所有指令呢?

有趣的故事是,如果我正常运行程序,自己提供输入而不是从文件中获取输入,则不会发生同样的事情,所以我的猜测是我的源函数或我的 fork-exec 出现了严重错误代码。

标签: cforkfgetsexecve

解决方案


至少对我来说,解决方案似乎是使用_exit而不是exit. 如果失败,使用前者会立即杀死孩子execve,而不是产生奇怪的无限循环效果。

虽然我仍然不知道为什么会exit导致无限循环,以及为什么在不_exit工作的地方工作......exit

更新:那效果不好。解决问题的方法是fgets在出现之前在循环中使用,并将所有行存储在字符串数组缓冲区中。然后,在下一个循环中,我只需从数组中逐行读取。由于exit正在重置文件指针,因此解决方案是不再在循环中使用文件指针。


推荐阅读