]>
Commit | Line | Data |
---|---|---|
9ff72da4 | 1 | /* Further tests for spawn in case of invalid binary paths. |
04277e02 | 2 | Copyright (C) 2016-2019 Free Software Foundation, Inc. |
9ff72da4 AZ |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
9ff72da4 AZ |
18 | |
19 | #include <spawn.h> | |
20 | #include <error.h> | |
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <sys/wait.h> | |
9ff72da4 AZ |
26 | #include <stdio.h> |
27 | ||
1a920d9c AZ |
28 | #include <support/check.h> |
29 | ||
9ff72da4 | 30 | int |
1a920d9c | 31 | do_test (void) |
9ff72da4 AZ |
32 | { |
33 | /* Check if posix_spawn correctly returns an error and an invalid pid | |
34 | by trying to spawn an invalid binary. */ | |
35 | ||
36 | const char *program = "/path/to/invalid/binary"; | |
37 | char * const args[] = { 0 }; | |
38 | pid_t pid = -1; | |
39 | ||
40 | int ret = posix_spawn (&pid, program, 0, 0, args, environ); | |
41 | if (ret != ENOENT) | |
1a920d9c AZ |
42 | { |
43 | errno = ret; | |
44 | FAIL_EXIT1 ("posix_spawn: %m"); | |
45 | } | |
9ff72da4 AZ |
46 | |
47 | /* POSIX states the value returned on pid variable in case of an error | |
48 | is not specified. GLIBC will update the value iff the child | |
49 | execution is successful. */ | |
50 | if (pid != -1) | |
1a920d9c | 51 | FAIL_EXIT1 ("posix_spawn returned pid != -1 (%i)", (int) pid); |
9ff72da4 AZ |
52 | |
53 | /* Check if no child is actually created. */ | |
54 | ret = waitpid (-1, NULL, 0); | |
55 | if (ret != -1 || errno != ECHILD) | |
1a920d9c | 56 | FAIL_EXIT1 ("waitpid: %m)"); |
9ff72da4 AZ |
57 | |
58 | /* Same as before, but with posix_spawnp. */ | |
59 | char *args2[] = { (char*) program, 0 }; | |
60 | ||
61 | ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ); | |
62 | if (ret != ENOENT) | |
1a920d9c AZ |
63 | { |
64 | errno = ret; | |
65 | FAIL_EXIT1 ("posix_spawnp: %m"); | |
66 | } | |
9ff72da4 AZ |
67 | |
68 | if (pid != -1) | |
1a920d9c | 69 | FAIL_EXIT1 ("posix_spawnp returned pid != -1 (%i)", (int) pid); |
9ff72da4 AZ |
70 | |
71 | ret = waitpid (-1, NULL, 0); | |
72 | if (ret != -1 || errno != ECHILD) | |
1a920d9c | 73 | FAIL_EXIT1 ("waitpid: %m)"); |
9ff72da4 AZ |
74 | |
75 | return 0; | |
76 | } | |
77 | ||
1a920d9c | 78 | #include <support/test-driver.c> |