tst-cancel4.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390
  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. int tfd;
  762. struct sockaddr_un sun;
  763. /* To test a non-blocking accept call we make the call file by using
  764. a datagrame socket. */
  765. int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
  766. tempfd = socket (AF_UNIX, pf, 0);
  767. if (tempfd == -1)
  768. {
  769. printf ("%s: socket call failed\n", __FUNCTION__);
  770. exit (1);
  771. }
  772. int tries = 0;
  773. do
  774. {
  775. if (++tries > 10)
  776. {
  777. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  778. }
  779. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
  780. tfd = mkstemp(sun.sun_path);
  781. if (tfd < 0)
  782. {
  783. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  784. exit (1);
  785. }
  786. close(tfd);
  787. sun.sun_family = AF_UNIX;
  788. }
  789. while (bind (tempfd, (struct sockaddr *) &sun,
  790. offsetof (struct sockaddr_un, sun_path)
  791. + strlen (sun.sun_path) + 1) != 0);
  792. unlink (sun.sun_path);
  793. listen (tempfd, 5);
  794. socklen_t len = sizeof (sun);
  795. int r = pthread_barrier_wait (&b2);
  796. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  797. {
  798. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  799. exit (1);
  800. }
  801. if (arg != NULL)
  802. {
  803. r = pthread_barrier_wait (&b2);
  804. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  805. {
  806. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  807. exit (1);
  808. }
  809. }
  810. pthread_cleanup_push (cl, NULL);
  811. accept (tempfd, (struct sockaddr *) &sun, &len);
  812. pthread_cleanup_pop (0);
  813. printf ("%s: accept returned\n", __FUNCTION__);
  814. exit (1);
  815. }
  816. static void *
  817. tf_send (void *arg)
  818. {
  819. int tfd;
  820. struct sockaddr_un sun;
  821. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  822. if (tempfd == -1)
  823. {
  824. printf ("%s: first socket call failed\n", __FUNCTION__);
  825. exit (1);
  826. }
  827. int tries = 0;
  828. do
  829. {
  830. if (++tries > 10)
  831. {
  832. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  833. }
  834. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
  835. tfd = mkstemp(sun.sun_path);
  836. if (tfd < 0)
  837. {
  838. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  839. exit (1);
  840. }
  841. close(tfd);
  842. sun.sun_family = AF_UNIX;
  843. }
  844. while (bind (tempfd, (struct sockaddr *) &sun,
  845. offsetof (struct sockaddr_un, sun_path)
  846. + strlen (sun.sun_path) + 1) != 0);
  847. listen (tempfd, 5);
  848. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  849. if (tempfd2 == -1)
  850. {
  851. printf ("%s: second socket call failed\n", __FUNCTION__);
  852. exit (1);
  853. }
  854. if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
  855. {
  856. printf ("%s: connect failed\n", __FUNCTION__);
  857. exit(1);
  858. }
  859. unlink (sun.sun_path);
  860. int r = pthread_barrier_wait (&b2);
  861. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  862. {
  863. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  864. exit (1);
  865. }
  866. if (arg != NULL)
  867. {
  868. r = pthread_barrier_wait (&b2);
  869. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  870. {
  871. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  872. exit (1);
  873. }
  874. }
  875. pthread_cleanup_push (cl, NULL);
  876. /* Very large block, so that the send call blocks. */
  877. char mem[700000];
  878. send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
  879. pthread_cleanup_pop (0);
  880. printf ("%s: send returned\n", __FUNCTION__);
  881. exit (1);
  882. }
  883. static void *
  884. tf_recv (void *arg)
  885. {
  886. int tfd;
  887. struct sockaddr_un sun;
  888. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  889. if (tempfd == -1)
  890. {
  891. printf ("%s: first socket call failed\n", __FUNCTION__);
  892. exit (1);
  893. }
  894. int tries = 0;
  895. do
  896. {
  897. if (++tries > 10)
  898. {
  899. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  900. }
  901. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
  902. tfd = mkstemp(sun.sun_path);
  903. if (tfd < 0)
  904. {
  905. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  906. exit (1);
  907. }
  908. close(tfd);
  909. sun.sun_family = AF_UNIX;
  910. }
  911. while (bind (tempfd, (struct sockaddr *) &sun,
  912. offsetof (struct sockaddr_un, sun_path)
  913. + strlen (sun.sun_path) + 1) != 0);
  914. listen (tempfd, 5);
  915. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  916. if (tempfd2 == -1)
  917. {
  918. printf ("%s: second socket call failed\n", __FUNCTION__);
  919. exit (1);
  920. }
  921. if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
  922. {
  923. printf ("%s: connect failed\n", __FUNCTION__);
  924. exit(1);
  925. }
  926. unlink (sun.sun_path);
  927. int r = pthread_barrier_wait (&b2);
  928. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  929. {
  930. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  931. exit (1);
  932. }
  933. if (arg != NULL)
  934. {
  935. r = pthread_barrier_wait (&b2);
  936. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  937. {
  938. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  939. exit (1);
  940. }
  941. }
  942. pthread_cleanup_push (cl, NULL);
  943. char mem[70];
  944. recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
  945. pthread_cleanup_pop (0);
  946. printf ("%s: recv returned\n", __FUNCTION__);
  947. exit (1);
  948. }
  949. static void *
  950. tf_recvfrom (void *arg)
  951. {
  952. int tfd;
  953. struct sockaddr_un sun;
  954. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  955. if (tempfd == -1)
  956. {
  957. printf ("%s: first socket call failed\n", __FUNCTION__);
  958. exit (1);
  959. }
  960. int tries = 0;
  961. do
  962. {
  963. if (++tries > 10)
  964. {
  965. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  966. }
  967. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
  968. tfd = mkstemp(sun.sun_path);
  969. if (tfd < 0)
  970. {
  971. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  972. exit (1);
  973. }
  974. close(tfd);
  975. sun.sun_family = AF_UNIX;
  976. }
  977. while (bind (tempfd, (struct sockaddr *) &sun,
  978. offsetof (struct sockaddr_un, sun_path)
  979. + strlen (sun.sun_path) + 1) != 0);
  980. tempfname = strdup (sun.sun_path);
  981. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  982. if (tempfd2 == -1)
  983. {
  984. printf ("%s: second socket call failed\n", __FUNCTION__);
  985. exit (1);
  986. }
  987. int r = pthread_barrier_wait (&b2);
  988. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  989. {
  990. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  991. exit (1);
  992. }
  993. if (arg != NULL)
  994. {
  995. r = pthread_barrier_wait (&b2);
  996. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  997. {
  998. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  999. exit (1);
  1000. }
  1001. }
  1002. pthread_cleanup_push (cl, NULL);
  1003. char mem[70];
  1004. socklen_t len = sizeof (sun);
  1005. recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
  1006. (struct sockaddr *) &sun, &len);
  1007. pthread_cleanup_pop (0);
  1008. printf ("%s: recvfrom returned\n", __FUNCTION__);
  1009. exit (1);
  1010. }
  1011. static void *
  1012. tf_recvmsg (void *arg)
  1013. {
  1014. int tfd;
  1015. struct sockaddr_un sun;
  1016. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1017. if (tempfd == -1)
  1018. {
  1019. printf ("%s: first socket call failed\n", __FUNCTION__);
  1020. exit (1);
  1021. }
  1022. int tries = 0;
  1023. do
  1024. {
  1025. if (++tries > 10)
  1026. {
  1027. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1028. }
  1029. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
  1030. tfd = mkstemp(sun.sun_path);
  1031. if (tfd < 0)
  1032. {
  1033. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1034. exit (1);
  1035. }
  1036. close(tfd);
  1037. sun.sun_family = AF_UNIX;
  1038. }
  1039. while (bind (tempfd, (struct sockaddr *) &sun,
  1040. offsetof (struct sockaddr_un, sun_path)
  1041. + strlen (sun.sun_path) + 1) != 0);
  1042. tempfname = strdup (sun.sun_path);
  1043. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1044. if (tempfd2 == -1)
  1045. {
  1046. printf ("%s: second socket call failed\n", __FUNCTION__);
  1047. exit (1);
  1048. }
  1049. int r = pthread_barrier_wait (&b2);
  1050. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1051. {
  1052. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1053. exit (1);
  1054. }
  1055. if (arg != NULL)
  1056. {
  1057. r = pthread_barrier_wait (&b2);
  1058. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1059. {
  1060. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1061. exit (1);
  1062. }
  1063. }
  1064. pthread_cleanup_push (cl, NULL);
  1065. char mem[70];
  1066. struct iovec iov[1];
  1067. iov[0].iov_base = mem;
  1068. iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
  1069. struct msghdr m;
  1070. m.msg_name = &sun;
  1071. m.msg_namelen = sizeof (sun);
  1072. m.msg_iov = iov;
  1073. m.msg_iovlen = 1;
  1074. m.msg_control = NULL;
  1075. m.msg_controllen = 0;
  1076. recvmsg (tempfd2, &m, 0);
  1077. pthread_cleanup_pop (0);
  1078. printf ("%s: recvmsg returned\n", __FUNCTION__);
  1079. exit (1);
  1080. }
  1081. static void *
  1082. tf_open (void *arg)
  1083. {
  1084. if (arg == NULL)
  1085. // XXX If somebody can provide a portable test case in which open()
  1086. // blocks we can enable this test to run in both rounds.
  1087. abort ();
  1088. int r = pthread_barrier_wait (&b2);
  1089. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1090. {
  1091. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1092. exit (1);
  1093. }
  1094. r = pthread_barrier_wait (&b2);
  1095. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1096. {
  1097. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1098. exit (1);
  1099. }
  1100. pthread_cleanup_push (cl, NULL);
  1101. open ("Makefile", O_RDONLY);
  1102. pthread_cleanup_pop (0);
  1103. printf ("%s: open returned\n", __FUNCTION__);
  1104. exit (1);
  1105. }
  1106. static void *
  1107. tf_close (void *arg)
  1108. {
  1109. if (arg == NULL)
  1110. // XXX If somebody can provide a portable test case in which close()
  1111. // blocks we can enable this test to run in both rounds.
  1112. abort ();
  1113. char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
  1114. tempfd = mkstemp (fname);
  1115. if (tempfd == -1)
  1116. {
  1117. printf ("%s: mkstemp failed\n", __FUNCTION__);
  1118. exit (1);
  1119. }
  1120. unlink (fname);
  1121. int r = pthread_barrier_wait (&b2);
  1122. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1123. {
  1124. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1125. exit (1);
  1126. }
  1127. r = pthread_barrier_wait (&b2);
  1128. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1129. {
  1130. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1131. exit (1);
  1132. }
  1133. pthread_cleanup_push (cl, NULL);
  1134. close (tempfd);
  1135. pthread_cleanup_pop (0);
  1136. printf ("%s: close returned\n", __FUNCTION__);
  1137. exit (1);
  1138. }
  1139. static void *
  1140. tf_pread (void *arg)
  1141. {
  1142. if (arg == NULL)
  1143. // XXX If somebody can provide a portable test case in which pread()
  1144. // blocks we can enable this test to run in both rounds.
  1145. abort ();
  1146. tempfd = open ("Makefile", O_RDONLY);
  1147. if (tempfd == -1)
  1148. {
  1149. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1150. exit (1);
  1151. }
  1152. int r = pthread_barrier_wait (&b2);
  1153. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1154. {
  1155. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1156. exit (1);
  1157. }
  1158. r = pthread_barrier_wait (&b2);
  1159. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1160. {
  1161. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1162. exit (1);
  1163. }
  1164. pthread_cleanup_push (cl, NULL);
  1165. char mem[10];
  1166. pread (tempfd, mem, sizeof (mem), 0);
  1167. pthread_cleanup_pop (0);
  1168. printf ("%s: pread returned\n", __FUNCTION__);
  1169. exit (1);
  1170. }
  1171. static void *
  1172. tf_pwrite (void *arg)
  1173. {
  1174. if (arg == NULL)
  1175. // XXX If somebody can provide a portable test case in which pwrite()
  1176. // blocks we can enable this test to run in both rounds.
  1177. abort ();
  1178. char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
  1179. tempfd = mkstemp (fname);
  1180. if (tempfd == -1)
  1181. {
  1182. printf ("%s: mkstemp failed\n", __FUNCTION__);
  1183. exit (1);
  1184. }
  1185. unlink (fname);
  1186. int r = pthread_barrier_wait (&b2);
  1187. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1188. {
  1189. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1190. exit (1);
  1191. }
  1192. r = pthread_barrier_wait (&b2);
  1193. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1194. {
  1195. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1196. exit (1);
  1197. }
  1198. pthread_cleanup_push (cl, NULL);
  1199. char mem[10];
  1200. pwrite (tempfd, mem, sizeof (mem), 0);
  1201. pthread_cleanup_pop (0);
  1202. printf ("%s: pwrite returned\n", __FUNCTION__);
  1203. exit (1);
  1204. }
  1205. static void *
  1206. tf_fsync (void *arg)
  1207. {
  1208. if (arg == NULL)
  1209. // XXX If somebody can provide a portable test case in which fsync()
  1210. // blocks we can enable this test to run in both rounds.
  1211. abort ();
  1212. tempfd = open ("Makefile", O_RDONLY);
  1213. if (tempfd == -1)
  1214. {
  1215. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1216. exit (1);
  1217. }
  1218. int r = pthread_barrier_wait (&b2);
  1219. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1220. {
  1221. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1222. exit (1);
  1223. }
  1224. r = pthread_barrier_wait (&b2);
  1225. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1226. {
  1227. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1228. exit (1);
  1229. }
  1230. pthread_cleanup_push (cl, NULL);
  1231. fsync (tempfd);
  1232. pthread_cleanup_pop (0);
  1233. printf ("%s: fsync returned\n", __FUNCTION__);
  1234. exit (1);
  1235. }
  1236. static void *
  1237. tf_fdatasync (void *arg)
  1238. {
  1239. if (arg == NULL)
  1240. // XXX If somebody can provide a portable test case in which fdatasync()
  1241. // blocks we can enable this test to run in both rounds.
  1242. abort ();
  1243. tempfd = open ("Makefile", O_RDONLY);
  1244. if (tempfd == -1)
  1245. {
  1246. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1247. exit (1);
  1248. }
  1249. int r = pthread_barrier_wait (&b2);
  1250. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1251. {
  1252. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1253. exit (1);
  1254. }
  1255. r = pthread_barrier_wait (&b2);
  1256. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1257. {
  1258. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1259. exit (1);
  1260. }
  1261. pthread_cleanup_push (cl, NULL);
  1262. fdatasync (tempfd);
  1263. pthread_cleanup_pop (0);
  1264. printf ("%s: fdatasync returned\n", __FUNCTION__);
  1265. exit (1);
  1266. }
  1267. static void *
  1268. tf_msync (void *arg)
  1269. {
  1270. if (arg == NULL)
  1271. // XXX If somebody can provide a portable test case in which msync()
  1272. // blocks we can enable this test to run in both rounds.
  1273. abort ();
  1274. tempfd = open ("Makefile", O_RDONLY);
  1275. if (tempfd == -1)
  1276. {
  1277. printf ("%s: cannot open Makefile\n", __FUNCTION__);
  1278. exit (1);
  1279. }
  1280. void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
  1281. if (p == MAP_FAILED)
  1282. {
  1283. printf ("%s: mmap failed\n", __FUNCTION__);
  1284. exit (1);
  1285. }
  1286. int r = pthread_barrier_wait (&b2);
  1287. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1288. {
  1289. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1290. exit (1);
  1291. }
  1292. r = pthread_barrier_wait (&b2);
  1293. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1294. {
  1295. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1296. exit (1);
  1297. }
  1298. pthread_cleanup_push (cl, NULL);
  1299. msync (p, 10, 0);
  1300. pthread_cleanup_pop (0);
  1301. printf ("%s: msync returned\n", __FUNCTION__);
  1302. exit (1);
  1303. }
  1304. static void *
  1305. tf_sendto (void *arg)
  1306. {
  1307. int tfd;
  1308. if (arg == NULL)
  1309. // XXX If somebody can provide a portable test case in which sendto()
  1310. // blocks we can enable this test to run in both rounds.
  1311. abort ();
  1312. struct sockaddr_un sun;
  1313. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1314. if (tempfd == -1)
  1315. {
  1316. printf ("%s: first socket call failed\n", __FUNCTION__);
  1317. exit (1);
  1318. }
  1319. int tries = 0;
  1320. do
  1321. {
  1322. if (++tries > 10)
  1323. {
  1324. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1325. }
  1326. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
  1327. tfd = mkstemp(sun.sun_path);
  1328. if (tfd < 0)
  1329. {
  1330. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1331. exit (1);
  1332. }
  1333. close(tfd);
  1334. sun.sun_family = AF_UNIX;
  1335. }
  1336. while (bind (tempfd, (struct sockaddr *) &sun,
  1337. offsetof (struct sockaddr_un, sun_path)
  1338. + strlen (sun.sun_path) + 1) != 0);
  1339. tempfname = strdup (sun.sun_path);
  1340. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1341. if (tempfd2 == -1)
  1342. {
  1343. printf ("%s: second socket call failed\n", __FUNCTION__);
  1344. exit (1);
  1345. }
  1346. int r = pthread_barrier_wait (&b2);
  1347. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1348. {
  1349. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1350. exit (1);
  1351. }
  1352. r = pthread_barrier_wait (&b2);
  1353. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1354. {
  1355. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1356. exit (1);
  1357. }
  1358. pthread_cleanup_push (cl, NULL);
  1359. char mem[1];
  1360. sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
  1361. (struct sockaddr *) &sun,
  1362. offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
  1363. pthread_cleanup_pop (0);
  1364. printf ("%s: sendto returned\n", __FUNCTION__);
  1365. exit (1);
  1366. }
  1367. static void *
  1368. tf_sendmsg (void *arg)
  1369. {
  1370. int tfd;
  1371. if (arg == NULL)
  1372. // XXX If somebody can provide a portable test case in which sendmsg()
  1373. // blocks we can enable this test to run in both rounds.
  1374. abort ();
  1375. struct sockaddr_un sun;
  1376. tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
  1377. if (tempfd == -1)
  1378. {
  1379. printf ("%s: first socket call failed\n", __FUNCTION__);
  1380. exit (1);
  1381. }
  1382. int tries = 0;
  1383. do
  1384. {
  1385. if (++tries > 10)
  1386. {
  1387. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1388. }
  1389. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
  1390. tfd = mkstemp(sun.sun_path);
  1391. if (tfd < 0)
  1392. {
  1393. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1394. exit (1);
  1395. }
  1396. close(tfd);
  1397. sun.sun_family = AF_UNIX;
  1398. }
  1399. while (bind (tempfd, (struct sockaddr *) &sun,
  1400. offsetof (struct sockaddr_un, sun_path)
  1401. + strlen (sun.sun_path) + 1) != 0);
  1402. tempfname = strdup (sun.sun_path);
  1403. tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
  1404. if (tempfd2 == -1)
  1405. {
  1406. printf ("%s: second socket call failed\n", __FUNCTION__);
  1407. exit (1);
  1408. }
  1409. int r = pthread_barrier_wait (&b2);
  1410. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1411. {
  1412. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1413. exit (1);
  1414. }
  1415. r = pthread_barrier_wait (&b2);
  1416. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1417. {
  1418. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1419. exit (1);
  1420. }
  1421. pthread_cleanup_push (cl, NULL);
  1422. char mem[1];
  1423. struct iovec iov[1];
  1424. iov[0].iov_base = mem;
  1425. iov[0].iov_len = 1;
  1426. struct msghdr m;
  1427. m.msg_name = &sun;
  1428. m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
  1429. + strlen (sun.sun_path) + 1);
  1430. m.msg_iov = iov;
  1431. m.msg_iovlen = 1;
  1432. m.msg_control = NULL;
  1433. m.msg_controllen = 0;
  1434. sendmsg (tempfd2, &m, 0);
  1435. pthread_cleanup_pop (0);
  1436. printf ("%s: sendmsg returned\n", __FUNCTION__);
  1437. exit (1);
  1438. }
  1439. static void *
  1440. tf_creat (void *arg)
  1441. {
  1442. if (arg == NULL)
  1443. // XXX If somebody can provide a portable test case in which sendmsg()
  1444. // blocks we can enable this test to run in both rounds.
  1445. abort ();
  1446. int r = pthread_barrier_wait (&b2);
  1447. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1448. {
  1449. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1450. exit (1);
  1451. }
  1452. r = pthread_barrier_wait (&b2);
  1453. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1454. {
  1455. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1456. exit (1);
  1457. }
  1458. pthread_cleanup_push (cl, NULL);
  1459. creat ("tmp/tst-cancel-4-should-not-exist", 0666);
  1460. pthread_cleanup_pop (0);
  1461. printf ("%s: creat returned\n", __FUNCTION__);
  1462. exit (1);
  1463. }
  1464. static void *
  1465. tf_connect (void *arg)
  1466. {
  1467. int tfd;
  1468. if (arg == NULL)
  1469. // XXX If somebody can provide a portable test case in which connect()
  1470. // blocks we can enable this test to run in both rounds.
  1471. abort ();
  1472. struct sockaddr_un sun;
  1473. tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
  1474. if (tempfd == -1)
  1475. {
  1476. printf ("%s: first socket call failed\n", __FUNCTION__);
  1477. exit (1);
  1478. }
  1479. int tries = 0;
  1480. do
  1481. {
  1482. if (++tries > 10)
  1483. {
  1484. printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
  1485. }
  1486. strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
  1487. tfd = mkstemp(sun.sun_path);
  1488. if (tfd < 0)
  1489. {
  1490. printf ("%s: cannot generate temp file name\n", __FUNCTION__);
  1491. exit (1);
  1492. }
  1493. close(tfd);
  1494. sun.sun_family = AF_UNIX;
  1495. }
  1496. while (bind (tempfd, (struct sockaddr *) &sun,
  1497. offsetof (struct sockaddr_un, sun_path)
  1498. + strlen (sun.sun_path) + 1) != 0);
  1499. tempfname = strdup (sun.sun_path);
  1500. listen (tempfd, 5);
  1501. tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
  1502. if (tempfd2 == -1)
  1503. {
  1504. printf ("%s: second socket call failed\n", __FUNCTION__);
  1505. exit (1);
  1506. }
  1507. int r = pthread_barrier_wait (&b2);
  1508. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1509. {
  1510. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1511. exit (1);
  1512. }
  1513. if (arg != NULL)
  1514. {
  1515. r = pthread_barrier_wait (&b2);
  1516. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1517. {
  1518. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1519. exit (1);
  1520. }
  1521. }
  1522. pthread_cleanup_push (cl, NULL);
  1523. connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
  1524. pthread_cleanup_pop (0);
  1525. printf ("%s: connect returned\n", __FUNCTION__);
  1526. exit (1);
  1527. }
  1528. static void *
  1529. tf_tcdrain (void *arg)
  1530. {
  1531. if (arg == NULL)
  1532. // XXX If somebody can provide a portable test case in which tcdrain()
  1533. // blocks we can enable this test to run in both rounds.
  1534. abort ();
  1535. int r = pthread_barrier_wait (&b2);
  1536. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1537. {
  1538. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1539. exit (1);
  1540. }
  1541. if (arg != NULL)
  1542. {
  1543. r = pthread_barrier_wait (&b2);
  1544. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1545. {
  1546. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1547. exit (1);
  1548. }
  1549. }
  1550. pthread_cleanup_push (cl, NULL);
  1551. /* Regardless of stderr being a terminal, the tcdrain call should be
  1552. canceled. */
  1553. tcdrain (STDERR_FILENO);
  1554. pthread_cleanup_pop (0);
  1555. printf ("%s: tcdrain returned\n", __FUNCTION__);
  1556. exit (1);
  1557. }
  1558. static void *
  1559. tf_msgrcv (void *arg)
  1560. {
  1561. tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
  1562. if (tempmsg == -1)
  1563. {
  1564. printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
  1565. exit (1);
  1566. }
  1567. int r = pthread_barrier_wait (&b2);
  1568. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1569. {
  1570. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1571. exit (1);
  1572. }
  1573. if (arg != NULL)
  1574. {
  1575. r = pthread_barrier_wait (&b2);
  1576. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1577. {
  1578. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1579. exit (1);
  1580. }
  1581. }
  1582. ssize_t s;
  1583. pthread_cleanup_push (cl, NULL);
  1584. struct
  1585. {
  1586. long int type;
  1587. char mem[10];
  1588. } m;
  1589. int randnr;
  1590. /* We need a positive random number. */
  1591. do
  1592. randnr = random () % 64000;
  1593. while (randnr <= 0);
  1594. do
  1595. {
  1596. errno = 0;
  1597. s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
  1598. }
  1599. while (errno == EIDRM || errno == EINTR);
  1600. pthread_cleanup_pop (0);
  1601. printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
  1602. msgctl (tempmsg, IPC_RMID, NULL);
  1603. exit (1);
  1604. }
  1605. static void *
  1606. tf_msgsnd (void *arg)
  1607. {
  1608. if (arg == NULL)
  1609. // XXX If somebody can provide a portable test case in which msgsnd()
  1610. // blocks we can enable this test to run in both rounds.
  1611. abort ();
  1612. tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
  1613. if (tempmsg == -1)
  1614. {
  1615. printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
  1616. exit (1);
  1617. }
  1618. int r = pthread_barrier_wait (&b2);
  1619. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1620. {
  1621. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1622. exit (1);
  1623. }
  1624. r = pthread_barrier_wait (&b2);
  1625. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1626. {
  1627. printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
  1628. exit (1);
  1629. }
  1630. pthread_cleanup_push (cl, NULL);
  1631. struct
  1632. {
  1633. long int type;
  1634. char mem[1];
  1635. } m;
  1636. /* We need a positive random number. */
  1637. do
  1638. m.type = random () % 64000;
  1639. while (m.type <= 0);
  1640. msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
  1641. pthread_cleanup_pop (0);
  1642. printf ("%s: msgsnd returned\n", __FUNCTION__);
  1643. msgctl (tempmsg, IPC_RMID, NULL);
  1644. exit (1);
  1645. }
  1646. static struct
  1647. {
  1648. const char *name;
  1649. void *(*tf) (void *);
  1650. int nb;
  1651. int only_early;
  1652. } tests[] =
  1653. {
  1654. #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
  1655. ADD_TEST (read, 2, 0),
  1656. ADD_TEST (readv, 2, 0),
  1657. ADD_TEST (select, 2, 0),
  1658. ADD_TEST (pselect, 2, 0),
  1659. ADD_TEST (poll, 2, 0),
  1660. ADD_TEST (ppoll, 2, 0),
  1661. ADD_TEST (write, 2, 0),
  1662. ADD_TEST (writev, 2, 0),
  1663. ADD_TEST (sleep, 2, 0),
  1664. ADD_TEST (usleep, 2, 0),
  1665. ADD_TEST (nanosleep, 2, 0),
  1666. ADD_TEST (wait, 2, 0),
  1667. ADD_TEST (waitid, 2, 0),
  1668. ADD_TEST (waitpid, 2, 0),
  1669. ADD_TEST (sigpause, 2, 0),
  1670. ADD_TEST (sigsuspend, 2, 0),
  1671. ADD_TEST (sigwait, 2, 0),
  1672. ADD_TEST (sigwaitinfo, 2, 0),
  1673. ADD_TEST (sigtimedwait, 2, 0),
  1674. ADD_TEST (pause, 2, 0),
  1675. ADD_TEST (accept, 2, 0),
  1676. ADD_TEST (send, 2, 0),
  1677. ADD_TEST (recv, 2, 0),
  1678. ADD_TEST (recvfrom, 2, 0),
  1679. ADD_TEST (recvmsg, 2, 0),
  1680. ADD_TEST (open, 2, 1),
  1681. ADD_TEST (close, 2, 1),
  1682. ADD_TEST (pread, 2, 1),
  1683. ADD_TEST (pwrite, 2, 1),
  1684. ADD_TEST (fsync, 2, 1),
  1685. ADD_TEST (fdatasync, 2, 1),
  1686. ADD_TEST (msync, 2, 1),
  1687. ADD_TEST (sendto, 2, 1),
  1688. ADD_TEST (sendmsg, 2, 1),
  1689. ADD_TEST (creat, 2, 1),
  1690. ADD_TEST (connect, 2, 1),
  1691. ADD_TEST (tcdrain, 2, 1),
  1692. ADD_TEST (msgrcv, 2, 0),
  1693. ADD_TEST (msgsnd, 2, 1),
  1694. };
  1695. #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
  1696. static int
  1697. do_test (void)
  1698. {
  1699. int val;
  1700. socklen_t len;
  1701. if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
  1702. {
  1703. perror ("socketpair");
  1704. exit (1);
  1705. }
  1706. val = 1;
  1707. len = sizeof(val);
  1708. setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
  1709. if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
  1710. {
  1711. perror ("getsockopt");
  1712. exit (1);
  1713. }
  1714. if (val >= WRITE_BUFFER_SIZE)
  1715. {
  1716. puts ("minimum write buffer size too large");
  1717. exit (1);
  1718. }
  1719. setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
  1720. int result = 0;
  1721. size_t cnt;
  1722. for (cnt = 0; cnt < ntest_tf; ++cnt)
  1723. {
  1724. if (tests[cnt].only_early)
  1725. continue;
  1726. if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
  1727. {
  1728. puts ("b2 init failed");
  1729. exit (1);
  1730. }
  1731. /* Reset the counter for the cleanup handler. */
  1732. cl_called = 0;
  1733. pthread_t th;
  1734. if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
  1735. {
  1736. printf ("create for '%s' test failed\n", tests[cnt].name);
  1737. result = 1;
  1738. continue;
  1739. }
  1740. int r = pthread_barrier_wait (&b2);
  1741. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1742. {
  1743. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1744. result = 1;
  1745. continue;
  1746. }
  1747. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  1748. while (nanosleep (&ts, &ts) != 0)
  1749. continue;
  1750. if (pthread_cancel (th) != 0)
  1751. {
  1752. printf ("cancel for '%s' failed\n", tests[cnt].name);
  1753. result = 1;
  1754. continue;
  1755. }
  1756. void *status;
  1757. if (pthread_join (th, &status) != 0)
  1758. {
  1759. printf ("join for '%s' failed\n", tests[cnt].name);
  1760. result = 1;
  1761. continue;
  1762. }
  1763. if (status != PTHREAD_CANCELED)
  1764. {
  1765. printf ("thread for '%s' not canceled\n", tests[cnt].name);
  1766. result = 1;
  1767. continue;
  1768. }
  1769. if (pthread_barrier_destroy (&b2) != 0)
  1770. {
  1771. puts ("barrier_destroy failed");
  1772. result = 1;
  1773. continue;
  1774. }
  1775. if (cl_called == 0)
  1776. {
  1777. printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
  1778. result = 1;
  1779. continue;
  1780. }
  1781. if (cl_called > 1)
  1782. {
  1783. printf ("cleanup handler called more than once for '%s'\n",
  1784. tests[cnt].name);
  1785. result = 1;
  1786. continue;
  1787. }
  1788. printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
  1789. if (tempfd != -1)
  1790. {
  1791. close (tempfd);
  1792. tempfd = -1;
  1793. }
  1794. if (tempfd2 != -1)
  1795. {
  1796. close (tempfd2);
  1797. tempfd2 = -1;
  1798. }
  1799. if (tempfname != NULL)
  1800. {
  1801. unlink (tempfname);
  1802. free (tempfname);
  1803. tempfname = NULL;
  1804. }
  1805. if (tempmsg != -1)
  1806. {
  1807. msgctl (tempmsg, IPC_RMID, NULL);
  1808. tempmsg = -1;
  1809. }
  1810. }
  1811. for (cnt = 0; cnt < ntest_tf; ++cnt)
  1812. {
  1813. if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
  1814. {
  1815. puts ("b2 init failed");
  1816. exit (1);
  1817. }
  1818. /* Reset the counter for the cleanup handler. */
  1819. cl_called = 0;
  1820. pthread_t th;
  1821. if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
  1822. {
  1823. printf ("create for '%s' test failed\n", tests[cnt].name);
  1824. result = 1;
  1825. continue;
  1826. }
  1827. int r = pthread_barrier_wait (&b2);
  1828. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1829. {
  1830. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1831. result = 1;
  1832. continue;
  1833. }
  1834. if (pthread_cancel (th) != 0)
  1835. {
  1836. printf ("cancel for '%s' failed\n", tests[cnt].name);
  1837. result = 1;
  1838. continue;
  1839. }
  1840. r = pthread_barrier_wait (&b2);
  1841. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  1842. {
  1843. printf ("%s: barrier_wait failed\n", __FUNCTION__);
  1844. result = 1;
  1845. continue;
  1846. }
  1847. void *status;
  1848. if (pthread_join (th, &status) != 0)
  1849. {
  1850. printf ("join for '%s' failed\n", tests[cnt].name);
  1851. result = 1;
  1852. continue;
  1853. }
  1854. if (status != PTHREAD_CANCELED)
  1855. {
  1856. printf ("thread for '%s' not canceled\n", tests[cnt].name);
  1857. result = 1;
  1858. continue;
  1859. }
  1860. if (pthread_barrier_destroy (&b2) != 0)
  1861. {
  1862. puts ("barrier_destroy failed");
  1863. result = 1;
  1864. continue;
  1865. }
  1866. if (cl_called == 0)
  1867. {
  1868. printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
  1869. result = 1;
  1870. continue;
  1871. }
  1872. if (cl_called > 1)
  1873. {
  1874. printf ("cleanup handler called more than once for '%s'\n",
  1875. tests[cnt].name);
  1876. result = 1;
  1877. continue;
  1878. }
  1879. printf ("early cancel test of '%s' successful\n", tests[cnt].name);
  1880. if (tempfd != -1)
  1881. {
  1882. close (tempfd);
  1883. tempfd = -1;
  1884. }
  1885. if (tempfd2 != -1)
  1886. {
  1887. close (tempfd2);
  1888. tempfd2 = -1;
  1889. }
  1890. if (tempfname != NULL)
  1891. {
  1892. unlink (tempfname);
  1893. free (tempfname);
  1894. tempfname = NULL;
  1895. }
  1896. if (tempmsg != -1)
  1897. {
  1898. msgctl (tempmsg, IPC_RMID, NULL);
  1899. tempmsg = -1;
  1900. }
  1901. }
  1902. return result;
  1903. }
  1904. #define TIMEOUT 60
  1905. #define TEST_FUNCTION do_test ()
  1906. #include "../test-skeleton.c"