1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398 |
- /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
- This file is part of the GNU C Library.
- Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
- The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- The GNU C Library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, see
- <http://www.gnu.org/licenses/>. */
- /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
- exit to be called more than once. */
- #include <errno.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <pthread.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <termios.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <sys/msg.h>
- #include <sys/poll.h>
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <sys/uio.h>
- #include <sys/un.h>
- #include <sys/wait.h>
- #include "../test-skeleton.h"
- #if defined(__GLIBC__) || defined(__UCLIBC__)
- #define SIGCANCEL __SIGRTMIN
- #else
- #define SIGCANCEL SIGRTMIN
- #endif
- /* Since STREAMS are not supported in the standard Linux kernel and
- there we don't advertise STREAMS as supported is no need to test
- the STREAMS related functions. This affects
- getmsg() getpmsg() putmsg()
- putpmsg()
- lockf() and fcntl() are tested in tst-cancel16.
- pthread_join() is tested in tst-join5.
- pthread_testcancel()'s only purpose is to allow cancellation. This
- is tested in several places.
- sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
- mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
- in tst-mqueue8{,x} tests.
- aio_suspend() is tested in tst-cancel17.
- clock_nanosleep() is tested in tst-cancel18.
- */
- /* Pipe descriptors. */
- static int fds[2];
- /* Temporary file descriptor, to be closed after each round. */
- static int tempfd = -1;
- static int tempfd2 = -1;
- /* Name of temporary file to be removed after each round. */
- static char *tempfname;
- /* Temporary message queue. */
- static int tempmsg = -1;
- /* Often used barrier for two threads. */
- static pthread_barrier_t b2;
- #ifndef IPC_ADDVAL
- # define IPC_ADDVAL 0
- #endif
- /* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
- the socket send buffer size to '1', a write of this size on that
- socket will block.
- The Linux kernel imposes a minimum send socket buffer size which
- has changed over the years. As of Linux 3.10 the value is:
- 2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
- which is attempting to make sure that with standard MTUs,
- TCP can always queue up at least 2 full sized packets.
- Furthermore, there is logic in the socket send paths that
- will allow one more packet (of any size) to be queued up as
- long as some socket buffer space remains. Blocking only
- occurs when we try to queue up a new packet and the send
- buffer space has already been fully consumed.
- Therefore we must set this value to the largest possible value of
- the formula above (and since it depends upon the size of "struct
- sk_buff", it is dependent upon machine word size etc.) plus some
- slack space. */
- #define WRITE_BUFFER_SIZE 16384
- /* Cleanup handling test. */
- static int cl_called;
- static void
- cl (void *arg)
- {
- ++cl_called;
- }
- static void *
- tf_read (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- ssize_t s;
- pthread_cleanup_push (cl, NULL);
- char buf[100];
- s = read (fd, buf, sizeof (buf));
- pthread_cleanup_pop (0);
- printf ("%s: read returns with %zd\n", __FUNCTION__, s);
- exit (1);
- }
- static void *
- tf_readv (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- ssize_t s;
- pthread_cleanup_push (cl, NULL);
- char buf[100];
- struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
- s = readv (fd, iov, 1);
- pthread_cleanup_pop (0);
- printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
- exit (1);
- }
- static void *
- tf_write (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[1];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- ssize_t s;
- pthread_cleanup_push (cl, NULL);
- char buf[WRITE_BUFFER_SIZE];
- memset (buf, '\0', sizeof (buf));
- s = write (fd, buf, sizeof (buf));
- pthread_cleanup_pop (0);
- printf ("%s: write returns with %zd\n", __FUNCTION__, s);
- exit (1);
- }
- static void *
- tf_writev (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[1];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- ssize_t s;
- pthread_cleanup_push (cl, NULL);
- char buf[WRITE_BUFFER_SIZE];
- memset (buf, '\0', sizeof (buf));
- struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
- s = writev (fd, iov, 1);
- pthread_cleanup_pop (0);
- printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
- exit (1);
- }
- /* sleep is not early cancelable, disable for now */
- #if 0
- static void *
- tf_sleep (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- sleep (arg == NULL ? 1000000 : 0);
- pthread_cleanup_pop (0);
- printf ("%s: sleep returns\n", __FUNCTION__);
- exit (1);
- }
- #endif
- static void *
- tf_usleep (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
- pthread_cleanup_pop (0);
- printf ("%s: usleep returns\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_nanosleep (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
- TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
- pthread_cleanup_pop (0);
- printf ("%s: nanosleep returns\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_select (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- fd_set rfs;
- FD_ZERO (&rfs);
- FD_SET (fd, &rfs);
- int s;
- pthread_cleanup_push (cl, NULL);
- s = select (fd + 1, &rfs, NULL, NULL, NULL);
- pthread_cleanup_pop (0);
- printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_pselect (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- fd_set rfs;
- FD_ZERO (&rfs);
- FD_SET (fd, &rfs);
- int s;
- pthread_cleanup_push (cl, NULL);
- s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
- pthread_cleanup_pop (0);
- printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_poll (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
- int s;
- pthread_cleanup_push (cl, NULL);
- s = poll (rfs, 1, -1);
- pthread_cleanup_pop (0);
- printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_ppoll (void *arg)
- {
- int fd;
- int r;
- if (arg == NULL)
- fd = fds[0];
- else
- {
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = fd = mkstemp (fname);
- if (fd == -1)
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- unlink (fname);
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
- int s;
- pthread_cleanup_push (cl, NULL);
- s = ppoll (rfs, 1, NULL, NULL);
- pthread_cleanup_pop (0);
- printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_wait (void *arg)
- {
- pid_t pid = fork ();
- if (pid == -1)
- {
- puts ("fork failed");
- exit (1);
- }
- if (pid == 0)
- {
- /* Make the program disappear after a while. */
- if (arg == NULL)
- sleep (10);
- exit (0);
- }
- int r;
- if (arg != NULL)
- {
- struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
- while (nanosleep (&ts, &ts) != 0)
- continue;
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- int s;
- pthread_cleanup_push (cl, NULL);
- s = wait (NULL);
- pthread_cleanup_pop (0);
- printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_waitpid (void *arg)
- {
- pid_t pid = fork ();
- if (pid == -1)
- {
- puts ("fork failed");
- exit (1);
- }
- if (pid == 0)
- {
- /* Make the program disappear after a while. */
- if (arg == NULL)
- sleep (10);
- exit (0);
- }
- int r;
- if (arg != NULL)
- {
- struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
- while (nanosleep (&ts, &ts) != 0)
- continue;
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- int s;
- pthread_cleanup_push (cl, NULL);
- s = waitpid (-1, NULL, 0);
- pthread_cleanup_pop (0);
- printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_waitid (void *arg)
- {
- pid_t pid = fork ();
- if (pid == -1)
- {
- puts ("fork failed");
- exit (1);
- }
- if (pid == 0)
- {
- /* Make the program disappear after a while. */
- if (arg == NULL)
- sleep (10);
- exit (0);
- }
- int r;
- if (arg != NULL)
- {
- struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
- while (nanosleep (&ts, &ts) != 0)
- continue;
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- int s;
- pthread_cleanup_push (cl, NULL);
- #ifndef WEXITED
- # define WEXITED 0
- #endif
- siginfo_t si;
- s = waitid (P_PID, pid, &si, WEXITED);
- pthread_cleanup_pop (0);
- printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
- strerror (errno));
- exit (1);
- }
- static void *
- tf_sigpause (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- sigpause (SIGCANCEL);
- pthread_cleanup_pop (0);
- printf ("%s: sigpause returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_sigsuspend (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- /* Just for fun block all signals. */
- sigset_t mask;
- sigfillset (&mask);
- sigsuspend (&mask);
- pthread_cleanup_pop (0);
- printf ("%s: sigsuspend returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_sigwait (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- /* Block SIGUSR1. */
- sigset_t mask;
- sigemptyset (&mask);
- sigaddset (&mask, SIGUSR1);
- if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
- {
- printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
- exit (1);
- }
- int sig;
- pthread_cleanup_push (cl, NULL);
- /* Wait for SIGUSR1. */
- sigwait (&mask, &sig);
- pthread_cleanup_pop (0);
- printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
- exit (1);
- }
- static void *
- tf_sigwaitinfo (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- /* Block SIGUSR1. */
- sigset_t mask;
- sigemptyset (&mask);
- sigaddset (&mask, SIGUSR1);
- if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
- {
- printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
- exit (1);
- }
- siginfo_t info;
- pthread_cleanup_push (cl, NULL);
- /* Wait for SIGUSR1. */
- sigwaitinfo (&mask, &info);
- pthread_cleanup_pop (0);
- printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
- info.si_signo);
- exit (1);
- }
- static void *
- tf_sigtimedwait (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- /* Block SIGUSR1. */
- sigset_t mask;
- sigemptyset (&mask);
- sigaddset (&mask, SIGUSR1);
- if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
- {
- printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
- exit (1);
- }
- /* Wait for SIGUSR1. */
- siginfo_t info;
- struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
- pthread_cleanup_push (cl, NULL);
- sigtimedwait (&mask, &info, &ts);
- pthread_cleanup_pop (0);
- printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
- info.si_signo);
- exit (1);
- }
- static void *
- tf_pause (void *arg)
- {
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- pause ();
- pthread_cleanup_pop (0);
- printf ("%s: pause returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_accept (void *arg)
- {
- struct sockaddr_un sun;
- /* To test a non-blocking accept call we make the call file by using
- a datagrame socket. */
- int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
- tempfd = socket (AF_UNIX, pf, 0);
- if (tempfd == -1)
- {
- printf ("%s: socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- /* prevent endless loop, when bind fails forever */
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- unlink (sun.sun_path);
- listen (tempfd, 5);
- socklen_t len = sizeof (sun);
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- accept (tempfd, (struct sockaddr *) &sun, &len);
- pthread_cleanup_pop (0);
- printf ("%s: accept returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_send (void *arg)
- {
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- listen (tempfd, 5);
- tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
- {
- printf ("%s: connect failed\n", __FUNCTION__);
- exit(1);
- }
- unlink (sun.sun_path);
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- /* Very large block, so that the send call blocks. */
- char mem[700000];
- send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
- pthread_cleanup_pop (0);
- printf ("%s: send returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_recv (void *arg)
- {
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- listen (tempfd, 5);
- tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
- {
- printf ("%s: connect failed\n", __FUNCTION__);
- exit(1);
- }
- unlink (sun.sun_path);
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- char mem[70];
- recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
- pthread_cleanup_pop (0);
- printf ("%s: recv returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_recvfrom (void *arg)
- {
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- tempfname = strdup (sun.sun_path);
- tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- char mem[70];
- socklen_t len = sizeof (sun);
- recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
- (struct sockaddr *) &sun, &len);
- pthread_cleanup_pop (0);
- printf ("%s: recvfrom returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_recvmsg (void *arg)
- {
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- tempfname = strdup (sun.sun_path);
- tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- char mem[70];
- struct iovec iov[1];
- iov[0].iov_base = mem;
- iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
- struct msghdr m;
- m.msg_name = &sun;
- m.msg_namelen = sizeof (sun);
- m.msg_iov = iov;
- m.msg_iovlen = 1;
- m.msg_control = NULL;
- m.msg_controllen = 0;
- recvmsg (tempfd2, &m, 0);
- pthread_cleanup_pop (0);
- printf ("%s: recvmsg returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_open (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which open()
- // blocks we can enable this test to run in both rounds.
- abort ();
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- open ("Makefile", O_RDONLY);
- pthread_cleanup_pop (0);
- printf ("%s: open returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_close (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which close()
- // blocks we can enable this test to run in both rounds.
- abort ();
- char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
- tempfd = mkstemp (fname);
- if (tempfd == -1)
- {
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- exit (1);
- }
- unlink (fname);
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- close (tempfd);
- pthread_cleanup_pop (0);
- printf ("%s: close returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_pread (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which pread()
- // blocks we can enable this test to run in both rounds.
- abort ();
- tempfd = open ("Makefile", O_RDONLY);
- if (tempfd == -1)
- {
- printf ("%s: cannot open Makefile\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- char mem[10];
- pread (tempfd, mem, sizeof (mem), 0);
- pthread_cleanup_pop (0);
- printf ("%s: pread returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_pwrite (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which pwrite()
- // blocks we can enable this test to run in both rounds.
- abort ();
- char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
- tempfd = mkstemp (fname);
- if (tempfd == -1)
- {
- printf ("%s: mkstemp failed\n", __FUNCTION__);
- exit (1);
- }
- unlink (fname);
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- char mem[10];
- pwrite (tempfd, mem, sizeof (mem), 0);
- pthread_cleanup_pop (0);
- printf ("%s: pwrite returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_fsync (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which fsync()
- // blocks we can enable this test to run in both rounds.
- abort ();
- tempfd = open ("Makefile", O_RDONLY);
- if (tempfd == -1)
- {
- printf ("%s: cannot open Makefile\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- fsync (tempfd);
- pthread_cleanup_pop (0);
- printf ("%s: fsync returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_fdatasync (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which fdatasync()
- // blocks we can enable this test to run in both rounds.
- abort ();
- tempfd = open ("Makefile", O_RDONLY);
- if (tempfd == -1)
- {
- printf ("%s: cannot open Makefile\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- fdatasync (tempfd);
- pthread_cleanup_pop (0);
- printf ("%s: fdatasync returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_msync (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which msync()
- // blocks we can enable this test to run in both rounds.
- abort ();
- tempfd = open ("Makefile", O_RDONLY);
- if (tempfd == -1)
- {
- printf ("%s: cannot open Makefile\n", __FUNCTION__);
- exit (1);
- }
- void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
- if (p == MAP_FAILED)
- {
- printf ("%s: mmap failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- msync (p, 10, 0);
- pthread_cleanup_pop (0);
- printf ("%s: msync returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_sendto (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which sendto()
- // blocks we can enable this test to run in both rounds.
- abort ();
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- tempfname = strdup (sun.sun_path);
- tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- char mem[1];
- sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
- (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
- pthread_cleanup_pop (0);
- printf ("%s: sendto returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_sendmsg (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which sendmsg()
- // blocks we can enable this test to run in both rounds.
- abort ();
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- tempfname = strdup (sun.sun_path);
- tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- char mem[1];
- struct iovec iov[1];
- iov[0].iov_base = mem;
- iov[0].iov_len = 1;
- struct msghdr m;
- m.msg_name = &sun;
- m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1);
- m.msg_iov = iov;
- m.msg_iovlen = 1;
- m.msg_control = NULL;
- m.msg_controllen = 0;
- sendmsg (tempfd2, &m, 0);
- pthread_cleanup_pop (0);
- printf ("%s: sendmsg returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_creat (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which sendmsg()
- // blocks we can enable this test to run in both rounds.
- abort ();
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- creat ("tmp/tst-cancel-4-should-not-exist", 0666);
- pthread_cleanup_pop (0);
- printf ("%s: creat returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_connect (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which connect()
- // blocks we can enable this test to run in both rounds.
- abort ();
- struct sockaddr_un sun;
- tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd == -1)
- {
- printf ("%s: first socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int tries = 0;
- do
- {
- if (++tries > 10)
- {
- printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
- exit (1);
- }
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
- if (mktemp (sun.sun_path) == NULL)
- {
- printf ("%s: cannot generate temp file name\n", __FUNCTION__);
- exit (1);
- }
- sun.sun_family = AF_UNIX;
- }
- while (bind (tempfd, (struct sockaddr *) &sun,
- offsetof (struct sockaddr_un, sun_path)
- + strlen (sun.sun_path) + 1) != 0);
- tempfname = strdup (sun.sun_path);
- listen (tempfd, 5);
- tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
- if (tempfd2 == -1)
- {
- printf ("%s: second socket call failed\n", __FUNCTION__);
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
- pthread_cleanup_pop (0);
- printf ("%s: connect returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_tcdrain (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which tcdrain()
- // blocks we can enable this test to run in both rounds.
- abort ();
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- pthread_cleanup_push (cl, NULL);
- /* Regardless of stderr being a terminal, the tcdrain call should be
- canceled. */
- tcdrain (STDERR_FILENO);
- pthread_cleanup_pop (0);
- printf ("%s: tcdrain returned\n", __FUNCTION__);
- exit (1);
- }
- static void *
- tf_msgrcv (void *arg)
- {
- tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
- if (tempmsg == -1)
- {
- printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- if (arg != NULL)
- {
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- }
- ssize_t s;
- pthread_cleanup_push (cl, NULL);
- struct
- {
- long int type;
- char mem[10];
- } m;
- int randnr;
- /* We need a positive random number. */
- do
- randnr = random () % 64000;
- while (randnr <= 0);
- do
- {
- errno = 0;
- s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
- }
- while (errno == EIDRM || errno == EINTR);
- pthread_cleanup_pop (0);
- printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
- msgctl (tempmsg, IPC_RMID, NULL);
- exit (1);
- }
- static void *
- tf_msgsnd (void *arg)
- {
- if (arg == NULL)
- // XXX If somebody can provide a portable test case in which msgsnd()
- // blocks we can enable this test to run in both rounds.
- abort ();
- tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
- if (tempmsg == -1)
- {
- printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
- exit (1);
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
- exit (1);
- }
- pthread_cleanup_push (cl, NULL);
- struct
- {
- long int type;
- char mem[1];
- } m;
- /* We need a positive random number. */
- do
- m.type = random () % 64000;
- while (m.type <= 0);
- msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
- pthread_cleanup_pop (0);
- printf ("%s: msgsnd returned\n", __FUNCTION__);
- msgctl (tempmsg, IPC_RMID, NULL);
- exit (1);
- }
- static struct
- {
- const char *name;
- void *(*tf) (void *);
- int nb;
- int only_early;
- } tests[] =
- {
- #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
- ADD_TEST (read, 2, 0),
- ADD_TEST (readv, 2, 0),
- ADD_TEST (select, 2, 0),
- ADD_TEST (pselect, 2, 0),
- ADD_TEST (poll, 2, 0),
- ADD_TEST (ppoll, 2, 0),
- ADD_TEST (write, 2, 0),
- ADD_TEST (writev, 2, 0),
- ADD_TEST (usleep, 2, 0),
- ADD_TEST (nanosleep, 2, 0),
- ADD_TEST (wait, 2, 0),
- ADD_TEST (waitid, 2, 0),
- ADD_TEST (waitpid, 2, 0),
- ADD_TEST (sigpause, 2, 0),
- ADD_TEST (sigsuspend, 2, 0),
- ADD_TEST (sigwait, 2, 0),
- ADD_TEST (sigwaitinfo, 2, 0),
- ADD_TEST (sigtimedwait, 2, 0),
- ADD_TEST (pause, 2, 0),
- ADD_TEST (accept, 2, 0),
- ADD_TEST (send, 2, 0),
- ADD_TEST (recv, 2, 0),
- ADD_TEST (recvfrom, 2, 0),
- ADD_TEST (recvmsg, 2, 0),
- ADD_TEST (open, 2, 1),
- ADD_TEST (close, 2, 1),
- ADD_TEST (pread, 2, 1),
- ADD_TEST (pwrite, 2, 1),
- ADD_TEST (fsync, 2, 1),
- ADD_TEST (fdatasync, 2, 1),
- ADD_TEST (msync, 2, 1),
- ADD_TEST (sendto, 2, 1),
- ADD_TEST (sendmsg, 2, 1),
- ADD_TEST (creat, 2, 1),
- ADD_TEST (connect, 2, 1),
- ADD_TEST (tcdrain, 2, 1),
- ADD_TEST (msgrcv, 2, 0),
- ADD_TEST (msgsnd, 2, 1),
- };
- #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
- static int
- do_test (void)
- {
- int val;
- socklen_t len;
- if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
- {
- perror ("socketpair");
- exit (1);
- }
- val = 1;
- len = sizeof(val);
- setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
- if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
- {
- perror ("getsockopt");
- exit (1);
- }
- if (val >= WRITE_BUFFER_SIZE)
- {
- puts ("minimum write buffer size too large");
- exit (1);
- }
- setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
- int result = 0;
- size_t cnt;
- for (cnt = 0; cnt < ntest_tf; ++cnt)
- {
- if (tests[cnt].only_early)
- continue;
- if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
- {
- puts ("b2 init failed");
- exit (1);
- }
- /* Reset the counter for the cleanup handler. */
- cl_called = 0;
- pthread_t th;
- if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
- {
- printf ("create for '%s' test failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- result = 1;
- continue;
- }
- struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
- while (nanosleep (&ts, &ts) != 0)
- continue;
- if (pthread_cancel (th) != 0)
- {
- printf ("cancel for '%s' failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- void *status;
- if (pthread_join (th, &status) != 0)
- {
- printf ("join for '%s' failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (status != PTHREAD_CANCELED)
- {
- printf ("thread for '%s' not canceled\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (pthread_barrier_destroy (&b2) != 0)
- {
- puts ("barrier_destroy failed");
- result = 1;
- continue;
- }
- if (cl_called == 0)
- {
- printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (cl_called > 1)
- {
- printf ("cleanup handler called more than once for '%s'\n",
- tests[cnt].name);
- result = 1;
- continue;
- }
- printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
- if (tempfd != -1)
- {
- close (tempfd);
- tempfd = -1;
- }
- if (tempfd2 != -1)
- {
- close (tempfd2);
- tempfd2 = -1;
- }
- if (tempfname != NULL)
- {
- unlink (tempfname);
- free (tempfname);
- tempfname = NULL;
- }
- if (tempmsg != -1)
- {
- msgctl (tempmsg, IPC_RMID, NULL);
- tempmsg = -1;
- }
- }
- for (cnt = 0; cnt < ntest_tf; ++cnt)
- {
- if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
- {
- puts ("b2 init failed");
- exit (1);
- }
- /* Reset the counter for the cleanup handler. */
- cl_called = 0;
- pthread_t th;
- if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
- {
- printf ("create for '%s' test failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- int r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- result = 1;
- continue;
- }
- if (pthread_cancel (th) != 0)
- {
- printf ("cancel for '%s' failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- r = pthread_barrier_wait (&b2);
- if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
- {
- printf ("%s: barrier_wait failed\n", __FUNCTION__);
- result = 1;
- continue;
- }
- void *status;
- if (pthread_join (th, &status) != 0)
- {
- printf ("join for '%s' failed\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (status != PTHREAD_CANCELED)
- {
- printf ("thread for '%s' not canceled\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (pthread_barrier_destroy (&b2) != 0)
- {
- puts ("barrier_destroy failed");
- result = 1;
- continue;
- }
- if (cl_called == 0)
- {
- printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
- result = 1;
- continue;
- }
- if (cl_called > 1)
- {
- printf ("cleanup handler called more than once for '%s'\n",
- tests[cnt].name);
- result = 1;
- continue;
- }
- printf ("early cancel test of '%s' successful\n", tests[cnt].name);
- if (tempfd != -1)
- {
- close (tempfd);
- tempfd = -1;
- }
- if (tempfd2 != -1)
- {
- close (tempfd2);
- tempfd2 = -1;
- }
- if (tempfname != NULL)
- {
- unlink (tempfname);
- free (tempfname);
- tempfname = NULL;
- }
- if (tempmsg != -1)
- {
- msgctl (tempmsg, IPC_RMID, NULL);
- tempmsg = -1;
- }
- }
- return result;
- }
- #define TIMEOUT 60
- #define TEST_FUNCTION do_test ()
- #include "../test-skeleton.c"
|