-
Notifications
You must be signed in to change notification settings - Fork 1
/
pidfdgetfd.c
84 lines (76 loc) · 1.91 KB
/
pidfdgetfd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
static void
usage(void)
{
static char const message[] =
"Usage: pidfdgetfd pidfd targetfd fd cmd [args]...\n";
if (fputs(message, stderr) == EOF)
perror("fputs");
}
int
main(int const argc, char *const *const argv)
{
for (int opt; opt = getopt(argc, argv, "+"), opt != -1;) {
switch (opt) {
default:
usage();
return 2;
}
}
if (argc - optind < 4) {
usage();
return 2;
}
int pidfd, targetfd, fd;
for (int i = 0; i < 3; ++i) {
char *const str = argv[optind + i];
char *endptr;
errno = 0;
long const num = strtol(str, &endptr, 10);
if (errno) {
perror("strtol");
return 2;
}
if (endptr == str || num < 0 || num > INT_MAX || *endptr) {
if (fputs("Invalid argument.\n", stderr) == EOF)
perror("fputs");
return 2;
}
*(int *const[]){ &pidfd, &targetfd, &fd }[i] = (int)num;
}
int const gotfd = syscall(SYS_pidfd_getfd, pidfd, targetfd, 0);
if (gotfd == -1) {
perror("pidfd_getfd");
return 2;
}
if (gotfd != fd) {
int ret;
do {
ret = dup2(gotfd, fd);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
perror("dup2");
return 2;
}
} else {
int const flags = fcntl(fd, F_GETFD);
if (flags == -1) {
perror("fcntl(F_GETFD)");
return 2;
}
if ((flags & FD_CLOEXEC) &&
fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) == -1) {
perror("fcntl(F_SETFD)");
return 2;
}
}
(void)execvp(argv[optind + 3], &argv[optind + 3]);
perror("execvp");
return 2;
}