summaryrefslogtreecommitdiffstats
path: root/auto-virtserial-guest.c
blob: 9146c43d0e896cb32b19c9301416e340b2cce5ae (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
/*
 * auto-virtserial-guest: Exercise various options for virtio-serial ports
 *
 * This program accepts commands from the controlling program on the
 * host.  It then spawns tests on the other virtio-serial ports as per
 * the host's orders.
 *
 * 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 <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "virtserial.h"

#define CONTROL_PORT "/dev/vport0p1"
#define MAX_PORTS 10

/* The fd to work with for read / write requests. Set by the open message */
static int g_fd;
/* The events to poll for */
static int g_poll_events;
/* The fd to stuff in bytes for a big file receive command */
static int g_bigfile_fd;
/* The length to read / write. Set by the length message. Unset at close */
static int g_length;
/* The 'name' field in the sysfs port info */
static char g_sysfs_name[1024];
/* The thread that performs a blocking read operation */
static pthread_t g_read_thread_id;
/* Array to hold fds of all open ports. Used for polling host connect events */
static int g_open_fds[MAX_PORTS];
/* Array to hold poll results of g_open_fds from the sigio_handler() */
static int g_poll_results[MAX_PORTS];

static ssize_t safewrite(int fd, const void *buf, size_t count, bool eagain_ret)
{
	size_t ret, len;
	int flags;
	bool nonblock;

	nonblock = false;
	flags = fcntl(fd, F_GETFL);
	if (flags > 0 && flags & O_NONBLOCK)
		nonblock = true;

	len = count;
	while (len > 0) {
		ret = write(fd, buf, len);
		if (ret == -1) {
			if (errno == EINTR)
				continue;

			if (errno == EAGAIN) {
				if (nonblock && eagain_ret) {
					return -EAGAIN;
				} else {
					continue;
				}
			}
			return -errno;
		} else if (ret == 0) {
			break;
		} else {
			buf += ret;
			len -= ret;
		}
	}
	return count - len;
}

static ssize_t saferead(int fd, void *buf, size_t count)
{
	size_t ret, len;
	int flags;
	bool nonblock;

	nonblock = false;
	flags = fcntl(fd, F_GETFL);
	if (flags > 0 && flags & O_NONBLOCK)
		nonblock = true;

	len = count;
	while (len > 0) {
		ret = read(fd, buf, len);
		if (ret == -1) {
			if (errno == EINTR)
				continue;

			if (errno == EAGAIN) {
				if (nonblock) {
					return -EAGAIN;
				} else {
					continue;
				}
			}
			return -errno;
		} else if (ret == 0) {
			break;
		} else {
			buf += ret;
			len -= ret;
		}
	}
	return count - len;
}

static int safepoll(struct pollfd *fds, nfds_t nfds, int timeout)
{
	int ret;

	do {
		ret = poll(fds, nfds, timeout);
	} while (ret == -1 && errno == EINTR);

	if (ret == -1)
		ret = -errno;

	return ret;
}

static char *get_port_dev(unsigned int nr)
{
	char *buf;
	buf = malloc(strlen("/dev/vport0p10") + 1);
	if (!buf)
		return NULL;
	sprintf(buf, "/dev/vport0p%u", nr);
	return buf;
}

static int open_port(int nr)
{
	char *buf;
	int fd, ret;

	buf = get_port_dev(nr);
	if (!buf)
		return -ENOMEM;
	fd = open(buf, O_RDWR);
	free(buf);

	if (fd == -1)
		return -errno;
	g_fd = fd;

	ret = fcntl(fd, F_SETOWN, getpid());
	if (ret < 0) {
		perror("F_SETOWN");
	}
	ret = fcntl(fd, F_GETFL);
	ret = fcntl(fd, F_SETFL, ret | O_ASYNC);
	if (ret < 0) {
		perror("F_SETFL");
	}

	if (nr < MAX_PORTS)
		g_open_fds[nr] = fd;
	return fd;
}

static int set_poll_events(int events)
{
	return g_poll_events = events;
}

static int poll_port(int timeout)
{
	struct pollfd pollfds[1];
	int ret;

	pollfds[0].fd = g_fd;
	pollfds[0].events = g_poll_events ? : POLLIN | POLLOUT;
	ret = safepoll(pollfds, 1, timeout);
	if (ret <= 0)
		return ret;

	return pollfds[0].revents;
}

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

	if (!g_length) {
		/*
		 * Host just wants to read something. In this case, read
		 * a default length of 1024 bytes.
		 */
		g_length = 1024;
	}
	buf = malloc(g_length);
	if (!buf)
		return -ENOMEM;
	ret = saferead(g_open_fds[nr], buf, g_length);
	free(buf);
	return ret;
}

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

	if (!g_length)
		return 0;

	buf = malloc(g_length);
	if (!buf)
		return -ENOMEM;
	ret = safewrite(g_open_fds[nr], buf, g_length, true);
	free(buf);
	return ret;
}

