1 /*
2 * Copyright (c) 2018 Linux Test Project
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * 07/2001 Ported by Wayne Boyer
6 * 21/04/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23 * NAME
24 * execve05.c
25 *
26 * DESCRIPTION
27 * This testcase tests the basic functionality of the execve(2) system
28 * call.
29 *
30 * ALGORITHM
31 * This tests the functionality of the execve(2) system call by spawning
32 * a few children, each of which would execute "execve_child" simultaneously,
33 * and finally the parent ensures that they terminated correctly.
34 *
35 * USAGE
36 * execve05 -i 5 -n 20
37 */
38
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49 #include <pwd.h>
50
51 #include "tst_test.h"
52
53 #define TEST_APP "execve_child"
54
55 static int nchild = 8;
56
57 static char *opt_nchild;
58 static struct tst_option exe_options[] = {
59 {"n:", &opt_nchild, "-n numbers of children"},
60 {NULL, NULL, NULL}
61 };
62
63 static const char *const resource_files[] = {
64 TEST_APP,
65 NULL,
66 };
67
do_child(void)68 static void do_child(void)
69 {
70 char *argv[3] = {TEST_APP, "canary", NULL};
71
72 TST_CHECKPOINT_WAIT(0);
73
74 TEST(execve(TEST_APP, argv, environ));
75 tst_res(TFAIL | TERRNO, "execve() returned unexpected errno");
76 }
77
verify_execve(void)78 static void verify_execve(void)
79 {
80 int i;
81
82 for (i = 0; i < nchild; i++) {
83 if (SAFE_FORK() == 0)
84 do_child();
85 }
86
87 TST_CHECKPOINT_WAKE2(0, nchild);
88 }
89
setup(void)90 static void setup(void)
91 {
92 if (opt_nchild)
93 nchild = SAFE_STRTOL(opt_nchild, 1, INT_MAX);
94 }
95
96 static struct tst_test test = {
97 .test_all = verify_execve,
98 .options = exe_options,
99 .forks_child = 1,
100 .child_needs_reinit = 1,
101 .needs_checkpoints = 1,
102 .resource_files = resource_files,
103 .setup = setup,
104 };
105