【问题标题】:Execve '/bin/sleep' command freezing执行 '/bin/sleep' 命令冻结
【发布时间】:2015-12-06 17:13:19
【问题描述】:

我有一个客户端服务器程序,其中服务器是一个简单的外壳

命令工作正常,参数传递正常,但我认为 execve 正在冻结

所以这里是简单的shell代码——前半部分操作系统来验证这个shell中是否允许命令

  while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){ //loop until connection has been terminated
      if (!strcmp(buf, "quit\n")){
        printf("User %s disconnected.\n", username);
        flag1 = 0; //the user will need to login again
      }
      if (flag1 == 1){ //the user is loged in
        bg = p3parseline(buf, argv_for_shell);
        strtok(buf, "\n");
        printf("User %s sent the command '%s' to be executed.\n", username, buf);
        // temp = argv_for_shell[0];
        // strcat(temp, "\n");
    //check if the command is allowed
    flag2 = 0; //set command allowed? flag back to false
    file = Fopen("rrshcommands.txt", "r");
    while (Fgets(command, MAXLINE, file) != NULL){
      strtok(command, "\n");
      if (!strcmp(argv_for_shell[0], command)){
        flag2 = 1;
      }
    }
    Fclose(file);

    if (flag2 == 0){ //case where the command is not allowed
      printf("The command '%s' is not allowed.\n", buf);
      strcpy(buf, "Command not allowed\n");
      Rio_writen(connfd, buf, strlen(buf));
    }
    else{
      if ((pid = fork()) == 0) {   /* Child runs user job */
        printf("Fork/Execing the command %s on behalf of the user.\n", argv_for_shell[0]);
        Dup2(connfd, 1);

        if (execve(argv_for_shell[0], argv_for_shell, environ) < 0) {
          printf("%s: Command not found.\n", argv_for_shell[0]);
          exit(0);
        }
        printf("Finished execing\n");
      }
      /* Parent waits for foreground job to terminate */
      if (!bg) {
        int status;
        if (waitpid(pid, &status, 0) < 0)
          unix_error("waitfg: waitpid error");
        memset(&buf[0], 0, sizeof(buf)); //flush the buffer
        Rio_writen(connfd, buf, strlen(buf));
      }
      else{
        memset(&buf[0], 0, sizeof(buf)); //flush the buffer
        Rio_writen(connfd, buf, strlen(buf));
      }
      signal(SIGCHLD, reap_background);
    }
  } 

我之前发布了一个与此相关的问题,但它是由一个单独的问题引起的 - 请随时阅读该问题

我在 execve 下方添加了一条 printf 语句,以确认程序是否已到达该点,但实际上没有

shell 的输出看起来像

User k logging in from 127.0.0.1 at TCP port 1024.
User k successfully logged in.
User k sent the command '/bin/sleep 1&' to be executed.
Fork/Execing the command /bin/sleep on behalf of the user.

有什么想法吗?

【问题讨论】:

  • 函数:execve() 永远不会返回,除非新进程的创建失败。发布的代码似乎期望对execve() 的调用作为正常操作返回。
  • 这种线对:memset(&amp;buf[0], 0, sizeof(buf)); //flush the buffer Rio_writen(connfd, buf, strlen(buf)); 似乎有点“奇怪”,因为数组 buf[] 设置为所有 NUL 字节,然后使用 strlen(buf) 调用 RioWriten()strlen(buf) 将返回 0,因此尝试写入 0 个字节。但是我的研究 csapp.cs.cmu.edu/2e/ch10-preview.pdf> 表明 1) 拼写:rio_writen() 不是 `RioWriten(),那么您从哪里获得替代拼写?
  • bg 设置在哪里?也许用户输入的命令需要更多解析才能找到尾随的&amp; 来设置bg 变量。
  • 来自waitpid() 的手册页,请注意以下内容:“在终止子的情况下,执行等待允许系统释放与子关联的资源;如果等待不是执行,然后终止的孩子保持在“僵尸”状态(见下面的注释)。因此,发布的代码可以创建“僵尸”。可能不是你想要的。
  • @user3629249 为什么我有信号处理程序,并且 bg 设置正确

标签: c process background exec freeze


【解决方案1】:

据我所知,这是可以预料的。首先你fork(),然后你打印“代表用户分叉/执行命令/bin/sleep。”,然后你调用execve()。对execve() 的调用将永远不会返回(假设它成功),因为execve() 会替换当前程序。

因此这一行:

printf("Finished execing\n");

永远不会到达。

这正是您(大概)首先执行fork() 的原因。你应该做的是wait()ing 以完成fork()'d 过程。

还有,这条线是干什么用的?

signal(SIGCHLD, reap_background);

【讨论】:

  • 但我确实在下一个 if 语句中等待。这需要移动吗?
  • 对,但之后您不会打印任何内容,因此您粘贴的输出是预期的。
  • 你这么说很有趣,因为 gdb 显示行 if (!bg) 然后它冻结了。有什么想法吗?
  • bg 设置为什么?使用 gdb 调试 fork() 并非易事。阅读此链接:sourceware.org/gdb/onlinedocs/gdb/Forks.html
  • bg 设置为 1 -- 应该如此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2019-01-04
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多