Search code examples
clinuxunit-testingposixkill

Unit testing kill command


So I am trying to do something bad and dirty ;)

I want to call kill(0, SIGKILL) in my check unittest to kill child processes I launched with this test.

ck_assert_int_eq(magic(13), 13); //<- success, but I cannot stop magic now

if I do

ck_assert_int_eq(kill(0, SIGKILL), 0); 

I get "test: (after this point) Received signal 9 (Killed)"

Are there ways around it? kill(0, SIGKILL) also done in the actual code, so I think if I try to call destruction function from my test, I am going end with the same error.


Solution

  • int kill(pid_t pid, int sig)

    If pid equals 0, then sig is sent to every process in the process group of the calling process.

    Source: man 2 kill

    You need call kill( child_pid, SIGKILL), because child_pid == 0 will kill parent + child.

    pid_t fork(void);

    Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

    Source: man 3 fork