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