Ram
Ram

Reputation: 1213

Testing EINTR signal

In my program, I have a system call 'sendmsg()'. I want to test what will happen if this system call gets interrupted. How should I do it?

int test(args) {
          -----
          /* I can use GDB and stop at this point */  
          n = sendmsg(h, send_msg, 0);
          ----
          return n; 
 }

 int test_caller(args) {
           int a, err;
           a = test(arg);
           if (a != what_i_am_expecting) {
               err = error;
               switch (err) {
                   case EINTR:
                       syslog(LOG_ERR, "I WANT TO SEE THIS LOG");
                       break;
                   default:
                }
            } else printf("Everything went well\n");
    return 0;
   } 

In this same function I have registered a signal handler as follows:

1366         struct sigaction sa;
1367         
1368         memset (&sa, '\0', sizeof(sa));
1369         sa.sa_handler = sighdl;
1370         sa.sa_flags = 0; 
1371         (void)sigaction(SIGINT, &sa, NULL);

With this handler:

1349 static void
1350 sighdl(int signo)
1351 {
1352         int i = 0;
1353         syslog(LOG_ERR, "got signal %d", signo);
1354         for (i = 0; i < 100; i++) {
1355         }
1356 }

My idea is to break in test() function before calling sendmsg(), then send sigint to pid. But not sure with this signa;, does it go to EINTR case in test-caller.

Please help!

Upvotes: 1

Views: 836

Answers (1)

SKi
SKi

Reputation: 8476

Usually sendmsg call is quick. So it hard to signal during it. But there is a way:

Depending of protocol sendmsg will block if the queue of receiver is full. So, fill the queue some way, and then press CTRL+C when your program is stuck in the sendmsg call (CTRL-C causes SIGINT).

Because you catch SIGINT, program will not exit. The sendmsg will return with code -1 and errno EINTR.

Upvotes: 1

Related Questions