blob: 82e32c6062382150e0042fc35f0e07404732751f [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levin23168c12016-02-07 14:37:53 +00002 * This file is part of execve strace test.
3 *
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00005 * 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
Dmitry V. Levin23168c12016-02-07 14:37:53 +000030#include "tests.h"
31#include <errno.h>
32#include <stdio.h>
Dmitry V. Levin4ff687b2015-07-27 10:02:33 +000033#include <unistd.h>
34
Dmitry V. Levin23168c12016-02-07 14:37:53 +000035#define FILENAME "test.execve\nfilename"
36#define Q_FILENAME "test.execve\\nfilename"
37
38static const char * const argv[] = {
39 FILENAME, "first", "second", (const char *) -1L,
40 (const char *) -2L, (const char *) -3L
41};
42static const char * const q_argv[] = {
43 Q_FILENAME, "first", "second"
44};
45
46static const char * const envp[] = {
47 "foobar=1", "foo\nbar=2", (const char *) -1L,
48 (const char *) -2L, (const char *) -3L
49};
50static const char * const q_envp[] = {
51 "foobar=1", "foo\\nbar=2"
52};
Dmitry V. Levin4ff687b2015-07-27 10:02:33 +000053
54int
55main(void)
56{
Dmitry V. Levin23168c12016-02-07 14:37:53 +000057 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, ???]"
63#ifdef VERBOSE_EXECVE
64 ", [\"%s\", \"%s\", %p, %p, %p, ???]"
65#else
66 ", [/* 5 vars, unterminated */]"
67#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000068 ") = -1 ENOENT (%m)\n",
Dmitry V. Levin23168c12016-02-07 14:37:53 +000069 Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000070 argv[3], argv[4], argv[5]
Dmitry V. Levin23168c12016-02-07 14:37:53 +000071#ifdef VERBOSE_EXECVE
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000072 , q_envp[0], q_envp[1], envp[2], envp[3], envp[4]
Dmitry V. Levin23168c12016-02-07 14:37:53 +000073#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000074 );
Dmitry V. Levin23168c12016-02-07 14:37:53 +000075
76 tail_argv[ARRAY_SIZE(q_argv)] = NULL;
77 tail_envp[ARRAY_SIZE(q_envp)] = NULL;
78
79 execve(FILENAME, tail_argv, tail_envp);
80 printf("execve(\"%s\", [\"%s\", \"%s\", \"%s\"]"
81#ifdef VERBOSE_EXECVE
82 ", [\"%s\", \"%s\"]"
83#else
84 ", [/* 2 vars */]"
85#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000086 ") = -1 ENOENT (%m)\n",
87 Q_FILENAME, q_argv[0], q_argv[1], q_argv[2]
Dmitry V. Levin23168c12016-02-07 14:37:53 +000088#ifdef VERBOSE_EXECVE
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000089 , q_envp[0], q_envp[1]
Dmitry V. Levin23168c12016-02-07 14:37:53 +000090#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +000091 );
Dmitry V. Levin23168c12016-02-07 14:37:53 +000092
93 execve(FILENAME, tail_argv + 2, tail_envp + 1);
94 printf("execve(\"%s\", [\"%s\"]"
95#ifdef VERBOSE_EXECVE
96 ", [\"%s\"]"
97#else
98 ", [/* 1 var */]"
99#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000100 ") = -1 ENOENT (%m)\n",
101 Q_FILENAME, q_argv[2]
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000102#ifdef VERBOSE_EXECVE
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000103 , q_envp[1]
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000104#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000105 );
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000106
107 char **const empty = tail_alloc(sizeof(*empty));
Dmitry V. Levin84be84d2016-02-13 01:23:26 +0000108 char **const efault = empty + 1;
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000109 *empty = NULL;
110
111 execve(FILENAME, empty, empty);
112 printf("execve(\"%s\", []"
113#ifdef VERBOSE_EXECVE
114 ", []"
115#else
116 ", [/* 0 vars */]"
117#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000118 ") = -1 ENOENT (%m)\n", Q_FILENAME);
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000119
120 char str_a[] = "012345678901234567890123456789012";
121 char str_b[] = "_abcdefghijklmnopqrstuvwxyz()[]{}";
122#define DEFAULT_STRLEN ((unsigned int) sizeof(str_a) - 2)
123 char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
124 char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
125 unsigned int i;
126 for (i = 0; i <= DEFAULT_STRLEN; ++i) {
127 a[i] = &str_a[i];
128 b[i] = &str_b[i];
129 }
130 a[i] = b[i] = NULL;
131
132 execve(FILENAME, a, b);
133 printf("execve(\"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
134 for (i = 1; i < DEFAULT_STRLEN; ++i)
135 printf(", \"%s\"", a[i]);
136#ifdef VERBOSE_EXECVE
137 printf(", \"%s\"", a[i]);
138#else
139 printf(", ...");
140#endif
141#ifdef VERBOSE_EXECVE
142 printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
143 for (i = 1; i <= DEFAULT_STRLEN; ++i)
144 printf(", \"%s\"", b[i]);
145#else
146 printf("], [/* %u vars */", DEFAULT_STRLEN + 1);
147#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000148 printf("]) = -1 ENOENT (%m)\n");
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000149
150 execve(FILENAME, a + 1, b + 1);
151 printf("execve(\"%s\", [\"%s\"", Q_FILENAME, a[1]);
152 for (i = 2; i <= DEFAULT_STRLEN; ++i)
153 printf(", \"%s\"", a[i]);
154#ifdef VERBOSE_EXECVE
155 printf("], [\"%s\"", b[1]);
156 for (i = 2; i <= DEFAULT_STRLEN; ++i)
157 printf(", \"%s\"", b[i]);
158#else
159 printf("], [/* %d vars */", DEFAULT_STRLEN);
160#endif
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000161 printf("]) = -1 ENOENT (%m)\n");
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000162
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000163 execve(FILENAME, (char **) tail_argv[ARRAY_SIZE(q_argv)], efault);
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000164 printf("execve(\"%s\", NULL, %p) = -1 ENOENT (%m)\n",
165 Q_FILENAME, efault);
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000166
167 execve(FILENAME, efault, NULL);
Dmitry V. Levinb5faea42016-02-13 01:20:39 +0000168 printf("execve(\"%s\", %p, NULL) = -1 ENOENT (%m)\n",
169 Q_FILENAME, efault);
Dmitry V. Levin23168c12016-02-07 14:37:53 +0000170
Dmitry V. Levin4ff687b2015-07-27 10:02:33 +0000171 return 0;
172}