]> git.saurik.com Git - apple/libc.git/blob - sys/fork.c
Libc-1081.1.3.tar.gz
[apple/libc.git] / sys / fork.c
1 /*
2 * Copyright (c) 2010 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <TargetConditionals.h>
27
28 #include "libc_private.h"
29
30 extern pid_t __fork(void);
31
32 static void (*_libSystem_atfork_prepare)(void) = 0;
33 static void (*_libSystem_atfork_parent)(void) = 0;
34 static void (*_libSystem_atfork_child)(void) = 0;
35
36 __private_extern__
37 void _libc_fork_init(const struct _libc_functions *funcs)
38 {
39 _libSystem_atfork_prepare = funcs->atfork_prepare;
40 _libSystem_atfork_parent = funcs->atfork_parent;
41 _libSystem_atfork_child = funcs->atfork_child;
42 }
43
44 /*
45 * fork stub
46 */
47 pid_t
48 fork(void)
49 {
50 int ret;
51
52 _libSystem_atfork_prepare();
53 // Reader beware: this __fork() call is yet another wrapper around the actual syscall
54 // and lives inside libsyscall. The fork syscall needs some cuddling by asm before it's
55 // allowed to see the big wide C world.
56 ret = __fork();
57 if (-1 == ret)
58 {
59 // __fork already set errno for us
60 _libSystem_atfork_parent();
61 return ret;
62 }
63
64 if (0 == ret)
65 {
66 // We're the child in this part.
67 _libSystem_atfork_child();
68 return 0;
69 }
70
71 _libSystem_atfork_parent();
72 return ret;
73 }
74