register-atfork.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include "fork.h"
  19. int
  20. __register_atfork (prepare, parent, child, dso_handle)
  21. void (*prepare) (void);
  22. void (*parent) (void);
  23. void (*child) (void);
  24. void *dso_handle;
  25. {
  26. struct fork_handler *new_prepare = NULL;
  27. struct fork_handler *new_parent = NULL;
  28. struct fork_handler *new_child = NULL;
  29. if (prepare != NULL)
  30. {
  31. new_prepare = (struct fork_handler *) malloc (sizeof (*new_prepare));
  32. if (new_prepare == NULL)
  33. goto out1;
  34. new_prepare->handler = prepare;
  35. new_prepare->dso_handle = dso_handle;
  36. }
  37. if (parent != NULL)
  38. {
  39. new_parent = (struct fork_handler *) malloc (sizeof (*new_parent));
  40. if (new_parent == NULL)
  41. goto out2;
  42. new_parent->handler = parent;
  43. new_parent->dso_handle = dso_handle;
  44. }
  45. if (child != NULL)
  46. {
  47. new_child = (struct fork_handler *) malloc (sizeof (*new_child));
  48. if (new_child == NULL)
  49. {
  50. free (new_parent);
  51. out2:
  52. free (new_prepare);
  53. out1:
  54. return errno;
  55. }
  56. new_child->handler = child;
  57. new_child->dso_handle = dso_handle;
  58. }
  59. /* Get the lock to not conflict with running forks. */
  60. __libc_lock_lock (__fork_block.lock);
  61. /* Now that we have all the handlers allocate enqueue them. */
  62. if (new_prepare != NULL)
  63. list_add_tail (&new_prepare->list, &__fork_block.prepare_list);
  64. if (new_parent != NULL)
  65. list_add_tail (&new_parent->list, &__fork_block.parent_list);
  66. if (new_child != NULL)
  67. list_add_tail (&new_child->list, &__fork_block.child_list);
  68. /* Release the lock. */
  69. __libc_lock_unlock (__fork_block.lock);
  70. return 0;
  71. }