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
47 static const char *const resource_files[] = {
48 TEST_APP,
49 NULL,
50 };
51
do_child(void)52 static void do_child(void)
53 {
54 char *argv[3] = {TEST_APP, "canary", NULL};
55
56 TST_CHECKPOINT_WAIT(0);
57
58 TEST(execve(TEST_APP, argv, environ));
59 tst_res(TFAIL | TTERRNO, "execve() returned unexpected errno");
60 }
61
verify_execve(void)62 static void verify_execve(void)
63 {
64 int i;
65
66 for (i = 0; i < nchild; i++) {
67 if (SAFE_FORK() == 0)
68 do_child();
69 }
70
71 TST_CHECKPOINT_WAKE2(0, nchild);
72 }
73
setup(void)74 static void setup(void)
75 {
76 if (opt_nchild)
77 nchild = SAFE_STRTOL(opt_nchild, 1, INT_MAX);
78 }
79
80 static struct tst_test test = {
81 .test_all = verify_execve,
82 .options = (struct tst_option[]) {
83 {"n:", &opt_nchild, "Numbers of children"},
84 {}
85 },
86 .forks_child = 1,
87 .child_needs_reinit = 1,
88 .needs_checkpoints = 1,
89 .resource_files = resource_files,
90 .setup = setup,
91 };
92