1 /* Copyright 2007, Red Hat Inc. */
14 #include <sys/syscall.h>
23 #define TIMEOUT_SECS 4
25 #define LOOPS_MIN 1000
28 #define tkill(tid, sig) syscall (SYS_tkill, (tid), (sig))
31 static void detach_checked (struct attach_state_struct *attach_state)
33 pid_t pid = attach_state_pid_get (attach_state);
35 if (attach_state_stopped_get (attach_state) != 0)
37 STATE (pid, 1 << STATE_STOPPED);
38 detach (attach_state);
39 STATE (pid, 1 << STATE_STOPPED);
43 STATE (pid, (1 << STATE_PTRACED));
44 detach (attach_state);
45 STATE (pid, ((1 << STATE_SLEEPING) | (1 << STATE_RUNNING)));
49 static volatile int spawned_threads_count; /* Set for the specific spawner. */
51 static struct attach_state_struct *attach_checked (pid_t pid, int redelivered_expect)
53 struct attach_state_struct *attach_state;
54 time_t timeout = time (NULL) + TIMEOUT_SECS;
57 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING) | (1 << STATE_STOPPED));
60 attach_state = attach (pid);
61 if (redelivered_expect != attach_state_redelivered_get (attach_state))
63 fprintf (stderr, "Expecting redelivery of %d but found %d\n",
65 attach_state_redelivered_get (attach_state));
68 if (attach_state_threads_count_get (attach_state)
69 == spawned_threads_count)
71 /* During the inferior's initialization we may catch less threads. */
72 assert (attach_state_threads_count_get (attach_state)
73 < spawned_threads_count);
74 /* WARNING: Currently we never use REDELIVERED_EXPECT but we would have to
75 probably reset it back to 0 otherwise. */
76 assert (redelivered_expect == 0);
77 detach_checked (attach_state);
79 while (loops++ < LOOPS_MIN || time (NULL) < timeout);
80 assert (attach_state_threads_count_get (attach_state) == spawned_threads_count);
82 /* FIXME: Why also STATE_STOPPED? */
83 STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
89 struct registry *next;
92 struct registry *registry_list;
94 static void registry_add (pid_t pid)
98 new = malloc (sizeof (*new));
101 new->next = registry_list;
105 static void registry_remove (pid_t pid)
107 struct registry **iter_pointer;
109 for (iter_pointer = ®istry_list; *iter_pointer != NULL;
110 iter_pointer = &(*iter_pointer)->next)
112 struct registry *found = *iter_pointer;
113 if (found->pid != pid)
116 *iter_pointer = found->next;
123 static void registry_atexit (void)
125 struct registry *iter;
127 for (iter = registry_list; iter != NULL; iter = iter->next)
129 tkill (iter->pid, SIGCONT);
130 tkill (iter->pid, SIGKILL);
131 kill (iter->pid, SIGKILL);
135 static void registry_cleanup (void)
137 struct registry *iter;
141 while ((pid = wait (NULL)) != -1)
143 for (iter = registry_list; iter != NULL; iter = iter->next)
144 if (iter->pid == pid)
146 assert (iter != NULL);
148 assert (errno == ECHILD);
149 while (registry_list)
151 iter = registry_list;
152 registry_list = iter->next;
157 static void registry_handler (int signo)
159 signal (signo, SIG_DFL);
164 static __attribute__((__noreturn__)) void child_pause (void)
172 static void child_alrm_handler (int signo)
174 assert (signo == SIGALRM);
178 static __attribute__((__noreturn__)) void child_alrm (void)
180 void (*handler_orig) (int signo);
186 handler_orig = signal (SIGALRM, child_alrm_handler);
187 assert (handler_orig == SIG_DFL);
190 i = sigprocmask (SIG_BLOCK, NULL, &oldset);
192 printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
201 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
202 void *input, int fd_close)
218 i = close (fd_close);
230 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
234 static void murder (pid_t pid)
243 i = kill (pid, SIGKILL);
246 pid_got = waitpid (pid, &status, 0);
247 if (!(pid_got == -1 && errno == ECHILD))
249 assert (pid_got == pid);
250 assert ((WIFSIGNALED (status) && (WTERMSIG (status) == SIGKILL
251 || WTERMSIG (status) == SIGUSR2))
252 || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
253 STATE (pid, 1 << STATE_ENOENT);
256 STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
258 registry_remove (pid);
264 void *(*child) (void *data, void *input);
269 static __attribute__((__noreturn__)) void *child_spawner (void *param_voidpointer, void *input)
271 struct child_spawner *param = param_voidpointer;
273 ssize_t inferior_size;
276 inferior = spawn (param->child, param->data, input, param->fd);
278 inferior_size = write (param->fd, &inferior, sizeof (inferior));
279 assert (inferior_size == sizeof (inferior));
280 i = close (param->fd);
283 waitpid (inferior, NULL, 0);
284 _exit (EXIT_SUCCESS);
291 void *(*func) (void *data, void *input);
295 static void *spawn_with_waiter (void *data, void *input)
298 struct child_spawner param_local;
302 ssize_t inferior_size;
305 struct spawner *param = data;
307 assert (data != NULL);
312 param_local.child = param->func;
313 param_local.data = param->data;
314 param_local.fd = pipefds[1];
315 waiter = spawn (child_spawner, ¶m_local, input, pipefds[0]);
317 i = close (pipefds[1]);
319 inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
320 assert (inferior_size == sizeof (inferior));
321 buf_size = read (pipefds[0], &buf, sizeof (buf));
322 assert (buf_size == 0);
323 i = close (pipefds[0]);
326 registry_add (inferior);
328 return (void *) (unsigned long) inferior;
331 static void *spawn_without_waiter (void *data, void *input)
333 struct spawner *param = data;
335 return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
338 static void body_spawner (void *(*child) (void *data, void *input), void *data,
342 struct attach_state_struct *attach_state;
345 assert (input == NULL);
347 /* Plain attach/detach. */
348 inferior = (unsigned long) (*child) (data, child_pause);
349 attach_state = attach_checked (inferior, 0);
350 assert (attach_state_stopped_get (attach_state) == 0);
351 detach_checked (attach_state);
354 /* Attach to a process stopped by standard kill(2). */
355 inferior = (unsigned long) (*child) (data, child_pause);
357 i = kill (inferior, SIGSTOP);
359 STATE (inferior, 1 << STATE_STOPPED);
360 attach_state = attach_checked (inferior, 0);
361 assert (attach_state_stopped_get (attach_state) == 1);
362 detach_checked (attach_state);
365 /* Attach to a process stopped by Linux specific tkill(2). */
366 inferior = (unsigned long) (*child) (data, child_pause);
368 i = tkill (inferior, SIGSTOP);
370 STATE (inferior, 1 << STATE_STOPPED);
371 attach_state = attach_checked (inferior, 0);
372 assert (attach_state_stopped_get (attach_state) == 1);
373 detach_checked (attach_state);
376 /* Attach to a stopped process with already pending SIGALRM. */
377 inferior = (unsigned long) (*child) (data, child_alrm);
378 STATE (inferior, 1 << STATE_SLEEPING);
380 i = tkill (inferior, SIGSTOP);
382 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
383 STATE (inferior, 1 << STATE_STOPPED);
385 i = tkill (inferior, SIGALRM);
387 STATE (inferior, 1 << STATE_STOPPED);
388 /* FIXME: SIGALRM did not get redelivered? */
390 attach_state = attach_checked (inferior, SIGALRM);
392 attach_state = attach_checked (inferior, 0);
394 assert (attach_state_stopped_get (attach_state) == 1);
395 detach_checked (attach_state);
396 STATE (inferior, 1 << STATE_STOPPED);
398 i = tkill (inferior, SIGCONT);
400 /* This is a race, we may not prove the successful SIGALRM delivery by it.
401 Rather recheck it below. */
402 STATE (inferior, 1 << STATE_RUNNING);
404 i = tkill (inferior, SIGSTOP);
406 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
407 STATE (inferior, 1 << STATE_STOPPED);
409 i = tkill (inferior, SIGUSR2);
411 STATE (inferior, 1 << STATE_STOPPED);
413 i = tkill (inferior, SIGCONT);
415 /* Not just STATE_ZOMBIE as we can get spawn with waiter. FIXME. */
416 STATE (inferior, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE) | (1 << STATE_DEAD));
417 /* We would fail on: murder (inferior); */
420 static void *pass (void *data, void *input)
422 struct spawner *param = data;
423 void (*input_func) (void) = input;
426 return (*param->func) (param->data, input);
428 assert (input_func != NULL);
434 static void *spawn_singlethreaded (void *data, void *input)
436 return pass (data, input);
439 static __attribute__((__noreturn__)) void *spawn_threaded_parent_start (void *arg)
447 static void *libpthread_handle;
448 static int (*libpthread_create_pointer) (pthread_t *thread,
449 const pthread_attr_t *attr,
450 void *(*start_routine) (void *),
452 static int (*libpthread_join_pointer) (pthread_t thread, void **value_ptr);
454 static void libpthread_open (void)
456 assert (libpthread_handle == NULL);
458 libpthread_handle = dlopen ("libpthread.so.0", RTLD_LAZY);
459 assert (libpthread_handle != NULL);
461 libpthread_create_pointer = dlsym (libpthread_handle, "pthread_create");
462 assert (libpthread_create_pointer != NULL);
463 libpthread_join_pointer = dlsym (libpthread_handle, "pthread_join");
464 assert (libpthread_join_pointer != NULL);
468 static void libpthread_close (void)
472 assert (libpthread_handle != NULL);
474 i = dlclose (libpthread_handle);
476 libpthread_handle = NULL;
479 static void *spawn_threaded_parent (void *data, void *input)
484 assert (libpthread_create_pointer != NULL);
485 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_parent_start,
488 return pass (data, input);
491 struct spawn_threaded_child_start
497 static void *spawn_threaded_child_start (void *arg_voidpointer)
499 struct spawn_threaded_child_start *arg = arg_voidpointer;
501 return pass (arg->data, arg->input);
504 static __attribute__((__noreturn__)) void *spawn_threaded_child (void *data, void *input)
508 struct spawn_threaded_child_start arg_local;
510 arg_local.data = data;
511 arg_local.input = input;
512 assert (libpthread_create_pointer != NULL);
513 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_child_start,
516 assert (libpthread_join_pointer != NULL);
517 i = (*libpthread_join_pointer) (thread, NULL);
520 _exit (EXIT_SUCCESS);
525 static void body_maywaiter (void *(*child) (void *data, void *input),
526 void *data, void *input)
528 struct spawner param_local;
530 param_local.func = child;
531 param_local.data = data;
532 body_spawner (spawn_without_waiter, ¶m_local, NULL);
533 body_spawner (spawn_with_waiter, ¶m_local, NULL);
536 static volatile unsigned long loops = 0;
537 static volatile int loops_print = 0;
539 static void handler_sigusr1 (int signo)
541 assert (signo == SIGUSR1);
546 static __attribute__((__noreturn__)) void *tests_threaded (void *data, void *input)
550 spawned_threads_count = 2;
551 body_maywaiter (spawn_threaded_parent, NULL, NULL);
553 spawned_threads_count = 2;
554 body_maywaiter (spawn_threaded_child, NULL, NULL);
559 _exit (EXIT_SUCCESS);
564 int main (int argc, char **argv)
569 #ifdef MEASURE_STATE_PERFORMANCE
570 STATE (1, 1 << STATE_ZOMBIE);
572 #endif /* MEASURE_STATE_PERFORMANCE */
576 else if (argc == 2 && strcmp (argv[1], "-l") == 0)
584 atexit (registry_atexit);
585 signal (SIGINT, registry_handler);
586 signal (SIGABRT, registry_handler);
588 signal (SIGUSR1, handler_sigusr1);
592 pid_t pid_threaded, pid_threaded_got;
595 while (loops_print > 0)
597 printf ("%lu\n", loops);
601 spawned_threads_count = 0;
602 body_maywaiter (spawn_singlethreaded, NULL, NULL);
604 /* We cannot rely on LIBPTHREAD_CLOSE as libpthread would be still loaded
605 with ATTACH_STATE_REDELIVERED_GET () reporting 1 for
606 SPAWN_SINGLETHREADED. */
607 pid_threaded = spawn (tests_threaded, NULL, NULL, -1);
608 pid_threaded_got = waitpid (pid_threaded, &status, 0);
609 assert (pid_threaded_got == pid_threaded);
610 assert (!WIFSIGNALED (status)); /* Improve readability in the failed case. */
611 assert (!WIFSTOPPED (status)); /* Improve readability in the failed case. */
612 assert (WIFEXITED (status));
613 assert (WEXITSTATUS (status) == 0);
614 STATE (pid_threaded, 1 << STATE_ENOENT);
615 registry_remove (pid_threaded);
620 /* Run at least twice to test libpthread_close (). */
621 while (loop != 0 || loops == 1);