]> granicus.if.org Git - strace/blob - tests/execve.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / execve.c
1 /*
2  * This file is part of execve strace test.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2015-2018 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12 #include <stdio.h>
13 #include <unistd.h>
14
15 #define FILENAME "test.execve\nfilename"
16 #define Q_FILENAME "test.execve\\nfilename"
17
18 static const char * const argv[] = {
19         FILENAME, "first", "second", (const char *) -1L,
20         (const char *) -2L, (const char *) -3L
21 };
22 static const char * const q_argv[] = {
23         Q_FILENAME, "first", "second"
24 };
25
26 static const char * const envp[] = {
27         "foobar=1", "foo\nbar=2", (const char *) -1L,
28         (const char *) -2L, (const char *) -3L
29 };
30 static const char * const q_envp[] = {
31         "foobar=1", "foo\\nbar=2"
32 };
33
34 int
35 main(void)
36 {
37         char ** const tail_argv = tail_memdup(argv, sizeof(argv));
38         char ** const tail_envp = tail_memdup(envp, sizeof(envp));
39
40         execve(FILENAME, tail_argv, tail_envp);
41         printf("execve(\"%s\""
42                ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
43 #if VERBOSE
44                ", [\"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
45 #else
46                ", %p /* 5 vars, unterminated */"
47 #endif
48                ") = -1 ENOENT (%m)\n",
49                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
50                argv[3], argv[4], argv[5], (char *) tail_argv + sizeof(argv)
51 #if VERBOSE
52                , q_envp[0], q_envp[1], envp[2], envp[3], envp[4],
53                (char *) tail_envp + sizeof(envp)
54 #else
55                , tail_envp
56 #endif
57                );
58
59         tail_argv[ARRAY_SIZE(q_argv)] = NULL;
60         tail_envp[ARRAY_SIZE(q_envp)] = NULL;
61         (void) q_envp;  /* workaround for clang bug #33068 */
62
63         execve(FILENAME, tail_argv, tail_envp);
64         printf("execve(\"%s\", [\"%s\", \"%s\", \"%s\"]"
65 #if VERBOSE
66                ", [\"%s\", \"%s\"]"
67 #else
68                ", %p /* 2 vars */"
69 #endif
70                ") = -1 ENOENT (%m)\n",
71                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2]
72 #if VERBOSE
73                , q_envp[0], q_envp[1]
74 #else
75                , tail_envp
76 #endif
77                );
78
79         execve(FILENAME, tail_argv + 2, tail_envp + 1);
80         printf("execve(\"%s\", [\"%s\"]"
81 #if VERBOSE
82                ", [\"%s\"]"
83 #else
84                ", %p /* 1 var */"
85 #endif
86                ") = -1 ENOENT (%m)\n",
87                Q_FILENAME, q_argv[2]
88 #if VERBOSE
89                , q_envp[1]
90 #else
91                , tail_envp + 1
92 #endif
93                );
94
95         TAIL_ALLOC_OBJECT_CONST_PTR(char *, empty);
96         char **const efault = empty + 1;
97         *empty = NULL;
98
99         execve(FILENAME, empty, empty);
100         printf("execve(\"%s\", []"
101 #if VERBOSE
102                ", []"
103 #else
104                ", %p /* 0 vars */"
105 #endif
106                ") = -1 ENOENT (%m)\n", Q_FILENAME
107 #if !VERBOSE
108                , empty
109 #endif
110                );
111
112         char *const str_a = tail_alloc(DEFAULT_STRLEN + 2);
113         fill_memory_ex(str_a, DEFAULT_STRLEN + 1, '0', 10);
114         str_a[DEFAULT_STRLEN + 1] = '\0';
115
116         char *const str_b = tail_alloc(DEFAULT_STRLEN + 2);
117         fill_memory_ex(str_b, DEFAULT_STRLEN + 1, '_', 32);
118         str_b[DEFAULT_STRLEN + 1] = '\0';
119
120         char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
121         char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
122         unsigned int i;
123         for (i = 0; i <= DEFAULT_STRLEN; ++i) {
124                 a[i] = &str_a[i];
125                 b[i] = &str_b[i];
126         }
127         a[i] = b[i] = NULL;
128
129         execve(FILENAME, a, b);
130         printf("execve(\"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
131         for (i = 1; i < DEFAULT_STRLEN; ++i)
132                 printf(", \"%s\"", a[i]);
133 #if VERBOSE
134         printf(", \"%s\"", a[i]);
135 #else
136         printf(", ...");
137 #endif
138 #if VERBOSE
139         printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
140         for (i = 1; i <= DEFAULT_STRLEN; ++i)
141                 printf(", \"%s\"", b[i]);
142         printf("]");
143 #else
144         printf("], %p /* %u vars */", b, DEFAULT_STRLEN + 1);
145 #endif
146         printf(") = -1 ENOENT (%m)\n");
147
148         execve(FILENAME, a + 1, b + 1);
149         printf("execve(\"%s\", [\"%s\"", Q_FILENAME, a[1]);
150         for (i = 2; i <= DEFAULT_STRLEN; ++i)
151                 printf(", \"%s\"", a[i]);
152 #if VERBOSE
153         printf("], [\"%s\"", b[1]);
154         for (i = 2; i <= DEFAULT_STRLEN; ++i)
155                 printf(", \"%s\"", b[i]);
156         printf("]");
157 #else
158         printf("], %p /* %d vars */", b + 1, DEFAULT_STRLEN);
159 #endif
160         printf(") = -1 ENOENT (%m)\n");
161
162         execve(FILENAME, (char **) tail_argv[ARRAY_SIZE(q_argv)], efault);
163         printf("execve(\"%s\", NULL, %p) = -1 ENOENT (%m)\n",
164                Q_FILENAME, efault);
165
166         execve(FILENAME, efault, NULL);
167         printf("execve(\"%s\", %p, NULL) = -1 ENOENT (%m)\n",
168                Q_FILENAME, efault);
169
170         return 0;
171 }