c -/bin/ sleep : invalid time interval ‘’

标签 c shell background exec sleep

解决方案 我不小心剪掉了 100

我正在编写一个客户端-服务器程序,其中服务器是一个简单的 shell

所有命令都有效,除了/bin/sleep - 即 rrsh>/bin/sleep 1&

我已经包含了整个 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);
    strcat(argv_for_shell[0], "\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){
      if (!strcmp(argv_for_shell[0], command)){
        flag2 = 1;
      }
    }
    Fclose(file);

    strtok(argv_for_shell[0], "\n");

    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);
        }
      }
      /* 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);
    }
  }
  Close(connfd);
}

当我执行/bin/sleep 1& 时,它应该移动到最后一个 else block 并向客户端写回一个空字符串

从调试中它陷入了一个奇怪的地方

93            if ((pid = fork()) == 0) {   /* Child runs user job */
(gdb) p bg
$1 = 1
(gdb) n
104           if (!bg) {
(gdb) Fork/Execing the command /bin/sleep on behalf of the user.
/bin/sleep: invalid time interval ‘’
Try '/bin/sleep --help' for more information.

你可以看到 bg == 1,所以我猜它卡在了 execve 代码中——这就是我收到这个错误的原因。但为什么 gdb 会移动到下一行呢?

最佳答案

从 sleep 中——帮助:

用法: sleep NUMBER[SUFFIX]... 或: sleep 选项 暂停 NUMBER 秒

也许您缺少 NUMBER/OPTION 参数?

关于c -/bin/ sleep : invalid time interval ‘’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119179/

相关文章:

复制到 C 中的 Null 位置并传递无效数据时崩溃

c - malloc 是确定性的吗?

shell - 使用将 csv 转换为 html 表

shell - zsh 并不总是扩展路径

css - div 背景颜色,悬停时更改

c - 编辑字符串时出现段错误?

python - 使用Python的ctypes调用涉及自定义类型的函数

shell - 在 OS X 上使用 sed 和 shell 变量进行路径替换

html - Bootstrap 2列,2个背景扩展

css - 使两列 div 等高