mq_open_tests.c 16 KB

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