summaryrefslogtreecommitdiffstats
path: root/tests/helpers/test_sigreceive.c
blob: 12b563a0ef8a4c51070691cdd40fdaa5ace5dc2b (plain) (blame)
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
/*
 * test_sigreceive - wait for signal and exit with value of it
 *
 * Written by Sami Kerola <kerolasa@iki.fi>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would 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 <err.h>
#include <getopt.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>

#include "strutils.h"

#define TEST_SIGRECEIVE_FAILURE 0

static void __attribute__((__noreturn__)) usage(FILE *out)
{
	fputs("Usage: test_sigreceive [-s|--setuid <login|uid>]\n", out);
	exit(TEST_SIGRECEIVE_FAILURE);
}

static __attribute__((__noreturn__))
void exiter(int signo __attribute__((__unused__)),
	    siginfo_t *info,
	    void *context __attribute__((__unused__)))
{
	int ret = info->si_signo;

	if (info->si_code == SI_QUEUE && info->si_value.sival_int != 0)
		ret = info->si_value.sival_int;
	_exit(ret);
}

int main(int argc, char **argv)
{
	struct sigaction sigact;
	fd_set rfds;
	struct timeval timeout;
	char *user = NULL;
	int c;

	static const struct option longopts[] = {
		{"setuid", required_argument, NULL, 's'},
		{NULL, 0, NULL, 0}
	};

	while ((c = getopt_long(argc, argv, "s:h", longopts, NULL)) != -1)
		switch (c) {
		case 's':
			user = optarg;
			break;
		case 'h':
			usage(stdout);
		default:
			usage(stderr);
		}

	if (user) {
		struct passwd *pw;
		uid_t uid;

		pw = getpwnam(user);
		if (pw)
			uid = pw->pw_uid;
		else
			uid = strtou32_or_err(user, "failed to parse uid");
		if (setuid(uid) < 0)
			err(TEST_SIGRECEIVE_FAILURE, "setuid failed");
	}

	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = SA_SIGINFO;
	sigact.sa_sigaction = exiter;
	timeout.tv_sec = 5;
	timeout.tv_usec = 0;

	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGQUIT, &sigact, NULL);
	sigaction(SIGILL, &sigact, NULL);
#ifdef SIGTRAP
	sigaction(SIGTRAP, &sigact, NULL);
#endif
	sigaction(SIGABRT, &sigact, NULL);
#ifdef SIGIOT
	sigaction(SIGIOT, &sigact, NULL);
#endif
#ifdef SIGEMT
	sigaction(SIGEMT, &sigact, NULL);
#endif
#ifdef SIGBUS
	sigaction(SIGBUS, &sigact, NULL);
#endif
	sigaction(SIGFPE, &sigact, NULL);
	sigaction(SIGUSR1, &sigact, NULL);
	sigaction(SIGSEGV, &sigact, NULL);
	sigaction(SIGUSR2, &sigact, NULL);
	sigaction(SIGPIPE, &sigact, NULL);
	sigaction(SIGALRM, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
#ifdef SIGSTKFLT
	sigaction(SIGSTKFLT, &sigact, NULL);
#endif
	sigaction(SIGCHLD, &sigact, NULL);
#ifdef SIGCLD
	sigaction(SIGCLD, &sigact, NULL);
#endif
	sigaction(SIGCONT, &sigact, NULL);
	sigaction(SIGTSTP, &sigact, NULL);
	sigaction(SIGTTIN, &sigact, NULL);
	sigaction(SIGTTOU, &sigact, NULL);
#ifdef SIGURG
	sigaction(SIGURG, &sigact, NULL);
#endif
#ifdef SIGXCPU
	sigaction(SIGXCPU, &sigact, NULL);
#endif
#ifdef SIGXFSZ
	sigaction(SIGXFSZ, &sigact, NULL);
#endif
#ifdef SIGVTALRM
	sigaction(SIGVTALRM, &sigact, NULL);
#endif
#ifdef SIGPROF
	sigaction(SIGPROF, &sigact, NULL);
#endif
#ifdef SIGWINCH
	sigaction(SIGWINCH, &sigact, NULL);
#endif
#ifdef SIGIO
	sigaction(SIGIO, &sigact, NULL);
#endif
#ifdef SIGPOLL
	sigaction(SIGPOLL, &sigact, NULL);
#endif
#ifdef SIGINFO
	sigaction(SIGINFO, &sigact, NULL);
#endif
#ifdef SIGLOST
	sigaction(SIGLOST, &sigact, NULL);
#endif
#ifdef SIGPWR
	sigaction(SIGPWR, &sigact, NULL);
#endif
#ifdef SIGUNUSED
	sigaction(SIGUNUSED, &sigact, NULL);
#endif
#ifdef SIGSYS
	sigaction(SIGSYS, &sigact, NULL);
#endif
#ifdef SIGRTMIN
	sigaction(SIGRTMIN, &sigact, NULL);
	sigaction(SIGRTMAX, &sigact, NULL);
#endif
	/* Keep SIGHUP last, the bit it flips tells to check script the
	 * helper is ready to be killed.  */
	sigaction(SIGHUP, &sigact, NULL);

	FD_ZERO(&rfds);
	FD_SET(STDIN_FILENO, &rfds);
	select(0, &rfds, NULL, NULL, &timeout);

	exit(TEST_SIGRECEIVE_FAILURE);
}