summaryrefslogtreecommitdiffstats
path: root/auto-virtserial.c
blob: 58bf77964186dd349151c6eb1c62920eeac9e8ac (plain)
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
 * auto-virtserial: Exercise various options for virtio-serial ports
 *
 * This program is the controlling program to be run on the host.
 *
 * Assumptions:
 * - The first port, that'll get seen as /dev/vcon1, will be the port
 *   for control information
 * - The second port (vcon2) is started with no extra params (cache_buffers=1)
 * - The third port (vcon3) is started with cache_buffers=0
 * - The fourth port (vcon4) is started with host and guest limits to 1MB.
 *
 * Most of these tests have been described in the test-virtserial.c file
 *
 * Some information on the virtio serial ports is available at
 *  http://www.linux-kvm.org/page/VMchannel_Requirements
 *  https://fedoraproject.org/wiki/Features/VirtioSerial
 *
 * Copyright (C) 2009, Red Hat, Inc.
 *
 * Author(s):
 *  Amit Shah <amit.shah@redhat.com>
 *
 * Licensed under the GNU General Public License v2. See the file COPYING
 * for more details.
 */

#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include "virtserial.h"

#define DEBUG 1

#ifdef DEBUG
#define debug(fmt, ...) 			\
	do {					\
		printf(fmt, __VA_ARGS__);	\
	} while(0)
#else
#define debug(fmt, ...) do { } while (0)
#endif

#define BUF_LENGTH 4080
#define UNIX_PATH_MAX 108

static unsigned int nr_passed, nr_failed;

static struct host_chars {
	char *path;
	int sock;
	bool caching;
	bool throttled;
} chardevs[] = {
	{
		.path = "/tmp/amit/test0",
		.caching = false,
		.throttled = false,
	}, {
		.path = "/tmp/amit/test1",
		.caching = true,
		.throttled = false,
	}, {
		.path = "/tmp/amit/test2",
		.caching = true,
		.throttled = false,
	}, {
		.path = "/tmp/amit/test3",
		.caching = false,
		.throttled = false,
	}, {
		.path = "/tmp/amit/test4",
		.caching = true,
		.throttled = true,
	}, {
		NULL,
	}
};

static void handle_guest_error(struct guest_packet *gpkt)
{
	char *buf;

	buf = malloc(gpkt->value);
	if (!buf)
		error(ENOMEM, ENOMEM, "Guest err");
	read(chardevs[1].sock, buf, gpkt->value);
	fprintf(stderr, "guest error: %s\n", buf);
	free(buf);
}

static int host_connect_chardev(int nr)
{
	struct sockaddr_un sock;
	int ret;

	chardevs[nr].sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (chardevs[nr].sock == -1)
		error(errno, errno, "socket %d", nr);

	sock.sun_family = AF_UNIX;
	memcpy(&sock.sun_path, chardevs[nr].path, sizeof(sock.sun_path));
	ret = connect(chardevs[nr].sock, (struct sockaddr *)&sock, sizeof(sock));
	if (ret == -1)
		error(errno, errno, "connect: %s", chardevs[nr].path);
	return 0;
}

static int host_close_chardev(int nr)
{
	return close(chardevs[nr].sock);
}

static int get_guest_response(struct guest_packet *gpkt)
{
	return read(chardevs[1].sock, gpkt, sizeof(*gpkt));
}

static int guest_cmd_only(struct guest_packet *gpkt)
{
	return write(chardevs[1].sock, gpkt, sizeof(*gpkt));
}

static int guest_cmd(struct guest_packet *gpkt)
{
	write(chardevs[1].sock, gpkt, sizeof(*gpkt));
	get_guest_response(gpkt);
	if (gpkt->key == KEY_RESULT)
		return gpkt->value;
	return -1;
}

static int guest_open_port(int nr)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_OPEN;
	gpkt.value = nr;
	return guest_cmd(&gpkt);
}

static int guest_close_port(int nr)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_CLOSE;
	gpkt.value = nr;
	return guest_cmd(&gpkt);
}

