-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathconn_sock.c
478 lines (404 loc) · 15.3 KB
/
conn_sock.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#define _GNU_SOURCE
#include "conn_sock.h"
#include "globals.h"
#include "utils.h"
#include "config.h"
#include "cli.h" // opt_stdin
#include <stdbool.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/stat.h>
static gboolean attach_cb(int fd, G_GNUC_UNUSED GIOCondition condition, gpointer user_data);
static gboolean remote_sock_cb(int fd, GIOCondition condition, gpointer user_data);
static void init_remote_sock(struct remote_sock_s *sock, struct remote_sock_s *src);
static gboolean read_remote_sock(struct remote_sock_s *sock);
static gboolean terminate_remote_sock(struct remote_sock_s *sock);
static void remote_sock_shutdown(struct remote_sock_s *sock, int how);
static void schedule_local_sock_write(struct local_sock_s *local_sock);
static void sock_try_write_to_local_sock(struct remote_sock_s *sock);
static gboolean local_sock_write_cb(G_GNUC_UNUSED int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data);
static char *bind_unix_socket(char *socket_relative_name, int sock_type, mode_t perms, struct remote_sock_s *remote_sock,
gboolean use_full_attach_path);
static char *socket_parent_dir(gboolean use_full_attach_path, size_t desired_len);
/*
Since our socket handling is abstract now, handling is based on sock_type, so we can pass around a structure
that contains everything we need to handle I/O. Callbacks used to handle IO, for example, and whether this
can be read from or written to or both, and the buffers used for the communication.
*/
/*
This defines the Container STDIN, attaches it to the correct FD and sets the flags for handling I/O.
setup_attach_socket() is responsible for setting the correct remote FD and pushing it onto the queue.
*/
static struct local_sock_s local_mainfd_stdin = {&mainfd_stdin, true, NULL, "container stdin", NULL};
struct remote_sock_s remote_attach_sock = {
SOCK_TYPE_CONSOLE, /* sock_type */
-1, /* fd */
&local_mainfd_stdin, /* dest */
true, /* listening */
false, /* data_ready */
true, /* readable */
true, /* writable */
0, /* remaining */
0, /* off */
{0} /* buf */
};
/*
This defines the Container SDNotify socket, attaches it to the correct FD and sets the flags for handling I/O.
setup_notify_socket() is responsible for initializing the unix sockets and pushing it onto the queue.
If the local_notify_host_fd stays -1 (i.e. we have not requested SD-NOTIFY) then setup was never run and
this has no effect.
*/
static int local_notify_host_fd = -1;
static struct sockaddr_un local_notify_host_addr = {0};
static struct local_sock_s local_notify_host = {&local_notify_host_fd, false, NULL, "host notify socket", &local_notify_host_addr};
struct remote_sock_s remote_notify_sock = {
SOCK_TYPE_NOTIFY, /* sock_type */
-1, /* fd */
&local_notify_host, /* dest */
false, /* listening */
false, /* data_ready */
true, /* readable */
false, /* writable */
0, /* remaining */
0, /* off */
{0} /* buf */
};
/* External */
char *setup_console_socket(void)
{
struct sockaddr_un addr = {0};
_cleanup_free_ const char *tmpdir = g_get_tmp_dir();
char *csname = g_build_filename(tmpdir, "conmon-term.XXXXXX", NULL);
/*
* Generate a temporary name. Is this unsafe? Probably, but we can
* replace it with a rename(2) setup if necessary.
*/
int unusedfd = g_mkstemp(csname);
if (unusedfd < 0)
pexit("Failed to generate random path for console-socket");
close(unusedfd);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, csname, sizeof(addr.sun_path) - 1);
ninfof("addr{sun_family=AF_UNIX, sun_path=%s}", addr.sun_path);
/* Bind to the console socket path. */
console_socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (console_socket_fd < 0)
pexit("Failed to create console-socket");
if (fchmod(console_socket_fd, 0700))
pexit("Failed to change console-socket permissions");
/* XXX: This should be handled with a rename(2). */
if (unlink(csname) < 0)
pexit("Failed to unlink temporary random path");
if (bind(console_socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
pexit("Failed to bind to console-socket");
if (listen(console_socket_fd, 128) < 0)
pexit("Failed to listen on console-socket");
return csname;
}
char *setup_attach_socket(void)
{
char *symlink_dir_path =
bind_unix_socket("attach", SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0700, &remote_attach_sock, opt_full_attach_path);
if (listen(remote_attach_sock.fd, 10) == -1)
pexitf("Failed to listen on attach socket: %s/%s", symlink_dir_path, "attach");
g_unix_fd_add(remote_attach_sock.fd, G_IO_IN, attach_cb, &remote_attach_sock);
return symlink_dir_path;
}
void setup_notify_socket(char *socket_path)
{
/* Connect to Host socket */
if (local_notify_host_fd < 0) {
local_notify_host_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (local_notify_host_fd == -1) {
pexit("Failed to create notify socket");
}
local_notify_host_addr.sun_family = AF_UNIX;
strncpy(local_notify_host_addr.sun_path, socket_path, sizeof(local_notify_host_addr.sun_path) - 1);
}
/* No _cleanup_free_ here so we don't get a warning about unused variables
* when compiling with clang */
char *symlink_dir_path =
bind_unix_socket("notify/notify.sock", SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0777, &remote_notify_sock, TRUE);
g_unix_fd_add(remote_notify_sock.fd, G_IO_IN | G_IO_HUP | G_IO_ERR, remote_sock_cb, &remote_notify_sock);
g_free(symlink_dir_path);
}
/* REMEMBER to g_free() the return value! */
static char *bind_unix_socket(char *socket_relative_name, int sock_type, mode_t perms, struct remote_sock_s *remote_sock,
gboolean use_full_attach_path)
{
int socket_fd = -1;
struct sockaddr_un socket_addr = {0};
socket_addr.sun_family = AF_UNIX;
/* get the parent_dir of the socket. We'll use this to get the location of the socket. */
char *parent_dir = socket_parent_dir(use_full_attach_path, sizeof(socket_addr.sun_path));
/*
* To be able to access the location of the attach socket, without first creating the attach socket
* but also be able to handle arbitrary length paths, we open the parent dir (base_path), and then use
* the corresponding entry in `/proc/self/fd` to act as the path to base_path, then we use the socket_relative_name
* to actually refer to the file where the socket will be created below.
*/
_cleanup_close_ int parent_dir_fd = open(parent_dir, O_PATH);
if (parent_dir_fd < 0)
pexitf("failed to open socket path parent dir %s", parent_dir);
_cleanup_free_ char *sock_proc_entry = g_strdup_printf("/proc/self/fd/%d/%s", parent_dir_fd, socket_relative_name);
strncpy(socket_addr.sun_path, sock_proc_entry, sizeof(socket_addr.sun_path) - 1);
ninfof("addr{sun_family=AF_UNIX, sun_path=%s}", socket_addr.sun_path);
/*
* We use the fullpath for operations that aren't as limited in length as socket_addr.sun_path
* Cleanup of this variable is up to the caller
*/
char *sock_fullpath = g_build_filename(parent_dir, socket_relative_name, NULL);
/*
* We make the socket non-blocking to avoid a race where client aborts connection
* before the server gets a chance to call accept. In that scenario, the server
* accept blocks till a new client connection comes in.
*/
socket_fd = socket(AF_UNIX, sock_type, 0);
if (socket_fd == -1)
pexitf("Failed to create socket %s", sock_fullpath);
if (fchmod(socket_fd, perms))
pexitf("Failed to change socket permissions %s", sock_fullpath);
if (unlink(sock_fullpath) == -1 && errno != ENOENT)
pexitf("Failed to remove existing socket: %s", sock_fullpath);
if (bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_un)) == -1)
pexitf("Failed to bind socket: %s", sock_fullpath);
if (chmod(sock_fullpath, perms))
pexitf("Failed to change socket permissions %s", sock_fullpath);
remote_sock->fd = socket_fd;
return sock_fullpath;
}
/*
* socket_parent_dir decides whether to truncate the socket path, to match
* the caller's expectation.
* use_full_attach_path is whether conmon was told to not truncate the path.
* base_path is the path of the socket
* desired_len is the length of socket_addr.sun_path (should be strlen(char[108]) on linux).
*/
char *socket_parent_dir(gboolean use_full_attach_path, size_t desired_len)
{
/* if we're to use the full path, ignore the socket path and only use the bundle_path */
if (use_full_attach_path)
return opt_bundle_path;
char *base_path = g_build_filename(opt_socket_path, opt_cuuid, NULL);
/*
* This is to address a corner case where the symlink path length can end up being
* the same as the socket. When it happens, the symlink prevents the socket from being
* be created. This could still be a problem with other containers, but it is safe
* to assume the CUUIDs don't change length in the same directory. As a workaround,
* in such case, make the symlink one char shorter.
*
* If we're using using_full_attach_path, this is unnecessary.
*/
if (strlen(base_path) == (desired_len - 1))
base_path[desired_len - 2] = '\0';
/*
* Create a symlink so we don't exceed unix domain socket
* path length limit. We use the base path passed in from our parent.
*/
if (unlink(base_path) == -1 && errno != ENOENT)
pexitf("Failed to remove existing symlink for socket directory %s", base_path);
if (symlink(opt_bundle_path, base_path) == -1)
pexit("Failed to create symlink for notify socket");
return base_path;
}
void schedule_main_stdin_write()
{
schedule_local_sock_write(&local_mainfd_stdin);
}
void write_back_to_remote_consoles(char *buf, int len)
{
if (local_mainfd_stdin.readers == NULL)
return;
for (int i = local_mainfd_stdin.readers->len; i > 0; i--) {
struct remote_sock_s *remote_sock = g_ptr_array_index(local_mainfd_stdin.readers, i - 1);
if (remote_sock->writable && write_all(remote_sock->fd, buf, len) < 0) {
nwarn("Failed to write to remote console socket");
remote_sock_shutdown(remote_sock, SHUT_WR);
}
}
}
/* Internal */
static gboolean attach_cb(int fd, G_GNUC_UNUSED GIOCondition condition, gpointer user_data)
{
struct remote_sock_s *srcsock = (struct remote_sock_s *)user_data;
int new_fd = accept(fd, NULL, NULL);
if (new_fd == -1) {
if (errno != EWOULDBLOCK)
nwarn("Failed to accept client connection on attach socket");
} else {
struct remote_sock_s *remote_sock;
if (srcsock->dest->readers == NULL) {
srcsock->dest->readers = g_ptr_array_new_with_free_func(free);
}
remote_sock = malloc(sizeof(*remote_sock));
if (remote_sock == NULL) {
pexit("Failed to allocate memory");
}
init_remote_sock(remote_sock, srcsock);
g_unix_set_fd_nonblocking(new_fd, TRUE, NULL);
remote_sock->fd = new_fd;
g_unix_fd_add(remote_sock->fd, G_IO_IN | G_IO_HUP | G_IO_ERR, remote_sock_cb, remote_sock);
g_ptr_array_add(remote_sock->dest->readers, remote_sock);
ninfof("Accepted%s connection %d", SOCK_IS_CONSOLE(srcsock->sock_type) ? " console" : "", remote_sock->fd);
}
return G_SOURCE_CONTINUE;
}
static gboolean remote_sock_cb(G_GNUC_UNUSED int fd, GIOCondition condition, gpointer user_data)
{
struct remote_sock_s *sock = (struct remote_sock_s *)user_data;
if (condition & G_IO_IN)
return read_remote_sock(sock);
return terminate_remote_sock(sock);
}
static gboolean read_remote_sock(struct remote_sock_s *sock)
{
ssize_t num_read;
/* There is still data in the buffer. */
if (sock->remaining) {
sock->data_ready = true;
return G_SOURCE_REMOVE;
}
if (SOCK_IS_STREAM(sock->sock_type)) {
num_read = read(sock->fd, sock->buf, CONN_SOCK_BUF_SIZE);
} else {
num_read = recvfrom(sock->fd, sock->buf, CONN_SOCK_BUF_SIZE - 1, 0, NULL, NULL);
}
if (num_read < 0)
return G_SOURCE_CONTINUE;
if (num_read == 0)
return terminate_remote_sock(sock);
/* num_read > 0 */
sock->remaining = num_read;
sock->off = 0;
if (SOCK_IS_NOTIFY(sock->sock_type)) {
/* Do what OCI runtime does - only pass READY=1 */
sock->buf[num_read] = '\0';
if (strstr(sock->buf, "READY=1")) {
strncpy(sock->buf, "READY=1", 8);
sock->remaining = 7;
} else {
sock->remaining = 0;
}
}
if (sock->remaining)
sock_try_write_to_local_sock(sock);
/* Not everything was written, let's wait for the fd to be ready. */
if (sock->remaining)
schedule_local_sock_write(sock->dest);
return G_SOURCE_CONTINUE;
}
static gboolean terminate_remote_sock(struct remote_sock_s *sock)
{
remote_sock_shutdown(sock, SHUT_RD);
if (SOCK_IS_CONSOLE(sock->sock_type)) {
// If we're terminating our STDIN holder, we need to close the FD too, based on the cmdline option
if (*(sock->dest->fd) >= 0 && opt_stdin) {
if (!opt_leave_stdin_open) {
close(*(sock->dest->fd));
*(sock->dest->fd) = -1;
} else {
ninfo("Not closing input");
}
}
}
return G_SOURCE_REMOVE;
}
static void remote_sock_shutdown(struct remote_sock_s *sock, int how)
{
if (sock->fd == -1)
return;
shutdown(sock->fd, how);
switch (how) {
case SHUT_RD:
sock->readable = false;
break;
case SHUT_WR:
sock->writable = false;
break;
case SHUT_RDWR:
sock->readable = false;
sock->writable = false;
break;
}
if (!sock->writable && !sock->readable) {
ndebugf("Closing %d", sock->fd);
close(sock->fd);
sock->fd = -1;
if (sock->dest->readers != NULL) {
g_ptr_array_remove(sock->dest->readers, sock);
}
}
}
static void write_to_local_sock(gpointer data, gpointer user_data)
{
struct remote_sock_s *sock = (struct remote_sock_s *)data;
bool *has_data = user_data;
sock_try_write_to_local_sock(sock);
if (sock->remaining)
*has_data = true;
else if (sock->data_ready) {
sock->data_ready = false;
g_unix_fd_add(sock->fd, G_IO_IN | G_IO_HUP | G_IO_ERR, remote_sock_cb, sock);
}
}
static void sock_try_write_to_local_sock(struct remote_sock_s *sock)
{
struct local_sock_s *local_sock = sock->dest;
ssize_t w = 0;
if (!sock->remaining || *(local_sock->fd) < 0)
return;
if (local_sock->is_stream) {
w = write(*(local_sock->fd), sock->buf + sock->off, sock->remaining);
} else {
w = sendto(*(local_sock->fd), sock->buf + sock->off, sock->remaining, MSG_DONTWAIT | MSG_NOSIGNAL,
(struct sockaddr *)local_sock->addr, sizeof(*(local_sock->addr)));
}
if (w < 0) {
nwarnf("Failed to write %s", local_sock->label);
} else {
sock->off += w;
sock->remaining -= w;
}
}
static gboolean local_sock_write_cb(G_GNUC_UNUSED int fd, G_GNUC_UNUSED GIOCondition condition, gpointer user_data)
{
struct local_sock_s *local_sock = (struct local_sock_s *)user_data;
bool has_data = FALSE;
if (*(local_sock->fd) < 0)
return G_SOURCE_REMOVE;
g_ptr_array_foreach(local_sock->readers, write_to_local_sock, &has_data);
if (has_data)
return G_SOURCE_CONTINUE;
return G_SOURCE_REMOVE;
}
static void schedule_local_sock_write(struct local_sock_s *local_sock)
{
if (*(local_sock->fd) < 0)
return;
g_unix_fd_add(*(local_sock->fd), G_IO_OUT, local_sock_write_cb, local_sock);
}
static void init_remote_sock(struct remote_sock_s *sock, struct remote_sock_s *src)
{
sock->off = 0;
sock->remaining = 0;
sock->data_ready = false;
sock->listening = false;
if (src) {
sock->readable = src->readable;
sock->writable = src->writable;
sock->dest = src->dest;
g_unix_set_fd_nonblocking(*sock->dest->fd, TRUE, NULL);
sock->sock_type = src->sock_type;
}
}
static void close_sock(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct remote_sock_s *sock = (struct remote_sock_s *)data;
close(sock->fd);
sock->fd = -1;
}
void close_all_readers()
{
g_ptr_array_foreach(local_mainfd_stdin.readers, close_sock, NULL);
}