]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-cancel7.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-cancel7.c
1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
4
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.
9
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.
14
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/>. */
18
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>
28
29 #include <support/xthread.h>
30
31 const char *command;
32 const char *pidfile;
33 char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
34
35 static void *
36 tf (void *arg)
37 {
38 const char *args = " --direct --pidfile ";
39 char *cmd = alloca (strlen (command) + strlen (args)
40 + strlen (pidfilename) + 1);
41
42 strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
43 system (cmd);
44 /* This call should never return. */
45 return NULL;
46 }
47
48
49 static void
50 sl (void)
51 {
52 FILE *f = fopen (pidfile, "w");
53 if (f == NULL)
54 exit (1);
55
56 fprintf (f, "%lld\n", (long long) getpid ());
57 fflush (f);
58
59 struct flock fl =
60 {
61 .l_type = F_WRLCK,
62 .l_start = 0,
63 .l_whence = SEEK_SET,
64 .l_len = 1
65 };
66 if (fcntl (fileno (f), F_SETLK, &fl) != 0)
67 exit (1);
68
69 sigset_t ss;
70 sigfillset (&ss);
71 sigsuspend (&ss);
72 exit (0);
73 }
74
75
76 static void
77 do_prepare (int argc, char *argv[])
78 {
79 if (command == NULL)
80 command = argv[0];
81
82 if (pidfile)
83 sl ();
84
85 int fd = mkstemp (pidfilename);
86 if (fd == -1)
87 {
88 puts ("mkstemp failed");
89 exit (1);
90 }
91
92 write (fd, " ", 1);
93 close (fd);
94 }
95
96
97 static int
98 do_test (void)
99 {
100 pthread_t th;
101 if (pthread_create (&th, NULL, tf, NULL) != 0)
102 {
103 puts ("pthread_create failed");
104 return 1;
105 }
106
107 do
108 sleep (1);
109 while (access (pidfilename, R_OK) != 0);
110
111 xpthread_cancel (th);
112 void *r = xpthread_join (th);
113
114 sleep (1);
115
116 FILE *f = fopen (pidfilename, "r+");
117 if (f == NULL)
118 {
119 puts ("no pidfile");
120 return 1;
121 }
122
123 long long ll;
124 if (fscanf (f, "%lld\n", &ll) != 1)
125 {
126 puts ("could not read pid");
127 unlink (pidfilename);
128 return 1;
129 }
130
131 struct flock fl =
132 {
133 .l_type = F_WRLCK,
134 .l_start = 0,
135 .l_whence = SEEK_SET,
136 .l_len = 1
137 };
138 if (fcntl (fileno (f), F_GETLK, &fl) != 0)
139 {
140 puts ("F_GETLK failed");
141 unlink (pidfilename);
142 return 1;
143 }
144
145 if (fl.l_type != F_UNLCK)
146 {
147 printf ("child %lld still running\n", (long long) fl.l_pid);
148 if (fl.l_pid == ll)
149 kill (fl.l_pid, SIGKILL);
150
151 unlink (pidfilename);
152 return 1;
153 }
154
155 fclose (f);
156
157 unlink (pidfilename);
158
159 return r != PTHREAD_CANCELED;
160 }
161
162 static void
163 do_cleanup (void)
164 {
165 FILE *f = fopen (pidfilename, "r+");
166 long long ll;
167
168 if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
169 {
170 struct flock fl =
171 {
172 .l_type = F_WRLCK,
173 .l_start = 0,
174 .l_whence = SEEK_SET,
175 .l_len = 1
176 };
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);
180
181 fclose (f);
182 }
183
184 unlink (pidfilename);
185 }
186
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)
194 {
195 switch (c)
196 {
197 case OPT_COMMAND:
198 command = optarg;
199 break;
200 case OPT_PIDFILE:
201 pidfile = optarg;
202 break;
203 }
204 }
205 #define CMDLINE_PROCESS cmdline_process
206 #define CLEANUP_HANDLER do_cleanup
207 #define PREPARE do_prepare
208 #include <support/test-driver.c>