sysdeps/mach/hurd/sys/param.h (DEV_BSIZE): New macro.
[glibc.git] / nptl / tst-cancel7.c
blobbe9b1c60647219aedc8b74277abcaff62ffb9df6
1 /* Copyright (C) 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 const char *command;
30 const char *pidfile;
31 char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
33 static void *
34 tf (void *arg)
36 const char *args = " --direct --pidfile ";
37 char *cmd = alloca (strlen (command) + strlen (args)
38 + strlen (pidfilename) + 1);
40 strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
41 system (cmd);
42 /* This call should never return. */
43 return NULL;
47 static void
48 sl (void)
50 FILE *f = fopen (pidfile, "w");
51 if (f == NULL)
52 exit (1);
54 fprintf (f, "%lld\n", (long long) getpid ());
55 fflush (f);
57 struct flock fl =
59 .l_type = F_WRLCK,
60 .l_start = 0,
61 .l_whence = SEEK_SET,
62 .l_len = 1
64 if (fcntl (fileno (f), F_SETLK, &fl) != 0)
65 exit (1);
67 sigset_t ss;
68 sigfillset (&ss);
69 sigsuspend (&ss);
70 exit (0);
74 static void
75 do_prepare (int argc, char *argv[])
77 if (command == NULL)
78 command = argv[0];
80 if (pidfile)
81 sl ();
83 int fd = mkstemp (pidfilename);
84 if (fd == -1)
86 puts ("mkstemp failed");
87 exit (1);
90 write (fd, " ", 1);
91 close (fd);
95 static int
96 do_test (void)
98 pthread_t th;
99 if (pthread_create (&th, NULL, tf, NULL) != 0)
101 puts ("pthread_create failed");
102 return 1;
106 sleep (1);
107 while (access (pidfilename, R_OK) != 0);
109 if (pthread_cancel (th) != 0)
111 puts ("pthread_cancel failed");
112 return 1;
115 void *r;
116 if (pthread_join (th, &r) != 0)
118 puts ("pthread_join failed");
119 return 1;
122 sleep (1);
124 FILE *f = fopen (pidfilename, "r+");
125 if (f == NULL)
127 puts ("no pidfile");
128 return 1;
131 long long ll;
132 if (fscanf (f, "%lld\n", &ll) != 1)
134 puts ("could not read pid");
135 unlink (pidfilename);
136 return 1;
139 struct flock fl =
141 .l_type = F_WRLCK,
142 .l_start = 0,
143 .l_whence = SEEK_SET,
144 .l_len = 1
146 if (fcntl (fileno (f), F_GETLK, &fl) != 0)
148 puts ("F_GETLK failed");
149 unlink (pidfilename);
150 return 1;
153 if (fl.l_type != F_UNLCK)
155 printf ("child %lld still running\n", (long long) fl.l_pid);
156 if (fl.l_pid == ll)
157 kill (fl.l_pid, SIGKILL);
159 unlink (pidfilename);
160 return 1;
163 fclose (f);
165 unlink (pidfilename);
167 return r != PTHREAD_CANCELED;
170 static void
171 do_cleanup (void)
173 FILE *f = fopen (pidfilename, "r+");
174 long long ll;
176 if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
178 struct flock fl =
180 .l_type = F_WRLCK,
181 .l_start = 0,
182 .l_whence = SEEK_SET,
183 .l_len = 1
185 if (fcntl (fileno (f), F_GETLK, &fl) == 0 && fl.l_type != F_UNLCK
186 && fl.l_pid == ll)
187 kill (fl.l_pid, SIGKILL);
189 fclose (f);
192 unlink (pidfilename);
195 #define OPT_COMMAND 10000
196 #define OPT_PIDFILE 10001
197 #define CMDLINE_OPTIONS \
198 { "command", required_argument, NULL, OPT_COMMAND }, \
199 { "pidfile", required_argument, NULL, OPT_PIDFILE },
200 #define CMDLINE_PROCESS \
201 case OPT_COMMAND: \
202 command = optarg; \
203 break; \
204 case OPT_PIDFILE: \
205 pidfile = optarg; \
206 break;
207 // #define CLEANUP_HANDLER do_cleanup ()
208 #define PREPARE(argc, argv) do_prepare (argc, argv)
209 #define TEST_FUNCTION do_test ()
210 #define TIMEOUT 5
211 #include "../test-skeleton.c"