1 /* Copyright 2007, Red Hat Inc. */
12 #include <sys/syscall.h>
20 #define tkill(tid, sig) syscall (SYS_tkill, (tid), (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)
90 tkill (iter->pid, SIGCONT);
91 tkill (iter->pid, SIGKILL);
92 kill (iter->pid, SIGKILL);
96 static void registry_cleanup (void)
98 struct registry *iter;
102 while ((pid = wait (NULL)) != -1)
104 for (iter = registry_list; iter != NULL; iter = iter->next)
105 if (iter->pid == pid)
107 assert (iter != NULL);
109 assert (errno == ECHILD);
110 while (registry_list)
112 iter = registry_list;
113 registry_list = iter->next;
118 static void registry_handler (int signo)
120 signal (signo, SIG_DFL);
125 static void child_pause (void)
133 static void child_alrm_handler (int signo)
135 assert (signo == SIGALRM);
139 static void child_alrm (void)
141 void (*handler_orig) (int signo);
147 handler_orig = signal (SIGALRM, child_alrm_handler);
148 assert (handler_orig == SIG_DFL);
151 i = sigprocmask (SIG_BLOCK, NULL, &oldset);
153 printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
162 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
163 void *input, int fd_close)
179 i = close (fd_close);
191 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
195 static void murder (pid_t pid)
204 i = kill (pid, SIGKILL);
207 pid_got = waitpid (pid, &status, 0);
208 if (!(pid_got == -1 && errno == ECHILD))
210 assert (pid_got == pid);
211 assert ((WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
212 || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
213 STATE (pid, 1 << STATE_ENOENT);
216 STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
218 registry_remove (pid);
224 void *(*child) (void *data, void *input);
229 static void *child_spawner (void *param_voidpointer, void *input)
231 struct child_spawner *param = param_voidpointer;
233 ssize_t inferior_size;
236 inferior = spawn (param->child, param->data, input, param->fd);
238 inferior_size = write (param->fd, &inferior, sizeof (inferior));
239 assert (inferior_size == sizeof (inferior));
240 i = close (param->fd);
243 waitpid (inferior, NULL, 0);
244 _exit (EXIT_SUCCESS);
251 void *(*func) (void *data, void *input);
255 static void *spawn_with_waiter (void *data, void *input)
258 struct child_spawner param_local;
262 ssize_t inferior_size;
265 struct spawner *param = data;
267 assert (data != NULL);
272 param_local.child = param->func;
273 param_local.data = param->data;
274 param_local.fd = pipefds[1];
275 waiter = spawn (child_spawner, ¶m_local, input, pipefds[0]);
277 i = close (pipefds[1]);
279 inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
280 assert (inferior_size == sizeof (inferior));
281 buf_size = read (pipefds[0], &buf, sizeof (buf));
282 assert (buf_size == 0);
283 i = close (pipefds[0]);
286 registry_add (inferior);
288 return (void *) (unsigned long) inferior;
291 static void *spawn_without_waiter (void *data, void *input)
293 struct spawner *param = data;
295 return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
298 static void body_spawner (void *(*child) (void *data, void *input), void *data,
305 assert (input == NULL);
307 /* Plain attach/detach. */
308 inferior = (unsigned long) (*child) (data, child_pause);
309 stopped = attach_checked (inferior, 0);
310 assert (stopped == 0);
311 detach_checked (inferior, stopped);
314 /* Attach to a process stopped by standard kill(2). */
315 inferior = (unsigned long) (*child) (data, child_pause);
317 i = kill (inferior, SIGSTOP);
319 STATE (inferior, 1 << STATE_STOPPED);
320 stopped = attach_checked (inferior, 0);
321 assert (stopped == 1);
322 detach_checked (inferior, stopped);
325 /* Attach to a process stopped by Linux specific tkill(2). */
326 inferior = (unsigned long) (*child) (data, child_pause);
328 i = tkill (inferior, SIGSTOP);
330 STATE (inferior, 1 << STATE_STOPPED);
331 stopped = attach_checked (inferior, 0);
332 assert (stopped == 1);
333 detach_checked (inferior, stopped);
336 /* Attach to a stopped process with already pending SIGALRM. */
337 inferior = (unsigned long) (*child) (data, child_alrm);
338 STATE (inferior, 1 << STATE_SLEEPING);
340 i = tkill (inferior, SIGSTOP);
342 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
343 STATE (inferior, 1 << STATE_STOPPED);
345 i = tkill (inferior, SIGALRM);
347 STATE (inferior, 1 << STATE_STOPPED);
348 /* FIXME: SIGALRM did not get redelivered? */
350 stopped = attach_checked (inferior, SIGALRM);
352 stopped = attach_checked (inferior, 0);
354 assert (stopped == 1);
355 detach_checked (inferior, stopped);
356 STATE (inferior, 1 << STATE_STOPPED);
358 i = tkill (inferior, SIGCONT);
360 /* This is a race, we may not prove the successful SIGALRM delivery by it. */
361 STATE (inferior, 1 << STATE_RUNNING);
365 static void *pass (void *data, void *input)
367 struct spawner *param = data;
368 void (*input_func) (void) = input;
371 return (*param->func) (param->data, input);
373 assert (input_func != NULL);
379 static void *spawn_singlethreaded (void *data, void *input)
381 return pass (data, input);
384 static void *spawn_threaded_parent_start (void *arg)
392 static void *spawn_threaded_parent (void *data, void *input)
397 i = pthread_create (&thread, NULL, spawn_threaded_parent_start, NULL);
399 return pass (data, input);
402 struct spawn_threaded_child_start
408 static void *spawn_threaded_child_start (void *arg_voidpointer)
410 struct spawn_threaded_child_start *arg = arg_voidpointer;
412 return pass (arg->data, arg->input);
416 static void *spawn_threaded_child (void *data, void *input)
420 struct spawn_threaded_child_start arg_local;
422 arg_local.data = data;
423 arg_local.input = input;
424 i = pthread_create (&thread, NULL, spawn_threaded_child_start, &arg_local);
426 i = pthread_join (thread, NULL);
429 _exit (EXIT_SUCCESS);
432 static void body_maywaiter (void *(*child) (void *data, void *input),
433 void *data, void *input)
435 struct spawner param_local;
437 param_local.func = child;
438 param_local.data = data;
439 body_spawner (spawn_without_waiter, ¶m_local, NULL);
440 body_spawner (spawn_with_waiter, ¶m_local, NULL);
443 static volatile unsigned long loops = 0;
444 static volatile int loops_print = 0;
446 static void handler_sigusr1 (int signo)
448 assert (signo == SIGUSR1);
453 int main (int argc, char **argv)
460 else if (argc == 2 && strcmp (argv[1], "-l") == 0)
468 atexit (registry_atexit);
469 signal (SIGINT, registry_handler);
470 signal (SIGABRT, registry_handler);
472 signal (SIGUSR1, handler_sigusr1);
476 while (loops_print > 0)
478 printf ("%lu\n", loops);
481 body_maywaiter (spawn_singlethreaded, NULL, NULL);
482 body_maywaiter (spawn_threaded_parent, NULL, NULL);
483 body_maywaiter (spawn_threaded_child, NULL, NULL);