2015-12-06 3 views
0

SOLUTION j'épissage de 100 par accident/bin/sommeil: intervalle de temps non valide ''

J'écris un programme client-serveur sur lequel le serveur est un simple shell

toutes les commandes fonctionnent, sauf pour/bin/sommeil - c'est rrsh> /bin/sleep 1&

J'ai inclus tout mon code shell - qui comprend: - vérifier si la commande est valide de la liste des virgules valides PDN - puis l'exécute si elle est

mon code shell ressemble à ceci:

 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); 
} 

quand j'Execute/bin/sleep 1 & il doit se déplacer sur le dernier bloc d'autre et écrire de nouveau une chaîne vide au client

de débogage il est coincé dans un endroit bizarre

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. 

vous pouvez voir que == bg 1, donc je gu Il est bloqué dans le code execve - c'est pourquoi je reçois cette erreur. Mais pourquoi gdb passerait-il à la ligne suivante?

+1

Il semble que '/ bin/sleep' soit appelé avec une chaîne vide pour le second argument. À quoi ressemble 'argv_for_shell' juste avant la' fork '? – dbush

+0

@dbush désolé j'ai oublié d'inclure mon entrée de commande, je ne manque pas le numéro – kendall

+0

Si je cours '/ bin/sleep" "' '' get '/ bin/sleep: intervalle de temps invalide" "' ' – dbush

Répondre

1

De --help sommeil:

Usage: sleep NUMBER[SUFFIX]... or: sleep OPTION Pause for NUMBER seconds

Peut-être vous manque des arguments NUMÉRO/OPTION?

+0

Désolé, j'ai oublié de dire que je ne manque pas le nombre – kendall