xref: /freebsd-src/contrib/netbsd-tests/lib/libc/sys/t_msgsnd.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /* $NetBSD: t_msgsnd.c,v 1.3 2017/01/13 20:44:45 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_msgsnd.c,v 1.3 2017/01/13 20:44:45 christos Exp $");
33 
34 #include <sys/msg.h>
35 #include <sys/stat.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
38 
39 #include <atf-c.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <pwd.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <time.h>
49 #include <unistd.h>
50 
51 #ifdef __FreeBSD__
52 #include <limits.h>
53 #endif
54 
55 #define MSG_KEY		1234
56 #define MSG_MTYPE_1	0x41
57 #define	MSG_MTYPE_2	0x42
58 #define MSG_MTYPE_3	0x43
59 
60 struct msg {
61 	long		 mtype;
62 	char		 buf[3];
63 };
64 
65 static void		clean(void);
66 
67 static void
68 clean(void)
69 {
70 	int id;
71 
72 	if ((id = msgget(MSG_KEY, 0)) != -1)
73 		(void)msgctl(id, IPC_RMID, 0);
74 }
75 
76 ATF_TC_WITH_CLEANUP(msgsnd_block);
77 ATF_TC_HEAD(msgsnd_block, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr", "Test that msgsnd(2) blocks");
80 	atf_tc_set_md_var(tc, "timeout", "10");
81 }
82 
83 ATF_TC_BODY(msgsnd_block, tc)
84 {
85 	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
86 	int id, sta;
87 	pid_t pid;
88 
89 	id = msgget(MSG_KEY, IPC_CREAT | 0600);
90 	ATF_REQUIRE(id != -1);
91 
92 	pid = fork();
93 	ATF_REQUIRE(pid >= 0);
94 
95 	if (pid == 0) {
96 
97 		/*
98 		 * Enqueue messages until some limit (e.g. the maximum
99 		 * number of messages in the queue or the maximum number
100 		 * of bytes in the queue) is reached. After this the call
101 		 * should block when the IPC_NOWAIT is not set.
102 		 */
103 		for (;;) {
104 
105 			if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0)
106 				_exit(EXIT_FAILURE);
107 		}
108 	}
109 
110 	(void)sleep(2);
111 	(void)kill(pid, SIGKILL);
112 	(void)wait(&sta);
113 
114 	if (WIFEXITED(sta) != 0 || WIFSIGNALED(sta) == 0)
115 		atf_tc_fail("msgsnd(2) did not block");
116 
117 	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
118 }
119 
120 ATF_TC_CLEANUP(msgsnd_block, tc)
121 {
122 	clean();
123 }
124 
125 ATF_TC_WITH_CLEANUP(msgsnd_count);
126 ATF_TC_HEAD(msgsnd_count, tc)
127 {
128 	atf_tc_set_md_var(tc, "descr",
129 	    "Test that msgsnd(2) increments the amount of "
130 	    "message in the queue, as given by msgctl(2)");
131 	atf_tc_set_md_var(tc, "timeout", "10");
132 }
133 
134 ATF_TC_BODY(msgsnd_count, tc)
135 {
136 	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
137 	struct msqid_ds ds;
138 	size_t i = 0;
139 	int id, rv;
140 
141 	id = msgget(MSG_KEY, IPC_CREAT | 0600);
142 	ATF_REQUIRE(id != -1);
143 
144 	for (;;) {
145 
146 		errno = 0;
147 		rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
148 
149 		if (rv == 0) {
150 			i++;
151 			continue;
152 		}
153 
154 		if (rv == -1 && errno == EAGAIN)
155 			break;
156 
157 		atf_tc_fail("failed to enqueue a message");
158 	}
159 
160 	(void)memset(&ds, 0, sizeof(struct msqid_ds));
161 	(void)msgctl(id, IPC_STAT, &ds);
162 
163 	if (ds.msg_qnum != i)
164 		atf_tc_fail("incorrect message count");
165 
166 	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
167 }
168 
169 ATF_TC_CLEANUP(msgsnd_count, tc)
170 {
171 	clean();
172 }
173 
174 ATF_TC_WITH_CLEANUP(msgsnd_err);
175 ATF_TC_HEAD(msgsnd_err, tc)
176 {
177 	atf_tc_set_md_var(tc, "descr", "Test errors from msgsnd(2)");
178 }
179 
180 ATF_TC_BODY(msgsnd_err, tc)
181 {
182 	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
183 	int id;
184 
185 	id = msgget(MSG_KEY, IPC_CREAT | 0600);
186 	ATF_REQUIRE(id != -1);
187 
188 	errno = 0;
189 
190 	ATF_REQUIRE_ERRNO(EFAULT, msgsnd(id, (void *)-1,
191 		sizeof(struct msg), IPC_NOWAIT) == -1);
192 
193 	errno = 0;
194 
195 	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
196 		sizeof(struct msg), IPC_NOWAIT) == -1);
197 
198 	errno = 0;
199 
200 	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
201 		SSIZE_MAX, IPC_NOWAIT) == -1);
202 
203 	errno = 0;
204 	msg.mtype = 0;
205 
206 	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(id, &msg,
207 		sizeof(struct msg), IPC_NOWAIT) == -1);
208 
209 	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
210 }
211 
212 ATF_TC_CLEANUP(msgsnd_err, tc)
213 {
214 	clean();
215 }
216 
217 ATF_TC_WITH_CLEANUP(msgsnd_nonblock);
218 ATF_TC_HEAD(msgsnd_nonblock, tc)
219 {
220 	atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) with IPC_NOWAIT");
221 	atf_tc_set_md_var(tc, "timeout", "10");
222 }
223 
224 ATF_TC_BODY(msgsnd_nonblock, tc)
225 {
226 	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
227 	int id, rv, sta;
228 	pid_t pid;
229 
230 	id = msgget(MSG_KEY, IPC_CREAT | 0600);
231 	ATF_REQUIRE(id != -1);
232 
233 	pid = fork();
234 	ATF_REQUIRE(pid >= 0);
235 
236 	if (pid == 0) {
237 
238 		for (;;) {
239 
240 			errno = 0;
241 			rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
242 
243 			if (rv == -1 && errno == EAGAIN)
244 				_exit(EXIT_SUCCESS);
245 		}
246 	}
247 
248 	(void)sleep(2);
249 	(void)kill(pid, SIGKILL);
250 	(void)wait(&sta);
251 
252 	if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
253 		atf_tc_fail("msgsnd(2) blocked with IPC_NOWAIT");
254 
255 	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
256 }
257 
258 ATF_TC_CLEANUP(msgsnd_nonblock, tc)
259 {
260 	clean();
261 }
262 
263 ATF_TC_WITH_CLEANUP(msgsnd_perm);
264 ATF_TC_HEAD(msgsnd_perm, tc)
265 {
266 	atf_tc_set_md_var(tc, "descr", "Test permissions with msgsnd(2)");
267 	atf_tc_set_md_var(tc, "require.user", "root");
268 }
269 
270 ATF_TC_BODY(msgsnd_perm, tc)
271 {
272 	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
273 	struct passwd *pw;
274 	int id, sta;
275 	pid_t pid;
276 	uid_t uid;
277 
278 	pw = getpwnam("nobody");
279 	id = msgget(MSG_KEY, IPC_CREAT | 0600);
280 
281 	ATF_REQUIRE(id != -1);
282 	ATF_REQUIRE(pw != NULL);
283 
284 	uid = pw->pw_uid;
285 	ATF_REQUIRE(uid != 0);
286 
287 	pid = fork();
288 	ATF_REQUIRE(pid >= 0);
289 
290 	if (pid == 0) {
291 
292 		/*
293 		 * Try to enqueue a message to the queue
294 		 * created by root as RW for owner only.
295 		 */
296 		if (setuid(uid) != 0)
297 			_exit(EX_OSERR);
298 
299 		id = msgget(MSG_KEY, 0);
300 
301 		if (id == -1)
302 			_exit(EX_OSERR);
303 
304 		errno = 0;
305 
306 		if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0)
307 			_exit(EXIT_FAILURE);
308 
309 		if (errno != EACCES)
310 			_exit(EXIT_FAILURE);
311 
312 		_exit(EXIT_SUCCESS);
313 	}
314 
315 	(void)wait(&sta);
316 
317 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
318 
319 		if (errno == EX_OSERR)
320 			atf_tc_fail("system call failed");
321 
322 		atf_tc_fail("UID %u enqueued message to root's queue", uid);
323 	}
324 
325 	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
326 }
327 
328 ATF_TC_CLEANUP(msgsnd_perm, tc)
329 {
330 	clean();
331 }
332 
333 ATF_TP_ADD_TCS(tp)
334 {
335 
336 	ATF_TP_ADD_TC(tp, msgsnd_block);
337 	ATF_TP_ADD_TC(tp, msgsnd_count);
338 	ATF_TP_ADD_TC(tp, msgsnd_err);
339 	ATF_TP_ADD_TC(tp, msgsnd_nonblock);
340 	ATF_TP_ADD_TC(tp, msgsnd_perm);
341 
342 	return atf_no_error();
343 }
344