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