static int guest_set_length(int nr, int len)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_LENGTH;
	gpkt.value = len;
	return guest_cmd(&gpkt);
}

static int guest_read(int nr, int len)
{
	struct guest_packet gpkt;
	int ret;

	ret = guest_set_length(nr, len);
	if (ret)
		return ret;

	gpkt.key = KEY_READ;
	gpkt.value = nr;

	return guest_cmd(&gpkt);
}

static int guest_write(int nr, int len)
{
	struct guest_packet gpkt;
	int ret;

	ret = guest_set_length(nr, len);
	if (ret)
		return ret;

	gpkt.key = KEY_WRITE;
	gpkt.value = nr;

	return guest_cmd(&gpkt);
}

static int guest_poll(int nr, int timeout)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_POLL;
	gpkt.value = timeout;
	return guest_cmd(&gpkt);
}

static int guest_set_port_nonblocking(int nr, int value)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_NONBLOCK;
	gpkt.value = value;
	return guest_cmd(&gpkt);
}

static void show_stats(void)
{
	fprintf(stderr, "-----\n");
	fprintf(stderr, "Total tests passed: %u\n", nr_passed);
	fprintf(stderr, "Total tests failed: %u\n", nr_failed);
	fprintf(stderr, "-----\n");
}

static void pass(const char *msg, const char *subtype)
{
	fprintf(stderr, "%s(%s):\t\tPASS\n", msg, subtype);
	nr_passed++;
}

static void fail(const char *msg, const char *subtype)
{
	fprintf(stderr, "%s(%s):\t\tFAIL\n", msg, subtype);
	nr_failed++;
}

static int test_open(int nr)
{
	int ret;

	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: return: %d\n", __func__, ret);
	} else {
		pass(__func__, "open");
	}
	return ret;
}

static int test_close(int nr)
{
	int ret;

	ret = guest_close_port(nr);
	if (ret) {
		fail(__func__, "close");
		debug("%s: return: %d\n", __func__, ret);
	} else {
		pass(__func__, "close");
	}
	return ret;
}

/* Reads should return 0 when host chardev isn't connected */
static int test_read_without_host(int nr)
{
	int ret;

	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: open: %d\n", __func__, ret);
		return ret;
	}
	ret = guest_read(nr, 0);
	if (ret)
		fail(__func__, "read");
	else
		pass(__func__, "read");
	guest_close_port(nr);
	return ret;
}

static int test_blocking_read(int nr)
{
	struct guest_packet gpkt;
	struct pollfd pollfd[1];
	int ret;

	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: open: %d\n", __func__, ret);
		return ret;
	}
	host_connect_chardev(nr);

	guest_set_length(nr, sizeof(gpkt));
	/* Reads should block now that the host is connected */
	gpkt.key = KEY_READ;
	gpkt.value = nr;
	guest_cmd_only(&gpkt);

	pollfd[0].fd = chardevs[1].sock;
	pollfd[0].events = POLLIN;
	/* See if we get something in 5s -- we shouldn't. */
	ret = poll(pollfd, 1, 5000);
	if (ret == -1)
		error(errno, errno, "%s: poll", __func__);
	if (ret) {
		fail(__func__, "poll");
		debug("%s: poll: %d\n", __func__, ret);
		ret = -1;
		goto out;
	}
	/* Write out anything -- doesn't matter what it is */
	write(chardevs[nr].sock, &gpkt, sizeof(gpkt));
	ret = poll(pollfd, 1, 5000);
	if (ret != 1) {
		fail(__func__, "poll");
		debug("%s: no response from guest\n", __func__);
		ret = -1;
		goto out;
	}
	get_guest_response(&gpkt);
	if (gpkt.key != KEY_RESULT)
		error(EINVAL, EINVAL, "%s: guest response\n", __func__);
	if (gpkt.value == sizeof(gpkt)) {
		pass(__func__, "read");
		ret = 0;
	} else {
		fail(__func__, "read");
		debug("%s: expected return %zu, got %d\n",
		      __func__, sizeof(gpkt), gpkt.value);
		ret = -EINVAL;
	}
