mq_open_tests.c 15 KB

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