powerpc64le: use common fmaf128 implementation
[glibc.git] / nptl / tst-cancel7.c
blob7548e93c6f0db73b053483a43cda5b09f13b7e1d
1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <getopt.h>
29 #include <support/xthread.h>
31 const char *command;
32 const char *pidfile;
33 char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
35 static void *
36 tf (void *arg)
38 const char *args = " --direct --pidfile ";
39 char *cmd = alloca (strlen (command) + strlen (args)
40 + strlen (pidfilename) + 1);
42 strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
43 system (cmd);
44 /* This call should never return. */
45 return NULL;
49 static void
50 sl (void)
52 FILE *f = fopen (pidfile, "w");
53 if (f == NULL)
54 exit (1);
56 fprintf (f, "%lld\n", (long long) getpid ());
57 fflush (f);
59 struct flock fl =
61 .l_type = F_WRLCK,
62 .l_start = 0,
63 .l_whence = SEEK_SET,
64 .l_len = 1
66 if (fcntl (fileno (f), F_SETLK, &fl) != 0)
67 exit (1);
69 sigset_t ss;
70 sigfillset (&ss);
71 sigsuspend (&ss);
72 exit (0);
76 static void
77 do_prepare (int argc, char *argv[])
79 if (command == NULL)
80 command = argv[0];
82 if (pidfile)
83 sl ();
85 int fd = mkstemp (pidfilename);
86 if (fd == -1)
88 puts ("mkstemp failed");
89 exit (1);
92 write (fd, " ", 1);
93 close (fd);
97 static int
98 do_test (void)
100 pthread_t th;
101 if (pthread_create (&th, NULL, tf, NULL) != 0)
103 puts ("pthread_create failed");
104 return 1;
108 sleep (1);
109 while (access (pidfilename, R_OK) != 0);
111 xpthread_cancel (th);
112 void *r = xpthread_join (th);
114 sleep (1);
116 FILE *f = fopen (pidfilename, "r+");
117 if (f == NULL)
119 puts ("no pidfile");
120 return 1;
123 long long ll;
124 if (fscanf (f, "%lld\n", &ll) != 1)
126 puts ("could not read pid");
127 unlink (pidfilename);
128 return 1;
131 struct flock fl =
133 .l_type = F_WRLCK,
134 .l_start = 0,
135 .l_whence = SEEK_SET,
136 .l_len = 1
138 if (fcntl (fileno (f), F_GETLK, &fl) != 0)
140 puts ("F_GETLK failed");
141 unlink (pidfilename);
142 return 1;
145 if (fl.l_type != F_UNLCK)
147 printf ("child %lld still running\n", (long long) fl.l_pid);
148 if (fl.l_pid == ll)
149 kill (fl.l_pid, SIGKILL);
151 unlink (pidfilename);
152 return 1;
155 fclose (f);
157 unlink (pidfilename);
159 return r != PTHREAD_CANCELED;
162 static void
163 do_cleanup (void)
165 FILE *f = fopen (pidfilename, "r+");
166 long long ll;
168 if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
170 struct flock fl =
172 .l_type = F_WRLCK,
173 .l_start = 0,
174 .l_whence = SEEK_SET,
175 .l_len = 1
177 if (fcntl (fileno (f), F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK
178 && fl.l_pid == ll)
179 kill (fl.l_pid, SIGKILL);
181 fclose (f);
184 unlink (pidfilename);
187 #define OPT_COMMAND 10000
188 #define OPT_PIDFILE 10001
189 #define CMDLINE_OPTIONS \
190 { "command", required_argument, NULL, OPT_COMMAND }, \
191 { "pidfile", required_argument, NULL, OPT_PIDFILE },
192 static void
193 cmdline_process (int c)
195 switch (c)
197 case OPT_COMMAND:
198 command = optarg;
199 break;
200 case OPT_PIDFILE:
201 pidfile = optarg;
202 break;
205 #define CMDLINE_PROCESS cmdline_process
206 #define CLEANUP_HANDLER do_cleanup
207 #define PREPARE do_prepare
208 #include <support/test-driver.c>