tst-mqueue5.c 24 KB

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