]> granicus.if.org Git - strace/blob - tests/execve.c
Update copyright headers
[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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "tests.h"
32 #include <stdio.h>
33 #include <unistd.h>
34
35 #define FILENAME "test.execve\nfilename"
36 #define Q_FILENAME "test.execve\\nfilename"
37
38 static const char * const argv[] = {
39         FILENAME, "first", "second", (const char *) -1L,
40         (const char *) -2L, (const char *) -3L
41 };
42 static const char * const q_argv[] = {
43         Q_FILENAME, "first", "second"
44 };
45
46 static const char * const envp[] = {
47         "foobar=1", "foo\nbar=2", (const char *) -1L,
48         (const char *) -2L, (const char *) -3L
49 };
50 static const char * const q_envp[] = {
51         "foobar=1", "foo\\nbar=2"
52 };
53
54 int
55 main(void)
56 {
57         char ** const tail_argv = tail_memdup(argv, sizeof(argv));
58         char ** const tail_envp = tail_memdup(envp, sizeof(envp));
59
60         execve(FILENAME, tail_argv, tail_envp);
61         printf("execve(\"%s\""
62                ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
63 #if VERBOSE
64                ", [\"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
65 #else
66                ", %p /* 5 vars, unterminated */"
67 #endif
68                ") = -1 ENOENT (%m)\n",
69                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
70                argv[3], argv[4], argv[5], (char *) tail_argv + sizeof(argv)
71 #if VERBOSE
72                , q_envp[0], q_envp[1], envp[2], envp[3], envp[4],
73                (char *) tail_envp + sizeof(envp)
74 #else
75                , tail_envp
76 #endif
77                );
78
79         tail_argv[ARRAY_SIZE(q_argv)] = NULL;
80         tail_envp[ARRAY_SIZE(q_envp)] = NULL;
81
82         execve(FILENAME, tail_argv, tail_envp);
83         printf("execve(\"%s\", [\"%s\", \"%s\", \"%s\"]"
84 #if VERBOSE
85                ", [\"%s\", \"%s\"]"
86 #else
87                ", %p /* 2 vars */"
88 #endif
89                ") = -1 ENOENT (%m)\n",
90                Q_FILENAME, q_argv[0], q_argv[1], q_argv[2]
91 #if VERBOSE
92                , q_envp[0], q_envp[1]
93 #else
94                , tail_envp
95 #endif
96                );
97
98         execve(FILENAME, tail_argv + 2, tail_envp + 1);
99         printf("execve(\"%s\", [\"%s\"]"
100 #if VERBOSE
101                ", [\"%s\"]"
102 #else
103                ", %p /* 1 var */"
104 #endif
105                ") = -1 ENOENT (%m)\n",
106                Q_FILENAME, q_argv[2]
107 #if VERBOSE
108                , q_envp[1]
109 #else
110                , tail_envp + 1
111 #endif
112                );
113
114         TAIL_ALLOC_OBJECT_CONST_PTR(char *, empty);
115         char **const efault = empty + 1;
116         *empty = NULL;
117
118         execve(FILENAME, empty, empty);
119         printf("execve(\"%s\", []"
120 #if VERBOSE
121                ", []"
122 #else
123                ", %p /* 0 vars */"
124 #endif
125                ") = -1 ENOENT (%m)\n", Q_FILENAME
126 #if !VERBOSE
127                , empty
128 #endif
129                );
130
131         char *const str_a = tail_alloc(DEFAULT_STRLEN + 2);
132         fill_memory_ex(str_a, DEFAULT_STRLEN + 1, '0', 10);
133         str_a[DEFAULT_STRLEN + 1] = '\0';
134
135         char *const str_b = tail_alloc(DEFAULT_STRLEN + 2);
136         fill_memory_ex(str_b, DEFAULT_STRLEN + 1, '_', 32);
137         str_b[DEFAULT_STRLEN + 1] = '\0';
138
139         char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
140         char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
141         unsigned int i;
142         for (i = 0; i <= DEFAULT_STRLEN; ++i) {
143                 a[i] = &str_a[i];
144                 b[i] = &str_b[i];
145         }
146         a[i] = b[i] = NULL;
147
148         execve(FILENAME, a, b);
149         printf("execve(\"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
150         for (i = 1; i < DEFAULT_STRLEN; ++i)
151                 printf(", \"%s\"", a[i]);
152 #if VERBOSE
153         printf(", \"%s\"", a[i]);
154 #else
155         printf(", ...");
156 #endif
157 #if VERBOSE
158         printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
159         for (i = 1; i <= DEFAULT_STRLEN; ++i)
160                 printf(", \"%s\"", b[i]);
161         printf("]");
162 #else
163         printf("], %p /* %u vars */", b, DEFAULT_STRLEN + 1);
164 #endif
165         printf(") = -1 ENOENT (%m)\n");
166
167         execve(FILENAME, a + 1, b + 1);
168         printf("execve(\"%s\", [\"%s\"", Q_FILENAME, a[1]);
169         for (i = 2; i <= DEFAULT_STRLEN; ++i)
170                 printf(", \"%s\"", a[i]);
171 #if VERBOSE
172         printf("], [\"%s\"", b[1]);
173         for (i = 2; i <= DEFAULT_STRLEN; ++i)
174                 printf(", \"%s\"", b[i]);
175         printf("]");
176 #else
177         printf("], %p /* %d vars */", b + 1, DEFAULT_STRLEN);
178 #endif
179         printf(") = -1 ENOENT (%m)\n");
180
181         execve(FILENAME, (char **) tail_argv[ARRAY_SIZE(q_argv)], efault);
182         printf("execve(\"%s\", NULL, %p) = -1 ENOENT (%m)\n",
183                Q_FILENAME, efault);
184
185         execve(FILENAME, efault, NULL);
186         printf("execve(\"%s\", %p, NULL) = -1 ENOENT (%m)\n",
187                Q_FILENAME, efault);
188
189         return 0;
190 }