static int close_port(int nr)
{
	int ret;

	ret = close(g_open_fds[nr]);
	if (ret < 0)
		ret = -errno;
	g_length = 0;

	if (nr < MAX_PORTS) {
		g_open_fds[nr] = -1;
		g_poll_results[nr] = 0;
	}
	return ret;
}

static int seek_port(int nr)
{
	int ret;

	ret = lseek(g_open_fds[nr], 20, SEEK_SET);
	if (ret < 0)
		ret = -errno;

	return ret;
}

static int set_port_nonblocking(int val)
{
	int ret, flags;

	flags = fcntl(g_fd, F_GETFL);
	if (flags == -1)
		return -errno;

	if (val)
		flags |= O_NONBLOCK;
	else
		flags &= ~O_NONBLOCK;

	ret = fcntl(g_fd, F_SETFL, flags);
	if (ret == -1)
		return -errno;
	return 0;
}

static int spawn_console(int val)
{
	/* Currently only works on hvc0 */
	int ret;

	ret = vfork();
	if (!ret) {
		/* Child */
		char *argv[] = { "/sbin/agetty", "/dev/hvc0", "9600", "vt100" };
		char *envp[] = { NULL };

		execve("/sbin/agetty", argv, envp);
		error(errno, errno, "execve");
	}
	return 0;
}

static int open_host_bigfile(int val)
{
	g_bigfile_fd = open(HOST_BIG_FILE, O_RDWR | O_CREAT);
	if (g_bigfile_fd < 0)
		return -errno;
	return 0;
}

static int recv_bytestream(int val)
{
	int ret;
	char *buf;

	buf = malloc(g_length);
	if (!buf)
		return -ENOMEM;

	while((ret = saferead(g_fd, buf, g_length)) > 0) {
		ret = safewrite(g_bigfile_fd, buf, ret, false);
		if (ret < 0)
			break;
	}
	free(buf);
	close(g_bigfile_fd);
	return ret;
}

static int send_host_csum(int nr)
{
	char *buf;
	int ret, csum_fd;

	buf = malloc(g_length);

	ret = system("sha1sum /tmp/amit/host-big-file > /tmp/amit/host-csumfile");
	if (ret == -1)
		return -errno;
	if (WIFEXITED(ret) != true)
		return -1;
	if (WEXITSTATUS(ret) != 0)
		return -WEXITSTATUS(ret);

	csum_fd = open("/tmp/amit/host-csumfile", O_RDONLY);
	if (!csum_fd) {
		return -errno;
	}
	ret = saferead(csum_fd, buf, g_length);
	close(csum_fd);

	if (ret > 0)
		ret = safewrite(g_fd, buf, ret, false);
	free(buf);
	return ret;
}

static int open_guest_bigfile(int val)
{
	g_bigfile_fd = open(GUEST_BIG_FILE, O_RDONLY);
	if (g_bigfile_fd < 0)
		return -errno;
	return 0;
}

