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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
USAGE("[-t decisecs] [-a] ([-f fd] path [command ...] | path)")
static int
erecv_fd(int sock)
{
int fd;
char buf[1];
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
char cms[CMSG_SPACE(sizeof(fd))];
iov.iov_base = buf;
iov.iov_len = 1;
memset(&msg, 0, sizeof(msg));
msg.msg_name = 0;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (caddr_t)cms;
msg.msg_controllen = sizeof(cms);
switch (recvmsg(sock, &msg, 0)) {
case -1:
eprintf("recvmsg:");
case 0:
eprintf("recvmsg: connection closed by peer");
default:
break;
}
cmsg = CMSG_FIRSTHDR(&msg);
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
return fd;
}
#if !defined(HAVE_SENDFILE)
# if !defined(PIPE_BUF)
# define PIPE_BUF BUFSIZ
# endif
static ssize_t
sendfile(int outfd, int infd, off_t *offset, size_t count)
{
char buf[PIPE_BUF];
ssize_t r, w, p, ret = 0;
(void) offset;
(void) count;
for (;;) {
r = read(infd, buf, sizeof(buf));
if (r < 0)
eprintf("read <received file>:");
if (!r)
break;
ret += r;
for (p = 0; p < r; p += w) {
w = write(outfd, buf + p, r - p);
if (w < 0)
eprintf("write <stdout>:");
}
}
return ret;
}
#endif
int
main(int argc, char *argv[])
{
struct sockaddr_un addr;
int abstract = 0;
int filedes = -1;
int tries = 11;
int sockfd, fd;
ARGBEGIN {
case 'a':
abstract = 1;
break;
case 'f':
filedes = etoi_flag('f', UARGF(), 0, INT_MAX);
break;
case 't':
tries = etoi_flag('t', UARGF(), 0, INT_MAX - 1) + 1;
break;
default:
usage();
} ARGEND;
if (argc < 1 || (filedes >= 0 && argc == 1))
usage();
if (filedes < 0)
filedes = STDIN_FILENO;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if (strlen(argv[0]) + 1 + abstract > sizeof(addr.sun_path)) {
errno = ENAMETOOLONG;
eprintf("%s:", argv[0]);
}
strcpy(addr.sun_path + abstract, argv[0]);
argv++, argc--;
sockfd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
if (sockfd < 0)
eprintf("socket PF_UNIX SOCK_SEQPACKET:");
retry:
if ((connect(sockfd, (const struct sockaddr *)&addr, (size_t)sizeof(addr))) < 0) {
if (--tries) {
usleep((useconds_t)100000L);
goto retry;
} else {
eprintf("bind %s%s%s:",
abstract ? "<abstract:" : "",
addr.sun_path + abstract,
abstract ? ">" : "");
}
}
fd = erecv_fd(sockfd);
close(sockfd);
if (argc) {
if (fd != filedes) {
if (dup2(fd, filedes) < 0)
eprintf("dup2 %i %i:", fd, filedes);
close(fd);
}
execvp(argv[0], argv);
eprintf("execvp %s:", argv[0]);
}
for (;;) {
switch (sendfile(STDOUT_FILENO, fd, NULL, SIZE_MAX)) {
case 0:
return 0;
case -1:
eprintf("sendfile <stdout> <received file>:");
default:
break;
}
}
}
|