xref: /aosp_15_r20/external/ltp/lib/tst_clone.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) 2021 SUSE LLC
3*49cdfc7eSAndroid Build Coastguard Worker  * Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker #include <stddef.h>
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
12*49cdfc7eSAndroid Build Coastguard Worker 
tst_clone(const struct tst_clone_args * tst_args)13*49cdfc7eSAndroid Build Coastguard Worker pid_t tst_clone(const struct tst_clone_args *tst_args)
14*49cdfc7eSAndroid Build Coastguard Worker {
15*49cdfc7eSAndroid Build Coastguard Worker 	struct clone_args args = {
16*49cdfc7eSAndroid Build Coastguard Worker 		.flags = tst_args->flags,
17*49cdfc7eSAndroid Build Coastguard Worker 		.exit_signal = tst_args->exit_signal,
18*49cdfc7eSAndroid Build Coastguard Worker 		.cgroup = tst_args->cgroup,
19*49cdfc7eSAndroid Build Coastguard Worker 	};
20*49cdfc7eSAndroid Build Coastguard Worker 	int flags;
21*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid = -1;
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker 	tst_flush();
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker 	errno = ENOSYS;
26*49cdfc7eSAndroid Build Coastguard Worker 	if (__NR_clone3 != __LTP__NR_INVALID_SYSCALL)
27*49cdfc7eSAndroid Build Coastguard Worker 		pid = syscall(__NR_clone3, &args, sizeof(args));
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == -1 && errno != ENOSYS)
30*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (pid != -1)
33*49cdfc7eSAndroid Build Coastguard Worker 		return pid;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	flags = args.exit_signal | args.flags;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker #ifdef __s390x__
38*49cdfc7eSAndroid Build Coastguard Worker 	pid = syscall(__NR_clone, NULL, flags);
39*49cdfc7eSAndroid Build Coastguard Worker #else
40*49cdfc7eSAndroid Build Coastguard Worker 	pid = syscall(__NR_clone, flags, NULL);
41*49cdfc7eSAndroid Build Coastguard Worker #endif
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == -1)
44*49cdfc7eSAndroid Build Coastguard Worker 		return -2;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	return pid;
47*49cdfc7eSAndroid Build Coastguard Worker }
48