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