linux: Return unsupported if procfs can not be mount on tst-ttyname-namespace
[glibc.git] / posix / tst-execvpe6.c
blob3da62c7ef638952227f4dfffcf37c4dec16228ac
1 /* Check execvpe script argument handling.
2 Copyright (C) 2016-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/param.h>
26 static char *fname1;
27 static char *fname2;
28 static char *logname;
30 static void do_prepare (void);
31 #define PREPARE(argc, argv) do_prepare ()
32 static int do_test (void);
33 #define TEST_FUNCTION do_test ()
35 #include "../test-skeleton.c"
37 static void
38 do_prepare (void)
40 int logfd = create_temp_file ("logfile", &logname);
41 close (logfd);
43 int fd1 = create_temp_file ("testscript", &fname1);
44 dprintf (fd1, "echo foo $1 $2 $3 > %s\n", logname);
45 fchmod (fd1, 0700);
46 close (fd1);
48 int fd2 = create_temp_file ("testscript", &fname2);
49 dprintf (fd2, "echo foo > %s\n", logname);
50 fchmod (fd2, 0700);
51 close (fd2);
54 static int
55 run_script (const char *fname, char *args[])
57 /* We want to test the `execvpe' function. To do this we restart the
58 program with an additional parameter. */
59 int status;
60 pid_t pid = fork ();
61 if (pid == 0)
63 execvpe (fname, args, NULL);
65 puts ("Cannot exec");
66 exit (EXIT_FAILURE);
68 else if (pid == (pid_t) -1)
70 puts ("Cannot fork");
71 return 1;
74 /* Wait for the child. */
75 if (waitpid (pid, &status, 0) != pid)
77 puts ("Wrong child");
78 return 1;
81 if (WTERMSIG (status) != 0)
83 puts ("Child terminated incorrectly");
84 return 1;
87 return 0;
90 static int
91 check_output (const char *expected)
93 /* Check log output. */
94 FILE *arq = fopen (logname, "r");
95 if (arq == NULL)
97 puts ("Error opening output file");
98 return 1;
101 char line[128];
102 if (fgets (line, sizeof (line), arq) == NULL)
104 puts ("Error reading output file");
105 return 1;
107 fclose (arq);
109 if (strcmp (line, expected) != 0)
111 puts ("Output file different than expected");
112 return 1;
115 return 0;
118 static int
119 do_test (void)
121 if (setenv ("PATH", test_dir, 1) != 0)
123 puts ("setenv failed");
124 return 1;
127 /* First check resulting script run with some arguments results in correct
128 output file. */
129 char *args1[] = { fname1, (char*) "1", (char *) "2", (char *) "3", NULL };
130 if (run_script (fname1,args1))
131 return 1;
132 if (check_output ("foo 1 2 3\n"))
133 return 1;
135 /* Same as before but with an expected empty argument list. */
136 char *args2[] = { fname2, NULL };
137 if (run_script (fname2, args2))
138 return 1;
139 if (check_output ("foo\n"))
140 return 1;
142 /* Same as before but with an empty argument list. */
143 char *args3[] = { NULL };
144 if (run_script (fname2, args3))
145 return 1;
146 if (check_output ("foo\n"))
147 return 1;
149 return 0;