File: test_child.c

package info (click to toggle)
libnih 1.0.3-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,824 kB
  • sloc: ansic: 188,730; sh: 11,217; makefile: 1,117; yacc: 291; xml: 239; sed: 16
file content (682 lines) | stat: -rw-r--r-- 15,683 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* libnih
 *
 * test_child.c - test suite for nih/child.c
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <nih/test.h>

#if HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#endif /* HAVE_VALGRIND_VALGRIND_H */

#include <sys/ptrace.h>

#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/string.h>
#include <nih/child.h>


static int handler_called = 0;
static void *last_data = NULL;
static pid_t last_pid;
static NihChildEvents last_event = -1;
static int last_status;

static void
my_handler (void           *data,
	    pid_t           pid,
	    NihChildEvents  event,
	    int             status)
{
	handler_called++;
	last_data = data;
	last_pid = pid;
	last_event = event;
	last_status = status;
}

void
test_add_watch (void)
{
	NihChildWatch *watch;

	TEST_FUNCTION ("nih_child_add_watch");
	nih_child_poll ();


	/* Check that we can add a watch on a specific pid, and that the
	 * structure is filled in correctly and part of a list.
	 */
	TEST_FEATURE ("with pid");
	TEST_ALLOC_FAIL {
		watch = nih_child_add_watch (NULL, getpid (), NIH_CHILD_EXITED,
					     my_handler, &watch);

		if (test_alloc_failed) {
			TEST_EQ_P (watch, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (watch, sizeof (NihChildWatch));
		TEST_EQ (watch->pid, getpid ());
		TEST_EQ (watch->events, NIH_CHILD_EXITED);
		TEST_EQ_P (watch->handler, my_handler);
		TEST_EQ_P (watch->data, &watch);
		TEST_LIST_NOT_EMPTY (&watch->entry);

		nih_free (watch);
	}


	/* Check that we can add a watch on a pid of -1, which represents
	 * any child.
	 */
	TEST_FEATURE ("with -1 for pid");
	TEST_ALLOC_FAIL {
		watch = nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
					     my_handler, &watch);

		if (test_alloc_failed) {
			TEST_EQ_P (watch, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (watch, sizeof (NihChildWatch));
		TEST_EQ (watch->pid, -1);
		TEST_EQ (watch->events, NIH_CHILD_ALL);
		TEST_EQ_P (watch->handler, my_handler);
		TEST_EQ_P (watch->data, &watch);
		TEST_LIST_NOT_EMPTY (&watch->entry);

		nih_free (watch);
	}
}


void
test_poll (void)
{
	NihChildWatch *watch;
	siginfo_t      siginfo;
	pid_t          pid, child;
	unsigned long  data;
	char           corefile[PATH_MAX + 1];

	TEST_FUNCTION ("nih_child_poll");

	/* Check that when a child exits normally, the handler receives
	 * an exited event and the zero status code and is then removed from
	 * the list and freed.
	 */
	TEST_FEATURE ("with normal termination");

	TEST_CHILD (pid) {
		exit (0);
	}

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_EXITED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_EXITED);
	TEST_EQ (last_status, 0);
	TEST_FREE (watch);


	/* Check that when a child exits with a non-zero status code, the
	 * reaper receives an exited event and the status code and is then
	 * removed from the list and freed.
	 */
	TEST_FEATURE ("with normal non-zero termination");

	TEST_CHILD (pid) {
		exit (123);
	}

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_EXITED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_EXITED);
	TEST_EQ (last_status, 123);
	TEST_FREE (watch);


	/* Check that when a child is killed by a signal, the reaper receives
	 * a killed event with the signal in the status field before being
	 * removed from the list and freed.
	 */
	TEST_FEATURE ("with termination by signal");

	TEST_CHILD (pid) {
		pause ();
	}

	watch = nih_child_add_watch (NULL, pid,
				     NIH_CHILD_KILLED | NIH_CHILD_DUMPED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_KILLED);
	TEST_EQ (last_status, SIGTERM);
	TEST_FREE (watch);


	/* Check that when a child is killed by aborting, the reaper receives
	 * a dumped event with the signal in the status field before being
	 * removed from the list and freed.
	 */
	TEST_FEATURE ("with termination by abort");

	TEST_CHILD (pid) {
		abort ();
	}

	watch = nih_child_add_watch (NULL, pid,
				     NIH_CHILD_KILLED | NIH_CHILD_DUMPED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	/* We might get killed if we never dumped core... fiddling with
	 * the limit doesn't help, since we might be under gdb and that
	 * never lets us dump core.
	 */
	if (last_event != NIH_CHILD_KILLED)
		TEST_EQ (last_event, NIH_CHILD_DUMPED);

	TEST_EQ (last_status, SIGABRT);
	TEST_FREE (watch);

	unlink ("core");

	sprintf (corefile, "core.%d", pid);
	unlink (corefile);

	sprintf (corefile, "vgcore.%d", pid);
	unlink (corefile);


	/* Check that when a child emits the stopped signal, the reaper
	 * receives a stopped event with nothing relevant in the status field.
	 * It should not be removed from the list, since the child hasn't
	 * gone anyway.
	 */
	TEST_FEATURE ("with stopped child");

	TEST_CHILD (pid) {
		raise (SIGSTOP);
		pause ();
		exit (0);
	}

	watch = nih_child_add_watch (NULL, pid,
				     NIH_CHILD_STOPPED | NIH_CHILD_CONTINUED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_STOPPED);
	TEST_EQ (last_status, SIGSTOP);
	TEST_NOT_FREE (watch);


	/* Check that when the child is continued again, the reaper
	 * receives a continued event with nothing relevant in the status
	 * field.  It should still not be removed from the list since the
	 * child still hasn't gone away.
	 */
	TEST_FEATURE ("with continued child");
	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	kill (pid, SIGCONT);

	waitid (P_PID, pid, &siginfo, WCONTINUED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_CONTINUED);
	TEST_EQ (last_status, SIGCONT);
	TEST_NOT_FREE (watch);

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED);
	nih_free (watch);


	/* Check that a signal raised from a traced child causes the reaper
	 * to be called with a traced event and the event in the status
	 * field.  It should not be removed from the list since the child
	 * hasn't gone away.
	 */
	TEST_FEATURE ("with signal from traced child");

	TEST_CHILD (pid) {
		assert0 (ptrace (PTRACE_TRACEME, 0, NULL, NULL));

		raise (SIGSTOP);
		raise (SIGCHLD);
		pause ();
		exit (0);
	}

	waitid (P_PID, pid, &siginfo, WSTOPPED);

	assert0 (ptrace (PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACESYSGOOD));
	assert0 (ptrace (PTRACE_CONT, pid, NULL, SIGCONT));

	waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);

	/* ptrace might catch the SIGCONT emitted with PTRACE_CONT, catch it 
	   and wait for the next event.
	 */
	if (siginfo.si_code == CLD_TRAPPED && siginfo.si_status == SIGCONT) {
		assert0 (ptrace (PTRACE_CONT, pid, NULL, NULL));
		waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);
	}

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_TRAPPED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_TRAPPED);
	TEST_EQ (last_status, SIGCHLD);
	TEST_NOT_FREE (watch);

	assert0 (ptrace (PTRACE_DETACH, pid, NULL, 0));

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED);
	nih_free (watch);