out:
	guest_close_port(nr);
	host_close_chardev(nr);
	return ret;
}

static int test_nonblocking_read(int nr)
{
	struct guest_packet gpkt;
	struct pollfd pollfd[1];
	int ret;

	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: open: %d\n", __func__, ret);
		return ret;
	}
	host_connect_chardev(nr);

	ret = guest_set_port_nonblocking(nr, true);
	if (ret) {
		fail(__func__, "blocking");
		debug("%s: blocking: %d\n", __func__, -ret);
		goto out;
	}
	guest_set_length(nr, sizeof(gpkt));
	/* Reads should block now that the host is connected */
	gpkt.key = KEY_READ;
	gpkt.value = nr;
	guest_cmd_only(&gpkt);

	pollfd[0].fd = chardevs[1].sock;
	pollfd[0].events = POLLIN;
	/* See if we get something in 5s -- we should. */
	ret = poll(pollfd, 1, 5000);
	if (ret == -1)
		error(errno, errno, "%s: poll", __func__);
	if (!ret) {
		fail(__func__, "poll");
		debug("%s: poll: %d\n", __func__, ret);
		ret = -1;
		goto out;
	}
	get_guest_response(&gpkt);
	if (gpkt.value != -EAGAIN) {
		fail(__func__, "guest response");
		debug("%s: Expected %d got %d\n",
		      __func__, -EAGAIN, gpkt.value);
	}
	/* Write out anything -- doesn't matter what it is */
	write(chardevs[nr].sock, &gpkt, sizeof(gpkt));

	ret = guest_read(nr, sizeof(gpkt));
	if (ret == sizeof(gpkt)) {
		pass(__func__, "read");
		ret = 0;
	} else {
		fail(__func__, "read");
		debug("%s: expected return %zu, got %d\n",
		      __func__, sizeof(gpkt), gpkt.value);
		ret = -EINVAL;
	}
out:
	guest_close_port(nr);
	host_close_chardev(nr);
	return ret;
}

static int test_poll(int nr)
{
	int ret;

	guest_open_port(nr);
	ret = guest_poll(nr, 0);
	if (ret < 0) {
		fail(__func__, "open");
		debug("%s: poll error: %d\n", __func__, ret);
		goto out;
	}
	if (ret != POLLHUP) {
		fail(__func__, "POLLHUP");
		debug("%s: poll ret: 0x%x, expected 0x%x\n",
		      __func__, ret, POLLHUP);
		goto out;
	}
	pass(__func__, "POLLHUP");

	host_connect_chardev(nr);
	ret = guest_poll(nr, 0);
	if (ret < 0) {
		fail(__func__, "poll");
		debug("%s: poll error: %d\n", __func__, ret);
		goto out_close;
	} else if (ret == POLLOUT) {
		pass(__func__, "POLLOUT");
	} else {
		fail(__func__, "POLLOUT");
		debug("%s: poll ret: 0x%x, expected 0x%x\n",
		      __func__, ret, POLLOUT);
		goto out_close;
	}
	write(chardevs[nr].sock, &ret, sizeof(ret));
	ret = guest_poll(nr, 0);
	if (ret < 0) {
		fail(__func__, "POLL");
		debug("%s: poll error: %d\n", __func__, ret);
		goto out_flush;
	} else if (ret == (POLLIN|POLLOUT)) {
		pass(__func__, "POLLIN");
	} else {
		fail(__func__, "POLLIN");
		debug("%s: poll ret: 0x%d, expected 0x%d\n",
		      __func__, ret, POLLIN|POLLOUT);
		goto out_flush;
	}
	ret = 0;
out_flush:
	guest_read(nr, sizeof(ret));
out_close:
	host_close_chardev(nr);
out:
	guest_close_port(nr);
	return ret;
}

/*
 * Tests if writes to guest get throttled after sending 1M data
 * The invert parameter specifies the test should pass for ports
 * that don't have throttling enabled
 */
