0

I am trying to handle SIGINT in my C code but am getting the following output and I am unsure why

COMMAND->hello
COMMAND->world
COMMAND->test
COMMAND->^CI don't know! 
error reading the command: Interrupted system call

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <string.h>

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */


//The function used to handle the signal:
void handle_SIGINT(){
    printf("I don't know! \n");
    fflush(stdout);
}

/**
 * setup() reads in the next command line, separating it into distinct tokens
 * using whitespace as delimiters. setup() sets the args parameter as a 
 * null-terminated string.
 */

void setup(char inputBuffer[], char *args[], int *background)
{
    int length, /* # of characters in the command line */
        i,      /* loop index for accessing inputBuffer array */
        start,  /* index where beginning of next command parameter is */
        ct;     /* index of where to place the next parameter into args[] */

    ct = 0;

    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);

    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0){
        perror("error reading the command");
    exit(-1);           /* terminate with error code of -1 */
    }

    /* examine every character in the inputBuffer */
    for (i = 0; i < length; i++) { 
        switch (inputBuffer[i]){
        case ' ':
        case '\t' :               /* argument separators */
            if(start != -1){
                args[ct] = &inputBuffer[start];    /* set up pointer */
                ct++;
            }
            inputBuffer[i] = '\0'; /* add a null char; make a C string */
            start = -1;
            break;

        case '\n':                 /* should be the final char examined */
            if (start != -1){
                args[ct] = &inputBuffer[start];     
                ct++;
            }
            inputBuffer[i] = '\0';
            args[ct] = NULL; /* no more arguments to this command */
            break;

        case '&':
            *background = 1;
            inputBuffer[i] = '\0';
            break;

        default :             /* some other character */
            if (start == -1)
                start = i;
    } 
    }    
    args[ct] = NULL; /* just in case the input line was > 80 */ 
} 

int main(void)
{
    char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

    //Setup the signal handler
    struct sigaction handler;
    handler.sa_handler = handle_SIGINT;
    sigaction(SIGINT, &handler, NULL);


    while (1){            /* Program terminates normally inside setup */
    background = 0;
    printf("COMMAND->");
        fflush(0);
        setup(inputBuffer, args, &background);       /* get next command */

    /* the steps are:
     (1) fork a child process using fork()
     (2) the child process will invoke execvp()
     (3) if background == 0, the parent will wait, 
        otherwise returns to the setup() function. */

     //The fork
     pid_t pid = fork();

     //If it is the child, execute the code
     if(pid == 0){
            execvp(inputBuffer, args);
            exit(0);
        }
    if(background == 0){
        wait(NULL);
    }

    }
}

I would like to be able to return to the "command line" and continue checking for input.

  • Why are you unsure why you're getting that output? – user253751 Mar 30 '16 at 23:57
  • 4
    You can't call `printf` or `fflush` from a signal handler! – David Schwartz Mar 30 '16 at 23:58
  • http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler – fukanchik Mar 30 '16 at 23:59
  • I still get the same output even without a call to either printf or fflush – Tavish Wille Mar 31 '16 at 00:04
  • Read your own code. You do a pretty good job of catching errors emitted by system functions, and you observe error-handling behavior that your code is written to exhibit. Your code will tell you what function call must be failing, and a read of that function's docs in light of the `perror()` error message will surely be illuminating. – John Bollinger Mar 31 '16 at 00:38

1 Answers1

0

Your program is doing what you told it, i.e., exit when read returns any kind of error:

/* read what the user enters on the command line */
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);

start = -1;
if (length == 0)
    exit(0);
if (length < 0){
    perror("error reading the command");
    exit(-1);   // <----- Exit at any kind of error
}

From read(2) man page:

On error, -1 is returned, and errno is set appropriately. In this case, it is left unspecified whether the file position (if any) changes.

EINTR The call was interrupted by a signal before any data was read; see signal(7).

Just check after read whether the error was a signal interrupt (include errno.h):

length = 0;
while (length <= 0) {
    printf("COMMAND->");
    fflush(0);

    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);

    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if (length < 0 && errno != EINTR) {
        perror("error reading the command");
        exit(-1);           /* terminate with error code of -1 */
    }
}
José Rios
  • 86
  • 5