Back to index

glibc  2.9
Defines | Functions | Variables
tst-sighandler1.c File Reference
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <limits.h>
#include "../test-skeleton.c"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define TEST_FUNCTION   do_test ()

Functions

static void * tf (void *arg)
static void sig_segv (int unused)
int do_test (void)

Variables

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER
pthread_cond_t cond = PTHREAD_COND_INITIALIZER

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 174 of file tst-sighandler1.c.


Function Documentation

int do_test ( void  )

Definition at line 88 of file tst-sighandler1.c.

{
  int fds[2];
  pthread_t thr;

  if (pipe (fds) < 0)
    {
      printf ("couldn't create pipe: %m\n");
      return 1;
    }

  pid_t pid = fork ();
  if (pid < 0)
    {
      printf ("fork failed: %m\n");
      return 1;
    }

  if (!pid)
    {
      void *ret;

      close (fds[0]);
      signal (SIGSEGV, sig_segv);
      pthread_mutex_lock (&mutex2);
      pthread_create (&thr, NULL, tf, (void *) (long) (fds[1]));
      /* Wait for the thread to go into cond_wait.  */
      pthread_mutex_lock (&mutex2);
      pthread_mutex_lock (&mutex);
      write (fds[1], "", 1);
      pthread_join (thr, &ret);
      exit (0);
    }

  close (fds[1]);

  int child_pid;
  if (TEMP_FAILURE_RETRY (read (fds[0], &child_pid, sizeof (int)))
      != sizeof (int))
    {
      puts ("could not read pid from child");
      return 1;
    }

  char dummy;
  if (TEMP_FAILURE_RETRY (read (fds[0], &dummy, 1)) != 1)
    {
      puts ("could not read sync byte from child");
      return 1;
    }

  close (fds[0]);

  /* The sync byte may come immediately after the mutex is released;
     wait a little bit more to make sure the thread has suspended, to
     test the right bug.  */
  struct timespec ts;
  ts.tv_sec = 0;
  ts.tv_nsec = 200000000;
  nanosleep (&ts, NULL);

  kill (child_pid, SIGSEGV);

  pid_t termpid;
  int status;
  termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  if (termpid == -1)
    {
      printf ("waitpid failed: %m\n");
      return 1;
    }
  else if (termpid != pid)
    {
      printf ("waitpid returned %ld != %ld\n",
             (long int) termpid, (long int) pid);
      return 1;
    }
  else if (!WIFEXITED (status) || WEXITSTATUS (status) != 2)
    {
      puts ("child hasn't exited with exit status 2");
      return 1;
    }

  return 0;
}

Here is the call graph for this function:

static void sig_segv ( int  unused) [static]

Definition at line 82 of file tst-sighandler1.c.

{
  exit (2);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void* tf ( void *  arg) [static]

Definition at line 34 of file tst-sighandler1.c.

{
  int fd = (long) arg;
  int pid = getpid ();
#ifdef TIMED
  struct timespec ts;
#endif

  if (pthread_mutex_lock (&mutex) != 0)
    {
      puts ("Could not lock mutex in thread");
      return (void *) 1L;
    }

  write (fd, &pid, sizeof (int));

  if (pthread_mutex_unlock (&mutex2) != 0)
    {
      puts ("Could not unlock mutex2 in thread");
      return (void *) 1L;
    }

#ifdef TIMED
  ts.tv_sec = INT_MAX;
  ts.tv_nsec = 0;
  if (pthread_cond_timedwait (&cond, &mutex, &ts) != 0)
    {
      puts ("pthread_cond_wait failed in thread");
      return (void *) 1L;
    }
#else
  if (pthread_cond_wait (&cond, &mutex) != 0)
    {
      puts ("pthread_cond_wait failed in thread");
      return (void *) 1L;
    }
#endif

  if (pthread_mutex_unlock (&mutex) != 0)
    {
      puts ("Could not unlock mutex in thread");
      return (void *) 1L;
    }

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 31 of file tst-sighandler1.c.

Definition at line 29 of file tst-sighandler1.c.

Definition at line 30 of file tst-sighandler1.c.