mq_open_tests.c 15 KB

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