Test cases in C for WIFSIGNALED, WIFSTOPPED, WIFCONTINUED
Asked Answered
B

4

5

I'm playing with waitpid() and signal() and I'm looking for reliable test cases for returning WIFSIGNALED(status) = WIFSTOPPED(status) = WIFCONTINUED (status) = true but can't find any...

Care to tell me how can I make sure those return true so I can debug my code?

Also, a few hints about what signals should I catch with signal() to test those macros would be helpful...

Bluefield answered 25/5, 2009 at 19:55 Comment(0)
R
7
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

#define NELEMS(x) (sizeof (x) / sizeof (x)[0])

static void testsignaled(void) {
   kill(getpid(), SIGINT);
}

static void teststopped(void) {
   kill(getpid(), SIGSTOP);
}

static void testcontinued(void) {
   kill(getpid(), SIGSTOP);
   /* Busy-work to keep us from exiting before the parent waits.
    * This is a race.
    */
   alarm(1);
   while(1) {}
}

int main(void) {
   void (*test[])(void) = {testsignaled, teststopped, testcontinued};
   pid_t pid[NELEMS(test)];
   int i, status;
   for(i = 0; i < sizeof test / sizeof test[0]; ++i) {
      pid[i] = fork();
      if(0 == pid[i]) {
         test[i]();
         return 0;
      }
   }
   /* Pause to let the child processes to do their thing.
    * This is a race.
    */
   sleep(1);
   /* Observe the stoppage of the third process and continue it. */
   wait4(pid[2], &status, WUNTRACED, 0);
   kill(pid[2], SIGCONT);
   /* Wait for the child processes. */
   for(i = 0; i < NELEMS(test); ++i) {
      wait4(pid[i], &status, WCONTINUED | WUNTRACED, 0);
      printf("%d%s%s%s\n", i, WIFCONTINUED(status) ? " CONTINUED" : "", WIFSIGNALED(status) ? " SIGNALED" : "", WIFSTOPPED(status) ? " STOPPED" : "");
   }
   return 0;
}
Rhizome answered 26/5, 2009 at 0:26 Comment(0)
H
3

Handling WIFSIGNALED is easy. The child process can commit suicide with the kill() system call. You can also check for core dumps - some signals create them (SIGQUIT, IIRC); some signals do not (SIGINT).

Handling WIFSTOPPED may be harder. The simple step to try is for the child to send itself SIGSTOP with the kill() system call again. Actually, I think that should work. Note that you may want to check on SIGTTIN and SIGTTOU and SIGTSTOP - I believe they count for WIFSTOPPED. (There's also a chance that SIGSTOP only works sanely when sent by a debugger to a process it is running via the non-POSIX system call, ptrace().)

Handling WIFCONTINUED is something that I think the parent has to do; after you detect a process has been stopped, your calling code should make it continue by sending it a SIGCONT signal (kill() again). The child can't deliver this itself; it has been stopped. Again, I'm not sure whether there are extra wrinkles to worry about - probably.

Hartwell answered 25/5, 2009 at 23:57 Comment(0)
C
1

A framework something like the below will allow you check the results of the wait() and waitpid() calls.

pid_t pid = fork();

if (pid == 0) {
    /* child */
    sleep(200);
}
else {
    /* parent */
    kill(pid, SIGSTOP);

    /* do wait(), waitpid() stuff */
}

You do not actually have to catch the signals (using signal() or related function) that are sent. signal() installs a handler that overrides the default behavior for the specific signal - so if you want to check for a signal terminating your process, pick one that has that default behavior - "man -s7 signal" will give you details a signal's default behavior.

For the macros you have mentioned use SIGSTOP for WIFSTOPPED(status), SIGCONT for WIFCONTINUED (status) and SIGINT for WIFSIGNALED(status)

If you want more flexibility for testing, you could use kill (see "man kill") to send signals to your process. kill -l will list all the signals that can be sent.

Captain answered 25/5, 2009 at 21:31 Comment(2)
The same as I commented above, what I'm most looking for is the specific signals to test those macros. There's a problem though, how can I check the WIFSTOPPED if I can't handle SIGSTOP?Bluefield
@Ricardo, you can't handle SIGSTOP in the child; WIFSTOPPED is meant for use in the parent: if( pid != 0 && WIFSTOPPED( status ) { /* do something */ }Pyuria
A
-1

in your tests you can fork() and send specific signal to your child processes? In this scenario your child processes are test cases?

EDIT

my answer is about coding a C test. you fork, get the pid of your child process (the process with signal handlers installed), then you can send signal to it by using kill(2). In this way you can test the exit status

Amersham answered 25/5, 2009 at 20:30 Comment(1)
Of course I already have that, I'm looking for the specific signals, which one will produce which macro equal to true... How can I send them through the bash to a specific process so I can test them, that's what I want to know.Bluefield

© 2022 - 2024 — McMap. All rights reserved.