S390: Optimize utf8-utf16 module.
[glibc.git] / posix / tst-spawn2.c
blob149f3d5cfad4041be0af3048251acc00554421a4
1 /* Further tests for spawn in case of invalid binary paths.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
27 #include <stdio.h>
29 int
30 posix_spawn_test (void)
32 /* Check if posix_spawn correctly returns an error and an invalid pid
33 by trying to spawn an invalid binary. */
35 const char *program = "/path/to/invalid/binary";
36 char * const args[] = { 0 };
37 pid_t pid = -1;
39 int ret = posix_spawn (&pid, program, 0, 0, args, environ);
40 if (ret != ENOENT)
41 error (EXIT_FAILURE, errno, "posix_spawn");
43 /* POSIX states the value returned on pid variable in case of an error
44 is not specified. GLIBC will update the value iff the child
45 execution is successful. */
46 if (pid != -1)
47 error (EXIT_FAILURE, errno, "posix_spawn returned pid != -1");
49 /* Check if no child is actually created. */
50 ret = waitpid (-1, NULL, 0);
51 if (ret != -1 || errno != ECHILD)
52 error (EXIT_FAILURE, errno, "waitpid");
54 /* Same as before, but with posix_spawnp. */
55 char *args2[] = { (char*) program, 0 };
57 ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
58 if (ret != ENOENT)
59 error (EXIT_FAILURE, errno, "posix_spawnp");
61 if (pid != -1)
62 error (EXIT_FAILURE, errno, "posix_spawnp returned pid != -1");
64 ret = waitpid (-1, NULL, 0);
65 if (ret != -1 || errno != ECHILD)
66 error (EXIT_FAILURE, errno, "waitpid");
68 return 0;
71 #define TEST_FUNCTION posix_spawn_test ()
72 #include "../test-skeleton.c"