static int test_guest_throttle(int nr)
{
	char *buf;
	struct pollfd pollfd[1];
	size_t size, copied;
	int ret;

	/* Open guest */
	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: open: %d\n", __func__, ret);
		return ret;
	}
	host_connect_chardev(nr);

	buf = malloc(BUF_LENGTH);
	if (!buf)
		error(ENOMEM, ENOMEM, "%s\n", __func__);

	pollfd[0].fd = chardevs[nr].sock;
	pollfd[0].events = POLLOUT;
	ret = poll(pollfd, 1, 0);
	if (ret == -1)
		error(errno, errno, "%s: poll\n", __func__);
	if (!(pollfd[0].revents & POLLOUT))
		error(ENOSPC, ENOSPC, "%s: can't write\n", __func__);

	copied = 0;
	do {
		size = write(chardevs[nr].sock, buf, BUF_LENGTH);
		if (size == -1)
			error(errno, errno, "%s: write", __func__);
		copied += size;

		ret = poll(pollfd, 1, 2000);
		if (ret == -1)
			error(errno, errno, "%s: poll\n", __func__);
	} while ((pollfd[0].revents & POLLOUT) && (copied < 1048576));

	if ((pollfd[0].revents & POLLOUT) && !chardevs[nr].throttled) {
		pass(__func__, "disabled");
	} else if (!(pollfd[0].revents & POLLOUT) && chardevs[nr].throttled) {
		pass(__func__, "enabled");
	} else {
		fail(__func__, "throttle");
		debug("%s: ret = %d, copied = %zu\n", __func__, ret, copied);
	}
	/* Cleanup - free all the buffers that were sent */
	while (guest_poll(nr, 0) & POLLIN) {
		guest_read(nr, BUF_LENGTH);
	}
	guest_close_port(nr);
	host_close_chardev(nr);
	free(buf);
	return 0;
}

static int test_host_throttle(int nr)
{
	char *buf;
	struct pollfd pollfds[1];
	size_t copied;
	int ret;

	ret = guest_open_port(nr);

	copied = 0;
	do {
		ret = guest_write(nr, BUF_LENGTH);
		if (ret > 0)
			copied += ret;
	} while ((copied < 1048576) && (ret > 0));

	if (ret && !chardevs[nr].throttled) {
		pass(__func__, "disabled");
	} else if (!(ret == -ENOSPC) && chardevs[nr].throttled) {
		pass(__func__, "enabled");
	} else {
		fail(__func__, "throttle");
		debug("%s: ret = %d, copied = %zu\n", __func__, ret, copied);
	}

	host_connect_chardev(nr);
	buf = malloc(BUF_LENGTH);
	if (!buf)
		error(ENOMEM, ENOMEM, "%s", __func__);
	pollfds[0].fd = chardevs[nr].sock;
	pollfds[0].events = POLLIN;
	while ((poll(pollfds, 1, 0) == 1) && (pollfds[0].revents & POLLIN))
		read(chardevs[nr].sock, buf, BUF_LENGTH);
	free(buf);

	host_close_chardev(nr);
	guest_close_port(nr);
	return ret;
}

static int test_guest_caching(int nr)
{
	char *buf;
	int ret;

	ret = guest_open_port(nr);
	if (ret) {
		fail(__func__, "open");
		debug("%s: open: %d\n", __func__, ret);
		return ret;
	}
	host_connect_chardev(nr);

	buf = malloc(BUF_LENGTH);
	if (!buf)
		error(ENOMEM, ENOMEM, "%s\n", __func__);

	ret = write(chardevs[nr].sock, buf, BUF_LENGTH);
	if (ret == -1)
		error(errno, errno, "%s: write", __func__);

	ret = guest_close_port(nr);
	host_close_chardev(nr);
	free(buf);

	ret = guest_open_port(nr);
	ret = guest_read(nr, BUF_LENGTH);
	if ((ret == BUF_LENGTH) && chardevs[nr].caching) {
		pass(__func__, "disabled");
	} else if (!ret && !chardevs[nr].caching) {
		pass(__func__, "enabled");
	} else {
		fail(__func__, "caching");
		debug("%s: ret = %d\n", __func__, ret);
	}
	guest_close_port(nr);
	return 0;
}

