|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
|
2
|
+#include <stdio.h>
|
|
3
|
+#include <stdlib.h>
|
|
4
|
+#include <errno.h>
|
|
5
|
+#include <stddef.h>
|
|
6
|
+#include <stdio.h>
|
|
7
|
+#include <string.h>
|
|
8
|
+#include <sys/socket.h>
|
|
9
|
+#include <sys/un.h>
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+int main(int argc, char *argv[]) {
|
|
13
|
+ if (argc != 2) {
|
|
14
|
+ fprintf(stderr, "Usage: %s <socket>\n", argv[0]);
|
|
15
|
+ return 1;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ size_t addrlen = strlen(argv[1]);
|
|
19
|
+
|
|
20
|
+ /* Allocate enough space for arbitrary-length paths */
|
|
21
|
+ char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1];
|
|
22
|
+ memset(addrbuf, 0, sizeof(addrbuf));
|
|
23
|
+
|
|
24
|
+ struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf;
|
|
25
|
+ addr->sun_family = AF_UNIX;
|
|
26
|
+ memcpy(addr->sun_path, argv[1], addrlen+1);
|
|
27
|
+
|
|
28
|
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
29
|
+ if (fd < 0) {
|
|
30
|
+ fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
|
|
31
|
+ return 1;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) {
|
|
35
|
+ fprintf(stderr, "Can't connect to `%s': %s\n", argv[1], strerror(errno));
|
|
36
|
+ return 1;
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ char buf[1024];
|
|
40
|
+ ssize_t r;
|
|
41
|
+ while (1) {
|
|
42
|
+ r = recv(fd, buf, sizeof(buf), 0);
|
|
43
|
+ if (r < 0) {
|
|
44
|
+ fprintf(stderr, "read: %s\n", strerror(errno));
|
|
45
|
+ return 1;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ if (r == 0)
|
|
49
|
+ return 0;
|
|
50
|
+
|
|
51
|
+ fwrite(buf, r, 1, stdout);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ return 0;
|
|
55
|
+}
|