tst-cancel4.c 49 KB

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