-
Notifications
You must be signed in to change notification settings - Fork 1
/
subsystem.c
119 lines (100 loc) · 2.4 KB
/
subsystem.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
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
#include "subsystem.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
/* translate_path - translate the given path and interpret the windows path
* semantics. Volumes are for simplicity hardcoded to:
*
* C -> /
* D -> home directory of the subsystem process
*/
static const char*
translate_path (const char *pathname)
{
static char buf[PATH_MAX] = { 0 };
// generate a path prefix from Volume
const char *prefix;
switch (pathname[0])
{
case 'C':
prefix = "/";
break;
case 'D':
prefix = getenv("HOME");
break;
default:
return NULL;
}
// copy prefix and path to output buffer
strcpy(buf, prefix);
strcpy(buf + strlen(prefix), pathname + 2);
// clean up forward / backward slash mess
char *p = buf;
while (*p++)
if (*p == '\\')
*p = '/';
// return output buffer
return buf;
}
/* main - start listening to a unix socket and accept connections, trying to
* answer path translation requests by clients
*/
int
main (void)
{
int s;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return -1;
}
struct sockaddr_un local;
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SUBSYSTEM_SOCKET_PATH);
unlink(SUBSYSTEM_SOCKET_PATH);
size_t len = strlen(SUBSYSTEM_SOCKET_PATH) + 1 + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1)
{
perror("bind");
return -1;
}
if (listen(s, 5) == -1)
{
perror("listen");
return -1;
}
// loop over client connections and keep answering requests
while (1)
{
struct sockaddr_un remote;
socklen_t t = sizeof(remote);
int s2;
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1)
{
perror("accept");
return -1;
}
static char buf[PATH_MAX] = { 0 };
ssize_t n;
if ((n = recv(s2, buf, PATH_MAX, 0)) == -1)
{
perror("recv");
return -1;
}
buf[n] = '\0';
printf("recv: %s\n", buf);
const char *translated = translate_path(buf);
printf("send: %s\n", translated);
if (translated && send(s2, translated, strlen(translated), 0) == -1)
{
perror("send");
return -1;
}
close(s2);
}
return 0;
}