mq_open_tests.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. 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. char *DEF_MSGS = "/proc/sys/fs/mqueue/msg_default";
  44. char *DEF_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_default";
  45. char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max";
  46. char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max";
  47. int default_settings;
  48. struct rlimit saved_limits, cur_limits;
  49. int saved_def_msgs, saved_def_msgsize, saved_max_msgs, saved_max_msgsize;
  50. int cur_def_msgs, cur_def_msgsize, cur_max_msgs, cur_max_msgsize;
  51. FILE *def_msgs, *def_msgsize, *max_msgs, *max_msgsize;
  52. char *queue_path;
  53. mqd_t queue = -1;
  54. static inline void __set(FILE *stream, int value, char *err_msg);
  55. void shutdown(int exit_val, char *err_cause, int line_no);
  56. static inline int get(FILE *stream);
  57. static inline void set(FILE *stream, int value);
  58. static inline void getr(int type, struct rlimit *rlim);
  59. static inline void setr(int type, struct rlimit *rlim);
  60. void validate_current_settings();
  61. static inline void test_queue(struct mq_attr *attr, struct mq_attr *result);
  62. static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result);
  63. static inline void __set(FILE *stream, int value, char *err_msg)
  64. {
  65. rewind(stream);
  66. if (fprintf(stream, "%d", value) < 0)
  67. perror(err_msg);
  68. }
  69. void shutdown(int exit_val, char *err_cause, int line_no)
  70. {
  71. static int in_shutdown = 0;
  72. /* In case we get called recursively by a set() call below */
  73. if (in_shutdown++)
  74. return;
  75. if (seteuid(0) == -1)
  76. perror("seteuid() failed");
  77. if (queue != -1)
  78. if (mq_close(queue))
  79. perror("mq_close() during shutdown");
  80. if (queue_path)
  81. /*
  82. * Be silent if this fails, if we cleaned up already it's
  83. * expected to fail
  84. */
  85. mq_unlink(queue_path);
  86. if (default_settings) {
  87. if (saved_def_msgs)
  88. __set(def_msgs, saved_def_msgs,
  89. "failed to restore saved_def_msgs");
  90. if (saved_def_msgsize)
  91. __set(def_msgsize, saved_def_msgsize,
  92. "failed to restore saved_def_msgsize");
  93. }
  94. if (saved_max_msgs)
  95. __set(max_msgs, saved_max_msgs,
  96. "failed to restore saved_max_msgs");
  97. if (saved_max_msgsize)
  98. __set(max_msgsize, saved_max_msgsize,
  99. "failed to restore saved_max_msgsize");
  100. if (exit_val)
  101. error(exit_val, errno, "%s at %d", err_cause, line_no);
  102. exit(0);
  103. }
  104. static inline int get(FILE *stream)
  105. {
  106. int value;
  107. rewind(stream);
  108. if (fscanf(stream, "%d", &value) != 1)
  109. shutdown(4, "Error reading /proc entry", __LINE__ - 1);
  110. return value;
  111. }
  112. static inline void set(FILE *stream, int value)
  113. {
  114. int new_value;
  115. rewind(stream);
  116. if (fprintf(stream, "%d", value) < 0)
  117. return shutdown(5, "Failed writing to /proc file",
  118. __LINE__ - 1);
  119. new_value = get(stream);
  120. if (new_value != value)
  121. return shutdown(5, "We didn't get what we wrote to /proc back",
  122. __LINE__ - 1);
  123. }
  124. static inline void getr(int type, struct rlimit *rlim)
  125. {
  126. if (getrlimit(type, rlim))
  127. shutdown(6, "getrlimit()", __LINE__ - 1);
  128. }
  129. static inline void setr(int type, struct rlimit *rlim)
  130. {
  131. if (setrlimit(type, rlim))
  132. shutdown(7, "setrlimit()", __LINE__ - 1);
  133. }
  134. void validate_current_settings()
  135. {
  136. int rlim_needed;
  137. if (cur_limits.rlim_cur < 4096) {
  138. printf("Current rlimit value for POSIX message queue bytes is "
  139. "unreasonably low,\nincreasing.\n\n");
  140. cur_limits.rlim_cur = 8192;
  141. cur_limits.rlim_max = 16384;
  142. setr(RLIMIT_MSGQUEUE, &cur_limits);
  143. }
  144. if (default_settings) {
  145. rlim_needed = (cur_def_msgs + 1) * (cur_def_msgsize + 1 +
  146. 2 * sizeof(void *));
  147. if (rlim_needed > cur_limits.rlim_cur) {
  148. printf("Temporarily lowering default queue parameters "
  149. "to something that will work\n"
  150. "with the current rlimit values.\n\n");
  151. set(def_msgs, 10);
  152. cur_def_msgs = 10;
  153. set(def_msgsize, 128);
  154. cur_def_msgsize = 128;
  155. }
  156. } else {
  157. rlim_needed = (cur_max_msgs + 1) * (cur_max_msgsize + 1 +
  158. 2 * sizeof(void *));
  159. if (rlim_needed > cur_limits.rlim_cur) {
  160. printf("Temporarily lowering maximum queue parameters "
  161. "to something that will work\n"
  162. "with the current rlimit values in case this is "
  163. "a kernel that ties the default\n"
  164. "queue parameters to the maximum queue "
  165. "parameters.\n\n");
  166. set(max_msgs, 10);
  167. cur_max_msgs = 10;
  168. set(max_msgsize, 128);
  169. cur_max_msgsize = 128;
  170. }
  171. }
  172. }
  173. /*
  174. * test_queue - Test opening a queue, shutdown if we fail. This should
  175. * only be called in situations that should never fail. We clean up
  176. * after ourselves and return the queue attributes in *result.
  177. */
  178. static inline void test_queue(struct mq_attr *attr, struct mq_attr *result)
  179. {
  180. int flags = O_RDWR | O_EXCL | O_CREAT;
  181. int perms = DEFFILEMODE;
  182. if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
  183. shutdown(1, "mq_open()", __LINE__);
  184. if (mq_getattr(queue, result))
  185. shutdown(1, "mq_getattr()", __LINE__);
  186. if (mq_close(queue))
  187. shutdown(1, "mq_close()", __LINE__);
  188. queue = -1;
  189. if (mq_unlink(queue_path))
  190. shutdown(1, "mq_unlink()", __LINE__);
  191. }
  192. /*
  193. * Same as test_queue above, but failure is not fatal.
  194. * Returns:
  195. * 0 - Failed to create a queue
  196. * 1 - Created a queue, attributes in *result
  197. */
  198. static inline int test_queue_fail(struct mq_attr *attr, struct mq_attr *result)
  199. {
  200. int flags = O_RDWR | O_EXCL | O_CREAT;
  201. int perms = DEFFILEMODE;
  202. if ((queue = mq_open(queue_path, flags, perms, attr)) == -1)
  203. return 0;
  204. if (mq_getattr(queue, result))
  205. shutdown(1, "mq_getattr()", __LINE__);
  206. if (mq_close(queue))
  207. shutdown(1, "mq_close()", __LINE__);
  208. queue = -1;
  209. if (mq_unlink(queue_path))
  210. shutdown(1, "mq_unlink()", __LINE__);
  211. return 1;
  212. }
  213. int main(int argc, char *argv[])
  214. {
  215. struct mq_attr attr, result;
  216. if (argc != 2) {
  217. fprintf(stderr, "Must pass a valid queue name\n\n");
  218. fprintf(stderr, usage, argv[0]);
  219. exit(1);
  220. }
  221. /*
  222. * Although we can create a msg queue with a non-absolute path name,
  223. * unlink will fail. So, if the name doesn't start with a /, add one
  224. * when we save it.
  225. */
  226. if (*argv[1] == '/')
  227. queue_path = strdup(argv[1]);
  228. else {
  229. queue_path = malloc(strlen(argv[1]) + 2);
  230. if (!queue_path) {
  231. perror("malloc()");
  232. exit(1);
  233. }
  234. queue_path[0] = '/';
  235. queue_path[1] = 0;
  236. strcat(queue_path, argv[1]);
  237. }
  238. if (getuid() != 0) {
  239. fprintf(stderr, "Not running as root, but almost all tests "
  240. "require root in order to modify\nsystem settings. "
  241. "Exiting.\n");
  242. exit(1);
  243. }
  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. }