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 /* During the inferior's initialization we may catch less threads. */
69 assert (attach_state_threads_count_get (attach_state)
70 <= spawned_threads_count);
71 if (attach_state_threads_count_get (attach_state)
72 == spawned_threads_count
73 /* Inferior may got stopped before it could initialize. */
74 || attach_state_stopped_get (attach_state) == 1)
76 /* FIXME: Why also STATE_STOPPED? */
77 STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
80 /* WARNING: Currently we never use REDELIVERED_EXPECT but we would have to
81 probably reset it back to 0 otherwise. */
82 assert (redelivered_expect == 0);
83 detach_checked (attach_state);
85 while (loops++ < LOOPS_MIN || time (NULL) < timeout);
91 struct registry *next;
94 struct registry *registry_list;
96 static void registry_add (pid_t pid)
100 new = malloc (sizeof (*new));
101 assert (new != NULL);
103 new->next = registry_list;
107 static void registry_remove (pid_t pid)
109 struct registry **iter_pointer;
111 for (iter_pointer = ®istry_list; *iter_pointer != NULL;
112 iter_pointer = &(*iter_pointer)->next)
114 struct registry *found = *iter_pointer;
115 if (found->pid != pid)
118 *iter_pointer = found->next;
125 static void registry_atexit (void)
127 struct registry *iter;
129 for (iter = registry_list; iter != NULL; iter = iter->next)
131 tkill (iter->pid, SIGCONT);
132 tkill (iter->pid, SIGKILL);
133 kill (iter->pid, SIGKILL);
137 static void registry_cleanup (void)
139 struct registry *iter;
143 while ((pid = wait (NULL)) != -1)
145 for (iter = registry_list; iter != NULL; iter = iter->next)
146 if (iter->pid == pid)
148 assert (iter != NULL);
150 assert (errno == ECHILD);
151 while (registry_list)
153 iter = registry_list;
154 registry_list = iter->next;
159 static void registry_handler (int signo)
161 signal (signo, SIG_DFL);
166 static __attribute__((__noreturn__)) void child_pause (void)
174 static void child_alrm_handler (int signo)
176 assert (signo == SIGALRM);
180 static __attribute__((__noreturn__)) void child_alrm (void)
182 void (*handler_orig) (int signo);
188 handler_orig = signal (SIGALRM, child_alrm_handler);
189 assert (handler_orig == SIG_DFL);
192 i = sigprocmask (SIG_BLOCK, NULL, &oldset);
194 printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
203 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
204 void *input, int fd_close)
220 i = close (fd_close);
232 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
236 static void murder (pid_t pid)
245 i = kill (pid, SIGKILL);
248 pid_got = waitpid (pid, &status, 0);
249 if (!(pid_got == -1 && errno == ECHILD))
251 assert (pid_got == pid);
252 assert ((WIFSIGNALED (status) && (WTERMSIG (status) == SIGKILL
253 || WTERMSIG (status) == SIGUSR2))
254 || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
255 STATE (pid, 1 << STATE_ENOENT);
258 STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
260 registry_remove (pid);
266 void *(*child) (void *data, void *input);
271 static __attribute__((__noreturn__)) void *child_spawner (void *param_voidpointer, void *input)
273 struct child_spawner *param = param_voidpointer;
275 ssize_t inferior_size;
278 inferior = spawn (param->child, param->data, input, param->fd);
280 inferior_size = write (param->fd, &inferior, sizeof (inferior));
281 assert (inferior_size == sizeof (inferior));
282 i = close (param->fd);
285 waitpid (inferior, NULL, 0);
286 _exit (EXIT_SUCCESS);
293 void *(*func) (void *data, void *input);
297 static void *spawn_with_waiter (void *data, void *input)
300 struct child_spawner param_local;
304 ssize_t inferior_size;
307 struct spawner *param = data;
309 assert (data != NULL);
314 param_local.child = param->func;
315 param_local.data = param->data;
316 param_local.fd = pipefds[1];
317 waiter = spawn (child_spawner, ¶m_local, input, pipefds[0]);
319 i = close (pipefds[1]);
321 inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
322 assert (inferior_size == sizeof (inferior));
323 buf_size = read (pipefds[0], &buf, sizeof (buf));
324 assert (buf_size == 0);
325 i = close (pipefds[0]);
328 registry_add (inferior);
330 return (void *) (unsigned long) inferior;
333 static void *spawn_without_waiter (void *data, void *input)
335 struct spawner *param = data;
337 return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
340 static void body_spawner (void *(*child) (void *data, void *input), void *data,
344 struct attach_state_struct *attach_state;
347 assert (input == NULL);
349 /* Plain attach/detach. */
350 inferior = (unsigned long) (*child) (data, child_pause);
351 attach_state = attach_checked (inferior, 0);
352 assert (attach_state_stopped_get (attach_state) == 0);
353 detach_checked (attach_state);
356 /* Attach to a process stopped by standard kill(2). */
357 inferior = (unsigned long) (*child) (data, child_pause);
359 i = kill (inferior, SIGSTOP);
361 STATE (inferior, 1 << STATE_STOPPED);
362 attach_state = attach_checked (inferior, 0);
363 assert (attach_state_stopped_get (attach_state) == 1);
364 detach_checked (attach_state);
367 /* Attach to a process stopped by Linux specific tkill(2). */
368 inferior = (unsigned long) (*child) (data, child_pause);
370 i = tkill (inferior, SIGSTOP);
372 STATE (inferior, 1 << STATE_STOPPED);
373 attach_state = attach_checked (inferior, 0);
374 assert (attach_state_stopped_get (attach_state) == 1);
375 detach_checked (attach_state);
378 /* Attach to a stopped process with already pending SIGALRM. */
379 inferior = (unsigned long) (*child) (data, child_alrm);
380 STATE (inferior, 1 << STATE_SLEEPING);
382 i = tkill (inferior, SIGSTOP);
384 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
385 STATE (inferior, 1 << STATE_STOPPED);
387 i = tkill (inferior, SIGALRM);
389 STATE (inferior, 1 << STATE_STOPPED);
390 /* FIXME: SIGALRM did not get redelivered? */
392 attach_state = attach_checked (inferior, SIGALRM);
394 attach_state = attach_checked (inferior, 0);
396 assert (attach_state_stopped_get (attach_state) == 1);
397 detach_checked (attach_state);
398 STATE (inferior, 1 << STATE_STOPPED);
400 i = tkill (inferior, SIGCONT);
402 /* This is a race, we may not prove the successful SIGALRM delivery by it.
403 Rather recheck it below. */
404 STATE (inferior, 1 << STATE_RUNNING);
406 i = tkill (inferior, SIGSTOP);
408 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
409 STATE (inferior, 1 << STATE_STOPPED);
411 i = tkill (inferior, SIGUSR2);
413 STATE (inferior, 1 << STATE_STOPPED);
415 i = tkill (inferior, SIGCONT);
417 /* Not just STATE_ZOMBIE as we can get spawn with waiter. FIXME. */
418 STATE (inferior, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE) | (1 << STATE_DEAD));
419 /* We would fail on: murder (inferior); */
422 static void *pass (void *data, void *input)
424 struct spawner *param = data;
425 void (*input_func) (void) = input;
428 return (*param->func) (param->data, input);
430 assert (input_func != NULL);
436 static void *spawn_singlethreaded (void *data, void *input)
438 return pass (data, input);
441 static __attribute__((__noreturn__)) void *spawn_threaded_parent_start (void *arg)
449 static void *libpthread_handle;
450 static int (*libpthread_create_pointer) (pthread_t *thread,
451 const pthread_attr_t *attr,
452 void *(*start_routine) (void *),
454 static int (*libpthread_join_pointer) (pthread_t thread, void **value_ptr);
456 static void libpthread_open (void)
458 assert (libpthread_handle == NULL);
460 libpthread_handle = dlopen ("libpthread.so.0", RTLD_LAZY);
461 assert (libpthread_handle != NULL);
463 libpthread_create_pointer = dlsym (libpthread_handle, "pthread_create");
464 assert (libpthread_create_pointer != NULL);
465 libpthread_join_pointer = dlsym (libpthread_handle, "pthread_join");
466 assert (libpthread_join_pointer != NULL);
470 static void libpthread_close (void)
474 assert (libpthread_handle != NULL);
476 i = dlclose (libpthread_handle);
478 libpthread_handle = NULL;
481 static void *spawn_threaded_parent (void *data, void *input)
486 assert (libpthread_create_pointer != NULL);
487 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_parent_start,
490 return pass (data, input);
493 struct spawn_threaded_child_start
499 static void *spawn_threaded_child_start (void *arg_voidpointer)
501 struct spawn_threaded_child_start *arg = arg_voidpointer;
503 return pass (arg->data, arg->input);
506 static __attribute__((__noreturn__)) void *spawn_threaded_child (void *data, void *input)
510 struct spawn_threaded_child_start arg_local;
512 arg_local.data = data;
513 arg_local.input = input;
514 assert (libpthread_create_pointer != NULL);
515 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_child_start,
518 assert (libpthread_join_pointer != NULL);
519 i = (*libpthread_join_pointer) (thread, NULL);
522 _exit (EXIT_SUCCESS);
527 static void body_maywaiter (void *(*child) (void *data, void *input),
528 void *data, void *input)
530 struct spawner param_local;
532 param_local.func = child;
533 param_local.data = data;
534 body_spawner (spawn_without_waiter, ¶m_local, NULL);
535 body_spawner (spawn_with_waiter, ¶m_local, NULL);
538 static volatile unsigned long loops = 0;
539 static volatile int loops_print = 0;
541 static void handler_sigusr1 (int signo)
543 assert (signo == SIGUSR1);
548 static __attribute__((__noreturn__)) void *tests_threaded (void *data, void *input)
552 spawned_threads_count = 2;
553 body_maywaiter (spawn_threaded_parent, NULL, NULL);
555 spawned_threads_count = 2;
556 body_maywaiter (spawn_threaded_child, NULL, NULL);
561 _exit (EXIT_SUCCESS);
566 int main (int argc, char **argv)
571 #ifdef MEASURE_STATE_PERFORMANCE
572 STATE (1, 1 << STATE_ZOMBIE);
574 #endif /* MEASURE_STATE_PERFORMANCE */
578 else if (argc == 2 && strcmp (argv[1], "-l") == 0)
586 atexit (registry_atexit);
587 signal (SIGINT, registry_handler);
588 signal (SIGABRT, registry_handler);
590 signal (SIGUSR1, handler_sigusr1);
594 pid_t pid_threaded, pid_threaded_got;
597 while (loops_print > 0)
599 printf ("%lu\n", loops);
603 spawned_threads_count = 0;
604 body_maywaiter (spawn_singlethreaded, NULL, NULL);
606 /* We cannot rely on LIBPTHREAD_CLOSE as libpthread would be still loaded
607 with ATTACH_STATE_REDELIVERED_GET () reporting 1 for
608 SPAWN_SINGLETHREADED. */
609 pid_threaded = spawn (tests_threaded, NULL, NULL, -1);
610 pid_threaded_got = waitpid (pid_threaded, &status, 0);
611 assert (pid_threaded_got == pid_threaded);
612 assert (!WIFSIGNALED (status)); /* Improve readability in the failed case. */
613 assert (!WIFSTOPPED (status)); /* Improve readability in the failed case. */
614 assert (WIFEXITED (status));
615 assert (WEXITSTATUS (status) == 0);
616 STATE (pid_threaded, 1 << STATE_ENOENT);
617 registry_remove (pid_threaded);
622 /* Run at least twice to test libpthread_close (). */
623 while (loop != 0 || loops == 1);