#if HAVE_VALGRIND_VALGRIND_H
	/* These tests fail when running under valgrind.
	 */
	if (! RUNNING_ON_VALGRIND) {
#endif
	/* Check that when a traced child forks it causes the reaper
	 * to be called with a ptrace event and the fork event in the
	 * status field.  It should not be removed from the list since the
	 * child hasn't gone away.
	 */
	TEST_FEATURE ("with fork by traced child");

	TEST_CHILD (pid) {
		assert0 (ptrace (PTRACE_TRACEME, 0, NULL, NULL));

		raise (SIGSTOP);

		child = fork ();
		assert (child >= 0);

		pause ();

		exit (0);
	}

	waitid (P_PID, pid, &siginfo, WSTOPPED);

	assert0 (ptrace (PTRACE_SETOPTIONS, pid, NULL,
			 PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK));
	assert0 (ptrace (PTRACE_CONT, pid, NULL, SIGCONT));

	/* Wait for ptrace to stop the parent (signalling the fork) */
	waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);

	/* ptrace might catch the SIGCONT emitted with PTRACE_CONT, catch it
	   and wait for the next event.
	 */
	if (siginfo.si_code == CLD_TRAPPED && siginfo.si_status == SIGCONT) {
		assert0 (ptrace (PTRACE_CONT, pid, NULL, NULL));
		waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);
	}

	/* Will be able to get the child pid now, we have to do it here
	 * because we want to wait on it to ensure the test is synchronous;
	 * otherwise nih_child_poll() could actually eat the child event
	 * before it returns -- normally we'd do this inside the handler,
	 * so things would probably work (we iter the handlers for each
	 * event, so you can add one).
	 */
	data = 0;
	assert0 (ptrace (PTRACE_GETEVENTMSG, pid, NULL, &data));
	assert (data != 0);
	child = (pid_t)data;

	/* Wait for ptrace to stop the child, otherwise it might not be
	 * ready for us to actually detach from.
	 */
	waitid (P_PID, child, &siginfo, WSTOPPED | WNOWAIT);

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_PTRACE,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_PTRACE);
	TEST_EQ (last_status, PTRACE_EVENT_FORK);
	TEST_NOT_FREE (watch);

	assert0 (ptrace (PTRACE_DETACH, child, NULL, SIGCONT));
	kill (child, SIGTERM);

	assert0 (ptrace (PTRACE_DETACH, pid, NULL, SIGCONT));
	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED);
	nih_free (watch);


	/* Check that when a traced child execs it causes the reaper
	 * to be called with a ptrace event and the exec event in the
	 * status field.  It should not be removed from the list since the
	 * child hasn't gone away.
	 */
	TEST_FEATURE ("with exec by traced child");

	TEST_CHILD (pid) {
		assert0 (ptrace (PTRACE_TRACEME, 0, NULL, NULL));

		raise (SIGSTOP);

		execl ("/bin/true", "true", NULL);
		exit (255);
	}

	waitid (P_PID, pid, &siginfo, WSTOPPED);

	assert0 (ptrace (PTRACE_SETOPTIONS, pid, NULL,
			 PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC));
	assert0 (ptrace (PTRACE_CONT, pid, NULL, SIGCONT));

	waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);

	/* ptrace might catch the SIGCONT emitted with PTRACE_CONT, catch it
	   and wait for the next event.
	 */
	if (siginfo.si_code == CLD_TRAPPED && siginfo.si_status == SIGCONT) {
		assert0 (ptrace (PTRACE_CONT, pid, NULL, NULL));
		waitid (P_PID, pid, &siginfo, WSTOPPED | WNOWAIT);
	}

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_PTRACE,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_PTRACE);
	TEST_EQ (last_status, PTRACE_EVENT_EXEC);
	TEST_NOT_FREE (watch);

	assert0 (ptrace (PTRACE_DETACH, pid, NULL, SIGCONT));

	waitid (P_PID, pid, &siginfo, WEXITED);
	nih_free (watch);