static int test_host_caching(int nr)
{
	int ret;
	char *buf;
	struct pollfd pollfds[1];

	guest_open_port(nr);
	ret = guest_write(nr, BUF_LENGTH);
	if (ret != BUF_LENGTH) {
		fail(__func__, "write");
		debug("%s: guest_write return: %d\n", __func__, ret);
		goto out;
	}
	host_connect_chardev(nr);

	pollfds[0].fd = chardevs[nr].sock;
	pollfds[0].events = POLLIN;
	ret = poll(pollfds, 1, 2000);
	if (ret < 0) {
		fail(__func__, "poll");
		debug("%s: poll returned %d, revents %d\n",
		      __func__, ret, pollfds[0].revents);
		goto out;
	}
	if (ret > 0 && !chardevs[nr].caching) {
		fail(__func__, "disabled");
		debug("%s: ret = %d, revents = %d\n",
		      __func__, ret, pollfds[0].revents);
		goto out;
	}
	if (ret != 1 && chardevs[nr].caching) {
		fail(__func__, "enabled");
		debug("%s: ret = %d, revents = %d\n",
		      __func__, ret, pollfds[0].revents);
		goto out;
	}
	if (!chardevs[nr].caching) {
		pass(__func__, "enabled");
		goto out;
	}
	buf = malloc(BUF_LENGTH);
	ret = read(chardevs[nr].sock, buf, BUF_LENGTH);
	free(buf);
	if (ret == BUF_LENGTH) {
		pass(__func__, "enabled");
	} else {
		fail(__func__, "caching");
		if (ret == -1)
			ret = -errno;
		debug("%s: read %d\n", __func__, ret);
	}
out:
	host_close_chardev(nr);
	guest_close_port(nr);
	return ret;
}

static int test_console(int nr)
{
	char buf[1024], *str;
	struct pollfd pollfds[1];
	int ret;

	ret = guest_open_port(nr);
	if (ret != -ENXIO) {
		fail(__func__, "open");
		debug("%s: guest_open ret: %d\n", __func__, ret);
		return ret;
	}
	host_connect_chardev(nr);

	pollfds[0].fd = chardevs[nr].sock;
	pollfds[0].events = POLLIN;

	/*
	 * A console in the guest at /dev/hvc0 is spawned by
	 * auto-virtserial-guest.c so we don't have to do that here
	 */

	/* Send a \n character to get the login prompt as that would
	 * have already been sent by the guest when the console was
	 * spawned and we would have missed it because caching is
	 * disabled on the port.
	 */
	str = "\n";
	write(chardevs[nr].sock, str, strlen(str));

	/* Skip any text before we're presented the login prompt */
	while (poll(pollfds, 1, 5000) == 1)
		read(chardevs[nr].sock, buf, 1024);

	str = strstr(buf, "login: ");
	if (!str) {
		fail(__func__, "console");
		debug("%s: no login prompt received\n", __func__);
		goto out;
	}
	str = "amit\n";
	write(chardevs[nr].sock, str, strlen(str));

	/* Skip any text before we're presented the password prompt */
	while (poll(pollfds, 1, 5000) == 1)
		read(chardevs[nr].sock, buf, 1024);

	str = strstr(buf, "Password: ");
	if (!str) {
		fail(__func__, "console");
		debug("%s: no password prompt received\n", __func__);
		goto out;
	}
	str = "123456\n";
	write(chardevs[nr].sock, str, strlen(str));

	/* Skip any text before we're presented the shell prompt */
	while (poll(pollfds, 1, 5000) == 1)
		read(chardevs[nr].sock, buf, 1024);

	/* Check if we have a prompt */
	str = strstr(buf, "~]$");
	if (!str) {
		fail(__func__, "console");
		debug("%s: didn't get a shell prompt\n", __func__);
		goto out;
	}
	/* 'ls' in the current dir */
	str = "ls\n";
	write(chardevs[nr].sock, str, strlen(str));

	/* Skip ls output */
	while (poll(pollfds, 1, 5000) == 1) {
		read(chardevs[nr].sock, buf, 1024);
	}
	/*
	 * 'find /' - time-consuming operation. Had exposed a locking bug
	 * in the guest kernel with > 1 vcpu, found by Christian.
	 */
	str = "find /\n";
	write(chardevs[nr].sock, str, strlen(str));

	while (poll(pollfds, 1, 5000) == 1) {
		read(chardevs[nr].sock, buf, 1024);
	}
	/* Check if we're back to a prompt again */
	str = strstr(buf, "~]$");
	if (!str) {
		/*
		 * We got the login prompt, passwd prompt, etc. but
		 * didn't finish the 'ls' and 'find /' tests. That's
		 * failure.
		 */
		fail(__func__, "console");
		debug("%s: didn't drop to bash prompt\n", __func__);
		debug("%s: last buf (%d) was %s\n", __func__, ret, buf);
	} else {
		pass(__func__, "console");
	}
out:
	host_close_chardev(nr);
	return ret;
}

