tst-mqueue5.c 24 KB

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