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
|
/*\
* Copyright (C) International Business Machines Corp., 2005
* Author(s): Anthony Liguori <aliguori@us.ibm.com>
*
* Xen Console Daemon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; under version 2 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
\*/
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <termios.h>
#include <signal.h>
#include <getopt.h>
#include <sys/select.h>
#include <err.h>
#include <string.h>
#ifdef __sun__
#include <sys/stropts.h>
#endif
#include <xenstore.h>
#include "xenctrl.h"
#define ESCAPE_CHARACTER 0x1d
static volatile sig_atomic_t received_signal = 0;
static char lockfile[sizeof (XEN_LOCK_DIR "/xenconsole.") + 8] = { 0 };
static int lockfd = -1;
static void sighandler(int signum)
{
received_signal = 1;
}
static bool write_sync(int fd, const void *data, size_t size)
{
size_t offset = 0;
ssize_t len;
while (offset < size) {
len = write(fd, data + offset, size - offset);
if (len < 1) {
return false;
}
offset += len;
}
return true;
}
static void usage(const char *program) {
printf("Usage: %s [OPTION] DOMID\n"
"Attaches to a virtual domain console\n"
"\n"
" -h, --help display this help and exit\n"
" -n, --num N use console number N\n"
" --type TYPE console type. must be 'pv', 'serial' or 'vuart'\n"
" --start-notify-fd N file descriptor used to notify parent\n"
, program);
}
#ifdef __sun__
void cfmakeraw(struct termios *termios_p)
{
termios_p->c_iflag &=
~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
termios_p->c_cflag &= ~(CSIZE|PARENB);
termios_p->c_cflag |= CS8;
termios_p->c_cc[VMIN] = 0;
termios_p->c_cc[VTIME] = 0;
}
#endif
static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)
/* Check for a pty in xenstore, open it and return its fd.
* Assumes there is already a watch set in the store for this path. */
{
struct timeval tv;
fd_set watch_fdset;
int xs_fd = xs_fileno(xs), pty_fd = -1;
time_t start, now;
unsigned int len = 0;
char *pty_path, **watch_paths;
start = now = time(NULL);
do {
tv.tv_usec = 0;
tv.tv_sec = (start + seconds) - now;
FD_ZERO(&watch_fdset);
FD_SET(xs_fd, &watch_fdset);
if (select(xs_fd + 1, &watch_fdset, NULL, NULL, &tv)) {
/* Read the watch to drain the buffer */
watch_paths = xs_read_watch(xs, &len);
free(watch_paths);
/* We only watch for one thing, so no need to
* disambiguate: just read the pty path */
pty_path = xs_read(xs, XBT_NULL, path, &len);
if (pty_path != NULL && pty_path[0] != '\0') {
pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
if (pty_fd == -1)
warn("Could not open tty `%s'", pty_path);
}
free(pty_path);
}
} while (pty_fd == -1 && (now = time(NULL)) < start + seconds);
#ifdef __sun__
if (pty_fd != -1) {
struct termios term;
/*
* The pty may come from either xend (with pygrub) or
* xenconsoled. It may have tty semantics set up, or not.
* While it isn't strictly necessary to have those
* semantics here, it is good to have a consistent
* state that is the same as under Linux.
*
* If tcgetattr fails, they have not been set up,
* so go ahead and set them up now, by pushing the
* ptem and ldterm streams modules.
*/
if (tcgetattr(pty_fd, &term) < 0) {
ioctl(pty_fd, I_PUSH, "ptem");
ioctl(pty_fd, I_PUSH, "ldterm");
}
}
#endif
return pty_fd;
}
/* don't worry too much if setting terminal attributes fail */
static void init_term(int fd, struct termios *old)
{
struct termios new_term;
if (tcgetattr(fd, old) == -1)
return;
new_term = *old;
cfmakeraw(&new_term);
tcsetattr(fd, TCSANOW, &new_term);
}
static void restore_term(int fd, struct termios *old)
{
tcsetattr(fd, TCSANOW, old);
}
static int console_loop(int fd, struct xs_handle *xs, char *pty_path,
bool interactive)
{
int ret, xs_fd = xs_fileno(xs), max_fd = -1;
do {
fd_set fds;
FD_ZERO(&fds);
if (interactive) {
FD_SET(STDIN_FILENO, &fds);
max_fd = STDIN_FILENO;
}
FD_SET(xs_fd, &fds);
if (xs_fd > max_fd) max_fd = xs_fd;
if (fd != -1) FD_SET(fd, &fds);
if (fd > max_fd) max_fd = fd;
ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
if (ret == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return -1;
}
if (FD_ISSET(xs_fileno(xs), &fds)) {
int newfd = get_pty_fd(xs, pty_path, 0);
if (fd != -1)
close(fd);
if (newfd == -1)
/* Console PTY has become invalid */
return 0;
fd = newfd;
continue;
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
ssize_t len;
char msg[60];
len = read(STDIN_FILENO, msg, sizeof(msg));
if (len == 1 && msg[0] == ESCAPE_CHARACTER) {
return 0;
}
if (len == 0 || len == -1) {
if (len == -1 &&
(errno == EINTR || errno == EAGAIN)) {
continue;
}
return -1;
}
if (!write_sync(fd, msg, len)) {
close(fd);
fd = -1;
continue;
}
}
if (fd != -1 && FD_ISSET(fd, &fds)) {
ssize_t len;
char msg[512];
len = read(fd, msg, sizeof(msg));
if (len == 0 || len == -1) {
if (len == -1 &&
(errno == EINTR || errno == EAGAIN)) {
continue;
}
close(fd);
fd = -1;
continue;
}
if (!write_sync(STDOUT_FILENO, msg, len)) {
perror("write() failed");
return -1;
}
}
} while (received_signal == 0);
return 0;
}
typedef enum {
CONSOLE_INVAL,
CONSOLE_PV,
CONSOLE_SERIAL,
CONSOLE_VUART,
} console_type;
static struct termios stdin_old_attr;
static void restore_term_stdin(void)
{
restore_term(STDIN_FILENO, &stdin_old_attr);
}
/* The following locking strategy is based on that from
* libxl__domain_userdata_lock(), with the difference that we want to fail if we
* cannot acquire the lock rather than wait indefinitely.
*/
static void console_lock(int domid)
{
struct stat stab, fstab;
int fd;
snprintf(lockfile, sizeof lockfile, "%s%d", XEN_LOCK_DIR "/xenconsole.", domid);
while (true) {
fd = open(lockfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0)
err(errno, "Could not open %s", lockfile);
while (flock(fd, LOCK_EX | LOCK_NB)) {
if (errno == EINTR)
continue;
else
err(errno, "Could not lock %s", lockfile);
}
if (fstat(fd, &fstab))
err(errno, "Could not fstat %s", lockfile);
if (stat(lockfile, &stab)) {
if (errno != ENOENT)
err(errno, "Could not stat %s", lockfile);
} else {
if (stab.st_dev == fstab.st_dev && stab.st_ino == fstab.st_ino)
break;
}
close(fd);
}
lockfd = fd;
return;
}
static void console_unlock(void)
{
if (lockfile[0] && lockfd != -1) {
unlink(lockfile);
close(lockfd);
}
}
int main(int argc, char **argv)
{
struct termios attr;
int domid;
const char *sopt = "hn:";
int ch;
unsigned int num = 0;
int opt_ind=0;
int start_notify_fd = -1;
struct option lopt[] = {
{ "type", 1, 0, 't' },
{ "num", 1, 0, 'n' },
{ "help", 0, 0, 'h' },
{ "start-notify-fd", 1, 0, 's' },
{ "interactive", 0, 0, 'i' },
{ 0 },
};
char *dom_path = NULL, *path = NULL, *test = NULL;
int spty, xsfd;
struct xs_handle *xs;
char *end;
console_type type = CONSOLE_INVAL;
bool interactive = 0;
const char *console_names = "serial, pv, vuart";
while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
switch(ch) {
case 'h':
usage(argv[0]);
exit(0);
break;
case 'n':
num = atoi(optarg);
break;
case 't':
if (!strcmp(optarg, "serial"))
type = CONSOLE_SERIAL;
else if (!strcmp(optarg, "pv"))
type = CONSOLE_PV;
else if (!strcmp(optarg, "vuart"))
type = CONSOLE_VUART;
else {
fprintf(stderr, "Invalid type argument\n");
fprintf(stderr, "Console types supported are: %s\n",
console_names);
exit(EINVAL);
}
break;
case 's':
start_notify_fd = atoi(optarg);
break;
case 'i':
interactive = 1;
break;
default:
fprintf(stderr, "Invalid argument\n");
fprintf(stderr, "Try `%s --help' for more information.\n",
argv[0]);
exit(EINVAL);
}
}
if (optind >= argc) {
fprintf(stderr, "DOMID should be specified\n");
fprintf(stderr, "Try `%s --help' for more information.\n",
argv[0]);
exit(EINVAL);
}
domid = strtol(argv[optind], &end, 10);
if (end && *end) {
fprintf(stderr, "Invalid DOMID `%s'\n", argv[optind]);
fprintf(stderr, "Try `%s --help' for more information.\n",
argv[0]);
exit(EINVAL);
}
xs = xs_open(0);
if (xs == NULL) {
err(errno, "Could not contact XenStore");
}
signal(SIGTERM, sighandler);
dom_path = xs_get_domain_path(xs, domid);
if (dom_path == NULL)
err(errno, "xs_get_domain_path()");
if (type == CONSOLE_INVAL) {
xc_domaininfo_t xcinfo;
xc_interface *xc_handle = xc_interface_open(0,0,0);
if (xc_handle == NULL)
err(errno, "Could not open xc interface");
if (xc_domain_getinfo_single(xc_handle, domid, &xcinfo) < 0) {
xc_interface_close(xc_handle);
err(errno, "Failed to get domain information");
}
/* default to pv console for pv guests and serial for hvm guests */
if (xcinfo.flags & XEN_DOMINF_hvm_guest)
type = CONSOLE_SERIAL;
else
type = CONSOLE_PV;
xc_interface_close(xc_handle);
}
path = malloc(strlen(dom_path) + strlen("/device/console/0/tty") + 5);
if (path == NULL)
err(ENOMEM, "malloc");
if (type == CONSOLE_SERIAL) {
snprintf(path, strlen(dom_path) + strlen("/serial/0/tty") + 5, "%s/serial/%d/tty", dom_path, num);
test = xs_read(xs, XBT_NULL, path, NULL);
free(test);
if (test == NULL)
type = CONSOLE_PV;
}
if (type == CONSOLE_PV) {
if (num == 0)
snprintf(path, strlen(dom_path) + strlen("/console/tty") + 1, "%s/console/tty", dom_path);
else
snprintf(path, strlen(dom_path) + strlen("/device/console/%d/tty") + 5, "%s/device/console/%d/tty", dom_path, num);
}
if (type == CONSOLE_VUART) {
snprintf(path, strlen(dom_path) + strlen("/vuart/0/tty") + 1,
"%s/vuart/0/tty", dom_path);
}
/* FIXME consoled currently does not assume domain-0 doesn't have a
console which is good when we break domain-0 up. To keep us
user friendly, we'll bail out here since no data will ever show
up on domain-0. */
if (domid == 0) {
fprintf(stderr, "Can't specify Domain-0\n");
exit(EINVAL);
}
console_lock(domid);
atexit(console_unlock);
/* Set a watch on this domain's console pty */
if (!xs_watch(xs, path, ""))
err(errno, "Can't set watch for console pty");
xsfd = xs_fileno(xs);
/* Wait a little bit for tty to appear. There is a race
condition that occurs after xend creates a domain. This code
might be running before consoled has noticed the new domain
and setup a pty for it. */
spty = get_pty_fd(xs, path, 5);
if (spty == -1) {
err(errno, "Could not read tty from store");
}
init_term(spty, &attr);
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
interactive = 1;
init_term(STDIN_FILENO, &stdin_old_attr);
atexit(restore_term_stdin); /* if this fails, oh dear */
}
if (start_notify_fd != -1) {
/* Write 0x00 to notify parent about client's readiness */
static const char msg[] = { 0x00 };
int r;
do {
r = write(start_notify_fd, msg, 1);
} while ((r == -1 && errno == EINTR) || r == 0);
if (r == -1)
err(errno, "Could not notify parent with fd %d",
start_notify_fd);
close(start_notify_fd);
}
console_loop(spty, xs, path, interactive);
free(path);
free(dom_path);
return 0;
}
|