]> granicus.if.org Git - strace/blob - tests/redirect-fds.c
f9afa9651bcb8d3cf7d01c10e7f6a5cbce7d62e4
[strace] / tests / redirect-fds.c
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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 <unistd.h>
11 #include <sys/stat.h>
12
13 #define N_FDS 3
14
15 /*
16  * Do not print any messages, indicate errors with return codes.
17  */
18 static int
19 check_fd(int fd, const char *fname)
20 {
21         const int should_be_closed = (fname[0] == '\0');
22
23         struct stat st_fd, st_fn;
24
25         if (fstat(fd, &st_fd)) {
26                 if (!should_be_closed)
27                         return 10 + fd;
28         } else {
29                 if (should_be_closed)
30                         return 20 + fd;
31
32                 if (stat(fname, &st_fn))
33                         return 30 + fd;
34
35                 if (st_fd.st_dev != st_fn.st_dev
36                     || st_fd.st_ino != st_fn.st_ino)
37                         return 40 + fd;
38         }
39
40         return 0;
41 }
42
43 int
44 main(int ac, char **av)
45 {
46         assert(ac == 1 + N_FDS);
47
48         int rc = 0, fd;
49         for (fd = 1; fd < 1 + N_FDS; ++fd)
50                 if ((rc = check_fd(fd - 1, av[fd])))
51                         break;
52
53         return rc;
54 }