2 * This application is Copyright 2012 Red Hat, Inc.
3 * Doug Ledford <dledford@redhat.com>
5 * mq_open_tests is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
9 * mq_open_tests is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * For the full text of the license, see <http://www.gnu.org/licenses/>.
17 * Tests the various situations that should either succeed or fail to
18 * open a posix message queue and then reports whether or not they
19 * did as they were supposed to.
29 #include <sys/types.h>
31 #include <sys/resource.h>
40 " path Path name of the message queue to create\n"
42 " Note: this program must be run as root in order to enable all tests\n"
45 char *DEF_MSGS
= "/proc/sys/fs/mqueue/msg_default";
46 char *DEF_MSGSIZE
= "/proc/sys/fs/mqueue/msgsize_default";
47 char *MAX_MSGS
= "/proc/sys/fs/mqueue/msg_max";
48 char *MAX_MSGSIZE
= "/proc/sys/fs/mqueue/msgsize_max";
51 struct rlimit saved_limits
, cur_limits
;
52 int saved_def_msgs
, saved_def_msgsize
, saved_max_msgs
, saved_max_msgsize
;
53 int cur_def_msgs
, cur_def_msgsize
, cur_max_msgs
, cur_max_msgsize
;
54 FILE *def_msgs
, *def_msgsize
, *max_msgs
, *max_msgsize
;
58 static inline void __set(FILE *stream
, int value
, char *err_msg
);
59 void shutdown(int exit_val
, char *err_cause
, int line_no
);
60 static inline int get(FILE *stream
);
61 static inline void set(FILE *stream
, int value
);
62 static inline void getr(int type
, struct rlimit
*rlim
);
63 static inline void setr(int type
, struct rlimit
*rlim
);
64 void validate_current_settings();
65 static inline void test_queue(struct mq_attr
*attr
, struct mq_attr
*result
);
66 static inline int test_queue_fail(struct mq_attr
*attr
, struct mq_attr
*result
);
68 static inline void __set(FILE *stream
, int value
, char *err_msg
)
71 if (fprintf(stream
, "%d", value
) < 0)
76 void shutdown(int exit_val
, char *err_cause
, int line_no
)
78 static int in_shutdown
= 0;
80 /* In case we get called recursively by a set() call below */
85 perror("seteuid() failed");
89 perror("mq_close() during shutdown");
92 * Be silent if this fails, if we cleaned up already it's
95 mq_unlink(queue_path
);
96 if (default_settings
) {
98 __set(def_msgs
, saved_def_msgs
,
99 "failed to restore saved_def_msgs");
100 if (saved_def_msgsize
)
101 __set(def_msgsize
, saved_def_msgsize
,
102 "failed to restore saved_def_msgsize");
105 __set(max_msgs
, saved_max_msgs
,
106 "failed to restore saved_max_msgs");
107 if (saved_max_msgsize
)
108 __set(max_msgsize
, saved_max_msgsize
,
109 "failed to restore saved_max_msgsize");
111 error(exit_val
, errno
, "%s at %d", err_cause
, line_no
);
115 static inline int get(FILE *stream
)
119 if (fscanf(stream
, "%d", &value
) != 1)
120 shutdown(4, "Error reading /proc entry", __LINE__
- 1);
124 static inline void set(FILE *stream
, int value
)
129 if (fprintf(stream
, "%d", value
) < 0)
130 return shutdown(5, "Failed writing to /proc file",
132 new_value
= get(stream
);
133 if (new_value
!= value
)
134 return shutdown(5, "We didn't get what we wrote to /proc back",
138 static inline void getr(int type
, struct rlimit
*rlim
)
140 if (getrlimit(type
, rlim
))
141 shutdown(6, "getrlimit()", __LINE__
- 1);
144 static inline void setr(int type
, struct rlimit
*rlim
)
146 if (setrlimit(type
, rlim
))
147 shutdown(7, "setrlimit()", __LINE__
- 1);
150 void validate_current_settings()
154 if (cur_limits
.rlim_cur
< 4096) {
155 printf("Current rlimit value for POSIX message queue bytes is "
156 "unreasonably low,\nincreasing.\n\n");
157 cur_limits
.rlim_cur
= 8192;
158 cur_limits
.rlim_max
= 16384;
159 setr(RLIMIT_MSGQUEUE
, &cur_limits
);
162 if (default_settings
) {
163 rlim_needed
= (cur_def_msgs
+ 1) * (cur_def_msgsize
+ 1 +
165 if (rlim_needed
> cur_limits
.rlim_cur
) {
166 printf("Temporarily lowering default queue parameters "
167 "to something that will work\n"
168 "with the current rlimit values.\n\n");
171 set(def_msgsize
, 128);
172 cur_def_msgsize
= 128;
175 rlim_needed
= (cur_max_msgs
+ 1) * (cur_max_msgsize
+ 1 +
177 if (rlim_needed
> cur_limits
.rlim_cur
) {
178 printf("Temporarily lowering maximum queue parameters "
179 "to something that will work\n"
180 "with the current rlimit values in case this is "
181 "a kernel that ties the default\n"
182 "queue parameters to the maximum queue "
186 set(max_msgsize
, 128);
187 cur_max_msgsize
= 128;
193 * test_queue - Test opening a queue, shutdown if we fail. This should
194 * only be called in situations that should never fail. We clean up
195 * after ourselves and return the queue attributes in *result.
197 static inline void test_queue(struct mq_attr
*attr
, struct mq_attr
*result
)
199 int flags
= O_RDWR
| O_EXCL
| O_CREAT
;
200 int perms
= DEFFILEMODE
;
202 if ((queue
= mq_open(queue_path
, flags
, perms
, attr
)) == -1)
203 shutdown(1, "mq_open()", __LINE__
);
204 if (mq_getattr(queue
, result
))
205 shutdown(1, "mq_getattr()", __LINE__
);
207 shutdown(1, "mq_close()", __LINE__
);
209 if (mq_unlink(queue_path
))
210 shutdown(1, "mq_unlink()", __LINE__
);
214 * Same as test_queue above, but failure is not fatal.
216 * 0 - Failed to create a queue
217 * 1 - Created a queue, attributes in *result
219 static inline int test_queue_fail(struct mq_attr
*attr
, struct mq_attr
*result
)
221 int flags
= O_RDWR
| O_EXCL
| O_CREAT
;
222 int perms
= DEFFILEMODE
;
224 if ((queue
= mq_open(queue_path
, flags
, perms
, attr
)) == -1)
226 if (mq_getattr(queue
, result
))
227 shutdown(1, "mq_getattr()", __LINE__
);
229 shutdown(1, "mq_close()", __LINE__
);
231 if (mq_unlink(queue_path
))
232 shutdown(1, "mq_unlink()", __LINE__
);
236 int main(int argc
, char *argv
[])
238 struct mq_attr attr
, result
;
241 fprintf(stderr
, "Must pass a valid queue name\n\n");
242 fprintf(stderr
, usage
, argv
[0]);
247 * Although we can create a msg queue with a non-absolute path name,
248 * unlink will fail. So, if the name doesn't start with a /, add one
252 queue_path
= strdup(argv
[1]);
254 queue_path
= malloc(strlen(argv
[1]) + 2);
261 strcat(queue_path
, argv
[1]);
265 fprintf(stderr
, "Not running as root, but almost all tests "
266 "require root in order to modify\nsystem settings. "
271 /* Find out what files there are for us to make tweaks in */
272 def_msgs
= fopen(DEF_MSGS
, "r+");
273 def_msgsize
= fopen(DEF_MSGSIZE
, "r+");
274 max_msgs
= fopen(MAX_MSGS
, "r+");
275 max_msgsize
= fopen(MAX_MSGSIZE
, "r+");
278 shutdown(2, "Failed to open msg_max", __LINE__
);
280 shutdown(2, "Failed to open msgsize_max", __LINE__
);
281 if (def_msgs
|| def_msgsize
)
282 default_settings
= 1;
284 /* Load up the current system values for everything we can */
285 getr(RLIMIT_MSGQUEUE
, &saved_limits
);
286 cur_limits
= saved_limits
;
287 if (default_settings
) {
288 saved_def_msgs
= cur_def_msgs
= get(def_msgs
);
289 saved_def_msgsize
= cur_def_msgsize
= get(def_msgsize
);
291 saved_max_msgs
= cur_max_msgs
= get(max_msgs
);
292 saved_max_msgsize
= cur_max_msgsize
= get(max_msgsize
);
294 /* Tell the user our initial state */
295 printf("\nInitial system state:\n");
296 printf("\tUsing queue path:\t\t%s\n", queue_path
);
297 printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n",
298 (long) saved_limits
.rlim_cur
);
299 printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n",
300 (long) saved_limits
.rlim_max
);
301 printf("\tMaximum Message Size:\t\t%d\n", saved_max_msgsize
);
302 printf("\tMaximum Queue Size:\t\t%d\n", saved_max_msgs
);
303 if (default_settings
) {
304 printf("\tDefault Message Size:\t\t%d\n", saved_def_msgsize
);
305 printf("\tDefault Queue Size:\t\t%d\n", saved_def_msgs
);
307 printf("\tDefault Message Size:\t\tNot Supported\n");
308 printf("\tDefault Queue Size:\t\tNot Supported\n");
312 validate_current_settings();
314 printf("Adjusted system state for testing:\n");
315 printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n", (long) cur_limits
.rlim_cur
);
316 printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n", (long) cur_limits
.rlim_max
);
317 printf("\tMaximum Message Size:\t\t%d\n", cur_max_msgsize
);
318 printf("\tMaximum Queue Size:\t\t%d\n", cur_max_msgs
);
319 if (default_settings
) {
320 printf("\tDefault Message Size:\t\t%d\n", cur_def_msgsize
);
321 printf("\tDefault Queue Size:\t\t%d\n", cur_def_msgs
);
324 printf("\n\nTest series 1, behavior when no attr struct "
325 "passed to mq_open:\n");
326 if (!default_settings
) {
327 test_queue(NULL
, &result
);
328 printf("Given sane system settings, mq_open without an attr "
329 "struct succeeds:\tPASS\n");
330 if (result
.mq_maxmsg
!= cur_max_msgs
||
331 result
.mq_msgsize
!= cur_max_msgsize
) {
332 printf("Kernel does not support setting the default "
333 "mq attributes,\nbut also doesn't tie the "
334 "defaults to the maximums:\t\t\tPASS\n");
336 set(max_msgs
, ++cur_max_msgs
);
337 set(max_msgsize
, ++cur_max_msgsize
);
338 test_queue(NULL
, &result
);
339 if (result
.mq_maxmsg
== cur_max_msgs
&&
340 result
.mq_msgsize
== cur_max_msgsize
)
341 printf("Kernel does not support setting the "
342 "default mq attributes and\n"
343 "also ties system wide defaults to "
344 "the system wide maximums:\t\t"
347 printf("Kernel does not support setting the "
348 "default mq attributes,\n"
349 "but also doesn't tie the defaults to "
350 "the maximums:\t\t\tPASS\n");
353 printf("Kernel supports setting defaults separately from "
354 "maximums:\t\tPASS\n");
356 * While we are here, go ahead and test that the kernel
357 * properly follows the default settings
359 test_queue(NULL
, &result
);
360 printf("Given sane values, mq_open without an attr struct "
361 "succeeds:\t\tPASS\n");
362 if (result
.mq_maxmsg
!= cur_def_msgs
||
363 result
.mq_msgsize
!= cur_def_msgsize
)
364 printf("Kernel supports setting defaults, but does "
365 "not actually honor them:\tFAIL\n\n");
367 set(def_msgs
, ++cur_def_msgs
);
368 set(def_msgsize
, ++cur_def_msgsize
);
369 /* In case max was the same as the default */
370 set(max_msgs
, ++cur_max_msgs
);
371 set(max_msgsize
, ++cur_max_msgsize
);
372 test_queue(NULL
, &result
);
373 if (result
.mq_maxmsg
!= cur_def_msgs
||
374 result
.mq_msgsize
!= cur_def_msgsize
)
375 printf("Kernel supports setting defaults, but "
376 "does not actually honor them:\t"
379 printf("Kernel properly honors default setting "
380 "knobs:\t\t\t\tPASS\n");
382 set(def_msgs
, cur_max_msgs
+ 1);
383 cur_def_msgs
= cur_max_msgs
+ 1;
384 set(def_msgsize
, cur_max_msgsize
+ 1);
385 cur_def_msgsize
= cur_max_msgsize
+ 1;
386 if (cur_def_msgs
* (cur_def_msgsize
+ 2 * sizeof(void *)) >=
387 cur_limits
.rlim_cur
) {
388 cur_limits
.rlim_cur
= (cur_def_msgs
+ 2) *
389 (cur_def_msgsize
+ 2 * sizeof(void *));
390 cur_limits
.rlim_max
= 2 * cur_limits
.rlim_cur
;
391 setr(RLIMIT_MSGQUEUE
, &cur_limits
);
393 if (test_queue_fail(NULL
, &result
)) {
394 if (result
.mq_maxmsg
== cur_max_msgs
&&
395 result
.mq_msgsize
== cur_max_msgsize
)
396 printf("Kernel properly limits default values "
397 "to lesser of default/max:\t\tPASS\n");
399 printf("Kernel does not properly set default "
400 "queue parameters when\ndefaults > "
401 "max:\t\t\t\t\t\t\t\tFAIL\n");
403 printf("Kernel fails to open mq because defaults are "
404 "greater than maximums:\tFAIL\n");
405 set(def_msgs
, --cur_def_msgs
);
406 set(def_msgsize
, --cur_def_msgsize
);
407 cur_limits
.rlim_cur
= cur_limits
.rlim_max
= cur_def_msgs
*
409 setr(RLIMIT_MSGQUEUE
, &cur_limits
);
410 if (test_queue_fail(NULL
, &result
))
411 printf("Kernel creates queue even though defaults "
412 "would exceed\nrlimit setting:"
413 "\t\t\t\t\t\t\t\tFAIL\n");
415 printf("Kernel properly fails to create queue when "
416 "defaults would\nexceed rlimit:"
417 "\t\t\t\t\t\t\t\tPASS\n");
421 * Test #2 - open with an attr struct that exceeds rlimit
423 printf("\n\nTest series 2, behavior when attr struct is "
424 "passed to mq_open:\n");
426 cur_max_msgsize
= cur_limits
.rlim_max
>> 4;
427 set(max_msgs
, cur_max_msgs
);
428 set(max_msgsize
, cur_max_msgsize
);
429 attr
.mq_maxmsg
= cur_max_msgs
;
430 attr
.mq_msgsize
= cur_max_msgsize
;
431 if (test_queue_fail(&attr
, &result
))
432 printf("Queue open in excess of rlimit max when euid = 0 "
433 "succeeded:\t\tFAIL\n");
435 printf("Queue open in excess of rlimit max when euid = 0 "
436 "failed:\t\tPASS\n");
437 attr
.mq_maxmsg
= cur_max_msgs
+ 1;
438 attr
.mq_msgsize
= 10;
439 if (test_queue_fail(&attr
, &result
))
440 printf("Queue open with mq_maxmsg > limit when euid = 0 "
441 "succeeded:\t\tPASS\n");
443 printf("Queue open with mq_maxmsg > limit when euid = 0 "
444 "failed:\t\tFAIL\n");
446 attr
.mq_msgsize
= cur_max_msgsize
+ 1;
447 if (test_queue_fail(&attr
, &result
))
448 printf("Queue open with mq_msgsize > limit when euid = 0 "
449 "succeeded:\t\tPASS\n");
451 printf("Queue open with mq_msgsize > limit when euid = 0 "
452 "failed:\t\tFAIL\n");
453 attr
.mq_maxmsg
= 65536;
454 attr
.mq_msgsize
= 65536;
455 if (test_queue_fail(&attr
, &result
))
456 printf("Queue open with total size > 2GB when euid = 0 "
457 "succeeded:\t\tFAIL\n");
459 printf("Queue open with total size > 2GB when euid = 0 "
460 "failed:\t\t\tPASS\n");
462 if (seteuid(99) == -1) {
463 perror("seteuid() failed");
467 attr
.mq_maxmsg
= cur_max_msgs
;
468 attr
.mq_msgsize
= cur_max_msgsize
;
469 if (test_queue_fail(&attr
, &result
))
470 printf("Queue open in excess of rlimit max when euid = 99 "
471 "succeeded:\t\tFAIL\n");
473 printf("Queue open in excess of rlimit max when euid = 99 "
474 "failed:\t\tPASS\n");
475 attr
.mq_maxmsg
= cur_max_msgs
+ 1;
476 attr
.mq_msgsize
= 10;
477 if (test_queue_fail(&attr
, &result
))
478 printf("Queue open with mq_maxmsg > limit when euid = 99 "
479 "succeeded:\t\tFAIL\n");
481 printf("Queue open with mq_maxmsg > limit when euid = 99 "
482 "failed:\t\tPASS\n");
484 attr
.mq_msgsize
= cur_max_msgsize
+ 1;
485 if (test_queue_fail(&attr
, &result
))
486 printf("Queue open with mq_msgsize > limit when euid = 99 "
487 "succeeded:\t\tFAIL\n");
489 printf("Queue open with mq_msgsize > limit when euid = 99 "
490 "failed:\t\tPASS\n");
491 attr
.mq_maxmsg
= 65536;
492 attr
.mq_msgsize
= 65536;
493 if (test_queue_fail(&attr
, &result
))
494 printf("Queue open with total size > 2GB when euid = 99 "
495 "succeeded:\t\tFAIL\n");
497 printf("Queue open with total size > 2GB when euid = 99 "
498 "failed:\t\t\tPASS\n");