Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 2 | * This file is part of execve strace test. |
| 3 | * |
| 4 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 30 | #include "tests.h" |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Dmitry V. Levin | 4ff687b | 2015-07-27 10:02:33 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 34 | #define FILENAME "test.execve\nfilename" |
| 35 | #define Q_FILENAME "test.execve\\nfilename" |
| 36 | |
| 37 | static const char * const argv[] = { |
| 38 | FILENAME, "first", "second", (const char *) -1L, |
| 39 | (const char *) -2L, (const char *) -3L |
| 40 | }; |
| 41 | static const char * const q_argv[] = { |
| 42 | Q_FILENAME, "first", "second" |
| 43 | }; |
| 44 | |
| 45 | static const char * const envp[] = { |
| 46 | "foobar=1", "foo\nbar=2", (const char *) -1L, |
| 47 | (const char *) -2L, (const char *) -3L |
| 48 | }; |
| 49 | static const char * const q_envp[] = { |
| 50 | "foobar=1", "foo\\nbar=2" |
| 51 | }; |
Dmitry V. Levin | 4ff687b | 2015-07-27 10:02:33 +0000 | [diff] [blame] | 52 | |
| 53 | int |
| 54 | main(void) |
| 55 | { |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 56 | char ** const tail_argv = tail_memdup(argv, sizeof(argv)); |
| 57 | char ** const tail_envp = tail_memdup(envp, sizeof(envp)); |
| 58 | |
| 59 | execve(FILENAME, tail_argv, tail_envp); |
| 60 | printf("execve(\"%s\"" |
| 61 | ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ???]" |
| 62 | #ifdef VERBOSE_EXECVE |
| 63 | ", [\"%s\", \"%s\", %p, %p, %p, ???]" |
| 64 | #else |
| 65 | ", [/* 5 vars, unterminated */]" |
| 66 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 67 | ") = -1 ENOENT (%m)\n", |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 68 | Q_FILENAME, q_argv[0], q_argv[1], q_argv[2], |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 69 | argv[3], argv[4], argv[5] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 70 | #ifdef VERBOSE_EXECVE |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 71 | , q_envp[0], q_envp[1], envp[2], envp[3], envp[4] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 72 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 73 | ); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 74 | |
| 75 | tail_argv[ARRAY_SIZE(q_argv)] = NULL; |
| 76 | tail_envp[ARRAY_SIZE(q_envp)] = NULL; |
| 77 | |
| 78 | execve(FILENAME, tail_argv, tail_envp); |
| 79 | printf("execve(\"%s\", [\"%s\", \"%s\", \"%s\"]" |
| 80 | #ifdef VERBOSE_EXECVE |
| 81 | ", [\"%s\", \"%s\"]" |
| 82 | #else |
| 83 | ", [/* 2 vars */]" |
| 84 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 85 | ") = -1 ENOENT (%m)\n", |
| 86 | Q_FILENAME, q_argv[0], q_argv[1], q_argv[2] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 87 | #ifdef VERBOSE_EXECVE |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 88 | , q_envp[0], q_envp[1] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 89 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 90 | ); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 91 | |
| 92 | execve(FILENAME, tail_argv + 2, tail_envp + 1); |
| 93 | printf("execve(\"%s\", [\"%s\"]" |
| 94 | #ifdef VERBOSE_EXECVE |
| 95 | ", [\"%s\"]" |
| 96 | #else |
| 97 | ", [/* 1 var */]" |
| 98 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 99 | ") = -1 ENOENT (%m)\n", |
| 100 | Q_FILENAME, q_argv[2] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 101 | #ifdef VERBOSE_EXECVE |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 102 | , q_envp[1] |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 103 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 104 | ); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 105 | |
| 106 | char **const empty = tail_alloc(sizeof(*empty)); |
Dmitry V. Levin | 84be84d | 2016-02-13 01:23:26 +0000 | [diff] [blame] | 107 | char **const efault = empty + 1; |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 108 | *empty = NULL; |
| 109 | |
| 110 | execve(FILENAME, empty, empty); |
| 111 | printf("execve(\"%s\", []" |
| 112 | #ifdef VERBOSE_EXECVE |
| 113 | ", []" |
| 114 | #else |
| 115 | ", [/* 0 vars */]" |
| 116 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 117 | ") = -1 ENOENT (%m)\n", Q_FILENAME); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 118 | |
| 119 | char str_a[] = "012345678901234567890123456789012"; |
| 120 | char str_b[] = "_abcdefghijklmnopqrstuvwxyz()[]{}"; |
| 121 | #define DEFAULT_STRLEN ((unsigned int) sizeof(str_a) - 2) |
| 122 | char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2)); |
| 123 | char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2)); |
| 124 | unsigned int i; |
| 125 | for (i = 0; i <= DEFAULT_STRLEN; ++i) { |
| 126 | a[i] = &str_a[i]; |
| 127 | b[i] = &str_b[i]; |
| 128 | } |
| 129 | a[i] = b[i] = NULL; |
| 130 | |
| 131 | execve(FILENAME, a, b); |
| 132 | printf("execve(\"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]); |
| 133 | for (i = 1; i < DEFAULT_STRLEN; ++i) |
| 134 | printf(", \"%s\"", a[i]); |
| 135 | #ifdef VERBOSE_EXECVE |
| 136 | printf(", \"%s\"", a[i]); |
| 137 | #else |
| 138 | printf(", ..."); |
| 139 | #endif |
| 140 | #ifdef VERBOSE_EXECVE |
| 141 | printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]); |
| 142 | for (i = 1; i <= DEFAULT_STRLEN; ++i) |
| 143 | printf(", \"%s\"", b[i]); |
| 144 | #else |
| 145 | printf("], [/* %u vars */", DEFAULT_STRLEN + 1); |
| 146 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 147 | printf("]) = -1 ENOENT (%m)\n"); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 148 | |
| 149 | execve(FILENAME, a + 1, b + 1); |
| 150 | printf("execve(\"%s\", [\"%s\"", Q_FILENAME, a[1]); |
| 151 | for (i = 2; i <= DEFAULT_STRLEN; ++i) |
| 152 | printf(", \"%s\"", a[i]); |
| 153 | #ifdef VERBOSE_EXECVE |
| 154 | printf("], [\"%s\"", b[1]); |
| 155 | for (i = 2; i <= DEFAULT_STRLEN; ++i) |
| 156 | printf(", \"%s\"", b[i]); |
| 157 | #else |
| 158 | printf("], [/* %d vars */", DEFAULT_STRLEN); |
| 159 | #endif |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 160 | printf("]) = -1 ENOENT (%m)\n"); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 161 | |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 162 | execve(FILENAME, (char **) tail_argv[ARRAY_SIZE(q_argv)], efault); |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 163 | printf("execve(\"%s\", NULL, %p) = -1 ENOENT (%m)\n", |
| 164 | Q_FILENAME, efault); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 165 | |
| 166 | execve(FILENAME, efault, NULL); |
Dmitry V. Levin | b5faea4 | 2016-02-13 01:20:39 +0000 | [diff] [blame] | 167 | printf("execve(\"%s\", %p, NULL) = -1 ENOENT (%m)\n", |
| 168 | Q_FILENAME, efault); |
Dmitry V. Levin | 23168c1 | 2016-02-07 14:37:53 +0000 | [diff] [blame] | 169 | |
Dmitry V. Levin | 4ff687b | 2015-07-27 10:02:33 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |