|
Packit Service |
82fcde |
/* Copyright (C) 2003-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
#include <pthread.h>
|
|
Packit Service |
82fcde |
#include <signal.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <stdlib.h>
|
|
Packit Service |
82fcde |
#include <unistd.h>
|
|
Packit Service |
82fcde |
#include <sys/mman.h>
|
|
Packit Service |
82fcde |
#include <sys/wait.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static char fname[] = "/tmp/tst-cond12-XXXXXX";
|
|
Packit Service |
82fcde |
static int fd;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static void prepare (void);
|
|
Packit Service |
82fcde |
#define PREPARE(argc, argv) prepare ()
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int do_test (void);
|
|
Packit Service |
82fcde |
#define TEST_FUNCTION do_test ()
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include "../test-skeleton.c"
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static void
|
|
Packit Service |
82fcde |
prepare (void)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
fd = mkstemp (fname);
|
|
Packit Service |
82fcde |
if (fd == -1)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("mkstemp failed: %m\n");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
add_temp_file (fname);
|
|
Packit Service |
82fcde |
if (ftruncate (fd, 1000) < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("ftruncate failed: %m\n");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
do_test (void)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
struct
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
pthread_mutex_t m;
|
|
Packit Service |
82fcde |
pthread_cond_t c;
|
|
Packit Service |
82fcde |
int var;
|
|
Packit Service |
82fcde |
} *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
Packit Service |
82fcde |
if (p == MAP_FAILED)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("initial mmap failed: %m\n");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
pthread_mutexattr_t ma;
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_init (&ma) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_setpshared (&ma, 1) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_setpshared failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_mutex_init (&p->m, &ma) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutex_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_destroy (&ma) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_destroy failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
pthread_condattr_t ca;
|
|
Packit Service |
82fcde |
if (pthread_condattr_init (&ca) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("condattr_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_condattr_setpshared (&ca, 1) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("condattr_setpshared failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_cond_init (&p->c, &ca) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutex_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (pthread_condattr_destroy (&ca) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("condattr_destroy failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_lock (&p->m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("initial mutex_lock failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
p->var = 42;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
pid_t pid = fork ();
|
|
Packit Service |
82fcde |
if (pid == -1)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("fork failed: %m\n");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pid == 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
void *oldp = p;
|
|
Packit Service |
82fcde |
p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (p == oldp)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("child: mapped to same address");
|
|
Packit Service |
82fcde |
kill (getppid (), SIGKILL);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
munmap (oldp, sizeof (*p));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_lock (&p->m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("child: mutex_lock failed");
|
|
Packit Service |
82fcde |
kill (getppid (), SIGKILL);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
p->var = 0;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef USE_COND_SIGNAL
|
|
Packit Service |
82fcde |
if (pthread_cond_broadcast (&p->c) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("child: cond_broadcast failed");
|
|
Packit Service |
82fcde |
kill (getppid (), SIGKILL);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
if (pthread_cond_signal (&p->c) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("child: cond_signal failed");
|
|
Packit Service |
82fcde |
kill (getppid (), SIGKILL);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_unlock (&p->m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("child: mutex_unlock failed");
|
|
Packit Service |
82fcde |
kill (getppid (), SIGKILL);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
exit (0);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
do
|
|
Packit Service |
82fcde |
pthread_cond_wait (&p->c, &p->m);
|
|
Packit Service |
82fcde |
while (p->var != 0);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("waitpid failed: %m\n");
|
|
Packit Service |
82fcde |
kill (pid, SIGKILL);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|