xref: /freebsd/lib/libsys/rfork_thread_gen.c (revision c1be185e3fb9afd6743683a8f5a43b9c364ab529)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2026 The FreeBSD Foundation
5  *
6  * This software were developed by
7  * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
8  * the FreeBSD Foundation.
9  */
10 
11 #include <errno.h>
12 #include <unistd.h>
13 
14 pid_t
rfork_thread(int flags,void * stack_addr,int (* start_fn)(void *),void * arg)15 rfork_thread(int flags, void *stack_addr, int (*start_fn)(void *), void *arg)
16 {
17 	pid_t res;
18 	int ret;
19 
20 	/*
21 	 * Generic implementation cannot switch stacks.  Only
22 	 * architecture-specific code knows how to do it.  Require
23 	 * that caller knows that, and refuse to do operate if the
24 	 * stack was supplied.
25 	 *
26 	 * Note that implementations that do switch stack, would fault
27 	 * immediately if the passed stack is NULL.  They do not need to
28 	 * specifically check for the NULL stack value.
29 	 */
30 	if (stack_addr != NULL) {
31 		errno = EOPNOTSUPP;
32 		return (-1);
33 	}
34 	res = rfork(flags);
35 	if (res == 0) {
36 		ret = start_fn(arg);
37 		_exit(ret);
38 	}
39 	return (res);
40 }
41