1#include <stdio.h>
2#include <unistd.h>
3#include <signal.h>
4#include <pthread.h>
5#include <assert.h>
6
7#include <sys/wait.h>
8
9void *
10slavethread(void *arg)
11{
12    sigset_t sigmask;
13
14    if (sigfillset(&sigmask))
15    {
16        fprintf(stderr, "Error line %u\n", __LINE__);
17        _exit(255);
18    }
19
20    if (pthread_sigmask(SIG_UNBLOCK, &sigmask, 0))
21    {
22        fprintf(stderr, "Error line %u\n", __LINE__);
23        _exit(255);
24    }
25
26    while (1)
27        sleep(1);
28}
29
30void
31childprocess()
32{
33    pthread_t slave;
34
35    if (pthread_create(&slave, 0, &slavethread, 0))
36    {
37        fprintf(stderr, "Error line %u\n", __LINE__);
38        _exit(255);
39    }
40
41    while (1)
42        sleep(1);
43}
44
45int main(int argc, char **argv)
46{
47    sigset_t sigmask;
48
49    if (sigfillset(&sigmask))
50    {
51        fprintf(stderr, "Error line %u\n", __LINE__);
52        return 255;
53    }
54
55    if (pthread_sigmask(SIG_BLOCK, &sigmask, 0))
56    {
57        fprintf(stderr, "Error line %u\n", __LINE__);
58        return 255;
59    }
60
61    int childpid = fork();
62
63    if (-1 == childpid)
64    {
65        fprintf(stderr, "Error line %u\n", __LINE__);
66        return 255;
67    }
68
69    if ( ! childpid)
70        childprocess();
71
72    if (kill(childpid, SIGTERM))
73    {
74        fprintf(stderr, "Error line %u\n", __LINE__);
75        return 255;
76    }
77
78    int status;
79    if (childpid != waitpid(childpid, &status, 0))
80    {
81        fprintf(stderr, "Error line %u\n", __LINE__);
82        return 255;
83    }
84
85    assert(WIFSIGNALED(status));
86
87    fprintf(stderr, "Signal %d\n", WTERMSIG(status));
88    assert(WTERMSIG(status) == SIGTERM);
89
90    return 0;
91}
92