static int send_bytestream(int val)
{
	int ret;
	char *buf;

	buf = malloc(g_length);
	if (!buf)
		return -ENOMEM;

	while((ret = saferead(g_bigfile_fd, buf, g_length)) > 0) {
		ret = safewrite(g_fd, buf, ret, false);
		if (ret < 0)
			break;
	}
	free(buf);
	close(g_bigfile_fd);
	close(g_fd);
	return ret;
}

static int send_guest_csum(int nr)
{
	char *buf;
	int ret, csum_fd;

	buf = malloc(g_length);

	ret = system("sha1sum /tmp/amit/guest-big-file > /tmp/amit/guest-csumfile");
	if (ret == -1)
		return -errno;
	if (WIFEXITED(ret) != true)
		return -1;
	if (WEXITSTATUS(ret) != 0)
		return -WEXITSTATUS(ret);

	csum_fd = open("/tmp/amit/guest-csumfile", O_RDONLY);
	if (!csum_fd) {
		return -errno;
	}
	ret = saferead(csum_fd, buf, g_length);
	close(csum_fd);

	if (ret > 0)
		ret = safewrite(g_fd, buf, ret, false);
	free(buf);
	return ret;
}

static int check_sysfs(int nr)
{
	char filename[1024];
	int fd, ret;

	sprintf(filename, "/sys/class/virtio-ports/vport0p%u/name", nr);
	fd = open(filename, O_RDONLY);
	if (fd < 0)
		return -errno;

	ret = saferead(fd, g_sysfs_name, 1024);
	if (ret < 0)
		goto out_close;
	ret = 0;

out_close:
	close(fd);
	return ret;
}

static int check_udev(int nr)
{
	char filename[1024], buf[1024];
	char *str;
	int ret, i;

	str = strstr(g_sysfs_name, "\n");
	if (str)
		*str = 0;

	sprintf(filename, "/dev/virtio-ports/%s", g_sysfs_name);
	ret = readlink(filename, buf, 1024);
	if (ret < 0) {
		ret = -errno;
		goto out;
	}
	sprintf(filename, "../vport0p%u", nr);
	for (i = 0; i < ret; i++) {
		if (buf[i] != filename[i]) {
			ret = -ERANGE;
			goto out;
		}
	}
	ret = 0;
out:
	return ret;
}

static void *t_blocked_read(void *arg)
{
	int port_nr = *(int *)arg;
	int *ret;

	ret = malloc(sizeof(int));
	if (!ret)
		return NULL;

	*ret = read_port(port_nr);

	return ret;
}

static int create_read_thread(int nr)
{
	int ret;

	ret = pthread_create(&g_read_thread_id, NULL, &t_blocked_read, &nr);
	if (ret)
		return -errno;

	/* Give a chance to the thread to start and block */
	sleep(2);

	return 0;
}

static int join_read_thread(int nr)
{
	int ret;
	void *ret2;

	ret = pthread_join(g_read_thread_id, &ret2);
	if (ret)
		return ret;

	ret = *(int *)ret2;

	free(ret2);

	return ret;
}

static int get_port_from_fd(int fd)
{
	unsigned int i;

	for (i = 0; i < MAX_PORTS; i++) {
		if (g_open_fds[i] == fd)
			return i;
	}
	return -1;
}

static void sigio_handler(int signal)
{
	struct pollfd pollfds[MAX_PORTS];
	unsigned int i, j;
	int ret;

	for (i = 0, j = 0; i < MAX_PORTS; i++) {
		if (g_open_fds[i] > -1) {
			pollfds[j].fd = g_open_fds[i];
			pollfds[j++].events = POLLIN|POLLOUT;
		}
	}
	ret = safepoll(pollfds, j, 0);
	if (ret == -1) {
		for (i = 0; i < MAX_PORTS; i++)
			g_poll_results[i] = 0;
		return;
	}

	for (i = 0; i < j; i++) {
		int port;

		port = get_port_from_fd(pollfds[i].fd);
		if (port == -1)
			continue;
		g_poll_results[port] = pollfds[i].revents;
	}
}

