Reputation: 41
I'm writing a client-server program where the server is a simple shell
all commands are working, except for /bin/sleep -- that is, rrsh> /bin/sleep 1&
I've included my entire shell code -- which includes:
my shell code looks like this:
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);
}
when I execute /bin/sleep 1& it should move onto the last else block and write back an empty string to the client
from debugging it's getting stuck in a weird place
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.
you can see that bg == 1, so I would guess it's hung up in the execve code -- which is why I'm getting that error. But why would gdb move onto the next line?
Upvotes: 0
Views: 8698
Reputation: 55610
Self-answer removed from question:
SOLUTION I was splicing off the 100 by accident
Upvotes: 0
Reputation: 696
From sleep --help:
Usage: sleep NUMBER[SUFFIX]...
or: sleep OPTION
Pause for NUMBER seconds
Perhaps you are missing NUMBER/OPTION arguments?
Upvotes: 1