Back to index

glibc  2.9
Defines | Functions | Variables
tst-cancel7.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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 OPT_COMMAND   10000
#define OPT_PIDFILE   10001
#define CMDLINE_OPTIONS
#define CMDLINE_PROCESS
#define PREPARE(argc, argv)   do_prepare (argc, argv)
#define TEST_FUNCTION   do_test ()
#define TIMEOUT   5

Functions

static void * tf (void *arg)
static void sl (void)
static void do_prepare (int argc, char *argv[])
static int do_test (void)
static void do_cleanup (void)

Variables

const char * command
const char * pidfile
char pidfilename [] = "/tmp/tst-cancel7-XXXXXX"

Define Documentation

#define CMDLINE_OPTIONS
Value:
{ "command", required_argument, NULL, OPT_COMMAND },    \
  { "pidfile", required_argument, NULL, OPT_PIDFILE },

Definition at line 197 of file tst-cancel7.c.

#define CMDLINE_PROCESS
Value:
case OPT_COMMAND:    \
    command = optarg;       \
    break;           \
  case OPT_PIDFILE:  \
    pidfile = optarg;       \
    break;

Definition at line 200 of file tst-cancel7.c.

#define OPT_COMMAND   10000

Definition at line 195 of file tst-cancel7.c.

#define OPT_PIDFILE   10001

Definition at line 196 of file tst-cancel7.c.

#define PREPARE (   argc,
  argv 
)    do_prepare (argc, argv)

Definition at line 208 of file tst-cancel7.c.

#define TEST_FUNCTION   do_test ()

Definition at line 209 of file tst-cancel7.c.

#define TIMEOUT   5

Definition at line 210 of file tst-cancel7.c.


Function Documentation

static void do_cleanup ( void  ) [static]

Definition at line 171 of file tst-cancel7.c.

{
  FILE *f = fopen (pidfilename, "r+");
  long long ll;

  if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
    {
      struct flock fl =
       {
         .l_type = F_WRLCK,
         .l_start = 0,
         .l_whence = SEEK_SET,
         .l_len = 1
       };
      if (fcntl (fileno (f), F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK
         && fl.l_pid == ll)
       kill (fl.l_pid, SIGKILL);

      fclose (f);
    }

  unlink (pidfilename);
}

Here is the call graph for this function:

static void do_prepare ( int  argc,
char *  argv[] 
) [static]

Definition at line 75 of file tst-cancel7.c.

{
  if (command == NULL)
    command = argv[0];

  if (pidfile)
    sl ();

  int fd = mkstemp (pidfilename);
  if (fd == -1)
    {
      puts ("mkstemp failed");
      exit (1);
    }

  write (fd, " ", 1);
  close (fd);
}

Here is the call graph for this function:

static int do_test ( void  ) [static]

Definition at line 96 of file tst-cancel7.c.

{
  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("pthread_create failed");
      return 1;
    }

  do
    sleep (1);
  while (access (pidfilename, R_OK) != 0);

  if (pthread_cancel (th) != 0)
    {
      puts ("pthread_cancel failed");
      return 1;
    }

  void *r;
  if (pthread_join (th, &r) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }

  sleep (1);

  FILE *f = fopen (pidfilename, "r+");
  if (f == NULL)
    {
      puts ("no pidfile");
      return 1;
    }

  long long ll;
  if (fscanf (f, "%lld\n", &ll) != 1)
    {
      puts ("could not read pid");
      unlink (pidfilename);
      return 1;
    }

  struct flock fl =
    {
      .l_type = F_WRLCK,
      .l_start = 0,
      .l_whence = SEEK_SET,
      .l_len = 1
    };
  if (fcntl (fileno (f), F_GETLK, &fl) != 0)
    {
      puts ("F_GETLK failed");
      unlink (pidfilename);
      return 1;
    }

  if (fl.l_type != F_UNLCK)
    {
      printf ("child %lld still running\n", (long long) fl.l_pid);
      if (fl.l_pid == ll)
       kill (fl.l_pid, SIGKILL);

      unlink (pidfilename);
      return 1;
    }

  fclose (f);

  unlink (pidfilename);

  return r != PTHREAD_CANCELED;
}

Here is the call graph for this function:

static void sl ( void  ) [static]

Definition at line 48 of file tst-cancel7.c.

{
  FILE *f = fopen (pidfile, "w");
  if (f == NULL)
    exit (1);

  fprintf (f, "%lld\n", (long long) getpid ());
  fflush (f);

  struct flock fl =
    {
      .l_type = F_WRLCK,
      .l_start = 0,
      .l_whence = SEEK_SET,
      .l_len = 1
    };
  if (fcntl (fileno (f), F_SETLK, &fl) != 0)
    exit (1);

  sigset_t ss;
  sigfillset (&ss);
  sigsuspend (&ss);
  exit (0);
}

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-cancel7.c.

{
  const char *args = " --direct --pidfile ";
  char *cmd = alloca (strlen (command) + strlen (args)
                    + strlen (pidfilename) + 1);

  strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
  system (cmd);
  /* This call should never return.  */
  return NULL;
}

Here is the call graph for this function:


Variable Documentation

const char* command

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

const char* pidfile

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

char pidfilename[] = "/tmp/tst-cancel7-XXXXXX"

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