xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/mqueue/mq_open_tests.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This application is Copyright 2012 Red Hat, Inc.
3*4882a593Smuzhiyun  *	Doug Ledford <dledford@redhat.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * mq_open_tests is free software: you can redistribute it and/or modify
6*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
7*4882a593Smuzhiyun  * the Free Software Foundation, version 3.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * mq_open_tests is distributed in the hope that it will be useful,
10*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4882a593Smuzhiyun  * GNU General Public License for more details.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * For the full text of the license, see <http://www.gnu.org/licenses/>.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * mq_open_tests.c
17*4882a593Smuzhiyun  *   Tests the various situations that should either succeed or fail to
18*4882a593Smuzhiyun  *   open a posix message queue and then reports whether or not they
19*4882a593Smuzhiyun  *   did as they were supposed to.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun #include <stdio.h>
23*4882a593Smuzhiyun #include <stdlib.h>
24*4882a593Smuzhiyun #include <unistd.h>
25*4882a593Smuzhiyun #include <fcntl.h>
26*4882a593Smuzhiyun #include <string.h>
27*4882a593Smuzhiyun #include <limits.h>
28*4882a593Smuzhiyun #include <errno.h>
29*4882a593Smuzhiyun #include <sys/types.h>
30*4882a593Smuzhiyun #include <sys/time.h>
31*4882a593Smuzhiyun #include <sys/resource.h>
32*4882a593Smuzhiyun #include <sys/stat.h>
33*4882a593Smuzhiyun #include <mqueue.h>
34*4882a593Smuzhiyun #include <error.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include "../kselftest.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static char *usage =
39*4882a593Smuzhiyun "Usage:\n"
40*4882a593Smuzhiyun "  %s path\n"
41*4882a593Smuzhiyun "\n"
42*4882a593Smuzhiyun "	path	Path name of the message queue to create\n"
43*4882a593Smuzhiyun "\n"
44*4882a593Smuzhiyun "	Note: this program must be run as root in order to enable all tests\n"
45*4882a593Smuzhiyun "\n";
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun char *DEF_MSGS = "/proc/sys/fs/mqueue/msg_default";
48*4882a593Smuzhiyun char *DEF_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_default";
49*4882a593Smuzhiyun char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max";
50*4882a593Smuzhiyun char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max";
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun int default_settings;
53*4882a593Smuzhiyun struct rlimit saved_limits, cur_limits;
54*4882a593Smuzhiyun int saved_def_msgs, saved_def_msgsize, saved_max_msgs, saved_max_msgsize;
55*4882a593Smuzhiyun int cur_def_msgs, cur_def_msgsize, cur_max_msgs, cur_max_msgsize;
56*4882a593Smuzhiyun FILE *def_msgs, *def_msgsize, *max_msgs, *max_msgsize;
57*4882a593Smuzhiyun char *queue_path;
58*4882a593Smuzhiyun char *default_queue_path = "/test1";
59*4882a593Smuzhiyun mqd_t queue = -1;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static inline void __set(FILE *stream, int value, char *err_msg);
62*4882a593Smuzhiyun void shutdown(int exit_val, char *err_cause, int line_no);
63*4882a593Smuzhiyun static inline int get(FILE *stream);
64*4882a593Smuzhiyun static inline void set(FILE *stream, int value);
65*4882a593Smuzhiyun static inline void getr(int type, struct rlimit *rlim);
66*4882a593Smuzhiyun static inline void setr(int type, struct rlimit *rlim);
67*4882a593Smuzhiyun void validate_current_settings();
68*4882a593Smuzhiyun static inline void test_queue(struct mq_attr *attr, struct mq_attr *result);
69*4882a593Smuzhiyun static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result);
70*4882a593Smuzhiyun 
__set(FILE * stream,int value,char * err_msg)71*4882a593Smuzhiyun static inline void __set(FILE *stream, int value, char *err_msg)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	rewind(stream);
74*4882a593Smuzhiyun 	if (fprintf(stream, "%d", value) < 0)
75*4882a593Smuzhiyun 		perror(err_msg);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 
shutdown(int exit_val,char * err_cause,int line_no)79*4882a593Smuzhiyun void shutdown(int exit_val, char *err_cause, int line_no)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	static int in_shutdown = 0;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* In case we get called recursively by a set() call below */
84*4882a593Smuzhiyun 	if (in_shutdown++)
85*4882a593Smuzhiyun 		return;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (seteuid(0) == -1)
88*4882a593Smuzhiyun 		perror("seteuid() failed");
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	if (queue != -1)
91*4882a593Smuzhiyun 		if (mq_close(queue))
92*4882a593Smuzhiyun 			perror("mq_close() during shutdown");
93*4882a593Smuzhiyun 	if (queue_path)
94*4882a593Smuzhiyun 		/*
95*4882a593Smuzhiyun 		 * Be silent if this fails, if we cleaned up already it's
96*4882a593Smuzhiyun 		 * expected to fail
97*4882a593Smuzhiyun 		 */
98*4882a593Smuzhiyun 		mq_unlink(queue_path);
99*4882a593Smuzhiyun 	if (default_settings) {
100*4882a593Smuzhiyun 		if (saved_def_msgs)
101*4882a593Smuzhiyun 			__set(def_msgs, saved_def_msgs,
102*4882a593Smuzhiyun 			      "failed to restore saved_def_msgs");
103*4882a593Smuzhiyun 		if (saved_def_msgsize)
104*4882a593Smuzhiyun 			__set(def_msgsize, saved_def_msgsize,
105*4882a593Smuzhiyun 			      "failed to restore saved_def_msgsize");
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 	if (saved_max_msgs)
108*4882a593Smuzhiyun 		__set(max_msgs, saved_max_msgs,
109*4882a593Smuzhiyun 		      "failed to restore saved_max_msgs");
110*4882a593Smuzhiyun 	if (saved_max_msgsize)
111*4882a593Smuzhiyun 		__set(max_msgsize, saved_max_msgsize,
112*4882a593Smuzhiyun 		      "failed to restore saved_max_msgsize");
113*4882a593Smuzhiyun 	if (exit_val)
114*4882a593Smuzhiyun 		error(exit_val, errno, "%s at %d", err_cause, line_no);
115*4882a593Smuzhiyun 	exit(0);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
get(FILE * stream)118*4882a593Smuzhiyun static inline int get(FILE *stream)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	int value;
121*4882a593Smuzhiyun 	rewind(stream);
122*4882a593Smuzhiyun 	if (fscanf(stream, "%d", &value) != 1)
123*4882a593Smuzhiyun 		shutdown(4, "Error reading /proc entry", __LINE__ - 1);
124*4882a593Smuzhiyun 	return value;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
set(FILE * stream,int value)127*4882a593Smuzhiyun static inline void set(FILE *stream, int value)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	int new_value;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	rewind(stream);
132*4882a593Smuzhiyun 	if (fprintf(stream, "%d", value) < 0)
133*4882a593Smuzhiyun 		return shutdown(5, "Failed writing to /proc file",
134*4882a593Smuzhiyun 				__LINE__ - 1);
135*4882a593Smuzhiyun 	new_value = get(stream);
136*4882a593Smuzhiyun 	if (new_value != value)
137*4882a593Smuzhiyun 		return shutdown(5, "We didn't get what we wrote to /proc back",
138*4882a593Smuzhiyun 				__LINE__ - 1);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
getr(int type,struct rlimit * rlim)141*4882a593Smuzhiyun static inline void getr(int type, struct rlimit *rlim)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	if (getrlimit(type, rlim))
144*4882a593Smuzhiyun 		shutdown(6, "getrlimit()", __LINE__ - 1);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
setr(int type,struct rlimit * rlim)147*4882a593Smuzhiyun static inline void setr(int type, struct rlimit *rlim)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	if (setrlimit(type, rlim))
150*4882a593Smuzhiyun 		shutdown(7, "setrlimit()", __LINE__ - 1);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
validate_current_settings()153*4882a593Smuzhiyun void validate_current_settings()
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	int rlim_needed;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (cur_limits.rlim_cur < 4096) {
158*4882a593Smuzhiyun 		printf("Current rlimit value for POSIX message queue bytes is "
159*4882a593Smuzhiyun 		       "unreasonably low,\nincreasing.\n\n");
160*4882a593Smuzhiyun 		cur_limits.rlim_cur = 8192;
161*4882a593Smuzhiyun 		cur_limits.rlim_max = 16384;
162*4882a593Smuzhiyun 		setr(RLIMIT_MSGQUEUE, &cur_limits);
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (default_settings) {
166*4882a593Smuzhiyun 		rlim_needed = (cur_def_msgs + 1) * (cur_def_msgsize + 1 +
167*4882a593Smuzhiyun 						    2 * sizeof(void *));
168*4882a593Smuzhiyun 		if (rlim_needed > cur_limits.rlim_cur) {
169*4882a593Smuzhiyun 			printf("Temporarily lowering default queue parameters "
170*4882a593Smuzhiyun 			       "to something that will work\n"
171*4882a593Smuzhiyun 			       "with the current rlimit values.\n\n");
172*4882a593Smuzhiyun 			set(def_msgs, 10);
173*4882a593Smuzhiyun 			cur_def_msgs = 10;
174*4882a593Smuzhiyun 			set(def_msgsize, 128);
175*4882a593Smuzhiyun 			cur_def_msgsize = 128;
176*4882a593Smuzhiyun 		}
177*4882a593Smuzhiyun 	} else {
178*4882a593Smuzhiyun 		rlim_needed = (cur_max_msgs + 1) * (cur_max_msgsize + 1 +
179*4882a593Smuzhiyun 						    2 * sizeof(void *));
180*4882a593Smuzhiyun 		if (rlim_needed > cur_limits.rlim_cur) {
181*4882a593Smuzhiyun 			printf("Temporarily lowering maximum queue parameters "
182*4882a593Smuzhiyun 			       "to something that will work\n"
183*4882a593Smuzhiyun 			       "with the current rlimit values in case this is "
184*4882a593Smuzhiyun 			       "a kernel that ties the default\n"
185*4882a593Smuzhiyun 			       "queue parameters to the maximum queue "
186*4882a593Smuzhiyun 			       "parameters.\n\n");
187*4882a593Smuzhiyun 			set(max_msgs, 10);
188*4882a593Smuzhiyun 			cur_max_msgs = 10;
189*4882a593Smuzhiyun 			set(max_msgsize, 128);
190*4882a593Smuzhiyun 			cur_max_msgsize = 128;
191*4882a593Smuzhiyun 		}
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun  * test_queue - Test opening a queue, shutdown if we fail.  This should
197*4882a593Smuzhiyun  * only be called in situations that should never fail.  We clean up
198*4882a593Smuzhiyun  * after ourselves and return the queue attributes in *result.
199*4882a593Smuzhiyun  */
test_queue(struct mq_attr * attr,struct mq_attr * result)200*4882a593Smuzhiyun static inline void test_queue(struct mq_attr *attr, struct mq_attr *result)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	int flags = O_RDWR | O_EXCL | O_CREAT;
203*4882a593Smuzhiyun 	int perms = DEFFILEMODE;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
206*4882a593Smuzhiyun 		shutdown(1, "mq_open()", __LINE__);
207*4882a593Smuzhiyun 	if (mq_getattr(queue, result))
208*4882a593Smuzhiyun 		shutdown(1, "mq_getattr()", __LINE__);
209*4882a593Smuzhiyun 	if (mq_close(queue))
210*4882a593Smuzhiyun 		shutdown(1, "mq_close()", __LINE__);
211*4882a593Smuzhiyun 	queue = -1;
212*4882a593Smuzhiyun 	if (mq_unlink(queue_path))
213*4882a593Smuzhiyun 		shutdown(1, "mq_unlink()", __LINE__);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * Same as test_queue above, but failure is not fatal.
218*4882a593Smuzhiyun  * Returns:
219*4882a593Smuzhiyun  * 0 - Failed to create a queue
220*4882a593Smuzhiyun  * 1 - Created a queue, attributes in *result
221*4882a593Smuzhiyun  */
test_queue_fail(struct mq_attr * attr,struct mq_attr * result)222*4882a593Smuzhiyun static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	int flags = O_RDWR | O_EXCL | O_CREAT;
225*4882a593Smuzhiyun 	int perms = DEFFILEMODE;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
228*4882a593Smuzhiyun 		return 0;
229*4882a593Smuzhiyun 	if (mq_getattr(queue, result))
230*4882a593Smuzhiyun 		shutdown(1, "mq_getattr()", __LINE__);
231*4882a593Smuzhiyun 	if (mq_close(queue))
232*4882a593Smuzhiyun 		shutdown(1, "mq_close()", __LINE__);
233*4882a593Smuzhiyun 	queue = -1;
234*4882a593Smuzhiyun 	if (mq_unlink(queue_path))
235*4882a593Smuzhiyun 		shutdown(1, "mq_unlink()", __LINE__);
236*4882a593Smuzhiyun 	return 1;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
main(int argc,char * argv[])239*4882a593Smuzhiyun int main(int argc, char *argv[])
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct mq_attr attr, result;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (argc != 2) {
244*4882a593Smuzhiyun 		printf("Using Default queue path - %s\n", default_queue_path);
245*4882a593Smuzhiyun 		queue_path = default_queue_path;
246*4882a593Smuzhiyun 	} else {
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/*
249*4882a593Smuzhiyun 	 * Although we can create a msg queue with a non-absolute path name,
250*4882a593Smuzhiyun 	 * unlink will fail.  So, if the name doesn't start with a /, add one
251*4882a593Smuzhiyun 	 * when we save it.
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 		if (*argv[1] == '/')
254*4882a593Smuzhiyun 			queue_path = strdup(argv[1]);
255*4882a593Smuzhiyun 		else {
256*4882a593Smuzhiyun 			queue_path = malloc(strlen(argv[1]) + 2);
257*4882a593Smuzhiyun 			if (!queue_path) {
258*4882a593Smuzhiyun 				perror("malloc()");
259*4882a593Smuzhiyun 				exit(1);
260*4882a593Smuzhiyun 			}
261*4882a593Smuzhiyun 			queue_path[0] = '/';
262*4882a593Smuzhiyun 			queue_path[1] = 0;
263*4882a593Smuzhiyun 			strcat(queue_path, argv[1]);
264*4882a593Smuzhiyun 		}
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if (getuid() != 0)
268*4882a593Smuzhiyun 		ksft_exit_skip("Not running as root, but almost all tests "
269*4882a593Smuzhiyun 			"require root in order to modify\nsystem settings.  "
270*4882a593Smuzhiyun 			"Exiting.\n");
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/* Find out what files there are for us to make tweaks in */
273*4882a593Smuzhiyun 	def_msgs = fopen(DEF_MSGS, "r+");
274*4882a593Smuzhiyun 	def_msgsize = fopen(DEF_MSGSIZE, "r+");
275*4882a593Smuzhiyun 	max_msgs = fopen(MAX_MSGS, "r+");
276*4882a593Smuzhiyun 	max_msgsize = fopen(MAX_MSGSIZE, "r+");
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (!max_msgs)
279*4882a593Smuzhiyun 		shutdown(2, "Failed to open msg_max", __LINE__);
280*4882a593Smuzhiyun 	if (!max_msgsize)
281*4882a593Smuzhiyun 		shutdown(2, "Failed to open msgsize_max", __LINE__);
282*4882a593Smuzhiyun 	if (def_msgs || def_msgsize)
283*4882a593Smuzhiyun 		default_settings = 1;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Load up the current system values for everything we can */
286*4882a593Smuzhiyun 	getr(RLIMIT_MSGQUEUE, &saved_limits);
287*4882a593Smuzhiyun 	cur_limits = saved_limits;
288*4882a593Smuzhiyun 	if (default_settings) {
289*4882a593Smuzhiyun 		saved_def_msgs = cur_def_msgs = get(def_msgs);
290*4882a593Smuzhiyun 		saved_def_msgsize = cur_def_msgsize = get(def_msgsize);
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	saved_max_msgs = cur_max_msgs = get(max_msgs);
293*4882a593Smuzhiyun 	saved_max_msgsize = cur_max_msgsize = get(max_msgsize);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Tell the user our initial state */
296*4882a593Smuzhiyun 	printf("\nInitial system state:\n");
297*4882a593Smuzhiyun 	printf("\tUsing queue path:\t\t%s\n", queue_path);
298*4882a593Smuzhiyun 	printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n",
299*4882a593Smuzhiyun 		(long) saved_limits.rlim_cur);
300*4882a593Smuzhiyun 	printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n",
301*4882a593Smuzhiyun 		(long) saved_limits.rlim_max);
302*4882a593Smuzhiyun 	printf("\tMaximum Message Size:\t\t%d\n", saved_max_msgsize);
303*4882a593Smuzhiyun 	printf("\tMaximum Queue Size:\t\t%d\n", saved_max_msgs);
304*4882a593Smuzhiyun 	if (default_settings) {
305*4882a593Smuzhiyun 		printf("\tDefault Message Size:\t\t%d\n", saved_def_msgsize);
306*4882a593Smuzhiyun 		printf("\tDefault Queue Size:\t\t%d\n", saved_def_msgs);
307*4882a593Smuzhiyun 	} else {
308*4882a593Smuzhiyun 		printf("\tDefault Message Size:\t\tNot Supported\n");
309*4882a593Smuzhiyun 		printf("\tDefault Queue Size:\t\tNot Supported\n");
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 	printf("\n");
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	validate_current_settings();
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	printf("Adjusted system state for testing:\n");
316*4882a593Smuzhiyun 	printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n", (long) cur_limits.rlim_cur);
317*4882a593Smuzhiyun 	printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n", (long) cur_limits.rlim_max);
318*4882a593Smuzhiyun 	printf("\tMaximum Message Size:\t\t%d\n", cur_max_msgsize);
319*4882a593Smuzhiyun 	printf("\tMaximum Queue Size:\t\t%d\n", cur_max_msgs);
320*4882a593Smuzhiyun 	if (default_settings) {
321*4882a593Smuzhiyun 		printf("\tDefault Message Size:\t\t%d\n", cur_def_msgsize);
322*4882a593Smuzhiyun 		printf("\tDefault Queue Size:\t\t%d\n", cur_def_msgs);
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	printf("\n\nTest series 1, behavior when no attr struct "
326*4882a593Smuzhiyun 	       "passed to mq_open:\n");
327*4882a593Smuzhiyun 	if (!default_settings) {
328*4882a593Smuzhiyun 		test_queue(NULL, &result);
329*4882a593Smuzhiyun 		printf("Given sane system settings, mq_open without an attr "
330*4882a593Smuzhiyun 		       "struct succeeds:\tPASS\n");
331*4882a593Smuzhiyun 		if (result.mq_maxmsg != cur_max_msgs ||
332*4882a593Smuzhiyun 		    result.mq_msgsize != cur_max_msgsize) {
333*4882a593Smuzhiyun 			printf("Kernel does not support setting the default "
334*4882a593Smuzhiyun 			       "mq attributes,\nbut also doesn't tie the "
335*4882a593Smuzhiyun 			       "defaults to the maximums:\t\t\tPASS\n");
336*4882a593Smuzhiyun 		} else {
337*4882a593Smuzhiyun 			set(max_msgs, ++cur_max_msgs);
338*4882a593Smuzhiyun 			set(max_msgsize, ++cur_max_msgsize);
339*4882a593Smuzhiyun 			test_queue(NULL, &result);
340*4882a593Smuzhiyun 			if (result.mq_maxmsg == cur_max_msgs &&
341*4882a593Smuzhiyun 			    result.mq_msgsize == cur_max_msgsize)
342*4882a593Smuzhiyun 				printf("Kernel does not support setting the "
343*4882a593Smuzhiyun 				       "default mq attributes and\n"
344*4882a593Smuzhiyun 				       "also ties system wide defaults to "
345*4882a593Smuzhiyun 				       "the system wide maximums:\t\t"
346*4882a593Smuzhiyun 				       "FAIL\n");
347*4882a593Smuzhiyun 			else
348*4882a593Smuzhiyun 				printf("Kernel does not support setting the "
349*4882a593Smuzhiyun 				       "default mq attributes,\n"
350*4882a593Smuzhiyun 				       "but also doesn't tie the defaults to "
351*4882a593Smuzhiyun 				       "the maximums:\t\t\tPASS\n");
352*4882a593Smuzhiyun 		}
353*4882a593Smuzhiyun 	} else {
354*4882a593Smuzhiyun 		printf("Kernel supports setting defaults separately from "
355*4882a593Smuzhiyun 		       "maximums:\t\tPASS\n");
356*4882a593Smuzhiyun 		/*
357*4882a593Smuzhiyun 		 * While we are here, go ahead and test that the kernel
358*4882a593Smuzhiyun 		 * properly follows the default settings
359*4882a593Smuzhiyun 		 */
360*4882a593Smuzhiyun 		test_queue(NULL, &result);
361*4882a593Smuzhiyun 		printf("Given sane values, mq_open without an attr struct "
362*4882a593Smuzhiyun 		       "succeeds:\t\tPASS\n");
363*4882a593Smuzhiyun 		if (result.mq_maxmsg != cur_def_msgs ||
364*4882a593Smuzhiyun 		    result.mq_msgsize != cur_def_msgsize)
365*4882a593Smuzhiyun 			printf("Kernel supports setting defaults, but does "
366*4882a593Smuzhiyun 			       "not actually honor them:\tFAIL\n\n");
367*4882a593Smuzhiyun 		else {
368*4882a593Smuzhiyun 			set(def_msgs, ++cur_def_msgs);
369*4882a593Smuzhiyun 			set(def_msgsize, ++cur_def_msgsize);
370*4882a593Smuzhiyun 			/* In case max was the same as the default */
371*4882a593Smuzhiyun 			set(max_msgs, ++cur_max_msgs);
372*4882a593Smuzhiyun 			set(max_msgsize, ++cur_max_msgsize);
373*4882a593Smuzhiyun 			test_queue(NULL, &result);
374*4882a593Smuzhiyun 			if (result.mq_maxmsg != cur_def_msgs ||
375*4882a593Smuzhiyun 			    result.mq_msgsize != cur_def_msgsize)
376*4882a593Smuzhiyun 				printf("Kernel supports setting defaults, but "
377*4882a593Smuzhiyun 				       "does not actually honor them:\t"
378*4882a593Smuzhiyun 				       "FAIL\n");
379*4882a593Smuzhiyun 			else
380*4882a593Smuzhiyun 				printf("Kernel properly honors default setting "
381*4882a593Smuzhiyun 				       "knobs:\t\t\t\tPASS\n");
382*4882a593Smuzhiyun 		}
383*4882a593Smuzhiyun 		set(def_msgs, cur_max_msgs + 1);
384*4882a593Smuzhiyun 		cur_def_msgs = cur_max_msgs + 1;
385*4882a593Smuzhiyun 		set(def_msgsize, cur_max_msgsize + 1);
386*4882a593Smuzhiyun 		cur_def_msgsize = cur_max_msgsize + 1;
387*4882a593Smuzhiyun 		if (cur_def_msgs * (cur_def_msgsize + 2 * sizeof(void *)) >=
388*4882a593Smuzhiyun 		    cur_limits.rlim_cur) {
389*4882a593Smuzhiyun 			cur_limits.rlim_cur = (cur_def_msgs + 2) *
390*4882a593Smuzhiyun 				(cur_def_msgsize + 2 * sizeof(void *));
391*4882a593Smuzhiyun 			cur_limits.rlim_max = 2 * cur_limits.rlim_cur;
392*4882a593Smuzhiyun 			setr(RLIMIT_MSGQUEUE, &cur_limits);
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 		if (test_queue_fail(NULL, &result)) {
395*4882a593Smuzhiyun 			if (result.mq_maxmsg == cur_max_msgs &&
396*4882a593Smuzhiyun 			    result.mq_msgsize == cur_max_msgsize)
397*4882a593Smuzhiyun 				printf("Kernel properly limits default values "
398*4882a593Smuzhiyun 				       "to lesser of default/max:\t\tPASS\n");
399*4882a593Smuzhiyun 			else
400*4882a593Smuzhiyun 				printf("Kernel does not properly set default "
401*4882a593Smuzhiyun 				       "queue parameters when\ndefaults > "
402*4882a593Smuzhiyun 				       "max:\t\t\t\t\t\t\t\tFAIL\n");
403*4882a593Smuzhiyun 		} else
404*4882a593Smuzhiyun 			printf("Kernel fails to open mq because defaults are "
405*4882a593Smuzhiyun 			       "greater than maximums:\tFAIL\n");
406*4882a593Smuzhiyun 		set(def_msgs, --cur_def_msgs);
407*4882a593Smuzhiyun 		set(def_msgsize, --cur_def_msgsize);
408*4882a593Smuzhiyun 		cur_limits.rlim_cur = cur_limits.rlim_max = cur_def_msgs *
409*4882a593Smuzhiyun 			cur_def_msgsize;
410*4882a593Smuzhiyun 		setr(RLIMIT_MSGQUEUE, &cur_limits);
411*4882a593Smuzhiyun 		if (test_queue_fail(NULL, &result))
412*4882a593Smuzhiyun 			printf("Kernel creates queue even though defaults "
413*4882a593Smuzhiyun 			       "would exceed\nrlimit setting:"
414*4882a593Smuzhiyun 			       "\t\t\t\t\t\t\t\tFAIL\n");
415*4882a593Smuzhiyun 		else
416*4882a593Smuzhiyun 			printf("Kernel properly fails to create queue when "
417*4882a593Smuzhiyun 			       "defaults would\nexceed rlimit:"
418*4882a593Smuzhiyun 			       "\t\t\t\t\t\t\t\tPASS\n");
419*4882a593Smuzhiyun 	}
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/*
422*4882a593Smuzhiyun 	 * Test #2 - open with an attr struct that exceeds rlimit
423*4882a593Smuzhiyun 	 */
424*4882a593Smuzhiyun 	printf("\n\nTest series 2, behavior when attr struct is "
425*4882a593Smuzhiyun 	       "passed to mq_open:\n");
426*4882a593Smuzhiyun 	cur_max_msgs = 32;
427*4882a593Smuzhiyun 	cur_max_msgsize = cur_limits.rlim_max >> 4;
428*4882a593Smuzhiyun 	set(max_msgs, cur_max_msgs);
429*4882a593Smuzhiyun 	set(max_msgsize, cur_max_msgsize);
430*4882a593Smuzhiyun 	attr.mq_maxmsg = cur_max_msgs;
431*4882a593Smuzhiyun 	attr.mq_msgsize = cur_max_msgsize;
432*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
433*4882a593Smuzhiyun 		printf("Queue open in excess of rlimit max when euid = 0 "
434*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
435*4882a593Smuzhiyun 	else
436*4882a593Smuzhiyun 		printf("Queue open in excess of rlimit max when euid = 0 "
437*4882a593Smuzhiyun 		       "failed:\t\tPASS\n");
438*4882a593Smuzhiyun 	attr.mq_maxmsg = cur_max_msgs + 1;
439*4882a593Smuzhiyun 	attr.mq_msgsize = 10;
440*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
441*4882a593Smuzhiyun 		printf("Queue open with mq_maxmsg > limit when euid = 0 "
442*4882a593Smuzhiyun 		       "succeeded:\t\tPASS\n");
443*4882a593Smuzhiyun 	else
444*4882a593Smuzhiyun 		printf("Queue open with mq_maxmsg > limit when euid = 0 "
445*4882a593Smuzhiyun 		       "failed:\t\tFAIL\n");
446*4882a593Smuzhiyun 	attr.mq_maxmsg = 1;
447*4882a593Smuzhiyun 	attr.mq_msgsize = cur_max_msgsize + 1;
448*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
449*4882a593Smuzhiyun 		printf("Queue open with mq_msgsize > limit when euid = 0 "
450*4882a593Smuzhiyun 		       "succeeded:\t\tPASS\n");
451*4882a593Smuzhiyun 	else
452*4882a593Smuzhiyun 		printf("Queue open with mq_msgsize > limit when euid = 0 "
453*4882a593Smuzhiyun 		       "failed:\t\tFAIL\n");
454*4882a593Smuzhiyun 	attr.mq_maxmsg = 65536;
455*4882a593Smuzhiyun 	attr.mq_msgsize = 65536;
456*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
457*4882a593Smuzhiyun 		printf("Queue open with total size > 2GB when euid = 0 "
458*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
459*4882a593Smuzhiyun 	else
460*4882a593Smuzhiyun 		printf("Queue open with total size > 2GB when euid = 0 "
461*4882a593Smuzhiyun 		       "failed:\t\t\tPASS\n");
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	if (seteuid(99) == -1) {
464*4882a593Smuzhiyun 		perror("seteuid() failed");
465*4882a593Smuzhiyun 		exit(1);
466*4882a593Smuzhiyun 	}
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	attr.mq_maxmsg = cur_max_msgs;
469*4882a593Smuzhiyun 	attr.mq_msgsize = cur_max_msgsize;
470*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
471*4882a593Smuzhiyun 		printf("Queue open in excess of rlimit max when euid = 99 "
472*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
473*4882a593Smuzhiyun 	else
474*4882a593Smuzhiyun 		printf("Queue open in excess of rlimit max when euid = 99 "
475*4882a593Smuzhiyun 		       "failed:\t\tPASS\n");
476*4882a593Smuzhiyun 	attr.mq_maxmsg = cur_max_msgs + 1;
477*4882a593Smuzhiyun 	attr.mq_msgsize = 10;
478*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
479*4882a593Smuzhiyun 		printf("Queue open with mq_maxmsg > limit when euid = 99 "
480*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
481*4882a593Smuzhiyun 	else
482*4882a593Smuzhiyun 		printf("Queue open with mq_maxmsg > limit when euid = 99 "
483*4882a593Smuzhiyun 		       "failed:\t\tPASS\n");
484*4882a593Smuzhiyun 	attr.mq_maxmsg = 1;
485*4882a593Smuzhiyun 	attr.mq_msgsize = cur_max_msgsize + 1;
486*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
487*4882a593Smuzhiyun 		printf("Queue open with mq_msgsize > limit when euid = 99 "
488*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
489*4882a593Smuzhiyun 	else
490*4882a593Smuzhiyun 		printf("Queue open with mq_msgsize > limit when euid = 99 "
491*4882a593Smuzhiyun 		       "failed:\t\tPASS\n");
492*4882a593Smuzhiyun 	attr.mq_maxmsg = 65536;
493*4882a593Smuzhiyun 	attr.mq_msgsize = 65536;
494*4882a593Smuzhiyun 	if (test_queue_fail(&attr, &result))
495*4882a593Smuzhiyun 		printf("Queue open with total size > 2GB when euid = 99 "
496*4882a593Smuzhiyun 		       "succeeded:\t\tFAIL\n");
497*4882a593Smuzhiyun 	else
498*4882a593Smuzhiyun 		printf("Queue open with total size > 2GB when euid = 99 "
499*4882a593Smuzhiyun 		       "failed:\t\t\tPASS\n");
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	shutdown(0,"",0);
502*4882a593Smuzhiyun }
503