150069a58SDoug Ledford /*
250069a58SDoug Ledford * This application is Copyright 2012 Red Hat, Inc.
350069a58SDoug Ledford * Doug Ledford <[email protected]>
450069a58SDoug Ledford *
550069a58SDoug Ledford * mq_open_tests is free software: you can redistribute it and/or modify
650069a58SDoug Ledford * it under the terms of the GNU General Public License as published by
750069a58SDoug Ledford * the Free Software Foundation, version 3.
850069a58SDoug Ledford *
950069a58SDoug Ledford * mq_open_tests is distributed in the hope that it will be useful,
1050069a58SDoug Ledford * but WITHOUT ANY WARRANTY; without even the implied warranty of
1150069a58SDoug Ledford * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1250069a58SDoug Ledford * GNU General Public License for more details.
1350069a58SDoug Ledford *
1450069a58SDoug Ledford * For the full text of the license, see <http://www.gnu.org/licenses/>.
1550069a58SDoug Ledford *
1650069a58SDoug Ledford * mq_open_tests.c
1750069a58SDoug Ledford * Tests the various situations that should either succeed or fail to
1850069a58SDoug Ledford * open a posix message queue and then reports whether or not they
1950069a58SDoug Ledford * did as they were supposed to.
2050069a58SDoug Ledford *
2150069a58SDoug Ledford */
2250069a58SDoug Ledford #include <stdio.h>
2350069a58SDoug Ledford #include <stdlib.h>
2450069a58SDoug Ledford #include <unistd.h>
2550069a58SDoug Ledford #include <fcntl.h>
2650069a58SDoug Ledford #include <string.h>
2750069a58SDoug Ledford #include <limits.h>
2850069a58SDoug Ledford #include <errno.h>
2950069a58SDoug Ledford #include <sys/types.h>
3050069a58SDoug Ledford #include <sys/time.h>
3150069a58SDoug Ledford #include <sys/resource.h>
3250069a58SDoug Ledford #include <sys/stat.h>
3350069a58SDoug Ledford #include <mqueue.h>
34c1ee4831SBen Hutchings #include <error.h>
3550069a58SDoug Ledford
36*e6ee6ae4SShuah Khan (Samsung OSG) #include "../kselftest.h"
37*e6ee6ae4SShuah Khan (Samsung OSG)
3850069a58SDoug Ledford static char *usage =
3950069a58SDoug Ledford "Usage:\n"
4050069a58SDoug Ledford " %s path\n"
4150069a58SDoug Ledford "\n"
4250069a58SDoug Ledford " path Path name of the message queue to create\n"
4350069a58SDoug Ledford "\n"
4450069a58SDoug Ledford " Note: this program must be run as root in order to enable all tests\n"
4550069a58SDoug Ledford "\n";
4650069a58SDoug Ledford
4750069a58SDoug Ledford char *DEF_MSGS = "/proc/sys/fs/mqueue/msg_default";
4850069a58SDoug Ledford char *DEF_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_default";
4950069a58SDoug Ledford char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max";
5050069a58SDoug Ledford char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max";
5150069a58SDoug Ledford
5250069a58SDoug Ledford int default_settings;
5350069a58SDoug Ledford struct rlimit saved_limits, cur_limits;
5450069a58SDoug Ledford int saved_def_msgs, saved_def_msgsize, saved_max_msgs, saved_max_msgsize;
5550069a58SDoug Ledford int cur_def_msgs, cur_def_msgsize, cur_max_msgs, cur_max_msgsize;
5650069a58SDoug Ledford FILE *def_msgs, *def_msgsize, *max_msgs, *max_msgsize;
5750069a58SDoug Ledford char *queue_path;
58978e9aa1SShuah Khan (Samsung OSG) char *default_queue_path = "/test1";
5950069a58SDoug Ledford mqd_t queue = -1;
6050069a58SDoug Ledford
6150069a58SDoug Ledford static inline void __set(FILE *stream, int value, char *err_msg);
6250069a58SDoug Ledford void shutdown(int exit_val, char *err_cause, int line_no);
6350069a58SDoug Ledford static inline int get(FILE *stream);
6450069a58SDoug Ledford static inline void set(FILE *stream, int value);
6550069a58SDoug Ledford static inline void getr(int type, struct rlimit *rlim);
6650069a58SDoug Ledford static inline void setr(int type, struct rlimit *rlim);
6750069a58SDoug Ledford void validate_current_settings();
6850069a58SDoug Ledford static inline void test_queue(struct mq_attr *attr, struct mq_attr *result);
6950069a58SDoug Ledford static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result);
7050069a58SDoug Ledford
__set(FILE * stream,int value,char * err_msg)7150069a58SDoug Ledford static inline void __set(FILE *stream, int value, char *err_msg)
7250069a58SDoug Ledford {
7350069a58SDoug Ledford rewind(stream);
7450069a58SDoug Ledford if (fprintf(stream, "%d", value) < 0)
7550069a58SDoug Ledford perror(err_msg);
7650069a58SDoug Ledford }
7750069a58SDoug Ledford
7850069a58SDoug Ledford
shutdown(int exit_val,char * err_cause,int line_no)7950069a58SDoug Ledford void shutdown(int exit_val, char *err_cause, int line_no)
8050069a58SDoug Ledford {
8150069a58SDoug Ledford static int in_shutdown = 0;
8250069a58SDoug Ledford
8350069a58SDoug Ledford /* In case we get called recursively by a set() call below */
8450069a58SDoug Ledford if (in_shutdown++)
8550069a58SDoug Ledford return;
8650069a58SDoug Ledford
87ef9feb68SShuah Khan if (seteuid(0) == -1)
88ef9feb68SShuah Khan perror("seteuid() failed");
8950069a58SDoug Ledford
9050069a58SDoug Ledford if (queue != -1)
9150069a58SDoug Ledford if (mq_close(queue))
9250069a58SDoug Ledford perror("mq_close() during shutdown");
9350069a58SDoug Ledford if (queue_path)
9450069a58SDoug Ledford /*
9550069a58SDoug Ledford * Be silent if this fails, if we cleaned up already it's
9650069a58SDoug Ledford * expected to fail
9750069a58SDoug Ledford */
9850069a58SDoug Ledford mq_unlink(queue_path);
9950069a58SDoug Ledford if (default_settings) {
10050069a58SDoug Ledford if (saved_def_msgs)
10150069a58SDoug Ledford __set(def_msgs, saved_def_msgs,
10250069a58SDoug Ledford "failed to restore saved_def_msgs");
10350069a58SDoug Ledford if (saved_def_msgsize)
10450069a58SDoug Ledford __set(def_msgsize, saved_def_msgsize,
10550069a58SDoug Ledford "failed to restore saved_def_msgsize");
10650069a58SDoug Ledford }
10750069a58SDoug Ledford if (saved_max_msgs)
10850069a58SDoug Ledford __set(max_msgs, saved_max_msgs,
10950069a58SDoug Ledford "failed to restore saved_max_msgs");
11050069a58SDoug Ledford if (saved_max_msgsize)
11150069a58SDoug Ledford __set(max_msgsize, saved_max_msgsize,
11250069a58SDoug Ledford "failed to restore saved_max_msgsize");
11350069a58SDoug Ledford if (exit_val)
11450069a58SDoug Ledford error(exit_val, errno, "%s at %d", err_cause, line_no);
11550069a58SDoug Ledford exit(0);
11650069a58SDoug Ledford }
11750069a58SDoug Ledford
get(FILE * stream)11850069a58SDoug Ledford static inline int get(FILE *stream)
11950069a58SDoug Ledford {
12050069a58SDoug Ledford int value;
12150069a58SDoug Ledford rewind(stream);
12250069a58SDoug Ledford if (fscanf(stream, "%d", &value) != 1)
12350069a58SDoug Ledford shutdown(4, "Error reading /proc entry", __LINE__ - 1);
12450069a58SDoug Ledford return value;
12550069a58SDoug Ledford }
12650069a58SDoug Ledford
set(FILE * stream,int value)12750069a58SDoug Ledford static inline void set(FILE *stream, int value)
12850069a58SDoug Ledford {
12950069a58SDoug Ledford int new_value;
13050069a58SDoug Ledford
13150069a58SDoug Ledford rewind(stream);
13250069a58SDoug Ledford if (fprintf(stream, "%d", value) < 0)
13350069a58SDoug Ledford return shutdown(5, "Failed writing to /proc file",
13450069a58SDoug Ledford __LINE__ - 1);
13550069a58SDoug Ledford new_value = get(stream);
13650069a58SDoug Ledford if (new_value != value)
13750069a58SDoug Ledford return shutdown(5, "We didn't get what we wrote to /proc back",
13850069a58SDoug Ledford __LINE__ - 1);
13950069a58SDoug Ledford }
14050069a58SDoug Ledford
getr(int type,struct rlimit * rlim)14150069a58SDoug Ledford static inline void getr(int type, struct rlimit *rlim)
14250069a58SDoug Ledford {
14350069a58SDoug Ledford if (getrlimit(type, rlim))
14450069a58SDoug Ledford shutdown(6, "getrlimit()", __LINE__ - 1);
14550069a58SDoug Ledford }
14650069a58SDoug Ledford
setr(int type,struct rlimit * rlim)14750069a58SDoug Ledford static inline void setr(int type, struct rlimit *rlim)
14850069a58SDoug Ledford {
14950069a58SDoug Ledford if (setrlimit(type, rlim))
15050069a58SDoug Ledford shutdown(7, "setrlimit()", __LINE__ - 1);
15150069a58SDoug Ledford }
15250069a58SDoug Ledford
validate_current_settings()15350069a58SDoug Ledford void validate_current_settings()
15450069a58SDoug Ledford {
15550069a58SDoug Ledford int rlim_needed;
15650069a58SDoug Ledford
15750069a58SDoug Ledford if (cur_limits.rlim_cur < 4096) {
15850069a58SDoug Ledford printf("Current rlimit value for POSIX message queue bytes is "
15950069a58SDoug Ledford "unreasonably low,\nincreasing.\n\n");
16050069a58SDoug Ledford cur_limits.rlim_cur = 8192;
16150069a58SDoug Ledford cur_limits.rlim_max = 16384;
16250069a58SDoug Ledford setr(RLIMIT_MSGQUEUE, &cur_limits);
16350069a58SDoug Ledford }
16450069a58SDoug Ledford
16550069a58SDoug Ledford if (default_settings) {
16650069a58SDoug Ledford rlim_needed = (cur_def_msgs + 1) * (cur_def_msgsize + 1 +
16750069a58SDoug Ledford 2 * sizeof(void *));
16850069a58SDoug Ledford if (rlim_needed > cur_limits.rlim_cur) {
16950069a58SDoug Ledford printf("Temporarily lowering default queue parameters "
17050069a58SDoug Ledford "to something that will work\n"
17150069a58SDoug Ledford "with the current rlimit values.\n\n");
17250069a58SDoug Ledford set(def_msgs, 10);
17350069a58SDoug Ledford cur_def_msgs = 10;
17450069a58SDoug Ledford set(def_msgsize, 128);
17550069a58SDoug Ledford cur_def_msgsize = 128;
17650069a58SDoug Ledford }
17750069a58SDoug Ledford } else {
17850069a58SDoug Ledford rlim_needed = (cur_max_msgs + 1) * (cur_max_msgsize + 1 +
17950069a58SDoug Ledford 2 * sizeof(void *));
18050069a58SDoug Ledford if (rlim_needed > cur_limits.rlim_cur) {
18150069a58SDoug Ledford printf("Temporarily lowering maximum queue parameters "
18250069a58SDoug Ledford "to something that will work\n"
18350069a58SDoug Ledford "with the current rlimit values in case this is "
18450069a58SDoug Ledford "a kernel that ties the default\n"
18550069a58SDoug Ledford "queue parameters to the maximum queue "
18650069a58SDoug Ledford "parameters.\n\n");
18750069a58SDoug Ledford set(max_msgs, 10);
18850069a58SDoug Ledford cur_max_msgs = 10;
18950069a58SDoug Ledford set(max_msgsize, 128);
19050069a58SDoug Ledford cur_max_msgsize = 128;
19150069a58SDoug Ledford }
19250069a58SDoug Ledford }
19350069a58SDoug Ledford }
19450069a58SDoug Ledford
19550069a58SDoug Ledford /*
19650069a58SDoug Ledford * test_queue - Test opening a queue, shutdown if we fail. This should
19750069a58SDoug Ledford * only be called in situations that should never fail. We clean up
19850069a58SDoug Ledford * after ourselves and return the queue attributes in *result.
19950069a58SDoug Ledford */
test_queue(struct mq_attr * attr,struct mq_attr * result)20050069a58SDoug Ledford static inline void test_queue(struct mq_attr *attr, struct mq_attr *result)
20150069a58SDoug Ledford {
20250069a58SDoug Ledford int flags = O_RDWR | O_EXCL | O_CREAT;
20350069a58SDoug Ledford int perms = DEFFILEMODE;
20450069a58SDoug Ledford
20550069a58SDoug Ledford if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
20650069a58SDoug Ledford shutdown(1, "mq_open()", __LINE__);
20750069a58SDoug Ledford if (mq_getattr(queue, result))
20850069a58SDoug Ledford shutdown(1, "mq_getattr()", __LINE__);
20950069a58SDoug Ledford if (mq_close(queue))
21050069a58SDoug Ledford shutdown(1, "mq_close()", __LINE__);
21150069a58SDoug Ledford queue = -1;
21250069a58SDoug Ledford if (mq_unlink(queue_path))
21350069a58SDoug Ledford shutdown(1, "mq_unlink()", __LINE__);
21450069a58SDoug Ledford }
21550069a58SDoug Ledford
21650069a58SDoug Ledford /*
21750069a58SDoug Ledford * Same as test_queue above, but failure is not fatal.
21850069a58SDoug Ledford * Returns:
21950069a58SDoug Ledford * 0 - Failed to create a queue
22050069a58SDoug Ledford * 1 - Created a queue, attributes in *result
22150069a58SDoug Ledford */
test_queue_fail(struct mq_attr * attr,struct mq_attr * result)22250069a58SDoug Ledford static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result)
22350069a58SDoug Ledford {
22450069a58SDoug Ledford int flags = O_RDWR | O_EXCL | O_CREAT;
22550069a58SDoug Ledford int perms = DEFFILEMODE;
22650069a58SDoug Ledford
22750069a58SDoug Ledford if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
22850069a58SDoug Ledford return 0;
22950069a58SDoug Ledford if (mq_getattr(queue, result))
23050069a58SDoug Ledford shutdown(1, "mq_getattr()", __LINE__);
23150069a58SDoug Ledford if (mq_close(queue))
23250069a58SDoug Ledford shutdown(1, "mq_close()", __LINE__);
23350069a58SDoug Ledford queue = -1;
23450069a58SDoug Ledford if (mq_unlink(queue_path))
23550069a58SDoug Ledford shutdown(1, "mq_unlink()", __LINE__);
23650069a58SDoug Ledford return 1;
23750069a58SDoug Ledford }
23850069a58SDoug Ledford
main(int argc,char * argv[])23950069a58SDoug Ledford int main(int argc, char *argv[])
24050069a58SDoug Ledford {
24150069a58SDoug Ledford struct mq_attr attr, result;
24250069a58SDoug Ledford
24350069a58SDoug Ledford if (argc != 2) {
244978e9aa1SShuah Khan (Samsung OSG) printf("Using Default queue path - %s\n", default_queue_path);
245978e9aa1SShuah Khan (Samsung OSG) queue_path = default_queue_path;
246978e9aa1SShuah Khan (Samsung OSG) } else {
24750069a58SDoug Ledford
24850069a58SDoug Ledford /*
24950069a58SDoug Ledford * Although we can create a msg queue with a non-absolute path name,
25050069a58SDoug Ledford * unlink will fail. So, if the name doesn't start with a /, add one
25150069a58SDoug Ledford * when we save it.
25250069a58SDoug Ledford */
25350069a58SDoug Ledford if (*argv[1] == '/')
25450069a58SDoug Ledford queue_path = strdup(argv[1]);
25550069a58SDoug Ledford else {
25650069a58SDoug Ledford queue_path = malloc(strlen(argv[1]) + 2);
25750069a58SDoug Ledford if (!queue_path) {
25850069a58SDoug Ledford perror("malloc()");
25950069a58SDoug Ledford exit(1);
26050069a58SDoug Ledford }
26150069a58SDoug Ledford queue_path[0] = '/';
26250069a58SDoug Ledford queue_path[1] = 0;
26350069a58SDoug Ledford strcat(queue_path, argv[1]);
26450069a58SDoug Ledford }
265978e9aa1SShuah Khan (Samsung OSG) }
26650069a58SDoug Ledford
267*e6ee6ae4SShuah Khan (Samsung OSG) if (getuid() != 0)
268*e6ee6ae4SShuah Khan (Samsung OSG) ksft_exit_skip("Not running as root, but almost all tests "
26950069a58SDoug Ledford "require root in order to modify\nsystem settings. "
27050069a58SDoug Ledford "Exiting.\n");
27150069a58SDoug Ledford
27250069a58SDoug Ledford /* Find out what files there are for us to make tweaks in */
27350069a58SDoug Ledford def_msgs = fopen(DEF_MSGS, "r+");
27450069a58SDoug Ledford def_msgsize = fopen(DEF_MSGSIZE, "r+");
27550069a58SDoug Ledford max_msgs = fopen(MAX_MSGS, "r+");
27650069a58SDoug Ledford max_msgsize = fopen(MAX_MSGSIZE, "r+");
27750069a58SDoug Ledford
27850069a58SDoug Ledford if (!max_msgs)
27950069a58SDoug Ledford shutdown(2, "Failed to open msg_max", __LINE__);
28050069a58SDoug Ledford if (!max_msgsize)
28150069a58SDoug Ledford shutdown(2, "Failed to open msgsize_max", __LINE__);
28250069a58SDoug Ledford if (def_msgs || def_msgsize)
28350069a58SDoug Ledford default_settings = 1;
28450069a58SDoug Ledford
28550069a58SDoug Ledford /* Load up the current system values for everything we can */
28650069a58SDoug Ledford getr(RLIMIT_MSGQUEUE, &saved_limits);
28750069a58SDoug Ledford cur_limits = saved_limits;
28850069a58SDoug Ledford if (default_settings) {
28950069a58SDoug Ledford saved_def_msgs = cur_def_msgs = get(def_msgs);
29050069a58SDoug Ledford saved_def_msgsize = cur_def_msgsize = get(def_msgsize);
29150069a58SDoug Ledford }
29250069a58SDoug Ledford saved_max_msgs = cur_max_msgs = get(max_msgs);
29350069a58SDoug Ledford saved_max_msgsize = cur_max_msgsize = get(max_msgsize);
29450069a58SDoug Ledford
29550069a58SDoug Ledford /* Tell the user our initial state */
29650069a58SDoug Ledford printf("\nInitial system state:\n");
29750069a58SDoug Ledford printf("\tUsing queue path:\t\t%s\n", queue_path);
298ef9feb68SShuah Khan printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n",
299ef9feb68SShuah Khan (long) saved_limits.rlim_cur);
300ef9feb68SShuah Khan printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n",
301ef9feb68SShuah Khan (long) saved_limits.rlim_max);
30250069a58SDoug Ledford printf("\tMaximum Message Size:\t\t%d\n", saved_max_msgsize);
30350069a58SDoug Ledford printf("\tMaximum Queue Size:\t\t%d\n", saved_max_msgs);
30450069a58SDoug Ledford if (default_settings) {
30550069a58SDoug Ledford printf("\tDefault Message Size:\t\t%d\n", saved_def_msgsize);
30650069a58SDoug Ledford printf("\tDefault Queue Size:\t\t%d\n", saved_def_msgs);
30750069a58SDoug Ledford } else {
30850069a58SDoug Ledford printf("\tDefault Message Size:\t\tNot Supported\n");
30950069a58SDoug Ledford printf("\tDefault Queue Size:\t\tNot Supported\n");
31050069a58SDoug Ledford }
31150069a58SDoug Ledford printf("\n");
31250069a58SDoug Ledford
31350069a58SDoug Ledford validate_current_settings();
31450069a58SDoug Ledford
31550069a58SDoug Ledford printf("Adjusted system state for testing:\n");
316ef9feb68SShuah Khan printf("\tRLIMIT_MSGQUEUE(soft):\t\t%ld\n", (long) cur_limits.rlim_cur);
317ef9feb68SShuah Khan printf("\tRLIMIT_MSGQUEUE(hard):\t\t%ld\n", (long) cur_limits.rlim_max);
31850069a58SDoug Ledford printf("\tMaximum Message Size:\t\t%d\n", cur_max_msgsize);
31950069a58SDoug Ledford printf("\tMaximum Queue Size:\t\t%d\n", cur_max_msgs);
32050069a58SDoug Ledford if (default_settings) {
32150069a58SDoug Ledford printf("\tDefault Message Size:\t\t%d\n", cur_def_msgsize);
32250069a58SDoug Ledford printf("\tDefault Queue Size:\t\t%d\n", cur_def_msgs);
32350069a58SDoug Ledford }
32450069a58SDoug Ledford
32550069a58SDoug Ledford printf("\n\nTest series 1, behavior when no attr struct "
32650069a58SDoug Ledford "passed to mq_open:\n");
32750069a58SDoug Ledford if (!default_settings) {
32850069a58SDoug Ledford test_queue(NULL, &result);
32950069a58SDoug Ledford printf("Given sane system settings, mq_open without an attr "
33050069a58SDoug Ledford "struct succeeds:\tPASS\n");
33150069a58SDoug Ledford if (result.mq_maxmsg != cur_max_msgs ||
33250069a58SDoug Ledford result.mq_msgsize != cur_max_msgsize) {
33350069a58SDoug Ledford printf("Kernel does not support setting the default "
33450069a58SDoug Ledford "mq attributes,\nbut also doesn't tie the "
33550069a58SDoug Ledford "defaults to the maximums:\t\t\tPASS\n");
33650069a58SDoug Ledford } else {
33750069a58SDoug Ledford set(max_msgs, ++cur_max_msgs);
33850069a58SDoug Ledford set(max_msgsize, ++cur_max_msgsize);
33950069a58SDoug Ledford test_queue(NULL, &result);
34050069a58SDoug Ledford if (result.mq_maxmsg == cur_max_msgs &&
34150069a58SDoug Ledford result.mq_msgsize == cur_max_msgsize)
34250069a58SDoug Ledford printf("Kernel does not support setting the "
34350069a58SDoug Ledford "default mq attributes and\n"
34450069a58SDoug Ledford "also ties system wide defaults to "
34550069a58SDoug Ledford "the system wide maximums:\t\t"
34650069a58SDoug Ledford "FAIL\n");
34750069a58SDoug Ledford else
34850069a58SDoug Ledford printf("Kernel does not support setting the "
34950069a58SDoug Ledford "default mq attributes,\n"
35050069a58SDoug Ledford "but also doesn't tie the defaults to "
35150069a58SDoug Ledford "the maximums:\t\t\tPASS\n");
35250069a58SDoug Ledford }
35350069a58SDoug Ledford } else {
35450069a58SDoug Ledford printf("Kernel supports setting defaults separately from "
35550069a58SDoug Ledford "maximums:\t\tPASS\n");
35650069a58SDoug Ledford /*
35750069a58SDoug Ledford * While we are here, go ahead and test that the kernel
35850069a58SDoug Ledford * properly follows the default settings
35950069a58SDoug Ledford */
36050069a58SDoug Ledford test_queue(NULL, &result);
36150069a58SDoug Ledford printf("Given sane values, mq_open without an attr struct "
36250069a58SDoug Ledford "succeeds:\t\tPASS\n");
36350069a58SDoug Ledford if (result.mq_maxmsg != cur_def_msgs ||
36450069a58SDoug Ledford result.mq_msgsize != cur_def_msgsize)
36550069a58SDoug Ledford printf("Kernel supports setting defaults, but does "
36650069a58SDoug Ledford "not actually honor them:\tFAIL\n\n");
36750069a58SDoug Ledford else {
36850069a58SDoug Ledford set(def_msgs, ++cur_def_msgs);
36950069a58SDoug Ledford set(def_msgsize, ++cur_def_msgsize);
37050069a58SDoug Ledford /* In case max was the same as the default */
37150069a58SDoug Ledford set(max_msgs, ++cur_max_msgs);
37250069a58SDoug Ledford set(max_msgsize, ++cur_max_msgsize);
37350069a58SDoug Ledford test_queue(NULL, &result);
37450069a58SDoug Ledford if (result.mq_maxmsg != cur_def_msgs ||
37550069a58SDoug Ledford result.mq_msgsize != cur_def_msgsize)
37650069a58SDoug Ledford printf("Kernel supports setting defaults, but "
37750069a58SDoug Ledford "does not actually honor them:\t"
37850069a58SDoug Ledford "FAIL\n");
37950069a58SDoug Ledford else
38050069a58SDoug Ledford printf("Kernel properly honors default setting "
38150069a58SDoug Ledford "knobs:\t\t\t\tPASS\n");
38250069a58SDoug Ledford }
38350069a58SDoug Ledford set(def_msgs, cur_max_msgs + 1);
38450069a58SDoug Ledford cur_def_msgs = cur_max_msgs + 1;
38550069a58SDoug Ledford set(def_msgsize, cur_max_msgsize + 1);
38650069a58SDoug Ledford cur_def_msgsize = cur_max_msgsize + 1;
38750069a58SDoug Ledford if (cur_def_msgs * (cur_def_msgsize + 2 * sizeof(void *)) >=
38850069a58SDoug Ledford cur_limits.rlim_cur) {
38950069a58SDoug Ledford cur_limits.rlim_cur = (cur_def_msgs + 2) *
39050069a58SDoug Ledford (cur_def_msgsize + 2 * sizeof(void *));
39150069a58SDoug Ledford cur_limits.rlim_max = 2 * cur_limits.rlim_cur;
39250069a58SDoug Ledford setr(RLIMIT_MSGQUEUE, &cur_limits);
39350069a58SDoug Ledford }
39450069a58SDoug Ledford if (test_queue_fail(NULL, &result)) {
39550069a58SDoug Ledford if (result.mq_maxmsg == cur_max_msgs &&
39650069a58SDoug Ledford result.mq_msgsize == cur_max_msgsize)
39750069a58SDoug Ledford printf("Kernel properly limits default values "
39850069a58SDoug Ledford "to lesser of default/max:\t\tPASS\n");
39950069a58SDoug Ledford else
40050069a58SDoug Ledford printf("Kernel does not properly set default "
40150069a58SDoug Ledford "queue parameters when\ndefaults > "
40250069a58SDoug Ledford "max:\t\t\t\t\t\t\t\tFAIL\n");
40350069a58SDoug Ledford } else
40450069a58SDoug Ledford printf("Kernel fails to open mq because defaults are "
40550069a58SDoug Ledford "greater than maximums:\tFAIL\n");
40650069a58SDoug Ledford set(def_msgs, --cur_def_msgs);
40750069a58SDoug Ledford set(def_msgsize, --cur_def_msgsize);
40850069a58SDoug Ledford cur_limits.rlim_cur = cur_limits.rlim_max = cur_def_msgs *
40950069a58SDoug Ledford cur_def_msgsize;
41050069a58SDoug Ledford setr(RLIMIT_MSGQUEUE, &cur_limits);
41150069a58SDoug Ledford if (test_queue_fail(NULL, &result))
41250069a58SDoug Ledford printf("Kernel creates queue even though defaults "
41350069a58SDoug Ledford "would exceed\nrlimit setting:"
41450069a58SDoug Ledford "\t\t\t\t\t\t\t\tFAIL\n");
41550069a58SDoug Ledford else
41650069a58SDoug Ledford printf("Kernel properly fails to create queue when "
41750069a58SDoug Ledford "defaults would\nexceed rlimit:"
41850069a58SDoug Ledford "\t\t\t\t\t\t\t\tPASS\n");
41950069a58SDoug Ledford }
42050069a58SDoug Ledford
42150069a58SDoug Ledford /*
42250069a58SDoug Ledford * Test #2 - open with an attr struct that exceeds rlimit
42350069a58SDoug Ledford */
42450069a58SDoug Ledford printf("\n\nTest series 2, behavior when attr struct is "
42550069a58SDoug Ledford "passed to mq_open:\n");
42650069a58SDoug Ledford cur_max_msgs = 32;
42750069a58SDoug Ledford cur_max_msgsize = cur_limits.rlim_max >> 4;
42850069a58SDoug Ledford set(max_msgs, cur_max_msgs);
42950069a58SDoug Ledford set(max_msgsize, cur_max_msgsize);
43050069a58SDoug Ledford attr.mq_maxmsg = cur_max_msgs;
43150069a58SDoug Ledford attr.mq_msgsize = cur_max_msgsize;
43250069a58SDoug Ledford if (test_queue_fail(&attr, &result))
43350069a58SDoug Ledford printf("Queue open in excess of rlimit max when euid = 0 "
43450069a58SDoug Ledford "succeeded:\t\tFAIL\n");
43550069a58SDoug Ledford else
43650069a58SDoug Ledford printf("Queue open in excess of rlimit max when euid = 0 "
43750069a58SDoug Ledford "failed:\t\tPASS\n");
43850069a58SDoug Ledford attr.mq_maxmsg = cur_max_msgs + 1;
43950069a58SDoug Ledford attr.mq_msgsize = 10;
44050069a58SDoug Ledford if (test_queue_fail(&attr, &result))
44150069a58SDoug Ledford printf("Queue open with mq_maxmsg > limit when euid = 0 "
44250069a58SDoug Ledford "succeeded:\t\tPASS\n");
44350069a58SDoug Ledford else
44450069a58SDoug Ledford printf("Queue open with mq_maxmsg > limit when euid = 0 "
44550069a58SDoug Ledford "failed:\t\tFAIL\n");
44650069a58SDoug Ledford attr.mq_maxmsg = 1;
44750069a58SDoug Ledford attr.mq_msgsize = cur_max_msgsize + 1;
44850069a58SDoug Ledford if (test_queue_fail(&attr, &result))
44950069a58SDoug Ledford printf("Queue open with mq_msgsize > limit when euid = 0 "
45050069a58SDoug Ledford "succeeded:\t\tPASS\n");
45150069a58SDoug Ledford else
45250069a58SDoug Ledford printf("Queue open with mq_msgsize > limit when euid = 0 "
45350069a58SDoug Ledford "failed:\t\tFAIL\n");
45450069a58SDoug Ledford attr.mq_maxmsg = 65536;
45550069a58SDoug Ledford attr.mq_msgsize = 65536;
45650069a58SDoug Ledford if (test_queue_fail(&attr, &result))
45750069a58SDoug Ledford printf("Queue open with total size > 2GB when euid = 0 "
45850069a58SDoug Ledford "succeeded:\t\tFAIL\n");
45950069a58SDoug Ledford else
46050069a58SDoug Ledford printf("Queue open with total size > 2GB when euid = 0 "
46150069a58SDoug Ledford "failed:\t\t\tPASS\n");
462ef9feb68SShuah Khan
463ef9feb68SShuah Khan if (seteuid(99) == -1) {
464ef9feb68SShuah Khan perror("seteuid() failed");
465ef9feb68SShuah Khan exit(1);
466ef9feb68SShuah Khan }
467ef9feb68SShuah Khan
46850069a58SDoug Ledford attr.mq_maxmsg = cur_max_msgs;
46950069a58SDoug Ledford attr.mq_msgsize = cur_max_msgsize;
47050069a58SDoug Ledford if (test_queue_fail(&attr, &result))
47150069a58SDoug Ledford printf("Queue open in excess of rlimit max when euid = 99 "
47250069a58SDoug Ledford "succeeded:\t\tFAIL\n");
47350069a58SDoug Ledford else
47450069a58SDoug Ledford printf("Queue open in excess of rlimit max when euid = 99 "
47550069a58SDoug Ledford "failed:\t\tPASS\n");
47650069a58SDoug Ledford attr.mq_maxmsg = cur_max_msgs + 1;
47750069a58SDoug Ledford attr.mq_msgsize = 10;
47850069a58SDoug Ledford if (test_queue_fail(&attr, &result))
47950069a58SDoug Ledford printf("Queue open with mq_maxmsg > limit when euid = 99 "
48050069a58SDoug Ledford "succeeded:\t\tFAIL\n");
48150069a58SDoug Ledford else
48250069a58SDoug Ledford printf("Queue open with mq_maxmsg > limit when euid = 99 "
48350069a58SDoug Ledford "failed:\t\tPASS\n");
48450069a58SDoug Ledford attr.mq_maxmsg = 1;
48550069a58SDoug Ledford attr.mq_msgsize = cur_max_msgsize + 1;
48650069a58SDoug Ledford if (test_queue_fail(&attr, &result))
48750069a58SDoug Ledford printf("Queue open with mq_msgsize > limit when euid = 99 "
48850069a58SDoug Ledford "succeeded:\t\tFAIL\n");
48950069a58SDoug Ledford else
49050069a58SDoug Ledford printf("Queue open with mq_msgsize > limit when euid = 99 "
49150069a58SDoug Ledford "failed:\t\tPASS\n");
49250069a58SDoug Ledford attr.mq_maxmsg = 65536;
49350069a58SDoug Ledford attr.mq_msgsize = 65536;
49450069a58SDoug Ledford if (test_queue_fail(&attr, &result))
49550069a58SDoug Ledford printf("Queue open with total size > 2GB when euid = 99 "
49650069a58SDoug Ledford "succeeded:\t\tFAIL\n");
49750069a58SDoug Ledford else
49850069a58SDoug Ledford printf("Queue open with total size > 2GB when euid = 99 "
49950069a58SDoug Ledford "failed:\t\t\tPASS\n");
50050069a58SDoug Ledford
50150069a58SDoug Ledford shutdown(0,"",0);
50250069a58SDoug Ledford }
503