Back to index

glibc  2.9
Functions
register-atfork.c File Reference
#include <errno.h>
#include <stdlib.h>
#include "fork.h"

Go to the source code of this file.

Functions

int __register_atfork (void(*)(void) prepare, void(*)(void) parent, void(*)(void) child, void *dso_handle)

Function Documentation

int __register_atfork ( void (*) (void)  prepare,
void (*) (void)  parent,
void (*) (void)  child,
void *  dso_handle 
)

Definition at line 26 of file register-atfork.c.

{
  struct fork_handler *new_prepare = NULL;
  struct fork_handler *new_parent = NULL;
  struct fork_handler *new_child = NULL;

  if (prepare != NULL)
    {
      new_prepare = (struct fork_handler *) malloc (sizeof (*new_prepare));
      if (new_prepare == NULL)
       goto out1;

      new_prepare->handler = prepare;
      new_prepare->dso_handle = dso_handle;
    }

  if (parent != NULL)
    {
      new_parent = (struct fork_handler *) malloc (sizeof (*new_parent));
      if (new_parent == NULL)
       goto out2;

      new_parent->handler = parent;
      new_parent->dso_handle = dso_handle;
    }

  if (child != NULL)
    {
      new_child = (struct fork_handler *) malloc (sizeof (*new_child));
      if (new_child == NULL)
       {
         free (new_parent);
       out2:
         free (new_prepare);
       out1:
         return errno;
       }

      new_child->handler = child;
      new_child->dso_handle = dso_handle;
    }

  /* Get the lock to not conflict with running forks.  */
  __libc_lock_lock (__fork_block.lock);

  /* Now that we have all the handlers allocate enqueue them.  */
  if (new_prepare != NULL)
    list_add_tail (&new_prepare->list, &__fork_block.prepare_list);
  if (new_parent != NULL)
    list_add_tail (&new_parent->list, &__fork_block.parent_list);
  if (new_child != NULL)
    list_add_tail (&new_child->list, &__fork_block.child_list);

  /* Release the lock.  */
  __libc_lock_unlock (__fork_block.lock);

  return 0;
}