1 /*
2 * Copyright (c) 2015-2021 Dmitry V. Levin <ldv@strace.io>
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "tests.h"
9 #include <assert.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/wait.h>
14
15 static int
16 logit_(const char *const str)
17 {
18 return !chdir(str);
19 }
20
21 #define prefix "fork-f."
22 #define logit(arg) logit_(prefix arg)
23
24 int main(int ac, char **av)
25 {
26 if (ac < 1)
27 return 1;
28 if (ac > 1)
29 return logit("exec");
30
31 logit("start");
32
33 int child_wait_fds[2];
34 (void) close(0);
35 if (pipe(child_wait_fds))
36 perror_msg_and_fail("pipe");
37
38 pid_t pid = fork();
39
40 if (pid < 0)
41 perror_msg_and_fail("fork");
42
43 if (!pid) {
44 close(child_wait_fds[1]);
45
46 if (read(0, child_wait_fds, sizeof(int)))
47 _exit(2);
48
49 char *const args[] = { av[0], (char *) "", NULL };
50 if (logit("child") || execve(args[0], args, args + 1))
51 _exit(2);
52 }
53
54 close(0);
55
56 logit("parent");
57 close(child_wait_fds[1]);
58
59 int status;
60 assert(wait(&status) == pid);
61 assert(status == 0);
62
63 pid_t ppid = getpid();
64 logit("finish");
65
66 printf("%-5d chdir(\"%sstart\") = -1 ENOENT (%m)\n"
67 "%-5d chdir(\"%sparent\") = -1 ENOENT (%m)\n"
68 "%-5d chdir(\"%schild\") = -1 ENOENT (%m)\n"
69 "%-5d chdir(\"%sexec\") = -1 ENOENT (%m)\n"
70 "%-5d chdir(\"%sfinish\") = -1 ENOENT (%m)\n",
71 ppid, prefix,
72 ppid, prefix,
73 pid, prefix,
74 pid, prefix,
75 ppid, prefix);
76 return 0;
77 }