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
31 char pidfilename
[] = "/tmp/tst-cancel7-XXXXXX";
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
);
42 /* This call should never return. */
50 FILE *f
= fopen (pidfile
, "w");
54 fprintf (f
, "%lld\n", (long long) getpid ());
64 if (fcntl (fileno (f
), F_SETLK
, &fl
) != 0)
75 do_prepare (int argc
, char *argv
[])
83 int fd
= mkstemp (pidfilename
);
86 puts ("mkstemp failed");
99 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
101 puts ("pthread_create failed");
107 if (pthread_cancel (th
) != 0)
109 puts ("pthread_cancel failed");
114 if (pthread_join (th
, &r
) != 0)
116 puts ("pthread_join failed");
122 FILE *f
= fopen (pidfilename
, "r+");
130 if (fscanf (f
, "%lld\n", &ll
) != 1)
132 puts ("could not read pid");
133 unlink (pidfilename
);
141 .l_whence
= SEEK_SET
,
144 if (fcntl (fileno (f
), F_GETLK
, &fl
) != 0)
146 puts ("F_GETLK failed");
147 unlink (pidfilename
);
151 if (fl
.l_type
!= F_UNLCK
)
153 printf ("child %lld still running\n", (long long) fl
.l_pid
);
155 kill (fl
.l_pid
, SIGKILL
);
157 unlink (pidfilename
);
163 unlink (pidfilename
);
165 return r
!= PTHREAD_CANCELED
;
171 FILE *f
= fopen (pidfilename
, "r+");
174 if (f
!= NULL
&& fscanf (f
, "%lld\n", &ll
) == 1)
180 .l_whence
= SEEK_SET
,
183 if (fcntl (fileno (f
), F_GETLK
, &fl
) == 0 && fl
.l_type
!= F_UNLCK
185 kill (fl
.l_pid
, SIGKILL
);
190 unlink (pidfilename
);
193 #define OPT_COMMAND 10000
194 #define OPT_PIDFILE 10001
195 #define CMDLINE_OPTIONS \
196 { "command", required_argument, NULL, OPT_COMMAND }, \
197 { "pidfile", required_argument, NULL, OPT_PIDFILE },
198 #define CMDLINE_PROCESS \
205 // #define CLEANUP_HANDLER do_cleanup ()
206 #define PREPARE(argc, argv) do_prepare (argc, argv)
207 #define TEST_FUNCTION do_test ()
209 #include "../test-skeleton.c"