tst-mqueue5.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /* Test mq_notify.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <mqueue.h>
  19. #include <limits.h>
  20. #include <signal.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/mman.h>
  26. #include <sys/time.h>
  27. #include <sys/wait.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include "tst-mqueue.h"
  31. #include "../test-skeleton.h"
  32. #ifdef __ARCH_USE_MMU__
  33. #define TIMEOUT 3
  34. #if _POSIX_THREADS
  35. # include <pthread.h>
  36. volatile int rtmin_cnt;
  37. volatile pid_t rtmin_pid;
  38. volatile uid_t rtmin_uid;
  39. volatile int rtmin_code;
  40. volatile union sigval rtmin_sigval;
  41. static void
  42. rtmin_handler (int sig, siginfo_t *info, void *ctx)
  43. {
  44. if (sig != SIGRTMIN)
  45. abort ();
  46. ++rtmin_cnt;
  47. rtmin_pid = info->si_pid;
  48. rtmin_uid = info->si_uid;
  49. rtmin_code = info->si_code;
  50. rtmin_sigval = info->si_value;
  51. }
  52. #define mqsend(q) (mqsend) (q, __LINE__)
  53. static int
  54. (mqsend) (mqd_t q, int line)
  55. {
  56. char c;
  57. if (mq_send (q, &c, 1, 1) != 0)
  58. {
  59. printf ("mq_send on line %d failed with: %m\n", line);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. #define mqrecv(q) (mqrecv) (q, __LINE__)
  65. static int
  66. (mqrecv) (mqd_t q, int line)
  67. {
  68. char c;
  69. ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
  70. if (rets != 1)
  71. {
  72. if (rets == -1)
  73. printf ("mq_receive on line %d failed with: %m\n", line);
  74. else
  75. printf ("mq_receive on line %d returned %zd != 1\n",
  76. line, rets);
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. struct thr_data
  82. {
  83. const char *name;
  84. pthread_barrier_t *b3;
  85. mqd_t q;
  86. };
  87. static void *
  88. thr (void *arg)
  89. {
  90. pthread_barrier_t *b3 = ((struct thr_data *)arg)->b3;
  91. mqd_t q = ((struct thr_data *)arg)->q;
  92. const char *name = ((struct thr_data *)arg)->name;
  93. int result = 0;
  94. result |= mqrecv (q);
  95. (void) pthread_barrier_wait (b3);
  96. /* Child verifies SIGRTMIN has not been sent. */
  97. (void) pthread_barrier_wait (b3);
  98. /* Parent calls mqsend (q), which should trigger notification. */
  99. (void) pthread_barrier_wait (b3);
  100. if (rtmin_cnt != 2)
  101. {
  102. puts ("SIGRTMIN signal in child did not arrive");
  103. result = 1;
  104. }
  105. else if (rtmin_pid != getppid ()
  106. || rtmin_uid != getuid ()
  107. || rtmin_code != SI_MESGQ
  108. || rtmin_sigval.sival_int != 0xdeadbeef)
  109. {
  110. printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (%d)\n",
  111. rtmin_pid, getppid (), rtmin_uid, getuid (),
  112. rtmin_code, SI_MESGQ, rtmin_sigval.sival_int, 0xdeadbeef);
  113. result = 1;
  114. }
  115. struct sigevent ev;
  116. memset (&ev, 0x82, sizeof (ev));
  117. ev.sigev_notify = SIGEV_NONE;
  118. if (mq_notify (q, &ev) != 0)
  119. {
  120. printf ("mq_notify in thread (q, { SIGEV_NONE }) failed with: %m\n");
  121. result = 1;
  122. }
  123. if (mq_notify (q, NULL) != 0)
  124. {
  125. printf ("mq_notify in thread (q, NULL) failed with: %m\n");
  126. result = 1;
  127. }
  128. result |= mqrecv (q);
  129. (void) pthread_barrier_wait (b3);
  130. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  131. (void) pthread_barrier_wait (b3);
  132. if (mq_notify (q, NULL) != 0)
  133. {
  134. printf ("second mq_notify in thread (q, NULL) failed with: %m\n");
  135. result = 1;
  136. }
  137. (void) pthread_barrier_wait (b3);
  138. /* Parent calls mqsend (q), which should not trigger notification. */
  139. (void) pthread_barrier_wait (b3);
  140. /* Child verifies SIGRTMIN has not been received. */
  141. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  142. (void) pthread_barrier_wait (b3);
  143. mqd_t q4 = mq_open (name, O_RDONLY);
  144. if (q4 == (mqd_t) -1)
  145. {
  146. printf ("mq_open in thread failed with: %m\n");
  147. result = 1;
  148. }
  149. if (mq_notify (q4, NULL) != 0)
  150. {
  151. printf ("mq_notify in thread (q4, NULL) failed with: %m\n");
  152. result = 1;
  153. }
  154. if (mq_close (q4) != 0)
  155. {
  156. printf ("mq_close in thread failed with: %m\n");
  157. result = 1;
  158. }
  159. (void) pthread_barrier_wait (b3);
  160. /* Parent calls mqsend (q), which should not trigger notification. */
  161. (void) pthread_barrier_wait (b3);
  162. /* Child verifies SIGRTMIN has not been received. */
  163. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  164. (void) pthread_barrier_wait (b3);
  165. mqd_t q5 = mq_open (name, O_WRONLY);
  166. if (q5 == (mqd_t) -1)
  167. {
  168. printf ("mq_open O_WRONLY in thread failed with: %m\n");
  169. result = 1;
  170. }
  171. if (mq_notify (q5, NULL) != 0)
  172. {
  173. printf ("mq_notify in thread (q5, NULL) failed with: %m\n");
  174. result = 1;
  175. }
  176. if (mq_close (q5) != 0)
  177. {
  178. printf ("mq_close in thread failed with: %m\n");
  179. result = 1;
  180. }
  181. (void) pthread_barrier_wait (b3);
  182. /* Parent calls mqsend (q), which should not trigger notification. */
  183. (void) pthread_barrier_wait (b3);
  184. /* Child verifies SIGRTMIN has not been received. */
  185. return (void *) (long) result;
  186. }
  187. static void
  188. do_child (const char *name, pthread_barrier_t *b2, pthread_barrier_t *b3,
  189. mqd_t q)
  190. {
  191. int result = 0;
  192. struct sigevent ev;
  193. memset (&ev, 0x55, sizeof (ev));
  194. ev.sigev_notify = SIGEV_SIGNAL;
  195. ev.sigev_signo = SIGRTMIN;
  196. ev.sigev_value.sival_ptr = &ev;
  197. if (mq_notify (q, &ev) == 0)
  198. {
  199. puts ("first mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
  200. result = 1;
  201. }
  202. else if (errno != EBUSY)
  203. {
  204. printf ("first mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  205. result = 1;
  206. }
  207. (void) pthread_barrier_wait (b2);
  208. /* Parent calls mqsend (q), which makes notification available. */
  209. (void) pthread_barrier_wait (b2);
  210. rtmin_cnt = 0;
  211. if (mq_notify (q, &ev) != 0)
  212. {
  213. printf ("second mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  214. result = 1;
  215. }
  216. if (rtmin_cnt != 0)
  217. {
  218. puts ("SIGRTMIN signal in child caught too early");
  219. result = 1;
  220. }
  221. (void) pthread_barrier_wait (b2);
  222. /* Parent unsuccessfully attempts to mq_notify. */
  223. /* Parent calls mqsend (q), which makes notification available
  224. and triggers a signal in the child. */
  225. /* Parent successfully calls mq_notify SIGEV_SIGNAL. */
  226. (void) pthread_barrier_wait (b2);
  227. if (rtmin_cnt != 1)
  228. {
  229. puts ("SIGRTMIN signal in child did not arrive");
  230. result = 1;
  231. }
  232. else if (rtmin_pid != getppid ()
  233. || rtmin_uid != getuid ()
  234. || rtmin_code != SI_MESGQ
  235. || rtmin_sigval.sival_ptr != &ev)
  236. {
  237. printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_ptr %p (%p)\n",
  238. rtmin_pid, getppid (), rtmin_uid, getuid (),
  239. rtmin_code, SI_MESGQ, rtmin_sigval.sival_ptr, &ev);
  240. result = 1;
  241. }
  242. result |= mqsend (q);
  243. (void) pthread_barrier_wait (b2);
  244. /* Parent verifies caught SIGRTMIN. */
  245. mqd_t q2 = mq_open (name, O_RDWR);
  246. if (q2 == (mqd_t) -1)
  247. {
  248. printf ("mq_open in child failed with: %m\n");
  249. result = 1;
  250. }
  251. (void) pthread_barrier_wait (b2);
  252. /* Parent mq_open's another mqd_t for the same queue (q3). */
  253. memset (&ev, 0x11, sizeof (ev));
  254. ev.sigev_notify = SIGEV_SIGNAL;
  255. ev.sigev_signo = SIGRTMIN;
  256. ev.sigev_value.sival_ptr = &ev;
  257. if (mq_notify (q2, &ev) != 0)
  258. {
  259. printf ("mq_notify in child (q2, { SIGEV_SIGNAL }) failed with: %m\n");
  260. result = 1;
  261. }
  262. (void) pthread_barrier_wait (b2);
  263. /* Parent unsuccessfully attempts to mq_notify { SIGEV_NONE } on q. */
  264. (void) pthread_barrier_wait (b2);
  265. if (mq_close (q2) != 0)
  266. {
  267. printf ("mq_close failed: %m\n");
  268. result = 1;
  269. }
  270. (void) pthread_barrier_wait (b2);
  271. /* Parent successfully calls mq_notify { SIGEV_NONE } on q3. */
  272. (void) pthread_barrier_wait (b2);
  273. memset (&ev, 0xbb, sizeof (ev));
  274. ev.sigev_notify = SIGEV_SIGNAL;
  275. ev.sigev_signo = SIGRTMIN;
  276. ev.sigev_value.sival_ptr = &b2;
  277. if (mq_notify (q, &ev) == 0)
  278. {
  279. puts ("third mq_notify in child (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
  280. result = 1;
  281. }
  282. else if (errno != EBUSY)
  283. {
  284. printf ("third mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  285. result = 1;
  286. }
  287. (void) pthread_barrier_wait (b2);
  288. /* Parent calls mq_close on q3, which makes the queue available again for
  289. notification. */
  290. (void) pthread_barrier_wait (b2);
  291. memset (&ev, 0x13, sizeof (ev));
  292. ev.sigev_notify = SIGEV_NONE;
  293. if (mq_notify (q, &ev) != 0)
  294. {
  295. printf ("mq_notify in child (q, { SIGEV_NONE }) failed with: %m\n");
  296. result = 1;
  297. }
  298. if (mq_notify (q, NULL) != 0)
  299. {
  300. printf ("mq_notify in child (q, NULL) failed with: %m\n");
  301. result = 1;
  302. }
  303. (void) pthread_barrier_wait (b2);
  304. struct thr_data thr_data = { .name = name, .b3 = b3, .q = q };
  305. pthread_t th;
  306. int ret = pthread_create (&th, NULL, thr, &thr_data);
  307. if (ret)
  308. {
  309. errno = ret;
  310. printf ("pthread_created failed with: %m\n");
  311. result = 1;
  312. }
  313. /* Wait till thr calls mq_receive on the empty queue q and blocks on it. */
  314. sleep (1);
  315. memset (&ev, 0x5f, sizeof (ev));
  316. ev.sigev_notify = SIGEV_SIGNAL;
  317. ev.sigev_signo = SIGRTMIN;
  318. ev.sigev_value.sival_int = 0xdeadbeef;
  319. if (mq_notify (q, &ev) != 0)
  320. {
  321. printf ("fourth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  322. result = 1;
  323. }
  324. (void) pthread_barrier_wait (b2);
  325. /* Parent calls mqsend (q), which should wake up mqrecv (q)
  326. in the thread but no notification should be sent. */
  327. (void) pthread_barrier_wait (b3);
  328. if (rtmin_cnt != 1)
  329. {
  330. puts ("SIGRTMIN signal caught while thr was blocked on mq_receive");
  331. result = 1;
  332. }
  333. (void) pthread_barrier_wait (b3);
  334. /* Parent calls mqsend (q), which should trigger notification. */
  335. (void) pthread_barrier_wait (b3);
  336. /* Thread verifies SIGRTMIN has been received. */
  337. /* Thread calls mq_notify (q, { SIGEV_NONE }) to verify notification is now
  338. available for registration. */
  339. /* Thread calls mq_notify (q, NULL). */
  340. (void) pthread_barrier_wait (b3);
  341. memset (&ev, 0x6a, sizeof (ev));
  342. ev.sigev_notify = SIGEV_SIGNAL;
  343. ev.sigev_signo = SIGRTMIN;
  344. ev.sigev_value.sival_ptr = do_child;
  345. if (mq_notify (q, &ev) != 0)
  346. {
  347. printf ("fifth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  348. result = 1;
  349. }
  350. (void) pthread_barrier_wait (b3);
  351. /* Thread calls mq_notify (q, NULL), which should unregister the above
  352. notification. */
  353. (void) pthread_barrier_wait (b3);
  354. /* Parent calls mqsend (q), which should not trigger notification. */
  355. (void) pthread_barrier_wait (b3);
  356. if (rtmin_cnt != 2)
  357. {
  358. puts ("SIGRTMIN signal caught while notification has been disabled");
  359. result = 1;
  360. }
  361. memset (&ev, 0x7b, sizeof (ev));
  362. ev.sigev_notify = SIGEV_SIGNAL;
  363. ev.sigev_signo = SIGRTMIN;
  364. ev.sigev_value.sival_ptr = thr;
  365. if (mq_notify (q, &ev) != 0)
  366. {
  367. printf ("sixth mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  368. result = 1;
  369. }
  370. (void) pthread_barrier_wait (b3);
  371. /* Thread opens a new O_RDONLY mqd_t (q4). */
  372. /* Thread calls mq_notify (q4, NULL), which should unregister the above
  373. notification. */
  374. /* Thread calls mq_close (q4). */
  375. (void) pthread_barrier_wait (b3);
  376. /* Parent calls mqsend (q), which should not trigger notification. */
  377. (void) pthread_barrier_wait (b3);
  378. if (rtmin_cnt != 2)
  379. {
  380. puts ("SIGRTMIN signal caught while notification has been disabled");
  381. result = 1;
  382. }
  383. memset (&ev, 0xe1, sizeof (ev));
  384. ev.sigev_notify = SIGEV_SIGNAL;
  385. ev.sigev_signo = SIGRTMIN;
  386. ev.sigev_value.sival_int = 127;
  387. if (mq_notify (q, &ev) != 0)
  388. {
  389. printf ("seventh mq_notify in child (q, { SIGEV_SIGNAL }) failed with: %m\n");
  390. result = 1;
  391. }
  392. (void) pthread_barrier_wait (b3);
  393. /* Thread opens a new O_WRONLY mqd_t (q5). */
  394. /* Thread calls mq_notify (q5, NULL), which should unregister the above
  395. notification. */
  396. /* Thread calls mq_close (q5). */
  397. (void) pthread_barrier_wait (b3);
  398. /* Parent calls mqsend (q), which should not trigger notification. */
  399. (void) pthread_barrier_wait (b3);
  400. if (rtmin_cnt != 2)
  401. {
  402. puts ("SIGRTMIN signal caught while notification has been disabled");
  403. result = 1;
  404. }
  405. void *thr_ret;
  406. ret = pthread_join (th, &thr_ret);
  407. if (ret)
  408. {
  409. errno = ret;
  410. printf ("pthread_join failed: %m\n");
  411. result = 1;
  412. }
  413. else if (thr_ret)
  414. result = 1;
  415. if (mq_close (q) != 0)
  416. {
  417. printf ("mq_close failed: %m\n");
  418. result = 1;
  419. }
  420. exit (result);
  421. }
  422. #define TEST_FUNCTION do_test ()
  423. static int
  424. do_test (void)
  425. {
  426. int result = 0;
  427. char tmpfname[] = "/tmp/tst-mqueue5-barrier.XXXXXX";
  428. int fd = mkstemp (tmpfname);
  429. if (fd == -1)
  430. {
  431. printf ("cannot open temporary file: %m\n");
  432. return 1;
  433. }
  434. /* Make sure it is always removed. */
  435. unlink (tmpfname);
  436. /* Create one page of data. */
  437. size_t ps = sysconf (_SC_PAGESIZE);
  438. char data[ps];
  439. memset (data, '\0', ps);
  440. /* Write the data to the file. */
  441. if (write (fd, data, ps) != (ssize_t) ps)
  442. {
  443. puts ("short write");
  444. return 1;
  445. }
  446. void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  447. if (mem == MAP_FAILED)
  448. {
  449. printf ("mmap failed: %m\n");
  450. return 1;
  451. }
  452. pthread_barrier_t *b2;
  453. b2 = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
  454. & ~(__alignof (pthread_barrier_t) - 1));
  455. pthread_barrier_t *b3;
  456. b3 = b2 + 1;
  457. pthread_barrierattr_t a;
  458. if (pthread_barrierattr_init (&a) != 0)
  459. {
  460. puts ("barrierattr_init failed");
  461. return 1;
  462. }
  463. if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  464. {
  465. puts ("barrierattr_setpshared failed, could not test");
  466. return 0;
  467. }
  468. if (pthread_barrier_init (b2, &a, 2) != 0)
  469. {
  470. puts ("barrier_init failed");
  471. return 1;
  472. }
  473. if (pthread_barrier_init (b3, &a, 3) != 0)
  474. {
  475. puts ("barrier_init failed");
  476. return 1;
  477. }
  478. if (pthread_barrierattr_destroy (&a) != 0)
  479. {
  480. puts ("barrierattr_destroy failed");
  481. return 1;
  482. }
  483. char name[sizeof "/tst-mqueue5-" + sizeof (pid_t) * 3];
  484. snprintf (name, sizeof (name), "/tst-mqueue5-%u", getpid ());
  485. struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
  486. mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  487. if (q == (mqd_t) -1)
  488. {
  489. printf ("mq_open failed with: %m\n");
  490. return result;
  491. }
  492. else
  493. add_temp_mq (name);
  494. struct sigevent ev;
  495. memset (&ev, 0xaa, sizeof (ev));
  496. ev.sigev_notify = SIGEV_NONE;
  497. if (mq_notify (q, &ev) != 0)
  498. {
  499. printf ("mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  500. result = 1;
  501. }
  502. if (mq_notify (q, &ev) == 0)
  503. {
  504. puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  505. result = 1;
  506. }
  507. else if (errno != EBUSY)
  508. {
  509. printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  510. result = 1;
  511. }
  512. result |= mqsend (q);
  513. if (mq_notify (q, &ev) != 0)
  514. {
  515. printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  516. result = 1;
  517. }
  518. result |= mqrecv (q);
  519. if (mq_notify (q, NULL) != 0)
  520. {
  521. printf ("mq_notify (q, NULL) failed with: %m\n");
  522. result = 1;
  523. }
  524. if (mq_notify (q, NULL) != 0)
  525. {
  526. /* Implementation-defined behaviour, so don't fail,
  527. just inform. */
  528. printf ("second mq_notify (q, NULL) failed with: %m\n");
  529. }
  530. struct sigaction sa = { .sa_sigaction = rtmin_handler,
  531. .sa_flags = SA_SIGINFO };
  532. sigemptyset (&sa.sa_mask);
  533. sigaction (SIGRTMIN, &sa, NULL);
  534. memset (&ev, 0x55, sizeof (ev));
  535. ev.sigev_notify = SIGEV_SIGNAL;
  536. ev.sigev_signo = SIGRTMIN;
  537. ev.sigev_value.sival_int = 26;
  538. if (mq_notify (q, &ev) != 0)
  539. {
  540. printf ("mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
  541. result = 1;
  542. }
  543. ev.sigev_value.sival_ptr = &ev;
  544. if (mq_notify (q, &ev) == 0)
  545. {
  546. puts ("second mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
  547. result = 1;
  548. }
  549. else if (errno != EBUSY)
  550. {
  551. printf ("second mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
  552. result = 1;
  553. }
  554. if (rtmin_cnt != 0)
  555. {
  556. puts ("SIGRTMIN signal caught too early");
  557. result = 1;
  558. }
  559. result |= mqsend (q);
  560. if (rtmin_cnt != 1)
  561. {
  562. puts ("SIGRTMIN signal did not arrive");
  563. result = 1;
  564. }
  565. else if (rtmin_pid != getpid ()
  566. || rtmin_uid != getuid ()
  567. || rtmin_code != SI_MESGQ
  568. || rtmin_sigval.sival_int != 26)
  569. {
  570. printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (26)\n",
  571. rtmin_pid, getpid (), rtmin_uid, getuid (),
  572. rtmin_code, SI_MESGQ, rtmin_sigval.sival_int);
  573. result = 1;
  574. }
  575. ev.sigev_value.sival_int = 75;
  576. if (mq_notify (q, &ev) != 0)
  577. {
  578. printf ("third mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
  579. result = 1;
  580. }
  581. result |= mqrecv (q);
  582. if (mq_notify (q, NULL) != 0)
  583. {
  584. printf ("mq_notify (q, NULL) failed with: %m\n");
  585. result = 1;
  586. }
  587. memset (&ev, 0x33, sizeof (ev));
  588. ev.sigev_notify = SIGEV_NONE;
  589. if (mq_notify (q, &ev) != 0)
  590. {
  591. printf ("fourth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  592. result = 1;
  593. }
  594. pid_t pid = fork ();
  595. if (pid == -1)
  596. {
  597. printf ("fork () failed: %m\n");
  598. mq_unlink (name);
  599. return 1;
  600. }
  601. if (pid == 0)
  602. do_child (name, b2, b3, q);
  603. /* Child unsuccessfully attempts to mq_notify. */
  604. (void) pthread_barrier_wait (b2);
  605. result |= mqsend (q);
  606. (void) pthread_barrier_wait (b2);
  607. /* Child successfully calls mq_notify SIGEV_SIGNAL now. */
  608. result |= mqrecv (q);
  609. (void) pthread_barrier_wait (b2);
  610. memset (&ev, 0xbb, sizeof (ev));
  611. ev.sigev_notify = SIGEV_SIGNAL;
  612. ev.sigev_signo = SIGRTMIN;
  613. ev.sigev_value.sival_int = 15;
  614. if (mq_notify (q, &ev) == 0)
  615. {
  616. puts ("fourth mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded");
  617. result = 1;
  618. }
  619. else if (errno != EBUSY)
  620. {
  621. printf ("fourth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
  622. result = 1;
  623. }
  624. result |= mqsend (q);
  625. if (mq_notify (q, &ev) != 0)
  626. {
  627. printf ("fifth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n");
  628. result = 1;
  629. }
  630. if (rtmin_cnt != 1)
  631. {
  632. puts ("SIGRTMIN signal caught too early");
  633. result = 1;
  634. }
  635. result |= mqrecv (q);
  636. (void) pthread_barrier_wait (b2);
  637. /* Child verifies caught SIGRTMIN signal. */
  638. /* Child calls mq_send (q) which triggers SIGRTMIN signal here. */
  639. (void) pthread_barrier_wait (b2);
  640. /* Child mq_open's another mqd_t for the same queue (q2). */
  641. if (rtmin_cnt != 2)
  642. {
  643. puts ("SIGRTMIN signal did not arrive");
  644. result = 1;
  645. }
  646. else if (rtmin_pid != pid
  647. || rtmin_uid != getuid ()
  648. || rtmin_code != SI_MESGQ
  649. || rtmin_sigval.sival_int != 15)
  650. {
  651. printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (15)\n",
  652. rtmin_pid, pid, rtmin_uid, getuid (),
  653. rtmin_code, SI_MESGQ, rtmin_sigval.sival_int);
  654. result = 1;
  655. }
  656. result |= mqrecv (q);
  657. (void) pthread_barrier_wait (b2);
  658. /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q2. */
  659. (void) pthread_barrier_wait (b2);
  660. memset (&ev, 0xbb, sizeof (ev));
  661. ev.sigev_notify = SIGEV_NONE;
  662. if (mq_notify (q, &ev) == 0)
  663. {
  664. puts ("fifth mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded");
  665. result = 1;
  666. }
  667. else if (errno != EBUSY)
  668. {
  669. printf ("fifth mq_notify (q, { SIGEV_NONE }) failed with: %m\n");
  670. result = 1;
  671. }
  672. (void) pthread_barrier_wait (b2);
  673. /* Child calls mq_close on q2, which makes the queue available again for
  674. notification. */
  675. mqd_t q3 = mq_open (name, O_RDWR);
  676. if (q3 == (mqd_t) -1)
  677. {
  678. printf ("mq_open q3 in parent failed with: %m\n");
  679. result = 1;
  680. }
  681. (void) pthread_barrier_wait (b2);
  682. memset (&ev, 0x12, sizeof (ev));
  683. ev.sigev_notify = SIGEV_NONE;
  684. if (mq_notify (q3, &ev) != 0)
  685. {
  686. printf ("mq_notify (q3, { SIGEV_NONE }) failed with: %m\n");
  687. result = 1;
  688. }
  689. (void) pthread_barrier_wait (b2);
  690. /* Child unsuccessfully attempts to mq_notify { SIGEV_SIGNAL } on q. */
  691. (void) pthread_barrier_wait (b2);
  692. if (mq_close (q3) != 0)
  693. {
  694. printf ("mq_close failed: %m\n");
  695. result = 1;
  696. }
  697. (void) pthread_barrier_wait (b2);
  698. /* Child successfully calls mq_notify { SIGEV_NONE } on q. */
  699. /* Child successfully calls mq_notify NULL on q. */
  700. (void) pthread_barrier_wait (b2);
  701. /* Child creates new thread. */
  702. /* Thread blocks on mqrecv (q). */
  703. /* Child sleeps for 1sec so that thread has time to reach that point. */
  704. /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q. */
  705. (void) pthread_barrier_wait (b2);
  706. result |= mqsend (q);
  707. (void) pthread_barrier_wait (b3);
  708. /* Child verifies SIGRTMIN has not been sent. */
  709. (void) pthread_barrier_wait (b3);
  710. result |= mqsend (q);
  711. (void) pthread_barrier_wait (b3);
  712. /* Thread verifies SIGRTMIN has been caught. */
  713. /* Thread calls mq_notify (q, { SIGEV_NONE }) to verify notification is now
  714. available for registration. */
  715. /* Thread calls mq_notify (q, NULL). */
  716. (void) pthread_barrier_wait (b3);
  717. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  718. (void) pthread_barrier_wait (b3);
  719. /* Thread calls mq_notify (q, NULL). */
  720. (void) pthread_barrier_wait (b3);
  721. result |= mqsend (q);
  722. result |= mqrecv (q);
  723. (void) pthread_barrier_wait (b3);
  724. /* Child verifies SIGRTMIN has not been sent. */
  725. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  726. (void) pthread_barrier_wait (b3);
  727. /* Thread opens a new O_RDONLY mqd_t (q4). */
  728. /* Thread calls mq_notify (q4, NULL). */
  729. /* Thread calls mq_close (q4). */
  730. (void) pthread_barrier_wait (b3);
  731. result |= mqsend (q);
  732. result |= mqrecv (q);
  733. (void) pthread_barrier_wait (b3);
  734. /* Child verifies SIGRTMIN has not been sent. */
  735. /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */
  736. (void) pthread_barrier_wait (b3);
  737. /* Thread opens a new O_WRONLY mqd_t (q5). */
  738. /* Thread calls mq_notify (q5, NULL). */
  739. /* Thread calls mq_close (q5). */
  740. (void) pthread_barrier_wait (b3);
  741. result |= mqsend (q);
  742. result |= mqrecv (q);
  743. (void) pthread_barrier_wait (b3);
  744. /* Child verifies SIGRTMIN has not been sent. */
  745. int status;
  746. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  747. {
  748. puts ("waitpid failed");
  749. kill (pid, SIGKILL);
  750. result = 1;
  751. }
  752. else if (!WIFEXITED (status) || WEXITSTATUS (status))
  753. {
  754. printf ("child failed with status %d\n", status);
  755. result = 1;
  756. }
  757. if (mq_unlink (name) != 0)
  758. {
  759. printf ("mq_unlink failed: %m\n");
  760. result = 1;
  761. }
  762. if (mq_close (q) != 0)
  763. {
  764. printf ("mq_close failed: %m\n");
  765. result = 1;
  766. }
  767. if (mq_notify (q, NULL) == 0)
  768. {
  769. puts ("mq_notify on closed mqd_t unexpectedly succeeded");
  770. result = 1;
  771. }
  772. else if (errno != EBADF)
  773. {
  774. printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
  775. result = 1;
  776. }
  777. memset (&ev, 0x55, sizeof (ev));
  778. ev.sigev_notify = SIGEV_NONE;
  779. if (mq_notify (q, &ev) == 0)
  780. {
  781. puts ("mq_notify on closed mqd_t unexpectedly succeeded");
  782. result = 1;
  783. }
  784. else if (errno != EBADF)
  785. {
  786. printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n");
  787. result = 1;
  788. }
  789. return result;
  790. }
  791. #else
  792. # define TEST_FUNCTION 0
  793. #endif
  794. #include "../test-skeleton.c"
  795. #else
  796. int main(void)
  797. {
  798. printf("Skipping test on non-mmu host!\n");
  799. return 23;
  800. }
  801. #endif