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 fprintf (stderr, "FIXME: Untested threads initialization - REMOVE.\n");
72 /* During the inferior's initialization we may catch less threads. */
73 assert (attach_state_threads_count_get (attach_state)
74 < spawned_threads_count);
75 /* WARNING: Currently we never use REDELIVERED_EXPECT but we would have to
76 probably reset it back to 0 otherwise. */
77 assert (redelivered_expect == 0);
78 detach_checked (attach_state);
80 while (loops++ < LOOPS_MIN || time (NULL) < timeout);
81 assert (attach_state_threads_count_get (attach_state) == spawned_threads_count);
83 /* FIXME: Why also STATE_STOPPED? */
84 STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
90 struct registry *next;
93 struct registry *registry_list;
95 static void registry_add (pid_t pid)
99 new = malloc (sizeof (*new));
100 assert (new != NULL);
102 new->next = registry_list;
106 static void registry_remove (pid_t pid)
108 struct registry **iter_pointer;
110 for (iter_pointer = ®istry_list; *iter_pointer != NULL;
111 iter_pointer = &(*iter_pointer)->next)
113 struct registry *found = *iter_pointer;
114 if (found->pid != pid)
117 *iter_pointer = found->next;
124 static void registry_atexit (void)
126 struct registry *iter;
128 for (iter = registry_list; iter != NULL; iter = iter->next)
130 tkill (iter->pid, SIGCONT);
131 tkill (iter->pid, SIGKILL);
132 kill (iter->pid, SIGKILL);
136 static void registry_cleanup (void)
138 struct registry *iter;
142 while ((pid = wait (NULL)) != -1)
144 for (iter = registry_list; iter != NULL; iter = iter->next)
145 if (iter->pid == pid)
147 assert (iter != NULL);
149 assert (errno == ECHILD);
150 while (registry_list)
152 iter = registry_list;
153 registry_list = iter->next;
158 static void registry_handler (int signo)
160 signal (signo, SIG_DFL);
165 static __attribute__((__noreturn__)) void child_pause (void)
173 static void child_alrm_handler (int signo)
175 assert (signo == SIGALRM);
179 static __attribute__((__noreturn__)) void child_alrm (void)
181 void (*handler_orig) (int signo);
187 handler_orig = signal (SIGALRM, child_alrm_handler);
188 assert (handler_orig == SIG_DFL);
191 i = sigprocmask (SIG_BLOCK, NULL, &oldset);
193 printf ("sigprocmask () -> sigismember (SIGALRM) == %d\n", sigismember (&oldset, SIGALRM));
202 static pid_t spawn (void *(*child) (void *data, void *input), void *data,
203 void *input, int fd_close)
219 i = close (fd_close);
231 STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING));
235 static void murder (pid_t pid)
244 i = kill (pid, SIGKILL);
247 pid_got = waitpid (pid, &status, 0);
248 if (!(pid_got == -1 && errno == ECHILD))
250 assert (pid_got == pid);
251 assert ((WIFSIGNALED (status) && (WTERMSIG (status) == SIGKILL
252 || WTERMSIG (status) == SIGUSR2))
253 || (WIFEXITED (status) && WEXITSTATUS (status) == 0));
254 STATE (pid, 1 << STATE_ENOENT);
257 STATE (pid, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE));
259 registry_remove (pid);
265 void *(*child) (void *data, void *input);
270 static __attribute__((__noreturn__)) void *child_spawner (void *param_voidpointer, void *input)
272 struct child_spawner *param = param_voidpointer;
274 ssize_t inferior_size;
277 inferior = spawn (param->child, param->data, input, param->fd);
279 inferior_size = write (param->fd, &inferior, sizeof (inferior));
280 assert (inferior_size == sizeof (inferior));
281 i = close (param->fd);
284 waitpid (inferior, NULL, 0);
285 _exit (EXIT_SUCCESS);
292 void *(*func) (void *data, void *input);
296 static void *spawn_with_waiter (void *data, void *input)
299 struct child_spawner param_local;
303 ssize_t inferior_size;
306 struct spawner *param = data;
308 assert (data != NULL);
313 param_local.child = param->func;
314 param_local.data = param->data;
315 param_local.fd = pipefds[1];
316 waiter = spawn (child_spawner, ¶m_local, input, pipefds[0]);
318 i = close (pipefds[1]);
320 inferior_size = read (pipefds[0], &inferior, sizeof (inferior));
321 assert (inferior_size == sizeof (inferior));
322 buf_size = read (pipefds[0], &buf, sizeof (buf));
323 assert (buf_size == 0);
324 i = close (pipefds[0]);
327 registry_add (inferior);
329 return (void *) (unsigned long) inferior;
332 static void *spawn_without_waiter (void *data, void *input)
334 struct spawner *param = data;
336 return (void *) (unsigned long) spawn (param->func, param->data, input, -1);
339 static void body_spawner (void *(*child) (void *data, void *input), void *data,
343 struct attach_state_struct *attach_state;
346 assert (input == NULL);
348 /* Plain attach/detach. */
349 inferior = (unsigned long) (*child) (data, child_pause);
350 attach_state = attach_checked (inferior, 0);
351 assert (attach_state_stopped_get (attach_state) == 0);
352 detach_checked (attach_state);
355 /* Attach to a process stopped by standard kill(2). */
356 inferior = (unsigned long) (*child) (data, child_pause);
358 i = kill (inferior, SIGSTOP);
360 STATE (inferior, 1 << STATE_STOPPED);
361 attach_state = attach_checked (inferior, 0);
362 assert (attach_state_stopped_get (attach_state) == 1);
363 detach_checked (attach_state);
366 /* Attach to a process stopped by Linux specific tkill(2). */
367 inferior = (unsigned long) (*child) (data, child_pause);
369 i = tkill (inferior, SIGSTOP);
371 STATE (inferior, 1 << STATE_STOPPED);
372 attach_state = attach_checked (inferior, 0);
373 assert (attach_state_stopped_get (attach_state) == 1);
374 detach_checked (attach_state);
377 /* Attach to a stopped process with already pending SIGALRM. */
378 inferior = (unsigned long) (*child) (data, child_alrm);
379 STATE (inferior, 1 << STATE_SLEEPING);
381 i = tkill (inferior, SIGSTOP);
383 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
384 STATE (inferior, 1 << STATE_STOPPED);
386 i = tkill (inferior, SIGALRM);
388 STATE (inferior, 1 << STATE_STOPPED);
389 /* FIXME: SIGALRM did not get redelivered? */
391 attach_state = attach_checked (inferior, SIGALRM);
393 attach_state = attach_checked (inferior, 0);
395 assert (attach_state_stopped_get (attach_state) == 1);
396 detach_checked (attach_state);
397 STATE (inferior, 1 << STATE_STOPPED);
399 i = tkill (inferior, SIGCONT);
401 /* This is a race, we may not prove the successful SIGALRM delivery by it.
402 Rather recheck it below. */
403 STATE (inferior, 1 << STATE_RUNNING);
405 i = tkill (inferior, SIGSTOP);
407 /* Wait till it gets stopped otherwise we may get STATE_ENOENT below. */
408 STATE (inferior, 1 << STATE_STOPPED);
410 i = tkill (inferior, SIGUSR2);
412 STATE (inferior, 1 << STATE_STOPPED);
414 i = tkill (inferior, SIGCONT);
416 /* Not just STATE_ZOMBIE as we can get spawn with waiter. FIXME. */
417 STATE (inferior, (1 << STATE_ENOENT) | (1 << STATE_ZOMBIE) | (1 << STATE_DEAD));
418 /* We would fail on: murder (inferior); */
421 static void *pass (void *data, void *input)
423 struct spawner *param = data;
424 void (*input_func) (void) = input;
427 return (*param->func) (param->data, input);
429 assert (input_func != NULL);
435 static void *spawn_singlethreaded (void *data, void *input)
437 return pass (data, input);
440 static __attribute__((__noreturn__)) void *spawn_threaded_parent_start (void *arg)
448 static void *libpthread_handle;
449 static int (*libpthread_create_pointer) (pthread_t *thread,
450 const pthread_attr_t *attr,
451 void *(*start_routine) (void *),
453 static int (*libpthread_join_pointer) (pthread_t thread, void **value_ptr);
455 static void libpthread_open (void)
457 assert (libpthread_handle == NULL);
459 libpthread_handle = dlopen ("libpthread.so.0", RTLD_LAZY);
460 assert (libpthread_handle != NULL);
462 libpthread_create_pointer = dlsym (libpthread_handle, "pthread_create");
463 assert (libpthread_create_pointer != NULL);
464 libpthread_join_pointer = dlsym (libpthread_handle, "pthread_join");
465 assert (libpthread_join_pointer != NULL);
469 static void libpthread_close (void)
473 assert (libpthread_handle != NULL);
475 i = dlclose (libpthread_handle);
477 libpthread_handle = NULL;
480 static void *spawn_threaded_parent (void *data, void *input)
485 assert (libpthread_create_pointer != NULL);
486 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_parent_start,
489 return pass (data, input);
492 struct spawn_threaded_child_start
498 static void *spawn_threaded_child_start (void *arg_voidpointer)
500 struct spawn_threaded_child_start *arg = arg_voidpointer;
502 return pass (arg->data, arg->input);
505 static __attribute__((__noreturn__)) void *spawn_threaded_child (void *data, void *input)
509 struct spawn_threaded_child_start arg_local;
511 arg_local.data = data;
512 arg_local.input = input;
513 assert (libpthread_create_pointer != NULL);
514 i = (*libpthread_create_pointer) (&thread, NULL, spawn_threaded_child_start,
517 assert (libpthread_join_pointer != NULL);
518 i = (*libpthread_join_pointer) (thread, NULL);
521 _exit (EXIT_SUCCESS);
526 static void body_maywaiter (void *(*child) (void *data, void *input),
527 void *data, void *input)
529 struct spawner param_local;
531 param_local.func = child;
532 param_local.data = data;
533 body_spawner (spawn_without_waiter, ¶m_local, NULL);
534 body_spawner (spawn_with_waiter, ¶m_local, NULL);
537 static volatile unsigned long loops = 0;
538 static volatile int loops_print = 0;
540 static void handler_sigusr1 (int signo)
542 assert (signo == SIGUSR1);
547 static __attribute__((__noreturn__)) void *tests_threaded (void *data, void *input)
551 spawned_threads_count = 2;
552 body_maywaiter (spawn_threaded_parent, NULL, NULL);
554 spawned_threads_count = 2;
555 body_maywaiter (spawn_threaded_child, NULL, NULL);
560 _exit (EXIT_SUCCESS);
565 int main (int argc, char **argv)
570 #ifdef MEASURE_STATE_PERFORMANCE
571 STATE (1, 1 << STATE_ZOMBIE);
573 #endif /* MEASURE_STATE_PERFORMANCE */
577 else if (argc == 2 && strcmp (argv[1], "-l") == 0)
585 atexit (registry_atexit);
586 signal (SIGINT, registry_handler);
587 signal (SIGABRT, registry_handler);
589 signal (SIGUSR1, handler_sigusr1);
593 pid_t pid_threaded, pid_threaded_got;
596 while (loops_print > 0)
598 printf ("%lu\n", loops);
602 spawned_threads_count = 0;
603 body_maywaiter (spawn_singlethreaded, NULL, NULL);
605 /* We cannot rely on LIBPTHREAD_CLOSE as libpthread would be still loaded
606 with ATTACH_STATE_REDELIVERED_GET () reporting 1 for
607 SPAWN_SINGLETHREADED. */
608 pid_threaded = spawn (tests_threaded, NULL, NULL, -1);
609 pid_threaded_got = waitpid (pid_threaded, &status, 0);
610 assert (pid_threaded_got == pid_threaded);
611 assert (!WIFSIGNALED (status)); /* Improve readability in the failed case. */
612 assert (!WIFSTOPPED (status)); /* Improve readability in the failed case. */
613 assert (WIFEXITED (status));
614 assert (WEXITSTATUS (status) == 0);
615 STATE (pid_threaded, 1 << STATE_ENOENT);
616 registry_remove (pid_threaded);
621 /* Run at least twice to test libpthread_close (). */
622 while (loop != 0 || loops == 1);