1 /* Copyright 2007, Red Hat Inc. */
8 #include <linux/unistd.h>
19 _syscall2(int, tkill, int, tid, int, sig)
20 int tkill(int tid, int sig);
23 static int attach_checked (pid_t pid, int redelivered_expect)
27 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING) | (1 << STATE_STOPPED));
28 stopped = attach (pid);
29 if (attach_redelivered != redelivered_expect)
31 fprintf (stderr, "Expecting redelivery of %d but found %d\n",
32 redelivered_expect, attach_redelivered);
35 /* FIXME: Why also STATE_STOPPED? */
36 STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
40 static void detach_checked (pid_t pid, int stopped)
42 /* FIXME: Why STATE_STOPPED? */
43 STATE (pid, (stopped ? 1 << STATE_STOPPED : 1 << STATE_PTRACED));
44 detach (pid, stopped);
45 STATE (pid, (stopped ? 1 << STATE_STOPPED : (1 << STATE_SLEEPING) | (1 << STATE_RUNNING)));
50 struct registry *next;
53 struct registry *registry_list;
55 static void registry_add (pid_t pid)
59 new = malloc (sizeof (*new));
62 new->next = registry_list;
66 static void registry_remove (pid_t pid)
68 struct registry **iter_pointer;
70 for (iter_pointer = ®istry_list; *iter_pointer != NULL;
71 iter_pointer = &(*iter_pointer)->next)
73 struct registry *found = *iter_pointer;
74 if (found->pid != pid)
77 *iter_pointer = found->next;
84 static void registry_atexit (void)
86 struct registry *iter;
88 for (iter = registry_list; iter != NULL; iter = iter->next)
89 kill (iter->pid, SIGKILL);
92 static void registry_cleanup (void)
94 struct registry *iter;
98 while ((pid = wait (NULL)) != -1)
100 for (iter = registry_list; iter != NULL; iter = iter->next)
101 if (iter->pid == pid)
103 assert (iter != NULL);
105 assert (errno == ECHILD);
106 while (registry_list)
108 iter = registry_list;
109 registry_list = iter->next;
114 static void registry_handler (int signo)
116 signal (signo, SIG_DFL);
121 static void child_pause (void)
129 static void child_alrm_handler (int signo)
131 assert (signo == SIGALRM);
135 static void child_alrm (void)
137 void (*handler_orig) (int signo);
143 handler_orig = signal (SIGALRM, child_alrm_handler);
144 assert (handler_orig == SIG_DFL);
147 i = sigprocmask (SIG_BLOCK, NULL, &oldset);
149 printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
158 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
159 void *input, int fd_close)
175 i = close (fd_close);
187 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
191 static void murder (pid_t pid)
200 i = kill (pid, SIGKILL);
203 pid_got = waitpid (pid, &status, 0);
204 if (!(pid_got == -1 && errno == ECHILD))
206 assert (pid_got == pid);
207 assert ((WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
208 || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
209 STATE (pid, 1 << STATE_ENOENT);
212 STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
214 registry_remove (pid);
220 void *(*child) (void *data, void *input);
225 static void *child_spawner (void *param_voidpointer, void *input)
227 struct child_spawner *param = param_voidpointer;
229 ssize_t inferior_size;
232 inferior = spawn (param->child, param->data, input, param->fd);
234 inferior_size = write (param->fd, &inferior, sizeof (inferior));
235 assert (inferior_size == sizeof (inferior));
236 i = close (param->fd);
239 waitpid (inferior, NULL, 0);
240 _exit (EXIT_SUCCESS);
247 void *(*func) (void *data, void *input);
251 static void *spawn_with_waiter (void *data, void *input)
254 struct child_spawner param_local;
258 ssize_t inferior_size;
261 struct spawner *param = data;
263 assert (data != NULL);
268 param_local.child = param->func;
269 param_local.data = param->data;
270 param_local.fd = pipefds[1];
271 waiter = spawn (child_spawner, ¶m_local, input, pipefds[0]);
273 i = close (pipefds[1]);
275 inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
276 assert (inferior_size == sizeof (inferior));
277 buf_size = read (pipefds[0], &buf, sizeof (buf));
278 assert (buf_size == 0);
279 i = close (pipefds[0]);
282 registry_add (inferior);
284 return (void *) (unsigned long) inferior;
287 static void *spawn_without_waiter (void *data, void *input)
289 struct spawner *param = data;
291 return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
294 static void body_spawner (void *(*child) (void *data, void *input), void *data,
301 assert (input == NULL);
303 /* Plain attach/detach. */
304 inferior = (unsigned long) (*child) (data, child_pause);
305 stopped = attach_checked (inferior, 0);
306 assert (stopped == 0);
307 detach_checked (inferior, stopped);
310 /* Attach to a process stopped by standard kill(2). */
311 inferior = (unsigned long) (*child) (data, child_pause);
313 i = kill (inferior, SIGSTOP);
315 STATE (inferior, 1 << STATE_STOPPED);
316 stopped = attach_checked (inferior, 0);
317 assert (stopped == 1);
318 detach_checked (inferior, stopped);
321 /* Attach to a process stopped by Linux specific tkill(2). */
322 inferior = (unsigned long) (*child) (data, child_pause);
324 i = tkill (inferior, SIGSTOP);
326 STATE (inferior, 1 << STATE_STOPPED);
327 stopped = attach_checked (inferior, 0);
328 assert (stopped == 1);
329 detach_checked (inferior, stopped);
332 /* Attach to a stopped process with already pending SIGALRM. */
333 inferior = (unsigned long) (*child) (data, child_alrm);
334 STATE (inferior, 1 << STATE_SLEEPING);
336 i = tkill (inferior, SIGSTOP);
338 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
339 STATE (inferior, 1 << STATE_STOPPED);
341 i = tkill (inferior, SIGALRM);
343 STATE (inferior, 1 << STATE_STOPPED);
344 /* FIXME: SIGALRM did not get redelivered? */
346 stopped = attach_checked (inferior, SIGALRM);
348 stopped = attach_checked (inferior, 0);
350 assert (stopped == 1);
351 detach_checked (inferior, stopped);
352 STATE (inferior, 1 << STATE_STOPPED);
354 i = tkill (inferior, SIGCONT);
356 /* This is a race, we may not prove the successful SIGALRM delivery by it. */
357 STATE (inferior, 1 << STATE_RUNNING);
361 static void *pass (void *data, void *input)
363 struct spawner *param = data;
364 void (*input_func) (void) = input;
367 return (*param->func) (param->data, input);
369 assert (input_func != NULL);
375 static void *spawn_singlethreaded (void *data, void *input)
377 return pass (data, input);
380 static void *spawn_threaded_parent_start (void *arg)
388 static void *spawn_threaded_parent (void *data, void *input)
393 i = pthread_create (&thread, NULL, spawn_threaded_parent_start, NULL);
395 return pass (data, input);
398 struct spawn_threaded_child_start
404 static void *spawn_threaded_child_start (void *arg_voidpointer)
406 struct spawn_threaded_child_start *arg = arg_voidpointer;
408 return pass (arg->data, arg->input);
412 static void *spawn_threaded_child (void *data, void *input)
416 struct spawn_threaded_child_start arg_local;
418 arg_local.data = data;
419 arg_local.input = input;
420 i = pthread_create (&thread, NULL, spawn_threaded_child_start, &arg_local);
422 i = pthread_join (thread, NULL);
425 _exit (EXIT_SUCCESS);
428 static void body_maywaiter (void *(*child) (void *data, void *input),
429 void *data, void *input)
431 struct spawner param_local;
433 param_local.func = child;
434 param_local.data = data;
435 body_spawner (spawn_without_waiter, ¶m_local, NULL);
436 body_spawner (spawn_with_waiter, ¶m_local, NULL);
439 static volatile unsigned long loops = 0;
440 static volatile int loops_print = 0;
442 static void handler_sigusr1 (int signo)
444 assert (signo == SIGUSR1);
449 int main (int argc, char **argv)
456 else if (argc == 2 && strcmp (argv[1], "-l") == 0)
464 atexit (registry_atexit);
465 signal (SIGINT, registry_handler);
466 signal (SIGABRT, registry_handler);
468 signal (SIGUSR1, handler_sigusr1);
472 while (loops_print > 0)
474 printf ("%lu\n", loops);
477 body_maywaiter (spawn_singlethreaded, NULL, NULL);
478 body_maywaiter (spawn_threaded_parent, NULL, NULL);
479 body_maywaiter (spawn_threaded_child, NULL, NULL);