xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/execve/execve05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2001-2023 Linux Test Project
4  * Copyright (c) International Business Machines Corp., 2001
5  * Copyright (c) 2008 Renaud Lottiaux <[email protected]>
6  * Ported to LTP: Wayne Boyer
7  */
8 
9 /*\
10  * [Description]
11  *
12  * This tests the functionality of the execve(2) system call by spawning
13  * a few children, each of which would execute "execve_child" simultaneously,
14  * and finally the parent ensures that they terminated correctly.
15  */
16 
17 #define _GNU_SOURCE
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <pwd.h>
27 
28 #include "tst_test.h"
29 
30 #define TEST_APP "execve_child"
31 
32 static int nchild = 8;
33 
34 static char *opt_nchild;
35 
do_child(void)36 static void do_child(void)
37 {
38 	char *argv[3] = {TEST_APP, "canary", NULL};
39 
40 	TST_CHECKPOINT_WAIT(0);
41 
42 	TEST(execve(TEST_APP, argv, environ));
43 	tst_res(TFAIL | TTERRNO, "execve() returned unexpected errno");
44 }
45 
verify_execve(void)46 static void verify_execve(void)
47 {
48 	int i;
49 
50 	for (i = 0; i < nchild; i++) {
51 		if (SAFE_FORK() == 0)
52 			do_child();
53 	}
54 
55 	TST_CHECKPOINT_WAKE2(0, nchild);
56 }
57 
setup(void)58 static void setup(void)
59 {
60 	if (opt_nchild)
61 		nchild = SAFE_STRTOL(opt_nchild, 1, INT_MAX);
62 }
63 
64 static struct tst_test test = {
65 	.test_all = verify_execve,
66 	.options = (struct tst_option[]) {
67 		{"n:", &opt_nchild, "Numbers of children"},
68 		{}
69 	},
70 	.forks_child = 1,
71 	.child_needs_reinit = 1,
72 	.needs_checkpoints = 1,
73 	.resource_files = (const char *const []) {
74 		TEST_APP,
75 		NULL
76 	},
77 	.setup = setup,
78 };
79