我有一个客户端服务器程序,其中服务器是一个简单的shell
命令工作正常,参数传递正常,但我认为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.
任何想法?
答案 0 :(得分:2)
据我所见,这是可以预期的。首先是fork()
,然后代表用户打印&#34; Fork /执行命令/ bin / sleep。&#34;,然后调用execve()
。对execve()
的调用永远不会返回(假设成功)execve()
替换当前程序。
因此这一行:
printf("Finished execing\n");
永远不会到达。
这正是你(大概)首先做fork()
的原因。你应该做的是wait()
fork()
&#39; d过程完成。
另外,这条线是什么?
signal(SIGCHLD, reap_background);