summaryrefslogtreecommitdiff
path: root/otherlibs/unix/fork.c
blob: 653034ac4b07ed6499cc5e41aa8f69ca238db201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
/*                                                                        */
/*   Copyright 1996 Institut National de Recherche en Informatique et     */
/*     en Automatique.                                                    */
/*                                                                        */
/*   All rights reserved.  This file is distributed under the terms of    */
/*   the GNU Lesser General Public License version 2.1, with the          */
/*   special exception on linking described in the file LICENSE.          */
/*                                                                        */
/**************************************************************************/

#define CAML_INTERNALS

#include <caml/mlvalues.h>
#include <caml/debugger.h>
#include <caml/runtime_events.h>
#include "unixsupport.h"
#include <caml/domain.h>
#include <caml/fail.h>

/* Post-fork tasks to be carried out in the parent */
void caml_atfork_parent(pid_t child_pid) {
  CAML_EV_LIFECYCLE(EV_FORK_PARENT, child_pid);
}

/* Post-fork tasks to be carried out in the child */
void caml_atfork_child(void) {
  caml_runtime_events_post_fork();
  CAML_EV_LIFECYCLE(EV_FORK_CHILD, 0);
}

CAMLprim value caml_unix_fork(value unit)
{
  int ret;
  if (caml_domain_is_multicore()) {
    caml_failwith
      ("Unix.fork may not be called while other domains were created");
  }

  ret = fork();

  if (ret == -1) caml_uerror("fork", Nothing);

  if (ret == 0) {
    caml_atfork_child();
    /* the following hook can be redefined in other places */
    caml_atfork_hook();
  } else {
    caml_atfork_parent(ret);
  }

  if (caml_debugger_in_use)
    if ((caml_debugger_fork_mode && ret == 0) ||
        (!caml_debugger_fork_mode && ret != 0))
      caml_debugger_cleanup_fork();

  return Val_int(ret);
}