tst-mqueue5.c 24 KB

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