OSDN Git Service

merge upstream changes
[uclinux-h8/uclibc-ng.git] / test / nptl / tst-cancel7.c
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.
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    <http://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
28 const char *command;
29 const char *pidfile;
30 char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
31
32 static void *
33 tf (void *arg)
34 {
35   const char *args = " --direct --pidfile ";
36   char *cmd = alloca (strlen (command) + strlen (args)
37                       + strlen (pidfilename) + 1);
38
39   strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
40   system (cmd);
41   /* This call should never return.  */
42   return NULL;
43 }
44
45
46 static void
47 sl (void)
48 {
49   FILE *f = fopen (pidfile, "w");
50   if (f == NULL)
51     exit (1);
52
53   fprintf (f, "%lld\n", (long long) getpid ());
54   fflush (f);
55
56   struct flock fl =
57     {
58       .l_type = F_WRLCK,
59       .l_start = 0,
60       .l_whence = SEEK_SET,
61       .l_len = 1
62     };
63   if (fcntl (fileno (f), F_SETLK, &fl) != 0)
64     exit (1);
65
66   sigset_t ss;
67   sigfillset (&ss);
68   sigsuspend (&ss);
69   exit (0);
70 }
71
72
73 static void
74 do_prepare (int argc, char *argv[])
75 {
76   if (command == NULL)
77     command = argv[0];
78
79   if (pidfile)
80     sl ();
81
82   int fd = mkstemp (pidfilename);
83   if (fd == -1)
84     {
85       puts ("mkstemp failed");
86       exit (1);
87     }
88
89   write (fd, " ", 1);
90   close (fd);
91 }
92
93
94 static int
95 do_test (void)
96 {
97   pthread_t th;
98   if (pthread_create (&th, NULL, tf, NULL) != 0)
99     {
100       puts ("pthread_create failed");
101       return 1;
102     }
103
104   do
105     sleep (1);
106   while (access (pidfilename, R_OK) != 0);
107
108   if (pthread_cancel (th) != 0)
109     {
110       puts ("pthread_cancel failed");
111       return 1;
112     }
113
114   void *r;
115   if (pthread_join (th, &r) != 0)
116     {
117       puts ("pthread_join failed");
118       return 1;
119     }
120
121   sleep (1);
122
123   FILE *f = fopen (pidfilename, "r+");
124   if (f == NULL)
125     {
126       puts ("no pidfile");
127       return 1;
128     }
129
130   long long ll;
131   if (fscanf (f, "%lld\n", &ll) != 1)
132     {
133       puts ("could not read pid");
134       unlink (pidfilename);
135       return 1;
136     }
137
138   struct flock fl =
139     {
140       .l_type = F_WRLCK,
141       .l_start = 0,
142       .l_whence = SEEK_SET,
143       .l_len = 1
144     };
145   if (fcntl (fileno (f), F_GETLK, &fl) != 0)
146     {
147       puts ("F_GETLK failed");
148       unlink (pidfilename);
149       return 1;
150     }
151
152   if (fl.l_type != F_UNLCK)
153     {
154       printf ("child %lld still running\n", (long long) fl.l_pid);
155       if (fl.l_pid == ll)
156         kill (fl.l_pid, SIGKILL);
157
158       unlink (pidfilename);
159       return 1;
160     }
161
162   fclose (f);
163
164   unlink (pidfilename);
165
166   return r != PTHREAD_CANCELED;
167 }
168
169 #if 0 /* unused */
170 static void
171 do_cleanup (void)
172 {
173   FILE *f = fopen (pidfilename, "r+");
174   long long ll;
175
176   if (f != NULL && fscanf (f, "%lld\n", &ll) == 1)
177     {
178       struct flock fl =
179         {
180           .l_type = F_WRLCK,
181           .l_start = 0,
182           .l_whence = SEEK_SET,
183           .l_len = 1
184         };
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);
188
189       fclose (f);
190     }
191
192   unlink (pidfilename);
193 }
194 #endif
195
196 #define OPT_COMMAND     10000
197 #define OPT_PIDFILE     10001
198 #define CMDLINE_OPTIONS \
199         "c:p:"
200 /*
201   { "command", required_argument, NULL, OPT_COMMAND },  \
202   { "pidfile", required_argument, NULL, OPT_PIDFILE },
203 */
204 #define CMDLINE_PROCESS \
205   case 'c':     \
206     command = optarg;   \
207     break;              \
208   case 'p':     \
209     pidfile = optarg;   \
210     break;
211 // #define CLEANUP_HANDLER do_cleanup ()
212 #define PREPARE(argc, argv) do_prepare (argc, argv)
213 #define TEST_FUNCTION do_test ()
214 #define TIMEOUT 5
215 #include "../test-skeleton.c"