Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F148610932
D31378.id93142.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
D31378.id93142.diff
View Options
diff --git a/include/unistd.h b/include/unistd.h
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -586,6 +586,7 @@
int unwhiteout(const char *);
void *valloc(size_t); /* obsoleted by malloc() */
int funlinkat(int, const char *, int, int);
+pid_t _Fork(void);
#ifndef _OPTRESET_DECLARED
#define _OPTRESET_DECLARED
diff --git a/lib/libc/include/compat.h b/lib/libc/include/compat.h
--- a/lib/libc/include/compat.h
+++ b/lib/libc/include/compat.h
@@ -37,7 +37,7 @@
#ifndef __LIBC_COMPAT_H__
#define __LIBC_COMPAT_H__
-#define __sym_compat(sym,impl,verid) \
+#define __sym_compat(sym, impl, verid) \
.symver impl, sym@verid
#ifndef NO_COMPAT7
@@ -74,7 +74,7 @@
#define __weak_reference(sym,alias) \
.weak alias;.equ alias,sym
-__weak_reference(__sys_fcntl,__fcntl_compat)
+__weak_reference(__sys_fcntl, __fcntl_compat)
#undef __weak_reference
diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -49,6 +49,7 @@
SRCS+= pipe.c
SRCS+= shm_open.c
SRCS+= vadvise.c
+SRCS+= _Fork.c
SRCS+= compat-stub.c
diff --git a/lib/libc/sys/Symbol.map b/lib/libc/sys/Symbol.map
--- a/lib/libc/sys/Symbol.map
+++ b/lib/libc/sys/Symbol.map
@@ -417,6 +417,10 @@
shm_rename;
};
+FBSD_1.7 {
+ _Fork;
+};
+
FBSDprivate_1.0 {
___acl_aclcheck_fd;
__sys___acl_aclcheck_fd;
diff --git a/lib/libc/sys/_Fork.c b/lib/libc/sys/_Fork.c
new file mode 100644
--- /dev/null
+++ b/lib/libc/sys/_Fork.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021 The FreeBSD Foundation.
+ * All rights reserved.
+ *
+ * Portions of this software were developed by Konstantin Belousov
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice(s), this list of conditions and the following disclaimer as
+ * the first lines of this file unmodified other than the possible
+ * addition of one or more copyright notices.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice(s), this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <unistd.h>
+#include "libc_private.h"
+
+#pragma weak _Fork
+pid_t
+_Fork(void)
+{
+ return (__sys_fork());
+}
diff --git a/lib/libc/sys/fork.2 b/lib/libc/sys/fork.2
--- a/lib/libc/sys/fork.2
+++ b/lib/libc/sys/fork.2
@@ -28,7 +28,7 @@
.\" @(#)fork.2 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
-.Dd April 20, 2021
+.Dd August 2, 2021
.Dt FORK 2
.Os
.Sh NAME
@@ -40,10 +40,12 @@
.In unistd.h
.Ft pid_t
.Fn fork void
+.Ft pid_t
+.Fn _Fork void
.Sh DESCRIPTION
The
.Fn fork
-system call causes creation of a new process.
+function causes creation of a new process.
The new process (child process) is an exact copy of the
calling process (parent process) except for the following:
.Bl -bullet -offset indent
@@ -77,6 +79,15 @@
All interval timers are cleared; see
.Xr setitimer 2 .
.It
+The robust mutexes list (see
+.Xr pthread_mutexattr_setrobust 3 )
+is cleared for the child.
+.It
+The atfork handlers established with the
+.Xr pthread_atfork 3
+function are called as appropriate before fork in the parent process,
+and after the child is created, in parent and child.
+.It
The child process has only one thread,
corresponding to the calling thread in the parent process.
If the process has more than one thread,
@@ -87,11 +98,58 @@
are guaranteed to work in the child process until a call to
.Xr execve 2
or a similar function.
+The
+.Fx
+implementation of
+.Fn fork
+provides a usable
+.Xr malloc 3
+and
+.Xr rtld 1
+services in the child process.
.El
+.Pp
+The
+.Fn fork
+function is not async-signal safe and creates a cancellation point
+in the parent process.
+It cannot be safely used from signal handlers, and the atfork handlers
+established by
+.Xr pthread_atfork 3
+do not need to be async-signal safe either.
+.Pp
+The
+.Fn _Fork
+function creates a new process, similarly to
+.Fn fork ,
+but it is async-signal safe.
+.Fn _Fork
+does not call atfork handlers, and does not create a cancellation point.
+It can be used safely from signal handlers, but then no userspace
+services (
+.Xr malloc 3
+or
+.Xr rtld 1 )
+are available in the child if forked from multi-threaded parent.
+In particular, if using dynamic linking, all dynamic symbols used by the
+child after
+.Fn _Fork
+must be pre-resolved.
+Note: resolving can be done globally by specifying the
+.Ev LD_BIND_NOW
+environment variable to dynamic linker, or per-binary by passing the
+.Fl z Ar now
+option to the static linker
+.Xr ld 1 ,
+or by using each symbol before the
+.Fn _Fork
+call to force the binding.
.Sh RETURN VALUES
Upon successful completion,
.Fn fork
-returns a value
+and
+.Fn _Fork
+return a value
of 0 to the child process and returns the process ID of the child
process to the parent process.
Otherwise, a value of -1 is returned
@@ -176,9 +234,21 @@
.Xr setrlimit 2 ,
.Xr sigaction 2 ,
.Xr vfork 2 ,
-.Xr wait 2
+.Xr wait 2 ,
+.Xr pthread_atfork 3
.Sh HISTORY
The
.Fn fork
function appeared in
.At v1 .
+.Pp
+The
+.Fn _Fork
+function was defined by Austin Group together with the removal
+of a requirement that the
+.Fn fork
+implementation must be async-signal safe.
+The
+.Fn _Fork
+function appeared in
+.Fx 14.0 .
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Mar 20, 3:25 AM (14 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
29995230
Default Alt Text
D31378.id93142.diff (6 KB)
Attached To
Mode
D31378: Add _Fork()
Attached
Detach File
Event Timeline
Log In to Comment