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
|
/*
* virsh-console.c: A dumb serial console client
*
* Copyright (C) 2007-2008, 2010-2014 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.1 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, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#ifndef WIN32
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <termios.h>
# include <poll.h>
# include <unistd.h>
# include <signal.h>
# include "internal.h"
# include "virsh.h"
# include "virsh-console.h"
# include "virsh-util.h"
# include "virlog.h"
# include "viralloc.h"
# include "virthread.h"
# include "virerror.h"
# include "virobject.h"
VIR_LOG_INIT("tools.virsh-console");
/*
* Convert given character to control character.
* Basically, we assume ASCII, and take lower 6 bits.
*/
# define CONTROL(c) ((c) ^ 0x40)
# define VIR_FROM_THIS VIR_FROM_NONE
struct virConsoleBuffer {
size_t length;
size_t offset;
char *data;
};
typedef struct virConsole virConsole;
struct virConsole {
virObjectLockable parent;
virStreamPtr st;
bool quit;
virCond cond;
int stdinWatch;
int stdoutWatch;
struct virConsoleBuffer streamToTerminal;
struct virConsoleBuffer terminalToStream;
char escapeChar;
virError error;
};
static virClass *virConsoleClass;
static void virConsoleDispose(void *obj);
static int
virConsoleOnceInit(void)
{
if (!VIR_CLASS_NEW(virConsole, virClassForObjectLockable()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virConsole);
static void
virConsoleHandleSignal(int sig G_GNUC_UNUSED)
{
}
static void
virConsoleShutdown(virConsole *con,
bool graceful)
{
virErrorPtr err = virGetLastError();
if (con->error.code == VIR_ERR_OK && err)
virCopyLastError(&con->error);
if (con->st) {
int rc;
virStreamEventRemoveCallback(con->st);
if (graceful)
rc = virStreamFinish(con->st);
else
rc = virStreamAbort(con->st);
if (rc < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cannot terminate console stream"));
}
g_clear_pointer(&con->st, virshStreamFree);
}
VIR_FREE(con->streamToTerminal.data);
VIR_FREE(con->terminalToStream.data);
if (con->stdinWatch != -1)
virEventRemoveHandle(con->stdinWatch);
if (con->stdoutWatch != -1)
virEventRemoveHandle(con->stdoutWatch);
con->stdinWatch = -1;
con->stdoutWatch = -1;
if (!con->quit) {
con->quit = true;
virCondSignal(&con->cond);
}
}
static void
virConsoleDispose(void *obj)
{
virConsole *con = obj;
virshStreamFree(con->st);
virCondDestroy(&con->cond);
virResetError(&con->error);
}
static void
virConsoleEventOnStream(virStreamPtr st,
int events, void *opaque)
{
virConsole *con = opaque;
virObjectLock(con);
/* we got late event after console was shutdown */
if (!con->st)
goto cleanup;
if (events & VIR_STREAM_EVENT_READABLE) {
size_t avail = con->streamToTerminal.length -
con->streamToTerminal.offset;
int got;
if (avail < 1024) {
VIR_REALLOC_N(con->streamToTerminal.data,
con->streamToTerminal.length + 1024);
con->streamToTerminal.length += 1024;
avail += 1024;
}
got = virStreamRecv(st,
con->streamToTerminal.data +
con->streamToTerminal.offset,
avail);
if (got == -2)
goto cleanup; /* blocking */
if (got <= 0) {
virConsoleShutdown(con, got == 0);
goto cleanup;
}
con->streamToTerminal.offset += got;
if (con->streamToTerminal.offset)
virEventUpdateHandle(con->stdoutWatch,
VIR_EVENT_HANDLE_WRITABLE);
}
if (events & VIR_STREAM_EVENT_WRITABLE &&
con->terminalToStream.offset) {
ssize_t done;
size_t avail;
done = virStreamSend(con->st,
con->terminalToStream.data,
con->terminalToStream.offset);
if (done == -2)
goto cleanup; /* blocking */
if (done < 0) {
virConsoleShutdown(con, false);
goto cleanup;
}
memmove(con->terminalToStream.data,
con->terminalToStream.data + done,
con->terminalToStream.offset - done);
con->terminalToStream.offset -= done;
avail = con->terminalToStream.length - con->terminalToStream.offset;
if (avail > 1024) {
VIR_REALLOC_N(con->terminalToStream.data,
con->terminalToStream.offset + 1024);
con->terminalToStream.length = con->terminalToStream.offset + 1024;
}
}
if (!con->terminalToStream.offset)
virStreamEventUpdateCallback(con->st,
VIR_STREAM_EVENT_READABLE);
if (events & VIR_STREAM_EVENT_ERROR ||
events & VIR_STREAM_EVENT_HANGUP) {
virConsoleShutdown(con, false);
}
cleanup:
virObjectUnlock(con);
}
static void
virConsoleEventOnStdin(int watch G_GNUC_UNUSED,
int fd G_GNUC_UNUSED,
int events,
void *opaque)
{
virConsole *con = opaque;
virObjectLock(con);
/* we got late event after console was shutdown */
if (!con->st)
goto cleanup;
if (events & VIR_EVENT_HANDLE_READABLE) {
size_t avail = con->terminalToStream.length -
con->terminalToStream.offset;
int got;
if (avail < 1024) {
VIR_REALLOC_N(con->terminalToStream.data,
con->terminalToStream.length + 1024);
con->terminalToStream.length += 1024;
avail += 1024;
}
got = read(fd,
con->terminalToStream.data +
con->terminalToStream.offset,
avail);
if (got < 0) {
if (errno != EAGAIN) {
virReportSystemError(errno, "%s", _("cannot read from stdin"));
virConsoleShutdown(con, false);
}
goto cleanup;
}
if (got == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
virConsoleShutdown(con, false);
goto cleanup;
}
if (con->terminalToStream.data[con->terminalToStream.offset] == con->escapeChar) {
virConsoleShutdown(con, true);
goto cleanup;
}
con->terminalToStream.offset += got;
if (con->terminalToStream.offset)
virStreamEventUpdateCallback(con->st,
VIR_STREAM_EVENT_READABLE |
VIR_STREAM_EVENT_WRITABLE);
}
if (events & VIR_EVENT_HANDLE_ERROR) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin"));
virConsoleShutdown(con, false);
goto cleanup;
}
if (events & VIR_EVENT_HANDLE_HANGUP) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin"));
virConsoleShutdown(con, false);
goto cleanup;
}
cleanup:
virObjectUnlock(con);
}
static void
virConsoleEventOnStdout(int watch G_GNUC_UNUSED,
int fd,
int events,
void *opaque)
{
virConsole *con = opaque;
virObjectLock(con);
/* we got late event after console was shutdown */
if (!con->st)
goto cleanup;
if (events & VIR_EVENT_HANDLE_WRITABLE &&
con->streamToTerminal.offset) {
ssize_t done;
size_t avail;
done = write(fd, /* sc_avoid_write */
con->streamToTerminal.data,
con->streamToTerminal.offset);
if (done < 0) {
if (errno != EAGAIN) {
virReportSystemError(errno, "%s", _("cannot write to stdout"));
virConsoleShutdown(con, false);
}
goto cleanup;
}
memmove(con->streamToTerminal.data,
con->streamToTerminal.data + done,
con->streamToTerminal.offset - done);
con->streamToTerminal.offset -= done;
avail = con->streamToTerminal.length - con->streamToTerminal.offset;
if (avail > 1024) {
VIR_REALLOC_N(con->streamToTerminal.data,
con->streamToTerminal.offset + 1024);
con->streamToTerminal.length = con->streamToTerminal.offset + 1024;
}
}
if (!con->streamToTerminal.offset)
virEventUpdateHandle(con->stdoutWatch, 0);
if (events & VIR_EVENT_HANDLE_ERROR) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout"));
virConsoleShutdown(con, false);
goto cleanup;
}
if (events & VIR_EVENT_HANDLE_HANGUP) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout"));
virConsoleShutdown(con, false);
goto cleanup;
}
cleanup:
virObjectUnlock(con);
}
static virConsole *
virConsoleNew(void)
{
virConsole *con;
if (virConsoleInitialize() < 0)
return NULL;
if (!(con = virObjectLockableNew(virConsoleClass)))
return NULL;
if (virCondInit(&con->cond) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cannot initialize console condition"));
goto error;
}
con->stdinWatch = -1;
con->stdoutWatch = -1;
return con;
error:
virObjectUnref(con);
return NULL;
}
static char
virshGetEscapeChar(const char *s)
{
if (*s == '^')
return CONTROL(g_ascii_toupper(s[1]));
return *s;
}
int
virshRunConsole(vshControl *ctl,
virDomainPtr dom,
const char *dev_name,
unsigned int flags)
{
virConsole *con = NULL;
virshControl *priv = ctl->privData;
int ret = -1;
struct sigaction old_sigquit;
struct sigaction old_sigterm;
struct sigaction old_sigint;
struct sigaction old_sighup;
struct sigaction old_sigpipe;
struct sigaction sighandler = {.sa_handler = virConsoleHandleSignal,
.sa_flags = SA_SIGINFO };
sigemptyset(&sighandler.sa_mask);
/* Put STDIN into raw mode so that stuff typed does not echo to the screen
* (the TTY reads will result in it being echoed back already), and also
* ensure Ctrl-C, etc is blocked, and misc other bits */
if (vshTTYMakeRaw(ctl, true) < 0)
goto resettty;
if (!(con = virConsoleNew()))
goto resettty;
virObjectLock(con);
/* Trap all common signals so that we can safely restore the original
* terminal settings on STDIN before the process exits - people don't like
* being left with a messed up terminal ! */
sigaction(SIGQUIT, &sighandler, &old_sigquit);
sigaction(SIGTERM, &sighandler, &old_sigterm);
sigaction(SIGINT, &sighandler, &old_sigint);
sigaction(SIGHUP, &sighandler, &old_sighup);
sigaction(SIGPIPE, &sighandler, &old_sigpipe);
con->escapeChar = virshGetEscapeChar(priv->escapeChar);
con->st = virStreamNew(virDomainGetConnect(dom),
VIR_STREAM_NONBLOCK);
if (!con->st)
goto cleanup;
if (virDomainOpenConsole(dom, dev_name, con->st, flags) < 0)
goto cleanup;
virObjectRef(con);
if ((con->stdinWatch = virEventAddHandle(STDIN_FILENO,
VIR_EVENT_HANDLE_READABLE,
virConsoleEventOnStdin,
con,
virObjectUnref)) < 0) {
virObjectUnref(con);
goto cleanup;
}
virObjectRef(con);
if ((con->stdoutWatch = virEventAddHandle(STDOUT_FILENO,
0,
virConsoleEventOnStdout,
con,
virObjectUnref)) < 0) {
virObjectUnref(con);
goto cleanup;
}
virObjectRef(con);
if (virStreamEventAddCallback(con->st,
VIR_STREAM_EVENT_READABLE,
virConsoleEventOnStream,
con,
virObjectUnref) < 0) {
virObjectUnref(con);
goto cleanup;
}
while (!con->quit) {
if (virCondWait(&con->cond, &con->parent.lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("unable to wait on console condition"));
goto cleanup;
}
}
if (con->error.code == VIR_ERR_OK)
ret = 0;
cleanup:
virConsoleShutdown(con, ret == 0);
if (ret < 0) {
vshResetLibvirtError();
virSetError(&con->error);
vshSaveLibvirtHelperError();
}
virObjectUnlock(con);
virObjectUnref(con);
/* Restore original signal handlers */
sigaction(SIGQUIT, &old_sigquit, NULL);
sigaction(SIGTERM, &old_sigterm, NULL);
sigaction(SIGINT, &old_sigint, NULL);
sigaction(SIGHUP, &old_sighup, NULL);
sigaction(SIGPIPE, &old_sigpipe, NULL);
resettty:
/* Put STDIN back into the (sane?) state we found
it in before starting */
vshTTYRestore(ctl);
return ret;
}
#endif /* !WIN32 */
|