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, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
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>
|
---|
28 |
|
---|
29 | const char *command;
|
---|
30 | const char *pidfile;
|
---|
31 | char pidfilename[] = "/tmp/tst-cancel7-XXXXXX";
|
---|
32 |
|
---|
33 | static void *
|
---|
34 | tf (void *arg)
|
---|
35 | {
|
---|
36 | const char *args = " --direct --pidfile ";
|
---|
37 | char *cmd = alloca (strlen (command) + strlen (args)
|
---|
38 | + strlen (pidfilename) + 1);
|
---|
39 |
|
---|
40 | strcpy (stpcpy (stpcpy (cmd, command), args), pidfilename);
|
---|
41 | system (cmd);
|
---|
42 | /* This call should never return. */
|
---|
43 | return NULL;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | static void
|
---|
48 | sl (void)
|
---|
49 | {
|
---|
50 | FILE *f = fopen (pidfile, "w");
|
---|
51 | if (f == NULL)
|
---|
52 | exit (1);
|
---|
53 |
|
---|
54 | fprintf (f, "%lld\n", (long long) getpid ());
|
---|
55 | fflush (f);
|
---|
56 |
|
---|
57 | struct flock fl =
|
---|
58 | {
|
---|
59 | .l_type = F_WRLCK,
|
---|
60 | .l_start = 0,
|
---|
61 | .l_whence = SEEK_SET,
|
---|
62 | .l_len = 1
|
---|
63 | };
|
---|
64 | if (fcntl (fileno (f), F_SETLK, &fl) != 0)
|
---|
65 | exit (1);
|
---|
66 |
|
---|
67 | sigset_t ss;
|
---|
68 | sigfillset (&ss);
|
---|
69 | sigsuspend (&ss);
|
---|
70 | exit (0);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static void
|
---|
75 | do_prepare (int argc, char *argv[])
|
---|
76 | {
|
---|
77 | if (command == NULL)
|
---|
78 | command = argv[0];
|
---|
79 |
|
---|
80 | if (pidfile)
|
---|
81 | sl ();
|
---|
82 |
|
---|
83 | int fd = mkstemp (pidfilename);
|
---|
84 | if (fd == -1)
|
---|
85 | {
|
---|
86 | puts ("mkstemp failed");
|
---|
87 | exit (1);
|
---|
88 | }
|
---|
89 |
|
---|
90 | write (fd, " ", 1);
|
---|
91 | close (fd);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | static int
|
---|
96 | do_test (void)
|
---|
97 | {
|
---|
98 | pthread_t th;
|
---|
99 | if (pthread_create (&th, NULL, tf, NULL) != 0)
|
---|
100 | {
|
---|
101 | puts ("pthread_create failed");
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | do
|
---|
106 | sleep (1);
|
---|
107 | while (access (pidfilename, R_OK) != 0);
|
---|
108 |
|
---|
109 | if (pthread_cancel (th) != 0)
|
---|
110 | {
|
---|
111 | puts ("pthread_cancel failed");
|
---|
112 | return 1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void *r;
|
---|
116 | if (pthread_join (th, &r) != 0)
|
---|
117 | {
|
---|
118 | puts ("pthread_join failed");
|
---|
119 | return 1;
|
---|
120 | }
|
---|
121 |
|
---|
122 | sleep (1);
|
---|
123 |
|
---|
124 | FILE *f = fopen (pidfilename, "r+");
|
---|
125 | if (f == NULL)
|
---|
126 | {
|
---|
127 | puts ("no pidfile");
|
---|
128 | return 1;
|
---|
129 | }
|
---|
130 |
|
---|
131 | long long ll;
|
---|
132 | if (fscanf (f, "%lld\n", &ll) != 1)
|
---|
133 | {
|
---|
134 | puts ("could not read pid");
|
---|
135 | unlink (pidfilename);
|
---|
136 | return 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | struct flock fl =
|
---|
140 | {
|
---|
141 | .l_type = F_WRLCK,
|
---|
142 | .l_start = 0,
|
---|
143 | .l_whence = SEEK_SET,
|
---|
144 | .l_len = 1
|
---|
145 | };
|
---|
146 | if (fcntl (fileno (f), F_GETLK, &fl) != 0)
|
---|
147 | {
|
---|
148 | puts ("F_GETLK failed");
|
---|
149 | unlink (pidfilename);
|
---|
150 | return 1;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (fl.l_type != F_UNLCK)
|
---|
154 | {
|
---|
155 | printf ("child %lld still running\n", (long long) fl.l_pid);
|
---|
156 | if (fl.l_pid == ll)
|
---|
157 | kill (fl.l_pid, SIGKILL);
|
---|
158 |
|
---|
159 | unlink (pidfilename);
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | fclose (f);
|
---|
164 |
|
---|
165 | unlink (pidfilename);
|
---|
166 |
|
---|
167 | return r != PTHREAD_CANCELED;
|
---|
168 | }
|
---|
169 |
|
---|
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 |
|
---|
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"
|
---|