diff options
Diffstat (limited to 'tools/testing/selftests/mqueue/mq_open_tests.c')
| -rw-r--r-- | tools/testing/selftests/mqueue/mq_open_tests.c | 492 |
1 files changed, 492 insertions, 0 deletions
diff --git a/tools/testing/selftests/mqueue/mq_open_tests.c b/tools/testing/selftests/mqueue/mq_open_tests.c new file mode 100644 index 000000000000..711cc2923047 --- /dev/null +++ b/tools/testing/selftests/mqueue/mq_open_tests.c | |||
| @@ -0,0 +1,492 @@ | |||
| 1 | /* | ||
| 2 | * This application is Copyright 2012 Red Hat, Inc. | ||
| 3 | * Doug Ledford <dledford@redhat.com> | ||
| 4 | * | ||
| 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. | ||
| 8 | * | ||
| 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. | ||
| 13 | * | ||
| 14 | * For the full text of the license, see <http://www.gnu.org/licenses/>. | ||
| 15 | * | ||
| 16 | * mq_open_tests.c | ||
| 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. | ||
| 20 | * | ||
| 21 | */ | ||
| 22 | #include <stdio.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <fcntl.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <limits.h> | ||
| 28 | #include <errno.h> | ||
| 29 | #include <sys/types.h> | ||
| 30 | #include <sys/time.h> | ||
| 31 | #include <sys/resource.h> | ||
| 32 | #include <sys/stat.h> | ||
| 33 | #include <mqueue.h> | ||
| 34 | |||
| 35 | static char *usage = | ||
| 36 | "Usage:\n" | ||
| 37 | " %s path\n" | ||
| 38 | "\n" | ||
| 39 | " path Path name of the message queue to create\n" | ||
| 40 | "\n" | ||
| 41 | " Note: this program must be run as root in order to enable all tests\n" | ||
| 42 | "\n"; | ||
| 43 | |||
| 44 | char *DEF_MSGS = "/proc/sys/fs/mqueue/msg_default"; | ||
| 45 | char *DEF_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_default"; | ||
| 46 | char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max"; | ||
| 47 | char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max"; | ||
| 48 | |||
| 49 | int default_settings; | ||
| 50 | struct rlimit saved_limits, cur_limits; | ||
| 51 | int saved_def_msgs, saved_def_msgsize, saved_max_msgs, saved_max_msgsize; | ||
| 52 | int cur_def_msgs, cur_def_msgsize, cur_max_msgs, cur_max_msgsize; | ||
| 53 | FILE *def_msgs, *def_msgsize, *max_msgs, *max_msgsize; | ||
| 54 | char *queue_path; | ||
| 55 | mqd_t queue = -1; | ||
| 56 | |||
| 57 | static inline void __set(FILE *stream, int value, char *err_msg); | ||
| 58 | void shutdown(int exit_val, char *err_cause, int line_no); | ||
| 59 | static inline int get(FILE *stream); | ||
| 60 | static inline void set(FILE *stream, int value); | ||
| 61 | static inline void getr(int type, struct rlimit *rlim); | ||
| 62 | static inline void setr(int type, struct rlimit *rlim); | ||
| 63 | void validate_current_settings(); | ||
| 64 | static inline void test_queue(struct mq_attr *attr, struct mq_attr *result); | ||
| 65 | static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result); | ||
| 66 | |||
| 67 | static inline void __set(FILE *stream, int value, char *err_msg) | ||
| 68 | { | ||
| 69 | rewind(stream); | ||
| 70 | if (fprintf(stream, "%d", value) < 0) | ||
| 71 | perror(err_msg); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | void shutdown(int exit_val, char *err_cause, int line_no) | ||
| 76 | { | ||
| 77 | static int in_shutdown = 0; | ||
| 78 | |||
| 79 | /* In case we get called recursively by a set() call below */ | ||
| 80 | if (in_shutdown++) | ||
| 81 | return; | ||
| 82 | |||
| 83 | seteuid(0); | ||
| 84 | |||
| 85 | if (queue != -1) | ||
| 86 | if (mq_close(queue)) | ||
| 87 | perror("mq_close() during shutdown"); | ||
| 88 | if (queue_path) | ||
| 89 | /* | ||
| 90 | * Be silent if this fails, if we cleaned up already it's | ||
| 91 | * expected to fail | ||
| 92 | */ | ||
| 93 | mq_unlink(queue_path); | ||
| 94 | if (default_settings) { | ||
| 95 | if (saved_def_msgs) | ||
| 96 | __set(def_msgs, saved_def_msgs, | ||
| 97 | "failed to restore saved_def_msgs"); | ||
| 98 | if (saved_def_msgsize) | ||
| 99 | __set(def_msgsize, saved_def_msgsize, | ||
| 100 | "failed to restore saved_def_msgsize"); | ||
| 101 | } | ||
| 102 | if (saved_max_msgs) | ||
| 103 | __set(max_msgs, saved_max_msgs, | ||
| 104 | "failed to restore saved_max_msgs"); | ||
| 105 | if (saved_max_msgsize) | ||
| 106 | __set(max_msgsize, saved_max_msgsize, | ||
| 107 | "failed to restore saved_max_msgsize"); | ||
| 108 | if (exit_val) | ||
| 109 | error(exit_val, errno, "%s at %d", err_cause, line_no); | ||
| 110 | exit(0); | ||
| 111 | } | ||
| 112 | |||
| 113 | static inline int get(FILE *stream) | ||
| 114 | { | ||
| 115 | int value; | ||
| 116 | rewind(stream); | ||
| 117 | if (fscanf(stream, "%d", &value) != 1) | ||
| 118 | shutdown(4, "Error reading /proc entry", __LINE__ - 1); | ||
| 119 | return value; | ||
| 120 | } | ||
| 121 | |||
| 122 | static inline void set(FILE *stream, int value) | ||
| 123 | { | ||
| 124 | int new_value; | ||
| 125 | |||
| 126 | rewind(stream); | ||
| 127 | if (fprintf(stream, "%d", value) < 0) | ||
| 128 | return shutdown(5, "Failed writing to /proc file", | ||
| 129 | __LINE__ - 1); | ||
| 130 | new_value = get(stream); | ||
| 131 | if (new_value != value) | ||
| 132 | return shutdown(5, "We didn't get what we wrote to /proc back", | ||
| 133 | __LINE__ - 1); | ||
| 134 | } | ||
| 135 | |||
| 136 | static inline void getr(int type, struct rlimit *rlim) | ||
| 137 | { | ||
| 138 | if (getrlimit(type, rlim)) | ||
| 139 | shutdown(6, "getrlimit()", __LINE__ - 1); | ||
| 140 | } | ||
| 141 | |||
| 142 | static inline void setr(int type, struct rlimit *rlim) | ||
| 143 | { | ||
| 144 | if (setrlimit(type, rlim)) | ||
| 145 | shutdown(7, "setrlimit()", __LINE__ - 1); | ||
| 146 | } | ||
| 147 | |||
| 148 | void validate_current_settings() | ||
| 149 | { | ||
| 150 | int rlim_needed; | ||
| 151 | |||
| 152 | if (cur_limits.rlim_cur < 4096) { | ||
| 153 | printf("Current rlimit value for POSIX message queue bytes is " | ||
| 154 | "unreasonably low,\nincreasing.\n\n"); | ||
| 155 | cur_limits.rlim_cur = 8192; | ||
| 156 | cur_limits.rlim_max = 16384; | ||
| 157 | setr(RLIMIT_MSGQUEUE, &cur_limits); | ||
| 158 | } | ||
| 159 | |||
| 160 | if (default_settings) { | ||
| 161 | rlim_needed = (cur_def_msgs + 1) * (cur_def_msgsize + 1 + | ||
| 162 | 2 * sizeof(void *)); | ||
| 163 | if (rlim_needed > cur_limits.rlim_cur) { | ||
| 164 | printf("Temporarily lowering default queue parameters " | ||
| 165 | "to something that will work\n" | ||
| 166 | "with the current rlimit values.\n\n"); | ||
| 167 | set(def_msgs, 10); | ||
| 168 | cur_def_msgs = 10; | ||
| 169 | set(def_msgsize, 128); | ||
| 170 | cur_def_msgsize = 128; | ||
| 171 | } | ||
| 172 | } else { | ||
| 173 | rlim_needed = (cur_max_msgs + 1) * (cur_max_msgsize + 1 + | ||
| 174 | 2 * sizeof(void *)); | ||
| 175 | if (rlim_needed > cur_limits.rlim_cur) { | ||
| 176 | printf("Temporarily lowering maximum queue parameters " | ||
| 177 | "to something that will work\n" | ||
| 178 | "with the current rlimit values in case this is " | ||
| 179 | "a kernel that ties the default\n" | ||
| 180 | "queue parameters to the maximum queue " | ||
| 181 | "parameters.\n\n"); | ||
| 182 | set(max_msgs, 10); | ||
| 183 | cur_max_msgs = 10; | ||
| 184 | |||
