1 /* Copyright (C) 2002-2021 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/>. */
29 #include <support/xthread.h>
33 char pidfilename
[] = "/tmp/tst-cancel7-XXXXXX";
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
);
44 /* This call should never return. */
52 FILE *f
= fopen (pidfile
, "w");
56 fprintf (f
, "%lld\n", (long long) getpid ());
66 if (fcntl (fileno (f
), F_SETLK
, &fl
) != 0)
77 do_prepare (int argc
, char *argv
[])
85 int fd
= mkstemp (pidfilename
);
88 puts ("mkstemp failed");
101 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
103 puts ("pthread_create failed");
109 while (access (pidfilename
, R_OK
) != 0);
111 xpthread_cancel (th
);
112 void *r
= xpthread_join (th
);
116 FILE *f
= fopen (pidfilename
, "r+");
124 if (fscanf (f
, "%lld\n", &ll
) != 1)
126 puts ("could not read pid");
127 unlink (pidfilename
);
135 .l_whence
= SEEK_SET
,
138 if (fcntl (fileno (f
), F_GETLK
, &fl
) != 0)
140 puts ("F_GETLK failed");
141 unlink (pidfilename
);
145 if (fl
.l_type
!= F_UNLCK
)
147 printf ("child %lld still running\n", (long long) fl
.l_pid
);
149 kill (fl
.l_pid
, SIGKILL
);
151 unlink (pidfilename
);
157 unlink (pidfilename
);
159 return r
!= PTHREAD_CANCELED
;
165 FILE *f
= fopen (pidfilename
, "r+");
168 if (f
!= NULL
&& fscanf (f
, "%lld\n", &ll
) == 1)
174 .l_whence
= SEEK_SET
,
177 if (fcntl (fileno (f
), F_GETLK
, &fl
) == 0 && fl
.l_type
!= F_UNLCK
179 kill (fl
.l_pid
, SIGKILL
);
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 },
193 cmdline_process (int c
)
205 #define CMDLINE_PROCESS cmdline_process
206 #define CLEANUP_HANDLER do_cleanup
207 #define PREPARE do_prepare
208 #include <support/test-driver.c>