dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / t / t-subproc.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * t-subproc.c - test sub-process module
4 *
5 * Copyright © 2011 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include <compat.h>
23
24#include <sys/types.h>
25
26#include <fcntl.h>
27#include <signal.h>
28#include <unistd.h>
29#include <stdlib.h>
30
31#include <dpkg/test.h>
32#include <dpkg/subproc.h>
33
34static void
35test_subproc_fork(void)
36{
37 struct sigaction sa;
38 pid_t pid;
39 int ret;
40
41 memset(&sa, 0, sizeof(sa));
42 sa.sa_handler = SIG_DFL;
43 sigaction(SIGINT, &sa, NULL);
44 sigaction(SIGTERM, &sa, NULL);
45 sigaction(SIGPIPE, &sa, NULL);
46
47 /* Test exit(). */
48 pid = subproc_fork();
49 if (pid == 0)
50 exit(0);
51 ret = subproc_reap(pid, "subproc exit pass", SUBPROC_RETERROR);
52 test_pass(ret == 0);
53
54 pid = subproc_fork();
55 if (pid == 0)
56 exit(128);
57 ret = subproc_reap(pid, "subproc exit fail", SUBPROC_RETERROR);
58 test_pass(ret == 128);
59
60 /* Test signals. */
61 pid = subproc_fork();
62 if (pid == 0)
63 raise(SIGINT);
64 ret = subproc_reap(pid, "subproc signal", SUBPROC_WARN);
65 test_pass(ret == -1);
66
67 pid = subproc_fork();
68 if (pid == 0)
69 raise(SIGTERM);
70 ret = subproc_reap(pid, "subproc signal", SUBPROC_WARN);
71 test_pass(ret == -1);
72
73 pid = subproc_fork();
74 if (pid == 0)
75 raise(SIGPIPE);
76 ret = subproc_reap(pid, "subproc SIGPIPE",
77 SUBPROC_WARN | SUBPROC_NOPIPE);
78 test_pass(ret == 0);
79
80 pid = subproc_fork();
81 if (pid == 0)
82 raise(SIGPIPE);
83 ret = subproc_reap(pid, "subproc SIGPIPE", SUBPROC_WARN);
84 test_pass(ret == -1);
85}
86
87TEST_ENTRY(test)
88{
89 int fd;
90
91 test_plan(6);
92
93 /* XXX: Shut up stderr, we don't want the error output. */
94 fd = open("/dev/null", O_RDWR);
95 if (fd < 0)
96 test_bail("cannot open /dev/null");
97 dup2(fd, 2);
98
99 test_subproc_fork();
100}