]>
Commit | Line | Data |
---|---|---|
1eb89306 AZ |
1 | /* General tests for execpve. |
2 | Copyright (C) 2016 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <errno.h> | |
20 | #include <fcntl.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | #include <wait.h> | |
25 | ||
26 | ||
27 | /* Nonzero if the program gets called via `exec'. */ | |
28 | static int restart; | |
29 | ||
30 | ||
31 | #define CMDLINE_OPTIONS \ | |
32 | { "restart", no_argument, &restart, 1 }, | |
33 | ||
34 | /* Prototype for our test function. */ | |
35 | extern void do_prepare (int argc, char *argv[]); | |
36 | extern int do_test (int argc, char *argv[]); | |
37 | ||
38 | #include "../test-skeleton.c" | |
39 | ||
40 | #define EXECVPE_KEY "EXECVPE_ENV" | |
41 | #define EXECVPE_VALUE "execvpe_test" | |
42 | ||
43 | ||
44 | static int | |
45 | handle_restart (void) | |
46 | { | |
47 | /* First check if only one variable is passed on execvpe. */ | |
48 | int env_count = 0; | |
49 | for (char **e = environ; *e != NULL; ++e) | |
50 | if (++env_count == INT_MAX) | |
51 | { | |
52 | printf ("Environment variable number overflow"); | |
53 | exit (EXIT_FAILURE); | |
54 | } | |
55 | if (env_count != 1) | |
56 | { | |
57 | printf ("Wrong number of environment variables"); | |
58 | exit (EXIT_FAILURE); | |
59 | } | |
60 | ||
61 | /* Check if the combinarion os "EXECVPE_ENV=execvpe_test" */ | |
62 | const char *env = getenv (EXECVPE_KEY); | |
63 | if (env == NULL) | |
64 | { | |
65 | printf ("Test environment variable not found"); | |
66 | exit (EXIT_FAILURE); | |
67 | } | |
68 | ||
69 | if (strncmp (env, EXECVPE_VALUE, sizeof (EXECVPE_VALUE))) | |
70 | { | |
71 | printf ("Test environment variable with wrong value"); | |
72 | exit (EXIT_FAILURE); | |
73 | } | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | ||
79 | int | |
80 | do_test (int argc, char *argv[]) | |
81 | { | |
82 | pid_t pid; | |
83 | int status; | |
84 | ||
85 | /* We must have | |
86 | - one or four parameters left if called initially | |
87 | + path for ld.so optional | |
88 | + "--library-path" optional | |
89 | + the library path optional | |
90 | + the application name | |
91 | */ | |
92 | ||
93 | if (restart) | |
94 | { | |
95 | if (argc != 1) | |
96 | { | |
97 | printf ("Wrong number of arguments (%d)\n", argc); | |
98 | exit (EXIT_FAILURE); | |
99 | } | |
100 | ||
101 | return handle_restart (); | |
102 | } | |
103 | ||
104 | if (argc != 2 && argc != 5) | |
105 | { | |
106 | printf ("Wrong number of arguments (%d)\n", argc); | |
107 | exit (EXIT_FAILURE); | |
108 | } | |
109 | ||
110 | /* We want to test the `execvpe' function. To do this we restart the | |
111 | program with an additional parameter. */ | |
112 | pid = fork (); | |
113 | if (pid == 0) | |
114 | { | |
115 | /* This is the child. Construct the command line. */ | |
116 | ||
117 | /* We cast here to char* because the test itself does not modify neither | |
118 | the argument nor the environment list. */ | |
119 | char *envs[] = { (char*)(EXECVPE_KEY "=" EXECVPE_VALUE), NULL }; | |
120 | if (argc == 5) | |
121 | { | |
122 | char *args[] = { argv[1], argv[2], argv[3], argv[4], | |
123 | (char *) "--direct", (char *) "--restart", NULL }; | |
124 | execvpe (args[0], args, envs); | |
125 | } | |
126 | else | |
127 | { | |
128 | char *args[] = { argv[1], argv[1], | |
129 | (char *) "--direct", (char *) "--restart", NULL }; | |
130 | execvpe (args[0], args, envs); | |
131 | } | |
132 | ||
133 | puts ("Cannot exec"); | |
134 | exit (EXIT_FAILURE); | |
135 | } | |
136 | else if (pid == (pid_t) -1) | |
137 | { | |
138 | puts ("Cannot fork"); | |
139 | return 1; | |
140 | } | |
141 | ||
142 | /* Wait for the child. */ | |
143 | if (waitpid (pid, &status, 0) != pid) | |
144 | { | |
145 | puts ("Wrong child"); | |
146 | return 1; | |
147 | } | |
148 | ||
149 | if (WTERMSIG (status) != 0) | |
150 | { | |
151 | puts ("Child terminated incorrectly"); | |
152 | return 1; | |
153 | } | |
154 | status = WEXITSTATUS (status); | |
155 | ||
156 | return status; | |
157 | } |