register-atfork.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include "fork.h"
  18. int
  19. __register_atfork (prepare, parent, child, dso_handle)
  20. void (*prepare) (void);
  21. void (*parent) (void);
  22. void (*child) (void);
  23. void *dso_handle;
  24. {
  25. struct fork_handler *new_prepare = NULL;
  26. struct fork_handler *new_parent = NULL;
  27. struct fork_handler *new_child = NULL;
  28. if (prepare != NULL)
  29. {
  30. new_prepare = (struct fork_handler *) malloc (sizeof (*new_prepare));
  31. if (new_prepare == NULL)
  32. goto out1;
  33. new_prepare->handler = prepare;
  34. new_prepare->dso_handle = dso_handle;
  35. }
  36. if (parent != NULL)
  37. {
  38. new_parent = (struct fork_handler *) malloc (sizeof (*new_parent));
  39. if (new_parent == NULL)
  40. goto out2;
  41. new_parent->handler = parent;
  42. new_parent->dso_handle = dso_handle;
  43. }
  44. if (child != NULL)
  45. {
  46. new_child = (struct fork_handler *) malloc (sizeof (*new_child));
  47. if (new_child == NULL)
  48. {
  49. free (new_parent);
  50. out2:
  51. free (new_prepare);
  52. out1:
  53. return errno;
  54. }
  55. new_child->handler = child;
  56. new_child->dso_handle = dso_handle;
  57. }
  58. /* Get the lock to not conflict with running forks. */
  59. __libc_lock_lock (__fork_block.lock);
  60. /* Now that we have all the handlers allocate enqueue them. */
  61. if (new_prepare != NULL)
  62. list_add_tail (&new_prepare->list, &__fork_block.prepare_list);
  63. if (new_parent != NULL)
  64. list_add_tail (&new_parent->list, &__fork_block.parent_list);
  65. if (new_child != NULL)
  66. list_add_tail (&new_child->list, &__fork_block.child_list);
  67. /* Release the lock. */
  68. __libc_lock_unlock (__fork_block.lock);
  69. return 0;
  70. }