@@ -30,31 +30,73 @@ void handler1(int sig) {
3030 global = 1 ;
3131}
3232
33+ // Test kill() on the current process, sending
34+ // "pid" to kill should cause this process to be interrupted
35+ // and signal handler for SIGUSR1 should already be installed
36+ void test_signal_self (pid_t pid){
37+ int r;
38+ char output[64 ];
39+
40+ global = 0 ;
41+ r = kill (pid, SIGUSR1);
42+ snprintf (output, 64 , " kill SIGUSR1 and pid %d succeeds" , pid);
43+ report (r == 0 , output);
44+
45+ for (int i = 0 ; i < 100 ; i++){
46+ if (global == 1 ) break ;
47+ usleep (10000 );
48+ }
49+ snprintf (output, 64 , " global now 1, process correctly interrupted with pid %d" , pid);
50+ report (global == 1 , output);
51+ }
52+
53+ // test kill() edges cases, pid should be a valid pid
54+ void test_edge_cases (pid_t pid){
55+ int r;
56+ char output[64 ];
57+
58+ // signal 0 always succeeds with valid pid
59+ r = kill (pid, 0 );
60+ snprintf (output, 64 , " kill succeeds with pid %d and signal 0" , pid);
61+ report (r == 0 , output);
62+
63+ // kill with invalid signal number
64+ r = kill (pid, -2 );
65+ snprintf (output, 64 , " kill with pid %d and invalid signal number" , pid);
66+ report (r == -1 && errno == EINVAL, output);
67+
68+ // another invalid signal number
69+ r = kill (pid, 12345 );
70+ snprintf (output, 64 , " kill with pid %d and invalid signal number" , pid);
71+ report (r == -1 && errno == EINVAL, output);
72+ }
73+
3374int main (int ac, char ** av)
3475{
3576 // Test kill() of current process:
3677 report (global == 0 , " 'global' initially 0" );
3778 auto sr = signal (SIGUSR1, handler1);
3879 report (sr != SIG_ERR, " set SIGUSR1 handler" );
80+
81+ // pid 0 = "all processes whose process group ID is
82+ // equal to process group ID of sender"
83+ test_signal_self (0 );
84+
85+ // pid -1 = "all processes for which the calling process
86+ // has permission to send signals"
87+ test_signal_self (-1 );
88+
89+ // our own pid
90+ test_signal_self (getpid ());
91+
92+ // Test various edge cases for kill() with various pids
93+ test_edge_cases (0 );
94+ test_edge_cases (-1 );
95+ test_edge_cases (getpid ());
96+
3997 int r;
40- r = kill (0 , SIGUSR1);
41- report (r == 0 , " kill SIGUSR1 succeeds" );
42- for (int i=0 ; i<100 ; i++) {
43- if (global == 1 ) break ;
44- usleep (10000 );
45- }
46- report (global == 1 , " 'global' is now 1" );
47- // Test various edge cases for kill():
48- r = kill (0 , 0 );
49- report (r == 0 , " kill with signal 0 succeeds (and does nothing)" );
50- r = kill (-1 , 0 );
51- report (r == 0 , " kill of pid -1 is also fine" );
5298 r = kill (17171717 , 0 );
5399 report (r == -1 && errno == ESRCH, " kill of non-existant process" );
54- r = kill (0 , -2 );
55- report (r == -1 && errno == EINVAL, " kill with invalid signal number" );
56- r = kill (0 , 12345 );
57- report (r == -1 && errno == EINVAL, " kill with invalid signal number" );
58100
59101 // Test alarm();
60102 global = 0 ;
@@ -128,27 +170,27 @@ int main(int ac, char** av)
128170 global = 0 ;
129171 r = sigaction (SIGUSR1, &act, nullptr );
130172 report (r == 0 , " set SIGUSR1 handler" );
131- r = kill ( 0 , SIGUSR1);
132- report (r == 0 , " kill SIGUSR1 succeeds " );
133- for ( int i= 0 ; i< 100 ; i++) {
134- if (global == 1 ) break ;
135- usleep ( 10000 );
136- }
137- report (global == 1 , " 'global' is now 1 " );
173+
174+ // ensure signal handler is called when kill is
175+ // called with any of these pids
176+ test_signal_self ( 0 ) ;
177+ test_signal_self (- 1 );
178+ test_signal_self ( getpid ());
179+
138180 struct sigaction oldact;
139181 r = sigaction (SIGUSR1, nullptr , &oldact);
140182 report (r == 0 && oldact.sa_handler == handler1, " without SA_RESETHAND, signal handler is not reset" );
141183 act.sa_flags = SA_RESETHAND;
142184 global = 0 ;
143185 r = sigaction (SIGUSR1, &act, nullptr );
144186 report (r == 0 , " set SIGUSR1 handler with SA_RESETHAND" );
145- r = kill ( 0 , SIGUSR1);
146- report (r == 0 , " kill SIGUSR1 succeeds " );
147- for ( int i= 0 ; i< 100 ; i++) {
148- if (global == 1 ) break ;
149- usleep ( 10000 );
150- }
151- report (global == 1 , " 'global' is now 1 " );
187+
188+ // ensure signal handler is called when kill is
189+ // called with any of these pids
190+ test_signal_self ( 0 ) ;
191+ test_signal_self (- 1 );
192+ test_signal_self ( getpid ());
193+
152194 r = sigaction (SIGUSR1, nullptr , &oldact);
153195 report (r == 0 && oldact.sa_handler == SIG_DFL, " with SA_RESETHAND, signal handler is reset" );
154196
0 commit comments