static int start_tests(void)
{
	test_open(2);
	test_close(2);
	test_read_without_host(2);

	test_blocking_read(2);
	test_nonblocking_read(2);

	test_poll(2);

	/* Throttling is not enabled on this port */
	test_guest_throttle(2);
	/* Throttling is enabled on this port */
	test_guest_throttle(4);

	/* Throttling is not enabled on this port */
	test_host_throttle(2);
	/* Throttling is enabled on this port */
	test_host_throttle(4);

	/* Caching is enabled on this port */
	test_guest_caching(2);
	/* Caching is not enabled on this port */
	test_guest_caching(3);

	/* Caching is enabled on this port */
	test_host_caching(2);
	/* Caching is not enabled on this port */
	test_host_caching(3);

	test_console(0);

	return 0;
}

int main(int argc, const char *argv[])
{
	struct guest_packet gpkt;
	struct pollfd pollfd[1];
	int i, ret;

	/* Check if host char drvs are ok */
	for (i = 1; chardevs[i].path; i++) {
		ret = access(chardevs[i].path, R_OK|W_OK);
		if (ret)
			error(errno, errno, "access %s", chardevs[i].path);
		if (strlen(chardevs[i].path) > UNIX_PATH_MAX)
			error(E2BIG, E2BIG, "%s", chardevs[i].path);
	}
	host_connect_chardev(1);
	/*
	 * Send a message to the guest indicating we're ready. If the
	 * guest isn't ready yet, it'll connect and let us know.
	 */
	gpkt.key = KEY_STATUS_OK;
	gpkt.value = 1;
	ret = write(chardevs[1].sock, &gpkt, sizeof(gpkt));
	if (ret == -1)
		error(errno, errno, "write %s", chardevs[1].path);
	/* Now wait till we receive guest's response */
	pollfd[0].fd = chardevs[1].sock;
	pollfd[0].events = POLLIN;
	while (1) {
		ret = poll(pollfd, 1, -1);
		if (ret == -1)
			error(errno, errno, "poll %s", chardevs[1].path);
		debug("poll revents = %u\n", pollfd[0].revents);
		if (pollfd[0].revents & POLLIN) {
			ret = read(chardevs[1].sock, &gpkt, sizeof(gpkt));
			if (ret < sizeof(gpkt))
				error(EINVAL, EINVAL, "Read error");
			if (gpkt.key == KEY_STATUS_OK && gpkt.value) {
				debug("Guest is up %d\n", gpkt.key);
				break;
			}
			if (gpkt.key == KEY_GUEST_ERR) {
				handle_guest_error(&gpkt);
				error(EINVAL, EINVAL, "Guest error");
			}
		}
	}
	/* Now we're all set to start our tests. */
	start_tests();
	show_stats();
	host_close_chardev(1);

	return nr_failed;
}