#if HAVE_VALGRIND_VALGRIND_H
	}
#endif


	/* Check that we can watch for events from any process, which
	 * shouldn't be freed when the child dies.
	 */
	TEST_FEATURE ("with generic watcher");

	TEST_CHILD (pid) {
		pause ();
	}

	watch = nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_TRUE (handler_called);
	TEST_EQ (last_pid, pid);
	TEST_EQ (last_event, NIH_CHILD_KILLED);
	TEST_EQ (last_status, SIGTERM);
	TEST_NOT_FREE (watch);

	nih_free (watch);


	/* Check that if we poll with an unknown pid, and no catch-all,
	 * nothing is triggered and the watch is not removed.
	 */
	TEST_FEATURE ("with pid-specific watcher and wrong pid");

	TEST_CHILD (pid) {
		pause ();
	}

	watch = nih_child_add_watch (NULL, pid - 1, NIH_CHILD_ALL,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_FALSE (handler_called);
	TEST_NOT_FREE (watch);

	nih_free (watch);


	/* Check that if we poll with a known pid but for a different event
	 * set, nothing is triggered and the watch is not removed.
	 */
	TEST_FEATURE ("with event-specific watcher and wrong event");

	TEST_CHILD (pid) {
		pause ();
	}

	watch = nih_child_add_watch (NULL, pid, NIH_CHILD_STOPPED,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	last_data = NULL;
	last_pid = 0;
	last_event = -1;
	last_status = 0;

	kill (pid, SIGTERM);
	waitid (P_PID, pid, &siginfo, WEXITED | WNOWAIT);

	nih_child_poll ();

	TEST_FALSE (handler_called);
	TEST_NOT_FREE (watch);

	nih_free (watch);


	/* Check that a poll when nothing has died does nothing. */
	TEST_FEATURE ("with nothing dead");

	TEST_CHILD (pid) {
		pause ();
	}

	watch = nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
				     my_handler, &watch);

	TEST_FREE_TAG (watch);

	handler_called = 0;
	nih_child_poll ();

	TEST_FALSE (handler_called);
	TEST_NOT_FREE (watch);

	kill (pid, SIGTERM);
	waitpid (pid, NULL, 0);


	/* Check that a poll when there are no child processes does nothing */
	TEST_FEATURE ("with no children");
	handler_called = 0;
	nih_child_poll ();

	TEST_FALSE (handler_called);
	TEST_NOT_FREE (watch);

	nih_free (watch);
}


int
main (int   argc,
      char *argv[])
{
	test_add_watch ();
	test_poll ();

	return 0;
}