tst-cancel4.c 49 KB

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