【发布时间】: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(&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设置在哪里?也许用户输入的命令需要更多解析才能找到尾随的&来设置bg变量。 -
来自
waitpid()的手册页,请注意以下内容:“在终止子的情况下,执行等待允许系统释放与子关联的资源;如果等待不是执行,然后终止的孩子保持在“僵尸”状态(见下面的注释)。因此,发布的代码可以创建“僵尸”。可能不是你想要的。 -
@user3629249 为什么我有信号处理程序,并且 bg 设置正确
标签: c process background exec freeze