tst-cancel4.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. <http://www.gnu.org/licenses/>. */
  15. /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
  16. exit to be called more than once. */
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <limits.h>
  20. #include <pthread.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <termios.h>
  26. #include <unistd.h>
  27. #include <sys/mman.h>
  28. #include <sys/msg.h>
  29. #include <sys/poll.h>
  30. #include <sys/select.h>
  31. #include <sys/socket.h>
  32. #include <sys/uio.h>
  33. #include <sys/un.h>
  34. #include <sys/wait.h>
  35. #include "pthreadP.h"
  36. /* Since STREAMS are not supported in the standard Linux kernel and
  37. there we don't advertise STREAMS as supported is no need to test
  38. the STREAMS related functions. This affects
  39. getmsg() getpmsg() putmsg()
  40. putpmsg()
  41. lockf() and fcntl() are tested in tst-cancel16.
  42. pthread_join() is tested in tst-join5.
  43. pthread_testcancel()'s only purpose is to allow cancellation. This
  44. is tested in several places.
  45. sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
  46. mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
  47. in tst-mqueue8{,x} tests.
  48. aio_suspend() is tested in tst-cancel17.
  49. clock_nanosleep() is tested in tst-cancel18.
  50. */
  51. /* Pipe descriptors. */
  52. static int fds[2];
  53. /* Temporary file descriptor, to be closed after each round. */
  54. static int tempfd = -1;
  55. static int tempfd2 = -1;
  56. /* Name of temporary file to be removed after each round. */
  57. static char *tempfname;
  58. /* Temporary message queue. */
  59. static int tempmsg = -1;
  60. /* Often used barrier for two threads. */
  61. static pthread_barrier_t b2;
  62. #ifndef IPC_ADDVAL
  63. # define IPC_ADDVAL 0
  64. #endif
  65. /* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
  66. the socket send buffer size to '1', a write of this size on that
  67. socket will block.
  68. The Linux kernel imposes a minimum send socket buffer size which
  69. has changed over the years. As of Linux 3.10 the value is:
  70. 2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
  71. which is attempting to make sure that with standard MTUs,
  72. TCP can always queue up at least 2 full sized packets.
  73. Furthermore, there is logic in the socket send paths that
  74. will allow one more packet (of any size) to be queued up as
  75. long as some socket buffer space remains. Blocking only
  76. occurs when we try to queue up a new packet and the send
  77. buffer space has already been fully consumed.
  78. Therefore we must set this value to the largest possible value of
  79. the formula above (and since it depends upon the size of "struct
  80. sk_buff", it is dependent upon machine word size etc.) plus some
  81. slack space. */
  82. #define WRITE_BUFFER_SIZE 16384
  83. /* Cleanup handling test. */
  84. static int cl_called;
  85. static void
  86. cl (void *arg)
  87. {
  88. ++cl_called;
  89. }
  90. static void *
  91. tf_read (void *arg)
  92. {
  93. int fd;
  94. int r;
  95. if (arg == NULL)
  96. fd = fds[0];
  97. else
  98. {
  99. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  100. tempfd = fd = mkstemp (fname);
  101. if (fd == -1)
  102. printf ("%s: mkstemp failed\n", __FUNCTION__);
  103. unlink (fname);
  104. r = pthread_barrier_wait (&b2);
  105. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  106. {
  107. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  108. exit (1);
  109. }
  110. }
  111. r = pthread_barrier_wait (&b2);
  112. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  113. {
  114. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  115. exit (1);
  116. }
  117. ssize_t s;
  118. pthread_cleanup_push (cl, NULL);
  119. char buf[100];
  120. s = read (fd, buf, sizeof (buf));
  121. pthread_cleanup_pop (0);
  122. printf ("%s: read returns with %zd\n", __FUNCTION__, s);
  123. exit (1);
  124. }
  125. static void *
  126. tf_readv (void *arg)
  127. {
  128. int fd;
  129. int r;
  130. if (arg == NULL)
  131. fd = fds[0];
  132. else
  133. {
  134. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  135. tempfd = fd = mkstemp (fname);
  136. if (fd == -1)
  137. printf ("%s: mkstemp failed\n", __FUNCTION__);
  138. unlink (fname);
  139. r = pthread_barrier_wait (&b2);
  140. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  141. {
  142. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  143. exit (1);
  144. }
  145. }
  146. r = pthread_barrier_wait (&b2);
  147. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  148. {
  149. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  150. exit (1);
  151. }
  152. ssize_t s;
  153. pthread_cleanup_push (cl, NULL);
  154. char buf[100];
  155. struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
  156. s = readv (fd, iov, 1);
  157. pthread_cleanup_pop (0);
  158. printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
  159. exit (1);
  160. }
  161. static void *
  162. tf_write (void *arg)
  163. {
  164. int fd;
  165. int r;
  166. if (arg == NULL)
  167. fd = fds[1];
  168. else
  169. {
  170. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  171. tempfd = fd = mkstemp (fname);
  172. if (fd == -1)
  173. printf ("%s: mkstemp failed\n", __FUNCTION__);
  174. unlink (fname);
  175. r = pthread_barrier_wait (&b2);
  176. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  177. {
  178. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  179. exit (1);
  180. }
  181. }
  182. r = pthread_barrier_wait (&b2);
  183. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  184. {
  185. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  186. exit (1);
  187. }
  188. ssize_t s;
  189. pthread_cleanup_push (cl, NULL);
  190. char buf[WRITE_BUFFER_SIZE];
  191. memset (buf, '\0', sizeof (buf));
  192. s = write (fd, buf, sizeof (buf));
  193. pthread_cleanup_pop (0);
  194. printf ("%s: write returns with %zd\n", __FUNCTION__, s);
  195. exit (1);
  196. }
  197. static void *
  198. tf_writev (void *arg)
  199. {
  200. int fd;
  201. int r;
  202. if (arg == NULL)
  203. fd = fds[1];
  204. else
  205. {
  206. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  207. tempfd = fd = mkstemp (fname);
  208. if (fd == -1)
  209. printf ("%s: mkstemp failed\n", __FUNCTION__);
  210. unlink (fname);
  211. r = pthread_barrier_wait (&b2);
  212. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  213. {
  214. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  215. exit (1);
  216. }
  217. }
  218. r = pthread_barrier_wait (&b2);
  219. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  220. {
  221. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  222. exit (1);
  223. }
  224. ssize_t s;
  225. pthread_cleanup_push (cl, NULL);
  226. char buf[WRITE_BUFFER_SIZE];
  227. memset (buf, '\0', sizeof (buf));
  228. struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
  229. s = writev (fd, iov, 1);
  230. pthread_cleanup_pop (0);
  231. printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
  232. exit (1);
  233. }
  234. static void *
  235. tf_sleep (void *arg)
  236. {
  237. int r = pthread_barrier_wait (&b2);
  238. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  239. {
  240. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  241. exit (1);
  242. }
  243. if (arg != NULL)
  244. {
  245. r = pthread_barrier_wait (&b2);
  246. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  247. {
  248. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  249. exit (1);
  250. }
  251. }
  252. pthread_cleanup_push (cl, NULL);
  253. sleep (arg == NULL ? 1000000 : 0);
  254. pthread_cleanup_pop (0);
  255. printf ("%s: sleep returns\n", __FUNCTION__);
  256. exit (1);
  257. }
  258. static void *
  259. tf_usleep (void *arg)
  260. {
  261. int r = pthread_barrier_wait (&b2);
  262. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  263. {
  264. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  265. exit (1);
  266. }
  267. if (arg != NULL)
  268. {
  269. r = pthread_barrier_wait (&b2);
  270. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  271. {
  272. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  273. exit (1);
  274. }
  275. }
  276. pthread_cleanup_push (cl, NULL);
  277. usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
  278. pthread_cleanup_pop (0);
  279. printf ("%s: usleep returns\n", __FUNCTION__);
  280. exit (1);
  281. }
  282. static void *
  283. tf_nanosleep (void *arg)
  284. {
  285. int r = pthread_barrier_wait (&b2);
  286. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  287. {
  288. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  289. exit (1);
  290. }
  291. if (arg != NULL)
  292. {
  293. r = pthread_barrier_wait (&b2);
  294. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  295. {
  296. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  297. exit (1);
  298. }
  299. }
  300. pthread_cleanup_push (cl, NULL);
  301. struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
  302. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  303. pthread_cleanup_pop (0);
  304. printf ("%s: nanosleep returns\n", __FUNCTION__);
  305. exit (1);
  306. }
  307. static void *
  308. tf_select (void *arg)
  309. {
  310. int fd;
  311. int r;
  312. if (arg == NULL)
  313. fd = fds[0];
  314. else
  315. {
  316. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  317. tempfd = fd = mkstemp (fname);
  318. if (fd == -1)
  319. printf ("%s: mkstemp failed\n", __FUNCTION__);
  320. unlink (fname);
  321. r = pthread_barrier_wait (&b2);
  322. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  323. {
  324. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  325. exit (1);
  326. }
  327. }
  328. r = pthread_barrier_wait (&b2);
  329. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  330. {
  331. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  332. exit (1);
  333. }
  334. fd_set rfs;
  335. FD_ZERO (&rfs);
  336. FD_SET (fd, &rfs);
  337. int s;
  338. pthread_cleanup_push (cl, NULL);
  339. s = select (fd + 1, &rfs, NULL, NULL, NULL);
  340. pthread_cleanup_pop (0);
  341. printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
  342. strerror (errno));
  343. exit (1);
  344. }
  345. static void *
  346. tf_pselect (void *arg)
  347. {
  348. int fd;
  349. int r;
  350. if (arg == NULL)
  351. fd = fds[0];
  352. else
  353. {
  354. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  355. tempfd = fd = mkstemp (fname);
  356. if (fd == -1)
  357. printf ("%s: mkstemp failed\n", __FUNCTION__);
  358. unlink (fname);
  359. r = pthread_barrier_wait (&b2);
  360. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  361. {
  362. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  363. exit (1);
  364. }
  365. }
  366. r = pthread_barrier_wait (&b2);
  367. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  368. {
  369. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  370. exit (1);
  371. }
  372. fd_set rfs;
  373. FD_ZERO (&rfs);
  374. FD_SET (fd, &rfs);
  375. int s;
  376. pthread_cleanup_push (cl, NULL);
  377. s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
  378. pthread_cleanup_pop (0);
  379. printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
  380. strerror (errno));
  381. exit (1);
  382. }
  383. static void *
  384. tf_poll (void *arg)
  385. {
  386. int fd;
  387. int r;
  388. if (arg == NULL)
  389. fd = fds[0];
  390. else
  391. {
  392. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  393. tempfd = fd = mkstemp (fname);
  394. if (fd == -1)
  395. printf ("%s: mkstemp failed\n", __FUNCTION__);
  396. unlink (fname);
  397. r = pthread_barrier_wait (&b2);
  398. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  399. {
  400. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  401. exit (1);
  402. }
  403. }
  404. r = pthread_barrier_wait (&b2);
  405. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  406. {
  407. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  408. exit (1);
  409. }
  410. struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
  411. int s;
  412. pthread_cleanup_push (cl, NULL);
  413. s = poll (rfs, 1, -1);
  414. pthread_cleanup_pop (0);
  415. printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
  416. strerror (errno));
  417. exit (1);
  418. }
  419. static void *
  420. tf_ppoll (void *arg)
  421. {
  422. int fd;
  423. int r;
  424. if (arg == NULL)
  425. fd = fds[0];
  426. else
  427. {
  428. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  429. tempfd = fd = mkstemp (fname);
  430. if (fd == -1)
  431. printf ("%s: mkstemp failed\n", __FUNCTION__);
  432. unlink (fname);
  433. r = pthread_barrier_wait (&b2);
  434. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  435. {
  436. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  437. exit (1);
  438. }
  439. }
  440. r = pthread_barrier_wait (&b2);
  441. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  442. {
  443. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  444. exit (1);
  445. }
  446. struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
  447. int s;
  448. pthread_cleanup_push (cl, NULL);
  449. s = ppoll (rfs, 1, NULL, NULL);
  450. pthread_cleanup_pop (0);
  451. printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
  452. strerror (errno));
  453. exit (1);
  454. }
  455. static void *
  456. tf_wait (void *arg)
  457. {
  458. pid_t pid = fork ();
  459. if (pid == -1)
  460. {
  461. puts ("fork failed");
  462. exit (1);
  463. }
  464. if (pid == 0)
  465. {
  466. /* Make the program disappear after a while. */
  467. if (arg == NULL)
  468. sleep (10);
  469. exit (0);
  470. }
  471. int r;
  472. if (arg != NULL)
  473. {
  474. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  475. while (nanosleep (&ts, &ts) != 0)
  476. continue;
  477. r = pthread_barrier_wait (&b2);
  478. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  479. {
  480. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  481. exit (1);
  482. }
  483. }
  484. r = pthread_barrier_wait (&b2);
  485. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  486. {
  487. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  488. exit (1);
  489. }
  490. int s;
  491. pthread_cleanup_push (cl, NULL);
  492. s = wait (NULL);
  493. pthread_cleanup_pop (0);
  494. printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
  495. strerror (errno));
  496. exit (1);
  497. }
  498. static void *
  499. tf_waitpid (void *arg)
  500. {
  501. pid_t pid = fork ();
  502. if (pid == -1)
  503. {
  504. puts ("fork failed");
  505. exit (1);
  506. }
  507. if (pid == 0)
  508. {
  509. /* Make the program disappear after a while. */
  510. if (arg == NULL)
  511. sleep (10);
  512. exit (0);
  513. }
  514. int r;
  515. if (arg != NULL)
  516. {
  517. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  518. while (nanosleep (&ts, &ts) != 0)
  519. continue;
  520. r = pthread_barrier_wait (&b2);
  521. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  522. {
  523. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  524. exit (1);
  525. }
  526. }
  527. r = pthread_barrier_wait (&b2);
  528. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  529. {
  530. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  531. exit (1);
  532. }
  533. int s;
  534. pthread_cleanup_push (cl, NULL);
  535. s = waitpid (-1, NULL, 0);
  536. pthread_cleanup_pop (0);
  537. printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
  538. strerror (errno));
  539. exit (1);
  540. }
  541. static void *
  542. tf_waitid (void *arg)
  543. {
  544. pid_t pid = fork ();
  545. if (pid == -1)
  546. {
  547. puts ("fork failed");
  548. exit (1);
  549. }
  550. if (pid == 0)
  551. {
  552. /* Make the program disappear after a while. */
  553. if (arg == NULL)
  554. sleep (10);
  555. exit (0);
  556. }
  557. int r;
  558. if (arg != NULL)
  559. {
  560. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  561. while (nanosleep (&ts, &ts) != 0)
  562. continue;
  563. r = pthread_barrier_wait (&b2);
  564. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  565. {
  566. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  567. exit (1);
  568. }
  569. }
  570. r = pthread_barrier_wait (&b2);
  571. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  572. {
  573. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  574. exit (1);
  575. }
  576. int s;
  577. pthread_cleanup_push (cl, NULL);
  578. #ifndef WEXITED
  579. # define WEXITED 0
  580. #endif
  581. siginfo_t si;
  582. s = waitid (P_PID, pid, &si, WEXITED);
  583. pthread_cleanup_pop (0);
  584. printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
  585. strerror (errno));
  586. exit (1);
  587. }
  588. static void *
  589. tf_sigpause (void *arg)
  590. {
  591. int r = pthread_barrier_wait (&b2);
  592. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  593. {
  594. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  595. exit (1);
  596. }
  597. if (arg != NULL)
  598. {
  599. r = pthread_barrier_wait (&b2);
  600. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  601. {
  602. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  603. exit (1);
  604. }
  605. }
  606. pthread_cleanup_push (cl, NULL);
  607. sigpause (SIGCANCEL);
  608. pthread_cleanup_pop (0);
  609. printf ("%s: sigpause returned\n", __FUNCTION__);
  610. exit (1);
  611. }
  612. static void *
  613. tf_sigsuspend (void *arg)
  614. {
  615. int r = pthread_barrier_wait (&b2);
  616. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  617. {
  618. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  619. exit (1);
  620. }
  621. if (arg != NULL)
  622. {
  623. r = pthread_barrier_wait (&b2);
  624. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  625. {
  626. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  627. exit (1);
  628. }
  629. }
  630. pthread_cleanup_push (cl, NULL);
  631. /* Just for fun block all signals. */
  632. sigset_t mask;
  633. sigfillset (&mask);
  634. sigsuspend (&mask);
  635. pthread_cleanup_pop (0);
  636. printf ("%s: sigsuspend returned\n", __FUNCTION__);
  637. exit (1);
  638. }
  639. static void *
  640. tf_sigwait (void *arg)
  641. {
  642. int r = pthread_barrier_wait (&b2);
  643. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  644. {
  645. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  646. exit (1);
  647. }
  648. if (arg != NULL)
  649. {
  650. r = pthread_barrier_wait (&b2);
  651. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  652. {
  653. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  654. exit (1);
  655. }
  656. }
  657. /* Block SIGUSR1. */
  658. sigset_t mask;
  659. sigemptyset (&mask);
  660. sigaddset (&mask, SIGUSR1);
  661. if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
  662. {
  663. printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
  664. exit (1);
  665. }
  666. int sig;
  667. pthread_cleanup_push (cl, NULL);
  668. /* Wait for SIGUSR1. */
  669. sigwait (&mask, &sig);
  670. pthread_cleanup_pop (0);
  671. printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
  672. exit (1);
  673. }
  674. static void *
  675. tf_sigwaitinfo (void *arg)
  676. {
  677. int r = pthread_barrier_wait (&b2);
  678. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  679. {
  680. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  681. exit (1);
  682. }
  683. if (arg != NULL)
  684. {
  685. r = pthread_barrier_wait (&b2);
  686. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  687. {
  688. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  689. exit (1);
  690. }
  691. }
  692. /* Block SIGUSR1. */
  693. sigset_t mask;
  694. sigemptyset (&mask);
  695. sigaddset (&mask, SIGUSR1);
  696. if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
  697. {
  698. printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
  699. exit (1);
  700. }
  701. siginfo_t info;
  702. pthread_cleanup_push (cl, NULL);
  703. /* Wait for SIGUSR1. */
  704. sigwaitinfo (&mask, &info);
  705. pthread_cleanup_pop (0);
  706. printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
  707. info.si_signo);
  708. exit (1);
  709. }
  710. static void *
  711. tf_sigtimedwait (void *arg)
  712. {
  713. int r = pthread_barrier_wait (&b2);
  714. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  715. {
  716. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  717. exit (1);
  718. }
  719. if (arg != NULL)
  720. {
  721. r = pthread_barrier_wait (&b2);
  722. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  723. {
  724. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  725. exit (1);
  726. }
  727. }
  728. /* Block SIGUSR1. */
  729. sigset_t mask;
  730. sigemptyset (&mask);
  731. sigaddset (&mask, SIGUSR1);
  732. if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
  733. {
  734. printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
  735. exit (1);
  736. }
  737. /* Wait for SIGUSR1. */
  738. siginfo_t info;
  739. struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
  740. pthread_cleanup_push (cl, NULL);
  741. sigtimedwait (&mask, &info, &ts);
  742. pthread_cleanup_pop (0);
  743. printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
  744. info.si_signo);
  745. exit (1);
  746. }
  747. static void *
  748. tf_pause (void *arg)
  749. {
  750. int r = pthread_barrier_wait (&b2);
  751. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  752. {
  753. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  754. exit (1);
  755. }
  756. if (arg != NULL)
  757. {
  758. r = pthread_barrier_wait (&b2);
  759. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  760. {
  761. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  762. exit (1);
  763. }
  764. }
  765. pthread_cleanup_push (cl, NULL);
  766. pause ();
  767. pthread_cleanup_pop (0);
  768. printf ("%s: pause returned\n", __FUNCTION__);
  769. exit (1);
  770. }
  771. static void *
  772. tf_accept (void *arg)
  773. {
  774. int tfd;
  775. struct sockaddr_un sun;
  776. /* To test a non-blocking accept call we make the call file by using
  777. a datagrame socket. */
  778. int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
  779. tempfd = socket (AF_UNIX, pf, 0);
  780. if (tempfd == -1)
  781. {
  782. printf ("%s: socket call failed\n", __FUNCTION__);
  783. exit (1);
  784. }
  785. int tries = 0;
  786. do
  787. {
  788. if (++tries > 10)
  789. {
  790. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  791. /* prevent endless loop, when bind fails forever */
  792. exit (1);
  793. }
  794. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
  795. tfd = mkstemp(sun.sun_path);
  796. if (tfd < 0)
  797. {
  798. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  799. exit (1);
  800. }
  801. close(tfd);
  802. sun.sun_family = AF_UNIX;
  803. }
  804. while (bind (tempfd, (struct sockaddr *) &sun,
  805. offsetof (struct sockaddr_un, sun_path)
  806. + strlen (sun.sun_path) + 1) != 0);
  807. unlink (sun.sun_path);
  808. listen (tempfd, 5);
  809. socklen_t len = sizeof (sun);
  810. int r = pthread_barrier_wait (&b2);
  811. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  812. {
  813. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  814. exit (1);
  815. }
  816. if (arg != NULL)
  817. {
  818. r = pthread_barrier_wait (&b2);
  819. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  820. {
  821. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  822. exit (1);
  823. }
  824. }
  825. pthread_cleanup_push (cl, NULL);
  826. accept (tempfd, (struct sockaddr *) &sun, &len);
  827. pthread_cleanup_pop (0);
  828. printf ("%s: accept returned\n", __FUNCTION__);
  829. exit (1);
  830. }
  831. static void *
  832. tf_send (void *arg)
  833. {
  834. int tfd;
  835. struct sockaddr_un sun;
  836. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  837. if (tempfd == -1)
  838. {
  839. printf ("%s: first socket call failed\n", __FUNCTION__);
  840. exit (1);
  841. }
  842. int tries = 0;
  843. do
  844. {
  845. if (++tries > 10)
  846. {
  847. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  848. }
  849. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
  850. tfd = mkstemp(sun.sun_path);
  851. if (tfd < 0)
  852. {
  853. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  854. exit (1);
  855. }
  856. close(tfd);
  857. sun.sun_family = AF_UNIX;
  858. }
  859. while (bind (tempfd, (struct sockaddr *) &sun,
  860. offsetof (struct sockaddr_un, sun_path)
  861. + strlen (sun.sun_path) + 1) != 0);
  862. listen (tempfd, 5);
  863. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  864. if (tempfd2 == -1)
  865. {
  866. printf ("%s: second socket call failed\n", __FUNCTION__);
  867. exit (1);
  868. }
  869. if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
  870. {
  871. printf ("%s: connect failed\n", __FUNCTION__);
  872. exit(1);
  873. }
  874. unlink (sun.sun_path);
  875. int r = pthread_barrier_wait (&b2);
  876. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  877. {
  878. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  879. exit (1);
  880. }
  881. if (arg != NULL)
  882. {
  883. r = pthread_barrier_wait (&b2);
  884. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  885. {
  886. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  887. exit (1);
  888. }
  889. }
  890. pthread_cleanup_push (cl, NULL);
  891. /* Very large block, so that the send call blocks. */
  892. char mem[700000];
  893. send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
  894. pthread_cleanup_pop (0);
  895. printf ("%s: send returned\n", __FUNCTION__);
  896. exit (1);
  897. }
  898. static void *
  899. tf_recv (void *arg)
  900. {
  901. int tfd;
  902. struct sockaddr_un sun;
  903. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  904. if (tempfd == -1)
  905. {
  906. printf ("%s: first socket call failed\n", __FUNCTION__);
  907. exit (1);
  908. }
  909. int tries = 0;
  910. do
  911. {
  912. if (++tries > 10)
  913. {
  914. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  915. }
  916. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
  917. tfd = mkstemp(sun.sun_path);
  918. if (tfd < 0)
  919. {
  920. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  921. exit (1);
  922. }
  923. close(tfd);
  924. sun.sun_family = AF_UNIX;
  925. }
  926. while (bind (tempfd, (struct sockaddr *) &sun,
  927. offsetof (struct sockaddr_un, sun_path)
  928. + strlen (sun.sun_path) + 1) != 0);
  929. listen (tempfd, 5);
  930. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  931. if (tempfd2 == -1)
  932. {
  933. printf ("%s: second socket call failed\n", __FUNCTION__);
  934. exit (1);
  935. }
  936. if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
  937. {
  938. printf ("%s: connect failed\n", __FUNCTION__);
  939. exit(1);
  940. }
  941. unlink (sun.sun_path);
  942. int r = pthread_barrier_wait (&b2);
  943. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  944. {
  945. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  946. exit (1);
  947. }
  948. if (arg != NULL)
  949. {
  950. r = pthread_barrier_wait (&b2);
  951. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  952. {
  953. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  954. exit (1);
  955. }
  956. }
  957. pthread_cleanup_push (cl, NULL);
  958. char mem[70];
  959. recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
  960. pthread_cleanup_pop (0);
  961. printf ("%s: recv returned\n", __FUNCTION__);
  962. exit (1);
  963. }
  964. static void *
  965. tf_recvfrom (void *arg)
  966. {
  967. int tfd;
  968. struct sockaddr_un sun;
  969. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  970. if (tempfd == -1)
  971. {
  972. printf ("%s: first socket call failed\n", __FUNCTION__);
  973. exit (1);
  974. }
  975. int tries = 0;
  976. do
  977. {
  978. if (++tries > 10)
  979. {
  980. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  981. }
  982. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
  983. tfd = mkstemp(sun.sun_path);
  984. if (tfd < 0)
  985. {
  986. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  987. exit (1);
  988. }
  989. close(tfd);
  990. sun.sun_family = AF_UNIX;
  991. }
  992. while (bind (tempfd, (struct sockaddr *) &sun,
  993. offsetof (struct sockaddr_un, sun_path)
  994. + strlen (sun.sun_path) + 1) != 0);
  995. tempfname = strdup (sun.sun_path);
  996. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  997. if (tempfd2 == -1)
  998. {
  999. printf ("%s: second socket call failed\n", __FUNCTION__);
  1000. exit (1);
  1001. }
  1002. int r = pthread_barrier_wait (&b2);
  1003. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1004. {
  1005. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1006. exit (1);
  1007. }
  1008. if (arg != NULL)
  1009. {
  1010. r = pthread_barrier_wait (&b2);
  1011. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1012. {
  1013. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1014. exit (1);
  1015. }
  1016. }
  1017. pthread_cleanup_push (cl, NULL);
  1018. char mem[70];
  1019. socklen_t len = sizeof (sun);
  1020. recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
  1021. (struct sockaddr *) &sun, &len);
  1022. pthread_cleanup_pop (0);
  1023. printf ("%s: recvfrom returned\n", __FUNCTION__);
  1024. exit (1);
  1025. }
  1026. static void *
  1027. tf_recvmsg (void *arg)
  1028. {
  1029. int tfd;
  1030. struct sockaddr_un sun;
  1031. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1032. if (tempfd == -1)
  1033. {
  1034. printf ("%s: first socket call failed\n", __FUNCTION__);
  1035. exit (1);
  1036. }
  1037. int tries = 0;
  1038. do
  1039. {
  1040. if (++tries > 10)
  1041. {
  1042. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1043. }
  1044. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
  1045. tfd = mkstemp(sun.sun_path);
  1046. if (tfd < 0)
  1047. {
  1048. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1049. exit (1);
  1050. }
  1051. close(tfd);
  1052. sun.sun_family = AF_UNIX;
  1053. }
  1054. while (bind (tempfd, (struct sockaddr *) &sun,
  1055. offsetof (struct sockaddr_un, sun_path)
  1056. + strlen (sun.sun_path) + 1) != 0);
  1057. tempfname = strdup (sun.sun_path);
  1058. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1059. if (tempfd2 == -1)
  1060. {
  1061. printf ("%s: second socket call failed\n", __FUNCTION__);
  1062. exit (1);
  1063. }
  1064. int r = pthread_barrier_wait (&b2);
  1065. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1066. {
  1067. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1068. exit (1);
  1069. }
  1070. if (arg != NULL)
  1071. {
  1072. r = pthread_barrier_wait (&b2);
  1073. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1074. {
  1075. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1076. exit (1);
  1077. }
  1078. }
  1079. pthread_cleanup_push (cl, NULL);
  1080. char mem[70];
  1081. struct iovec iov[1];
  1082. iov[0].iov_base = mem;
  1083. iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
  1084. struct msghdr m;
  1085. m.msg_name = &sun;
  1086. m.msg_namelen = sizeof (sun);
  1087. m.msg_iov = iov;
  1088. m.msg_iovlen = 1;
  1089. m.msg_control = NULL;
  1090. m.msg_controllen = 0;
  1091. recvmsg (tempfd2, &m, 0);
  1092. pthread_cleanup_pop (0);
  1093. printf ("%s: recvmsg returned\n", __FUNCTION__);
  1094. exit (1);
  1095. }
  1096. static void *
  1097. tf_open (void *arg)
  1098. {
  1099. if (arg == NULL)
  1100. // XXX If somebody can provide a portable test case in which open()
  1101. // blocks we can enable this test to run in both rounds.
  1102. abort ();
  1103. int r = pthread_barrier_wait (&b2);
  1104. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1105. {
  1106. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1107. exit (1);
  1108. }
  1109. r = pthread_barrier_wait (&b2);
  1110. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1111. {
  1112. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1113. exit (1);
  1114. }
  1115. pthread_cleanup_push (cl, NULL);
  1116. open ("Makefile", O_RDONLY);
  1117. pthread_cleanup_pop (0);
  1118. printf ("%s: open returned\n", __FUNCTION__);
  1119. exit (1);
  1120. }
  1121. static void *
  1122. tf_close (void *arg)
  1123. {
  1124. if (arg == NULL)
  1125. // XXX If somebody can provide a portable test case in which close()
  1126. // blocks we can enable this test to run in both rounds.
  1127. abort ();
  1128. char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
  1129. tempfd = mkstemp (fname);
  1130. if (tempfd == -1)
  1131. {
  1132. printf ("%s: mkstemp failed\n", __FUNCTION__);
  1133. exit (1);
  1134. }
  1135. unlink (fname);
  1136. int r = pthread_barrier_wait (&b2);
  1137. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1138. {
  1139. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1140. exit (1);
  1141. }
  1142. r = pthread_barrier_wait (&b2);
  1143. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1144. {
  1145. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1146. exit (1);
  1147. }
  1148. pthread_cleanup_push (cl, NULL);
  1149. close (tempfd);
  1150. pthread_cleanup_pop (0);
  1151. printf ("%s: close returned\n", __FUNCTION__);
  1152. exit (1);
  1153. }
  1154. static void *
  1155. tf_pread (void *arg)
  1156. {
  1157. if (arg == NULL)
  1158. // XXX If somebody can provide a portable test case in which pread()
  1159. // blocks we can enable this test to run in both rounds.
  1160. abort ();
  1161. tempfd = open ("Makefile", O_RDONLY);
  1162. if (tempfd == -1)
  1163. {
  1164. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1165. exit (1);
  1166. }
  1167. int r = pthread_barrier_wait (&b2);
  1168. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1169. {
  1170. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1171. exit (1);
  1172. }
  1173. r = pthread_barrier_wait (&b2);
  1174. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1175. {
  1176. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1177. exit (1);
  1178. }
  1179. pthread_cleanup_push (cl, NULL);
  1180. char mem[10];
  1181. pread (tempfd, mem, sizeof (mem), 0);
  1182. pthread_cleanup_pop (0);
  1183. printf ("%s: pread returned\n", __FUNCTION__);
  1184. exit (1);
  1185. }
  1186. static void *
  1187. tf_pwrite (void *arg)
  1188. {
  1189. if (arg == NULL)
  1190. // XXX If somebody can provide a portable test case in which pwrite()
  1191. // blocks we can enable this test to run in both rounds.
  1192. abort ();
  1193. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  1194. tempfd = mkstemp (fname);
  1195. if (tempfd == -1)
  1196. {
  1197. printf ("%s: mkstemp failed\n", __FUNCTION__);
  1198. exit (1);
  1199. }
  1200. unlink (fname);
  1201. int r = pthread_barrier_wait (&b2);
  1202. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1203. {
  1204. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1205. exit (1);
  1206. }
  1207. r = pthread_barrier_wait (&b2);
  1208. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1209. {
  1210. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1211. exit (1);
  1212. }
  1213. pthread_cleanup_push (cl, NULL);
  1214. char mem[10];
  1215. pwrite (tempfd, mem, sizeof (mem), 0);
  1216. pthread_cleanup_pop (0);
  1217. printf ("%s: pwrite returned\n", __FUNCTION__);
  1218. exit (1);
  1219. }
  1220. static void *
  1221. tf_fsync (void *arg)
  1222. {
  1223. if (arg == NULL)
  1224. // XXX If somebody can provide a portable test case in which fsync()
  1225. // blocks we can enable this test to run in both rounds.
  1226. abort ();
  1227. tempfd = open ("Makefile", O_RDONLY);
  1228. if (tempfd == -1)
  1229. {
  1230. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1231. exit (1);
  1232. }
  1233. int r = pthread_barrier_wait (&b2);
  1234. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1235. {
  1236. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1237. exit (1);
  1238. }
  1239. r = pthread_barrier_wait (&b2);
  1240. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1241. {
  1242. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1243. exit (1);
  1244. }
  1245. pthread_cleanup_push (cl, NULL);
  1246. fsync (tempfd);
  1247. pthread_cleanup_pop (0);
  1248. printf ("%s: fsync returned\n", __FUNCTION__);
  1249. exit (1);
  1250. }
  1251. static void *
  1252. tf_fdatasync (void *arg)
  1253. {
  1254. if (arg == NULL)
  1255. // XXX If somebody can provide a portable test case in which fdatasync()
  1256. // blocks we can enable this test to run in both rounds.
  1257. abort ();
  1258. tempfd = open ("Makefile", O_RDONLY);
  1259. if (tempfd == -1)
  1260. {
  1261. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1262. exit (1);
  1263. }
  1264. int r = pthread_barrier_wait (&b2);
  1265. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1266. {
  1267. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1268. exit (1);
  1269. }
  1270. r = pthread_barrier_wait (&b2);
  1271. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1272. {
  1273. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1274. exit (1);
  1275. }
  1276. pthread_cleanup_push (cl, NULL);
  1277. fdatasync (tempfd);
  1278. pthread_cleanup_pop (0);
  1279. printf ("%s: fdatasync returned\n", __FUNCTION__);
  1280. exit (1);
  1281. }
  1282. static void *
  1283. tf_msync (void *arg)
  1284. {
  1285. if (arg == NULL)
  1286. // XXX If somebody can provide a portable test case in which msync()
  1287. // blocks we can enable this test to run in both rounds.
  1288. abort ();
  1289. tempfd = open ("Makefile", O_RDONLY);
  1290. if (tempfd == -1)
  1291. {
  1292. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1293. exit (1);
  1294. }
  1295. void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
  1296. if (p == MAP_FAILED)
  1297. {
  1298. printf ("%s: mmap failed\n", __FUNCTION__);
  1299. exit (1);
  1300. }
  1301. int r = pthread_barrier_wait (&b2);
  1302. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1303. {
  1304. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1305. exit (1);
  1306. }
  1307. r = pthread_barrier_wait (&b2);
  1308. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1309. {
  1310. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1311. exit (1);
  1312. }
  1313. pthread_cleanup_push (cl, NULL);
  1314. msync (p, 10, 0);
  1315. pthread_cleanup_pop (0);
  1316. printf ("%s: msync returned\n", __FUNCTION__);
  1317. exit (1);
  1318. }
  1319. static void *
  1320. tf_sendto (void *arg)
  1321. {
  1322. int tfd;
  1323. if (arg == NULL)
  1324. // XXX If somebody can provide a portable test case in which sendto()
  1325. // blocks we can enable this test to run in both rounds.
  1326. abort ();
  1327. struct sockaddr_un sun;
  1328. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1329. if (tempfd == -1)
  1330. {
  1331. printf ("%s: first socket call failed\n", __FUNCTION__);
  1332. exit (1);
  1333. }
  1334. int tries = 0;
  1335. do
  1336. {
  1337. if (++tries > 10)
  1338. {
  1339. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1340. }
  1341. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
  1342. tfd = mkstemp(sun.sun_path);
  1343. if (tfd < 0)
  1344. {
  1345. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1346. exit (1);
  1347. }
  1348. close(tfd);
  1349. sun.sun_family = AF_UNIX;
  1350. }
  1351. while (bind (tempfd, (struct sockaddr *) &sun,
  1352. offsetof (struct sockaddr_un, sun_path)
  1353. + strlen (sun.sun_path) + 1) != 0);
  1354. tempfname = strdup (sun.sun_path);
  1355. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1356. if (tempfd2 == -1)
  1357. {
  1358. printf ("%s: second socket call failed\n", __FUNCTION__);
  1359. exit (1);
  1360. }
  1361. int r = pthread_barrier_wait (&b2);
  1362. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1363. {
  1364. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1365. exit (1);
  1366. }
  1367. r = pthread_barrier_wait (&b2);
  1368. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1369. {
  1370. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1371. exit (1);
  1372. }
  1373. pthread_cleanup_push (cl, NULL);
  1374. char mem[1];
  1375. sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
  1376. (struct sockaddr *) &sun,
  1377. offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
  1378. pthread_cleanup_pop (0);
  1379. printf ("%s: sendto returned\n", __FUNCTION__);
  1380. exit (1);
  1381. }
  1382. static void *
  1383. tf_sendmsg (void *arg)
  1384. {
  1385. int tfd;
  1386. if (arg == NULL)
  1387. // XXX If somebody can provide a portable test case in which sendmsg()
  1388. // blocks we can enable this test to run in both rounds.
  1389. abort ();
  1390. struct sockaddr_un sun;
  1391. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1392. if (tempfd == -1)
  1393. {
  1394. printf ("%s: first socket call failed\n", __FUNCTION__);
  1395. exit (1);
  1396. }
  1397. int tries = 0;
  1398. do
  1399. {
  1400. if (++tries > 10)
  1401. {
  1402. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1403. }
  1404. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
  1405. tfd = mkstemp(sun.sun_path);
  1406. if (tfd < 0)
  1407. {
  1408. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1409. exit (1);
  1410. }
  1411. close(tfd);
  1412. sun.sun_family = AF_UNIX;
  1413. }
  1414. while (bind (tempfd, (struct sockaddr *) &sun,
  1415. offsetof (struct sockaddr_un, sun_path)
  1416. + strlen (sun.sun_path) + 1) != 0);
  1417. tempfname = strdup (sun.sun_path);
  1418. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1419. if (tempfd2 == -1)
  1420. {
  1421. printf ("%s: second socket call failed\n", __FUNCTION__);
  1422. exit (1);
  1423. }
  1424. int r = pthread_barrier_wait (&b2);
  1425. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1426. {
  1427. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1428. exit (1);
  1429. }
  1430. r = pthread_barrier_wait (&b2);
  1431. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1432. {
  1433. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1434. exit (1);
  1435. }
  1436. pthread_cleanup_push (cl, NULL);
  1437. char mem[1];
  1438. struct iovec iov[1];
  1439. iov[0].iov_base = mem;
  1440. iov[0].iov_len = 1;
  1441. struct msghdr m;
  1442. m.msg_name = &sun;
  1443. m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
  1444. + strlen (sun.sun_path) + 1);
  1445. m.msg_iov = iov;
  1446. m.msg_iovlen = 1;
  1447. m.msg_control = NULL;
  1448. m.msg_controllen = 0;
  1449. sendmsg (tempfd2, &m, 0);
  1450. pthread_cleanup_pop (0);
  1451. printf ("%s: sendmsg returned\n", __FUNCTION__);
  1452. exit (1);
  1453. }
  1454. static void *
  1455. tf_creat (void *arg)
  1456. {
  1457. if (arg == NULL)
  1458. // XXX If somebody can provide a portable test case in which sendmsg()
  1459. // blocks we can enable this test to run in both rounds.
  1460. abort ();
  1461. int r = pthread_barrier_wait (&b2);
  1462. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1463. {
  1464. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1465. exit (1);
  1466. }
  1467. r = pthread_barrier_wait (&b2);
  1468. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1469. {
  1470. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1471. exit (1);
  1472. }
  1473. pthread_cleanup_push (cl, NULL);
  1474. creat ("tmp/tst-cancel-4-should-not-exist", 0666);
  1475. pthread_cleanup_pop (0);
  1476. printf ("%s: creat returned\n", __FUNCTION__);
  1477. exit (1);
  1478. }
  1479. static void *
  1480. tf_connect (void *arg)
  1481. {
  1482. int tfd;
  1483. if (arg == NULL)
  1484. // XXX If somebody can provide a portable test case in which connect()
  1485. // blocks we can enable this test to run in both rounds.
  1486. abort ();
  1487. struct sockaddr_un sun;
  1488. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  1489. if (tempfd == -1)
  1490. {
  1491. printf ("%s: first socket call failed\n", __FUNCTION__);
  1492. exit (1);
  1493. }
  1494. int tries = 0;
  1495. do
  1496. {
  1497. if (++tries > 10)
  1498. {
  1499. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1500. }
  1501. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
  1502. tfd = mkstemp(sun.sun_path);
  1503. if (tfd < 0)
  1504. {
  1505. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1506. exit (1);
  1507. }
  1508. close(tfd);
  1509. sun.sun_family = AF_UNIX;
  1510. }
  1511. while (bind (tempfd, (struct sockaddr *) &sun,
  1512. offsetof (struct sockaddr_un, sun_path)
  1513. + strlen (sun.sun_path) + 1) != 0);
  1514. tempfname = strdup (sun.sun_path);
  1515. listen (tempfd, 5);
  1516. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  1517. if (tempfd2 == -1)
  1518. {
  1519. printf ("%s: second socket call failed\n", __FUNCTION__);
  1520. exit (1);
  1521. }
  1522. int r = pthread_barrier_wait (&b2);
  1523. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1524. {
  1525. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1526. exit (1);
  1527. }
  1528. if (arg != NULL)
  1529. {
  1530. r = pthread_barrier_wait (&b2);
  1531. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1532. {
  1533. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1534. exit (1);
  1535. }
  1536. }
  1537. pthread_cleanup_push (cl, NULL);
  1538. connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
  1539. pthread_cleanup_pop (0);
  1540. printf ("%s: connect returned\n", __FUNCTION__);
  1541. exit (1);
  1542. }
  1543. static void *
  1544. tf_tcdrain (void *arg)
  1545. {
  1546. if (arg == NULL)
  1547. // XXX If somebody can provide a portable test case in which tcdrain()
  1548. // blocks we can enable this test to run in both rounds.
  1549. abort ();
  1550. int r = pthread_barrier_wait (&b2);
  1551. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1552. {
  1553. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1554. exit (1);
  1555. }
  1556. if (arg != NULL)
  1557. {
  1558. r = pthread_barrier_wait (&b2);
  1559. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1560. {
  1561. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1562. exit (1);
  1563. }
  1564. }
  1565. pthread_cleanup_push (cl, NULL);
  1566. /* Regardless of stderr being a terminal, the tcdrain call should be
  1567. canceled. */
  1568. tcdrain (STDERR_FILENO);
  1569. pthread_cleanup_pop (0);
  1570. printf ("%s: tcdrain returned\n", __FUNCTION__);
  1571. exit (1);
  1572. }
  1573. static void *
  1574. tf_msgrcv (void *arg)
  1575. {
  1576. tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
  1577. if (tempmsg == -1)
  1578. {
  1579. printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
  1580. exit (1);
  1581. }
  1582. int r = pthread_barrier_wait (&b2);
  1583. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1584. {
  1585. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1586. exit (1);
  1587. }
  1588. if (arg != NULL)
  1589. {
  1590. r = pthread_barrier_wait (&b2);
  1591. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1592. {
  1593. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1594. exit (1);
  1595. }
  1596. }
  1597. ssize_t s;
  1598. pthread_cleanup_push (cl, NULL);
  1599. struct
  1600. {
  1601. long int type;
  1602. char mem[10];
  1603. } m;
  1604. int randnr;
  1605. /* We need a positive random number. */
  1606. do
  1607. randnr = random () % 64000;
  1608. while (randnr <= 0);
  1609. do
  1610. {
  1611. errno = 0;
  1612. s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
  1613. }
  1614. while (errno == EIDRM || errno == EINTR);
  1615. pthread_cleanup_pop (0);
  1616. printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
  1617. msgctl (tempmsg, IPC_RMID, NULL);
  1618. exit (1);
  1619. }
  1620. static void *
  1621. tf_msgsnd (void *arg)
  1622. {
  1623. if (arg == NULL)
  1624. // XXX If somebody can provide a portable test case in which msgsnd()
  1625. // blocks we can enable this test to run in both rounds.
  1626. abort ();
  1627. tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
  1628. if (tempmsg == -1)
  1629. {
  1630. printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
  1631. exit (1);
  1632. }
  1633. int r = pthread_barrier_wait (&b2);
  1634. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1635. {
  1636. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1637. exit (1);
  1638. }
  1639. r = pthread_barrier_wait (&b2);
  1640. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1641. {
  1642. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1643. exit (1);
  1644. }
  1645. pthread_cleanup_push (cl, NULL);
  1646. struct
  1647. {
  1648. long int type;
  1649. char mem[1];
  1650. } m;
  1651. /* We need a positive random number. */
  1652. do
  1653. m.type = random () % 64000;
  1654. while (m.type <= 0);
  1655. msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
  1656. pthread_cleanup_pop (0);
  1657. printf ("%s: msgsnd returned\n", __FUNCTION__);
  1658. msgctl (tempmsg, IPC_RMID, NULL);
  1659. exit (1);
  1660. }
  1661. static struct
  1662. {
  1663. const char *name;
  1664. void *(*tf) (void *);
  1665. int nb;
  1666. int only_early;
  1667. } tests[] =
  1668. {
  1669. #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
  1670. ADD_TEST (read, 2, 0),
  1671. ADD_TEST (readv, 2, 0),
  1672. ADD_TEST (select, 2, 0),
  1673. ADD_TEST (pselect, 2, 0),
  1674. ADD_TEST (poll, 2, 0),
  1675. ADD_TEST (ppoll, 2, 0),
  1676. ADD_TEST (write, 2, 0),
  1677. ADD_TEST (writev, 2, 0),
  1678. ADD_TEST (sleep, 2, 0),
  1679. ADD_TEST (usleep, 2, 0),
  1680. ADD_TEST (nanosleep, 2, 0),
  1681. ADD_TEST (wait, 2, 0),
  1682. ADD_TEST (waitid, 2, 0),
  1683. ADD_TEST (waitpid, 2, 0),
  1684. ADD_TEST (sigpause, 2, 0),
  1685. ADD_TEST (sigsuspend, 2, 0),
  1686. ADD_TEST (sigwait, 2, 0),
  1687. ADD_TEST (sigwaitinfo, 2, 0),
  1688. ADD_TEST (sigtimedwait, 2, 0),
  1689. ADD_TEST (pause, 2, 0),
  1690. ADD_TEST (accept, 2, 0),
  1691. ADD_TEST (send, 2, 0),
  1692. ADD_TEST (recv, 2, 0),
  1693. ADD_TEST (recvfrom, 2, 0),
  1694. ADD_TEST (recvmsg, 2, 0),
  1695. ADD_TEST (open, 2, 1),
  1696. ADD_TEST (close, 2, 1),
  1697. ADD_TEST (pread, 2, 1),
  1698. ADD_TEST (pwrite, 2, 1),
  1699. ADD_TEST (fsync, 2, 1),
  1700. ADD_TEST (fdatasync, 2, 1),
  1701. ADD_TEST (msync, 2, 1),
  1702. ADD_TEST (sendto, 2, 1),
  1703. ADD_TEST (sendmsg, 2, 1),
  1704. ADD_TEST (creat, 2, 1),
  1705. ADD_TEST (connect, 2, 1),
  1706. ADD_TEST (tcdrain, 2, 1),
  1707. ADD_TEST (msgrcv, 2, 0),
  1708. ADD_TEST (msgsnd, 2, 1),
  1709. };
  1710. #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
  1711. static int
  1712. do_test (void)
  1713. {
  1714. int val;
  1715. socklen_t len;
  1716. if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
  1717. {
  1718. perror ("socketpair");
  1719. exit (1);
  1720. }
  1721. val = 1;
  1722. len = sizeof(val);
  1723. setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
  1724. if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
  1725. {
  1726. perror ("getsockopt");
  1727. exit (1);
  1728. }
  1729. if (val >= WRITE_BUFFER_SIZE)
  1730. {
  1731. puts ("minimum write buffer size too large");
  1732. exit (1);
  1733. }
  1734. setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
  1735. int result = 0;
  1736. size_t cnt;
  1737. for (cnt = 0; cnt < ntest_tf; ++cnt)
  1738. {
  1739. if (tests[cnt].only_early)
  1740. continue;
  1741. if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
  1742. {
  1743. puts ("b2 init failed");
  1744. exit (1);
  1745. }
  1746. /* Reset the counter for the cleanup handler. */
  1747. cl_called = 0;
  1748. pthread_t th;
  1749. if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
  1750. {
  1751. printf ("create for '%s' test failed\n", tests[cnt].name);
  1752. result = 1;
  1753. continue;
  1754. }
  1755. int r = pthread_barrier_wait (&b2);
  1756. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1757. {
  1758. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1759. result = 1;
  1760. continue;
  1761. }
  1762. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  1763. while (nanosleep (&ts, &ts) != 0)
  1764. continue;
  1765. if (pthread_cancel (th) != 0)
  1766. {
  1767. printf ("cancel for '%s' failed\n", tests[cnt].name);
  1768. result = 1;
  1769. continue;
  1770. }
  1771. void *status;
  1772. if (pthread_join (th, &status) != 0)
  1773. {
  1774. printf ("join for '%s' failed\n", tests[cnt].name);
  1775. result = 1;
  1776. continue;
  1777. }
  1778. if (status != PTHREAD_CANCELED)
  1779. {
  1780. printf ("thread for '%s' not canceled\n", tests[cnt].name);
  1781. result = 1;
  1782. continue;
  1783. }
  1784. if (pthread_barrier_destroy (&b2) != 0)
  1785. {
  1786. puts ("barrier_destroy failed");
  1787. result = 1;
  1788. continue;
  1789. }
  1790. if (cl_called == 0)
  1791. {
  1792. printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
  1793. result = 1;
  1794. continue;
  1795. }
  1796. if (cl_called > 1)
  1797. {
  1798. printf ("cleanup handler called more than once for '%s'\n",
  1799. tests[cnt].name);
  1800. result = 1;
  1801. continue;
  1802. }
  1803. printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
  1804. if (tempfd != -1)
  1805. {
  1806. close (tempfd);
  1807. tempfd = -1;
  1808. }
  1809. if (tempfd2 != -1)
  1810. {
  1811. close (tempfd2);
  1812. tempfd2 = -1;
  1813. }
  1814. if (tempfname != NULL)
  1815. {
  1816. unlink (tempfname);
  1817. free (tempfname);
  1818. tempfname = NULL;
  1819. }
  1820. if (tempmsg != -1)
  1821. {
  1822. msgctl (tempmsg, IPC_RMID, NULL);
  1823. tempmsg = -1;
  1824. }
  1825. }
  1826. for (cnt = 0; cnt < ntest_tf; ++cnt)
  1827. {
  1828. if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
  1829. {
  1830. puts ("b2 init failed");
  1831. exit (1);
  1832. }
  1833. /* Reset the counter for the cleanup handler. */
  1834. cl_called = 0;
  1835. pthread_t th;
  1836. if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
  1837. {
  1838. printf ("create for '%s' test failed\n", tests[cnt].name);
  1839. result = 1;
  1840. continue;
  1841. }
  1842. int r = pthread_barrier_wait (&b2);
  1843. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1844. {
  1845. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1846. result = 1;
  1847. continue;
  1848. }
  1849. if (pthread_cancel (th) != 0)
  1850. {
  1851. printf ("cancel for '%s' failed\n", tests[cnt].name);
  1852. result = 1;
  1853. continue;
  1854. }
  1855. r = pthread_barrier_wait (&b2);
  1856. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1857. {
  1858. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1859. result = 1;
  1860. continue;
  1861. }
  1862. void *status;
  1863. if (pthread_join (th, &status) != 0)
  1864. {
  1865. printf ("join for '%s' failed\n", tests[cnt].name);
  1866. result = 1;
  1867. continue;
  1868. }
  1869. if (status != PTHREAD_CANCELED)
  1870. {
  1871. printf ("thread for '%s' not canceled\n", tests[cnt].name);
  1872. result = 1;
  1873. continue;
  1874. }
  1875. if (pthread_barrier_destroy (&b2) != 0)
  1876. {
  1877. puts ("barrier_destroy failed");
  1878. result = 1;
  1879. continue;
  1880. }
  1881. if (cl_called == 0)
  1882. {
  1883. printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
  1884. result = 1;
  1885. continue;
  1886. }
  1887. if (cl_called > 1)
  1888. {
  1889. printf ("cleanup handler called more than once for '%s'\n",
  1890. tests[cnt].name);
  1891. result = 1;
  1892. continue;
  1893. }
  1894. printf ("early cancel test of '%s' successful\n", tests[cnt].name);
  1895. if (tempfd != -1)
  1896. {
  1897. close (tempfd);
  1898. tempfd = -1;
  1899. }
  1900. if (tempfd2 != -1)
  1901. {
  1902. close (tempfd2);
  1903. tempfd2 = -1;
  1904. }
  1905. if (tempfname != NULL)
  1906. {
  1907. unlink (tempfname);
  1908. free (tempfname);
  1909. tempfname = NULL;
  1910. }
  1911. if (tempmsg != -1)
  1912. {
  1913. msgctl (tempmsg, IPC_RMID, NULL);
  1914. tempmsg = -1;
  1915. }
  1916. }
  1917. return result;
  1918. }
  1919. #define TIMEOUT 60
  1920. #define TEST_FUNCTION do_test ()
  1921. #include "../test-skeleton.c"