xref: /dflybsd-src/lib/libc/sys/fork.c (revision 8265747159d6085c462f06fcb5dcee70b773f66b)
1*82657471SMarkus Pfeiffer /*
2*82657471SMarkus Pfeiffer  * Copyright (c) 2013 Larisa Grigore <larisagrigore@gmail.com>
3*82657471SMarkus Pfeiffer  *
4*82657471SMarkus Pfeiffer  * Redistribution and use in source and binary forms, with or without
5*82657471SMarkus Pfeiffer  * modification, are permitted provided that the following conditions
6*82657471SMarkus Pfeiffer  * are met:
7*82657471SMarkus Pfeiffer  * 1. Redistributions of source code must retain the above copyright
8*82657471SMarkus Pfeiffer  *    notice, this list of conditions and the following disclaimer.
9*82657471SMarkus Pfeiffer  * 2. Redistributions in binary form must reproduce the above copyright
10*82657471SMarkus Pfeiffer  *    notice, this list of conditions and the following disclaimer in the
11*82657471SMarkus Pfeiffer  *    documentation and/or other materials provided with the distribution.
12*82657471SMarkus Pfeiffer  * 3. All advertising materials mentioning features or use of this software
13*82657471SMarkus Pfeiffer  *    must display the following acknowledgement:
14*82657471SMarkus Pfeiffer  *	This product includes software developed by Adam Glass and Charles
15*82657471SMarkus Pfeiffer  *	Hannum.
16*82657471SMarkus Pfeiffer  * 4. The names of the authors may not be used to endorse or promote products
17*82657471SMarkus Pfeiffer  *    derived from this software without specific prior written permission.
18*82657471SMarkus Pfeiffer  *
19*82657471SMarkus Pfeiffer  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20*82657471SMarkus Pfeiffer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*82657471SMarkus Pfeiffer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*82657471SMarkus Pfeiffer  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*82657471SMarkus Pfeiffer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*82657471SMarkus Pfeiffer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*82657471SMarkus Pfeiffer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*82657471SMarkus Pfeiffer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*82657471SMarkus Pfeiffer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*82657471SMarkus Pfeiffer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*82657471SMarkus Pfeiffer  */
30*82657471SMarkus Pfeiffer 
31*82657471SMarkus Pfeiffer #include <sys/types.h>
32*82657471SMarkus Pfeiffer #include <sys/syscall.h>
33*82657471SMarkus Pfeiffer #include <unistd.h>
34*82657471SMarkus Pfeiffer 
35*82657471SMarkus Pfeiffer void (*cb_prepare)(void) = 0;
36*82657471SMarkus Pfeiffer void (*cb_parent)(void) = 0;
37*82657471SMarkus Pfeiffer void (*cb_child)(void) = 0;
38*82657471SMarkus Pfeiffer 
39*82657471SMarkus Pfeiffer int
40*82657471SMarkus Pfeiffer __fork()
41*82657471SMarkus Pfeiffer {
42*82657471SMarkus Pfeiffer 	int ret;
43*82657471SMarkus Pfeiffer 
44*82657471SMarkus Pfeiffer 	if (cb_prepare)
45*82657471SMarkus Pfeiffer 		cb_prepare();
46*82657471SMarkus Pfeiffer 
47*82657471SMarkus Pfeiffer 	if ((ret = __syscall(SYS_fork)) == 0) {
48*82657471SMarkus Pfeiffer 		if (cb_child)
49*82657471SMarkus Pfeiffer 			cb_child();
50*82657471SMarkus Pfeiffer 	} else {
51*82657471SMarkus Pfeiffer 			if (cb_parent)
52*82657471SMarkus Pfeiffer 				cb_parent();
53*82657471SMarkus Pfeiffer 	}
54*82657471SMarkus Pfeiffer 	return (ret);
55*82657471SMarkus Pfeiffer }
56*82657471SMarkus Pfeiffer __weak_reference(__fork, fork);
57