tst-cancel4.c 48 KB

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