]> git.saurik.com Git - apple/libc.git/blame - sys/fork.c
Libc-997.90.3.tar.gz
[apple/libc.git] / sys / fork.c
CommitLineData
1f2f436a
A
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>
6465356a 26#include <TargetConditionals.h>
1f2f436a 27
6465356a
A
28#if TARGET_IPHONE_SIMULATOR
29extern pid_t (*_host_fork)(void);
30#else
1f2f436a 31extern pid_t __fork(void);
6465356a 32#endif
1f2f436a
A
33
34static void (*_libSystem_atfork_prepare)(void) = 0;
35static void (*_libSystem_atfork_parent)(void) = 0;
36static void (*_libSystem_atfork_child)(void) = 0;
37
6465356a
A
38#if !TARGET_IPHONE_SIMULATOR
39__private_extern__
40#endif
41void _libc_fork_init(void (*prepare)(void), void (*parent)(void), void (*child)(void))
1f2f436a
A
42{
43 _libSystem_atfork_prepare = prepare;
44 _libSystem_atfork_parent = parent;
45 _libSystem_atfork_child = child;
46}
47
48/*
49 * fork stub
50 */
51pid_t
52fork(void)
53{
54 int ret;
55
56 _libSystem_atfork_prepare();
57 // Reader beware: this __fork() call is yet another wrapper around the actual syscall
58 // and lives inside libsyscall. The fork syscall needs some cuddling by asm before it's
59 // allowed to see the big wide C world.
6465356a
A
60#if TARGET_IPHONE_SIMULATOR
61 // _host_fork is yet another layer of wrapping that lives in the simulator's libSystem
62 ret = _host_fork();
63#else
1f2f436a 64 ret = __fork();
6465356a 65#endif
1f2f436a
A
66 if (-1 == ret)
67 {
68 // __fork already set errno for us
69 _libSystem_atfork_parent();
70 return ret;
71 }
72
73 if (0 == ret)
74 {
75 // We're the child in this part.
76 _libSystem_atfork_child();
77 return 0;
78 }
79
80 _libSystem_atfork_parent();
81 return ret;
82}
83