-
Notifications
You must be signed in to change notification settings - Fork 164
/
conn-socket.c
519 lines (438 loc) · 13.1 KB
/
conn-socket.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* libguestfs
* Copyright (C) 2013-2023 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This file handles connections to the child process where this is
* done over regular POSIX sockets.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/socket.h> /* accept4 */
#include <sys/types.h>
#include <assert.h>
#include <libintl.h>
#include "ignore-value.h"
#include "nonblocking.h"
#include "guestfs.h"
#include "guestfs-internal.h"
struct connection_socket {
const struct connection_ops *ops;
int console_sock; /* Appliance console (for debug info). */
int daemon_sock; /* Daemon communications socket. */
/* Socket for accepting a connection from the daemon. Only used
* before and during accept_connection.
*/
int daemon_accept_sock;
};
static int handle_log_message (guestfs_h *g, struct connection_socket *conn);
static int
accept_connection (guestfs_h *g, struct connection *connv)
{
struct connection_socket *conn = (struct connection_socket *) connv;
int sock = -1;
time_t start_t, now_t;
int timeout_ms;
time (&start_t);
if (conn->daemon_accept_sock == -1) {
error (g, _("accept_connection called twice"));
return -1;
}
while (sock == -1) {
struct pollfd fds[2];
nfds_t nfds = 1;
int r;
fds[0].fd = conn->daemon_accept_sock;
fds[0].events = POLLIN;
fds[0].revents = 0;
if (conn->console_sock >= 0) {
fds[1].fd = conn->console_sock;
fds[1].events = POLLIN;
fds[1].revents = 0;
nfds++;
}
time (&now_t);
timeout_ms = 1000 * (APPLIANCE_TIMEOUT - (now_t - start_t));
r = poll (fds, nfds, timeout_ms);
if (r == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
perrorf (g, "accept_connection: poll");
return -1;
}
if (r == 0) { /* timeout reached */
guestfs_int_launch_timeout (g);
return -1;
}
/* Log message? */
if (nfds > 1 && (fds[1].revents & POLLIN) != 0) {
r = handle_log_message (g, conn);
if (r <= 0)
return r;
}
/* Accept on socket? */
if ((fds[0].revents & POLLIN) != 0) {
sock = accept4 (conn->daemon_accept_sock, NULL, NULL, SOCK_CLOEXEC);
if (sock == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
perrorf (g, "accept_connection: accept");
return -1;
}
}
}
/* Got a connection and accepted it, so update the connection's
* internal status.
*/
close (conn->daemon_accept_sock);
conn->daemon_accept_sock = -1;
conn->daemon_sock = sock;
/* Make sure the new socket is non-blocking. */
if (set_nonblocking_flag (conn->daemon_sock, 1) == -1) {
perrorf (g, "accept_connection: set_nonblocking_flag");
return -1;
}
return 1;
}
static ssize_t
read_data (guestfs_h *g, struct connection *connv, void *bufv, size_t len)
{
char *buf = bufv;
struct connection_socket *conn = (struct connection_socket *) connv;
const size_t original_len = len;
if (conn->daemon_sock == -1) {
error (g, _("read_data: socket not connected"));
return -1;
}
while (len > 0) {
struct pollfd fds[2];
nfds_t nfds = 1;
int r;
fds[0].fd = conn->daemon_sock;
fds[0].events = POLLIN;
fds[0].revents = 0;
if (conn->console_sock >= 0) {
fds[1].fd = conn->console_sock;
fds[1].events = POLLIN;
fds[1].revents = 0;
nfds++;
}
r = poll (fds, nfds, -1);
if (r == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
perrorf (g, "read_data: poll");
return -1;
}
/* Log message? */
if (nfds > 1 && (fds[1].revents & POLLIN) != 0) {
r = handle_log_message (g, conn);
if (r <= 0)
return r;
}
/* Read data on daemon socket? */
if ((fds[0].revents & POLLIN) != 0) {
ssize_t n = read (conn->daemon_sock, buf, len);
if (n == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
if (errno == ECONNRESET) /* essentially the same as EOF case */
goto closed;
perrorf (g, "read_data: read");
return -1;
}
if (n == 0) {
closed:
/* Even though qemu has gone away, there could be more log
* messages in the console socket buffer in the kernel. Read
* them out here.
*/
if (g->verbose && conn->console_sock >= 0) {
while (handle_log_message (g, conn) == 1)
;
}
return 0;
}
buf += n;
len -= n;
}
}
return original_len;
}
static int
can_read_data (guestfs_h *g, struct connection *connv)
{
struct connection_socket *conn = (struct connection_socket *) connv;
struct pollfd fd;
int r;
if (conn->daemon_sock == -1) {
error (g, _("can_read_data: socket not connected"));
return -1;
}
fd.fd = conn->daemon_sock;
fd.events = POLLIN;
fd.revents = 0;
again:
r = poll (&fd, 1, 0);
if (r == -1) {
if (errno == EINTR || errno == EAGAIN)
goto again;
perrorf (g, "can_read_data: poll");
return -1;
}
return (fd.revents & POLLIN) != 0 ? 1 : 0;
}
static ssize_t
write_data (guestfs_h *g, struct connection *connv,
const void *bufv, size_t len)
{
const char *buf = bufv;
struct connection_socket *conn = (struct connection_socket *) connv;
const size_t original_len = len;
if (conn->daemon_sock == -1) {
error (g, _("write_data: socket not connected"));
return -1;
}
while (len > 0) {
struct pollfd fds[2];
nfds_t nfds = 1;
int r;
fds[0].fd = conn->daemon_sock;
fds[0].events = POLLOUT;
fds[0].revents = 0;
if (conn->console_sock >= 0) {
fds[1].fd = conn->console_sock;
fds[1].events = POLLIN;
fds[1].revents = 0;
nfds++;
}
r = poll (fds, nfds, -1);
if (r == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
perrorf (g, "write_data: poll");
return -1;
}
/* Log message? */
if (nfds > 1 && (fds[1].revents & POLLIN) != 0) {
r = handle_log_message (g, conn);
if (r <= 0)
return r;
}
/* Can write data on daemon socket? */
if ((fds[0].revents & POLLOUT) != 0) {
ssize_t n = write (conn->daemon_sock, buf, len);
if (n == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
if (errno == EPIPE) /* Disconnected from guest (RHBZ#508713). */
return 0;
perrorf (g, "write_data: write");
return -1;
}
buf += n;
len -= n;
}
}
return original_len;
}
/**
* This is called if C<conn-E<gt>console_sock> becomes ready to read
* while we are doing one of the connection operations above. It
* reads and deals with the log message.
*
* Returns:
*
* =over 4
*
* =item C<1>
*
* log message(s) were handled successfully
*
* =item C<0>
*
* connection to appliance closed
*
* =item C<-1>
*
* error
*
* =back
*/
static int
handle_log_message (guestfs_h *g,
struct connection_socket *conn)
{
CLEANUP_FREE char *buf = safe_malloc (g, BUFSIZ);
ssize_t n;
const char dsr_request[] = "\033[6n";
const char dsr_reply[] = "\033[24;80R";
const char dsr_reply_padding[] = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
/* Carried over from ancient proto.c code. The comment there was:
*
* "QEMU's console emulates a 16550A serial port. The real 16550A
* device has a small FIFO buffer (16 bytes) which means here we
* see lots of small reads of 1-16 bytes in length, usually single
* bytes. Sleeping here for a very brief period groups reads
* together (so we usually get a few lines of output at once) and
* improves overall throughput, as well as making the event
* interface a bit more sane for callers. With a virtio-serial
* based console (not yet implemented) we may be able to remove
* this. XXX"
*/
usleep (1000);
n = read (conn->console_sock, buf, BUFSIZ);
if (n == 0)
return 0;
if (n == -1) {
if (errno == EINTR || errno == EAGAIN)
return 1; /* not an error */
perrorf (g, _("error reading console messages from the appliance"));
return -1;
}
/* It's an actual log message. */
/* SGABIOS tries to query the "serial console" for its size using the
* ISO/IEC 6429 Device Status Report (ESC [ 6 n). If it doesn't
* read anything back, then it unfortunately hangs for 0.26 seconds.
* Therefore we detect this situation and send back a fake console
* size.
*/
if (memmem (buf, n, dsr_request, sizeof dsr_request - 1) != NULL) {
debug (g, "responding to serial console Device Status Report");
/* Ignore any error from this write, as it's just an optimization.
* We can't even be sure that console_sock is a socket or that
* it's writable.
*/
ignore_value (write (conn->console_sock, dsr_reply,
sizeof dsr_reply - 1));
/* Additionally, because of a bug in sgabios, it will still pause
* unless you write at least 14 bytes, so we have to pad the
* reply. We can't pad with NULs since sgabios's input routine
* ignores these, so we have to use some other safe padding
* characters. Backspace seems innocuous.
*/
ignore_value (write (conn->console_sock, dsr_reply_padding,
sizeof dsr_reply_padding - 1));
}
/* Send it upwards. */
guestfs_int_log_message_callback (g, buf, n);
return 1;
}
static int
get_console_sock (guestfs_h *g, struct connection *connv)
{
struct connection_socket *conn = (struct connection_socket *) connv;
if (conn->console_sock == -1) {
error (g, _("console socket not connected"));
return -1;
}
return conn->console_sock;
}
static void
free_conn_socket (guestfs_h *g, struct connection *connv)
{
struct connection_socket *conn = (struct connection_socket *) connv;
if (conn->console_sock >= 0)
close (conn->console_sock);
if (conn->daemon_sock >= 0)
close (conn->daemon_sock);
if (conn->daemon_accept_sock >= 0)
close (conn->daemon_accept_sock);
free (conn);
}
static struct connection_ops ops = {
.free_connection = free_conn_socket,
.accept_connection = accept_connection,
.read_data = read_data,
.write_data = write_data,
.can_read_data = can_read_data,
.get_console_sock = get_console_sock,
};
/**
* Create a new socket connection, listening.
*
* Note that it's OK for C<console_sock> to be passed as C<-1>,
* meaning there's no console available for this appliance.
*
* After calling this, C<daemon_accept_sock> is owned by the
* connection, and will be closed properly either in
* C<accept_connection> or C<free_connection>.
*/
struct connection *
guestfs_int_new_conn_socket_listening (guestfs_h *g,
int daemon_accept_sock,
int console_sock)
{
struct connection_socket *conn;
assert (daemon_accept_sock >= 0);
if (set_nonblocking_flag (daemon_accept_sock, 1) == -1) {
perrorf (g, "new_conn_socket_listening: set_nonblocking_flag");
return NULL;
}
if (console_sock >= 0) {
if (set_nonblocking_flag (console_sock, 1) == -1) {
perrorf (g, "new_conn_socket_listening: set_nonblocking_flag");
return NULL;
}
}
conn = safe_malloc (g, sizeof *conn);
/* Set the operations. */
conn->ops = &ops;
/* Set the internal state. */
conn->console_sock = console_sock;
conn->daemon_sock = -1;
conn->daemon_accept_sock = daemon_accept_sock;
return (struct connection *) conn;
}
/**
* Create a new socket connection, connected.
*
* As above, but the caller passes us a connected C<daemon_sock> and
* promises not to call C<accept_connection>.
*/
struct connection *
guestfs_int_new_conn_socket_connected (guestfs_h *g,
int daemon_sock,
int console_sock)
{
struct connection_socket *conn;
assert (daemon_sock >= 0);
if (set_nonblocking_flag (daemon_sock, 1) == -1) {
perrorf (g, "new_conn_socket_connected: set_nonblocking_flag");
return NULL;
}
if (console_sock >= 0) {
if (set_nonblocking_flag (console_sock, 1) == -1) {
perrorf (g, "new_conn_socket_connected: set_nonblocking_flag");
return NULL;
}
}
conn = safe_malloc (g, sizeof *conn);
/* Set the operations. */
conn->ops = &ops;
/* Set the internal state. */
conn->console_sock = console_sock;
conn->daemon_sock = daemon_sock;
conn->daemon_accept_sock = -1;
return (struct connection *) conn;
}