mirror of
git://sourceware.org/git/glibc.git
synced 2024-12-21 04:31:04 +08:00
30891f35fa
We stopped adding "Contributed by" or similar lines in sources in 2012 in favour of git logs and keeping the Contributors section of the glibc manual up to date. Removing these lines makes the license header a bit more consistent across files and also removes the possibility of error in attribution when license blocks or files are copied across since the contributed-by lines don't actually reflect reality in those cases. Move all "Contributed by" and similar lines (Written by, Test by, etc.) into a new file CONTRIBUTED-BY to retain record of these contributions. These contributors are also mentioned in manual/contrib.texi, so we just maintain this additional record as a courtesy to the earlier developers. The following scripts were used to filter a list of files to edit in place and to clean up the CONTRIBUTED-BY file respectively. These were not added to the glibc sources because they're not expected to be of any use in future given that this is a one time task: https://gist.github.com/siddhesh/b5ecac94eabfd72ed2916d6d8157e7dc https://gist.github.com/siddhesh/15ea1f5e435ace9774f485030695ee02 Reviewed-by: Carlos O'Donell <carlos@redhat.com>
208 lines
4.5 KiB
C
208 lines
4.5 KiB
C
/* Copyright (C) 2002-2021 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
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
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <getopt.h>
|
|
#include <signal.h>
|
|
#include <stdlib.h>
|
|
#include <semaphore.h>
|
|
#include <sys/mman.h>
|
|
|
|
#include <support/check.h>
|
|
#include <support/support.h>
|
|
#include <support/temp_file.h>
|
|
#include <support/xstdio.h>
|
|
#include <support/xunistd.h>
|
|
#include <support/xthread.h>
|
|
|
|
static const char *command;
|
|
static const char *pidfile;
|
|
static const char *semfile;
|
|
static char *pidfilename;
|
|
static char *semfilename;
|
|
|
|
static sem_t *sem;
|
|
|
|
static void *
|
|
tf (void *arg)
|
|
{
|
|
char *cmd = xasprintf ("%s --direct --sem %s --pidfile %s",
|
|
command, semfilename, pidfilename);
|
|
system (cmd);
|
|
/* This call should never return. */
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
sl (void)
|
|
{
|
|
FILE *f = xfopen (pidfile, "w");
|
|
|
|
fprintf (f, "%lld\n", (long long) getpid ());
|
|
fflush (f);
|
|
|
|
if (sem_post (sem) != 0)
|
|
FAIL_EXIT1 ("sem_post: %m");
|
|
|
|
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)
|
|
FAIL_EXIT1 ("fcntl (F_SETFL): %m");
|
|
|
|
sigset_t ss;
|
|
sigfillset (&ss);
|
|
sigsuspend (&ss);
|
|
exit (0);
|
|
}
|
|
|
|
|
|
static void
|
|
do_prepare (int argc, char *argv[])
|
|
{
|
|
int semfd;
|
|
if (semfile == NULL)
|
|
semfd = create_temp_file ("tst-cancel7.", &semfilename);
|
|
else
|
|
semfd = open (semfile, O_RDWR);
|
|
TEST_VERIFY_EXIT (semfd != -1);
|
|
|
|
sem = xmmap (NULL, sizeof (sem_t), PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
semfd);
|
|
TEST_VERIFY_EXIT (sem != SEM_FAILED);
|
|
if (semfile == NULL)
|
|
{
|
|
xftruncate (semfd, sizeof (sem_t));
|
|
TEST_VERIFY_EXIT (sem_init (sem, 1, 0) != -1);
|
|
}
|
|
|
|
if (command == NULL)
|
|
command = argv[0];
|
|
|
|
if (pidfile)
|
|
sl ();
|
|
|
|
int fd = create_temp_file ("tst-cancel7-pid-", &pidfilename);
|
|
if (fd == -1)
|
|
FAIL_EXIT1 ("create_temp_file failed: %m");
|
|
|
|
xwrite (fd, " ", 1);
|
|
xclose (fd);
|
|
}
|
|
|
|
|
|
static int
|
|
do_test (void)
|
|
{
|
|
pthread_t th = xpthread_create (NULL, tf, NULL);
|
|
|
|
do
|
|
nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 100000000 }, NULL);
|
|
while (access (pidfilename, R_OK) != 0);
|
|
|
|
xpthread_cancel (th);
|
|
void *r = xpthread_join (th);
|
|
|
|
if (sem_wait (sem) != 0)
|
|
FAIL_EXIT1 ("sem_wait: %m");
|
|
|
|
FILE *f = xfopen (pidfilename, "r+");
|
|
|
|
long long ll;
|
|
if (fscanf (f, "%lld\n", &ll) != 1)
|
|
FAIL_EXIT1 ("fscanf: %m");
|
|
|
|
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)
|
|
FAIL_EXIT1 ("fcntl: %m");
|
|
|
|
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);
|
|
|
|
return 1;
|
|
}
|
|
|
|
xfclose (f);
|
|
|
|
return r != PTHREAD_CANCELED;
|
|
}
|
|
|
|
static void
|
|
do_cleanup (void)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
#define OPT_COMMAND 10000
|
|
#define OPT_PIDFILE 10001
|
|
#define OPT_SEMFILE 10002
|
|
#define CMDLINE_OPTIONS \
|
|
{ "command", required_argument, NULL, OPT_COMMAND }, \
|
|
{ "pidfile", required_argument, NULL, OPT_PIDFILE }, \
|
|
{ "sem", required_argument, NULL, OPT_SEMFILE },
|
|
static void
|
|
cmdline_process (int c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case OPT_COMMAND:
|
|
command = optarg;
|
|
break;
|
|
case OPT_PIDFILE:
|
|
pidfile = optarg;
|
|
break;
|
|
case OPT_SEMFILE:
|
|
semfile = optarg;
|
|
break;
|
|
}
|
|
}
|
|
#define CMDLINE_PROCESS cmdline_process
|
|
#define CLEANUP_HANDLER do_cleanup
|
|
#define PREPARE do_prepare
|
|
#include <support/test-driver.c>
|