#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 "pthreadP.h"
-/* The following interfaces are defined to be cancellation points but
- tests are not yet implemented:
-
- aio_suspend() clock_nanosleep()
- connect() creat()
- msgrcv() msgsnd()
- sendmsg() sendto()
- tcdrain()
-
- Since STREAMS are not supported in the standard Linux kernel there
- is no need to test the STREAMS related functions. This affects
+/* 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()
POSIX message queues aren't implemented yet. This affects
mq_receive() mq_send() mq_timedreceive() mq_timedsend()
+
+ aio_suspend() is tested in tst-cancel17.
+
+ clock_nanosleep() is tested in tst-cancel18.
*/
/* Pipe descriptors. */
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;
exit (1);
}
+ ssize_t s;
pthread_cleanup_push (cl, NULL);
char buf[100];
- ssize_t s = read (fd, buf, sizeof (buf));
-
- printf ("%s: read returns with %zd\n", __FUNCTION__, s);
+ s = read (fd, buf, sizeof (buf));
pthread_cleanup_pop (0);
+ printf ("%s: read returns with %zd\n", __FUNCTION__, s);
+
exit (1);
}
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) } };
- ssize_t s = readv (fd, iov, 1);
-
- printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
+ s = readv (fd, iov, 1);
pthread_cleanup_pop (0);
+ printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
+
exit (1);
}
exit (1);
}
+ ssize_t s;
pthread_cleanup_push (cl, NULL);
char buf[100000];
memset (buf, '\0', sizeof (buf));
- ssize_t s = write (fd, buf, sizeof (buf));
-
- printf ("%s: write returns with %zd\n", __FUNCTION__, s);
+ s = write (fd, buf, sizeof (buf));
pthread_cleanup_pop (0);
+ printf ("%s: write returns with %zd\n", __FUNCTION__, s);
+
exit (1);
}
exit (1);
}
+ ssize_t s;
pthread_cleanup_push (cl, NULL);
char buf[100000];
memset (buf, '\0', sizeof (buf));
struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
- ssize_t s = writev (fd, iov, 1);
-
- printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
+ s = writev (fd, iov, 1);
pthread_cleanup_pop (0);
+ printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
+
exit (1);
}
pthread_cleanup_push (cl, NULL);
struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
- while (nanosleep (&ts, &ts) != 0)
- continue;
+ TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
pthread_cleanup_pop (0);
FD_ZERO (&rfs);
FD_SET (fd, &rfs);
+ int s;
pthread_cleanup_push (cl, NULL);
- int s = select (fd + 1, &rfs, NULL, NULL, 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));
- pthread_cleanup_pop (0);
-
exit (1);
}
FD_ZERO (&rfs);
FD_SET (fd, &rfs);
+ int s;
pthread_cleanup_push (cl, NULL);
- int s = pselect (fd + 1, &rfs, NULL, NULL, NULL, 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));
- pthread_cleanup_pop (0);
-
exit (1);
}
struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
+ int s;
pthread_cleanup_push (cl, NULL);
- int s = poll (rfs, 1, -1);
+ s = poll (rfs, 1, -1);
+
+ pthread_cleanup_pop (0);
printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
strerror (errno));
- pthread_cleanup_pop (0);
-
exit (1);
}
exit (1);
}
+ int s;
pthread_cleanup_push (cl, NULL);
- int s = wait (NULL);
+ s = wait (NULL);
+
+ pthread_cleanup_pop (0);
printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
strerror (errno));
- pthread_cleanup_pop (0);
-
exit (1);
}
exit (1);
}
+ int s;
pthread_cleanup_push (cl, NULL);
- int s = waitpid (-1, NULL, 0);
+ s = waitpid (-1, NULL, 0);
+
+ pthread_cleanup_pop (0);
printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
strerror (errno));
- pthread_cleanup_pop (0);
-
exit (1);
}
exit (1);
}
+ int s;
pthread_cleanup_push (cl, NULL);
siginfo_t si;
- int s = waitid (P_PID, pid, &si, 0);
+ s = waitid (P_PID, pid, &si, 0);
+
+ pthread_cleanup_pop (0);
printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
strerror (errno));
- pthread_cleanup_pop (0);
-
exit (1);
}
}
}
- pthread_cleanup_push (cl, NULL);
-
/* Block SIGUSR1. */
sigset_t mask;
sigaddset (&mask, SIGUSR1);
exit (1);
}
- /* Wait for SIGUSR1. */
int sig;
- sigwait (&mask, &sig);
+ pthread_cleanup_push (cl, NULL);
- printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
+ /* Wait for SIGUSR1. */
+ sigwait (&mask, &sig);
pthread_cleanup_pop (0);
+ printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
+
exit (1);
}
}
}
- pthread_cleanup_push (cl, NULL);
-
/* Block SIGUSR1. */
sigset_t mask;
sigaddset (&mask, SIGUSR1);
exit (1);
}
- /* Wait for SIGUSR1. */
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);
- pthread_cleanup_pop (0);
-
exit (1);
}
}
}
- pthread_cleanup_push (cl, NULL);
-
/* Block SIGUSR1. */
sigset_t mask;
sigaddset (&mask, SIGUSR1);
/* 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);
- pthread_cleanup_pop (0);
-
exit (1);
}
printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
}
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-XXXXXX");
+ 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__);
printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
}
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-XXXXXX");
+ 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__);
printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
}
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-XXXXXX");
+ 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__);
printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
}
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-XXXXXX");
+ 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__);
printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
}
- strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-XXXXXX");
+ 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__);
}
+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__);
+ }
+
+ 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__);
+ }
+
+ 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__);
+ }
+
+ 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 (random (), 0666 | IPC_CREAT);
+
+ 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);
+
+ struct
+ {
+ long int type;
+ char mem[10];
+ } m;
+ msgrcv (tempmsg, (struct msgbuf *) &m, 10, 100, 0);
+
+ pthread_cleanup_pop (0);
+
+ printf ("%s: msgrcv returned\n", __FUNCTION__);
+
+ 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 (random (), 0666 | IPC_CREAT);
+
+ 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;
+ m.type = 100;
+ msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
+
+ pthread_cleanup_pop (0);
+
+ printf ("%s: msgsnd returned\n", __FUNCTION__);
+
+ exit (1);
+}
+
+
static struct
{
const char *name;
ADD_TEST (pwrite, 2, 1),
ADD_TEST (fsync, 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]))
close (tempfd2);
tempfd2 = -1;
}
- free (tempfname);
- tempfname = NULL;
+ if (tempfname != NULL)
+ {
+ unlink (tempfname);
+ free (tempfname);
+ tempfname = NULL;
+ }
+ if (tempmsg != -1)
+ {
+ msgctl (tempmsg, IPC_RMID, NULL);
+ tempmsg = -1;
+ }
}
return result;