tst-cancel4.c 48 KB

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