static int install_sigio_handler(void)
{
	struct sigaction action;
	unsigned int i;
	int ret;

	action.sa_handler = sigio_handler;
	action.sa_flags = 0;
	ret = sigemptyset(&action.sa_mask);
	if (ret) {
		ret = -errno;
		goto out;
	}

	ret = sigaction(SIGIO, &action, NULL);
	if (ret)
		ret = -errno;

out:
	for (i = 0; i < MAX_PORTS; i++)
		g_poll_results[i] = 0;

	return ret;
}

static int get_sigio_result(int nr)
{
	return g_poll_results[nr];
}

static void send_report(int cfd, int ret)
{
	struct guest_packet gpkt;

	gpkt.key = KEY_RESULT;
	gpkt.value = ret;
	safewrite(cfd, &gpkt, sizeof(gpkt), false);
}

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

	/*
	 * Just spawn a console on the default console port -
	 * /dev/hvc0.  This helps in the case of running on new
	 * kernel-old qemu combination or old kernel-new qemu
	 * combination so that the console port test can be run and
	 * the other tests will just fail.
	 */
	spawn_console(0);

	/*
	 * Have to install it right away. Else we'll start getting
	 * SIGIOs and the default action is to terminate the process.
	 */
	install_sigio_handler();

	ret = access(CONTROL_PORT, R_OK|W_OK);
	if (ret == -1)
		error(errno, errno, "No control port found %s", CONTROL_PORT);

back_to_open:
	cfd = open(CONTROL_PORT, O_RDWR);
	if (cfd == -1)
		error(errno, errno, "open control port %s", CONTROL_PORT);

	gpkt.key = KEY_STATUS_OK;
	gpkt.value = 1;
	ret = safewrite(cfd, &gpkt, sizeof(gpkt), false);
	if (ret < 0)
		error(-ret, -ret, "write control port");

	for (i = 0; i < MAX_PORTS; i++)
		g_open_fds[i] = -1;

	pollfd[0].fd = cfd;
	pollfd[0].events = POLLIN;

	while (1) {
		ret = safepoll(pollfd, 1, -1);
		if (ret < 0)
			error(errno, errno, "poll");

		if (!(pollfd[0].revents & POLLIN))
			continue;

		ret = saferead(cfd, &gpkt, sizeof(gpkt));
		if (ret < sizeof(gpkt)) {
			/*
			 * Out of sync with host. Close port and start over.
			 * For us to get back in sync with host, this port
			 * has to have buffer cachin disabled
			 */
			close(cfd);
			goto back_to_open;
		}
		switch(gpkt.key) {
		case KEY_OPEN:
			ret = open_port(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_CLOSE:
			ret = close_port(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_READ:
			ret = read_port(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_NONBLOCK:
			ret = set_port_nonblocking(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_LENGTH:
			g_length = gpkt.value;
			send_report(cfd, 0);
			break;
		case KEY_WRITE:
			ret = write_port(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_POLL_EVENTS:
			ret = set_poll_events(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_POLL:
			ret = poll_port(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_OPEN_HOST_BIGFILE:
			ret = open_host_bigfile(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_HOST_BYTESTREAM:
			ret = recv_bytestream(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_HOST_CSUM:
			ret = send_host_csum(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_CHECK_SYSFS:
			ret = check_sysfs(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_CHECK_UDEV:
			ret = check_udev(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_OPEN_GUEST_BIGFILE:
			ret = open_guest_bigfile(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_GUEST_BYTESTREAM:
			ret = send_bytestream(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_GUEST_CSUM:
			ret = send_guest_csum(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_CREATE_READ_THREAD:
			ret = create_read_thread(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_JOIN_READ_THREAD:
			ret = join_read_thread(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_GET_SIGIO_RESULT:
			ret = get_sigio_result(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_SHUTDOWN:
			system("shutdown -h now");
			break;
		case KEY_LSEEK:
			ret = seek_port(gpkt.value);
			send_report(cfd, ret);
			break;
		default:
			send_report(cfd, -ERANGE);
			break;
		}
	}
	return 0;
}