summaryrefslogtreecommitdiffstats
path: root/auto-virtserial-guest.c
blob: cef1d26a779f364df9a43b7b7d976744e1d39ee8 (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
/*
 * 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"

/* The fd to work with for read / write requests. Set by the open message */
static int g_fd;
/* 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;


static ssize_t safewrite(int fd, const 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 = write(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 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;

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

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

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

	pollfds[0].fd = g_fd;
	pollfds[0].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_fd, 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_fd, buf, g_length);
	free(buf);
	return ret;
}

static int close_port(int nr)
{
	int ret;

	ret = close(g_fd);
	if (ret < 0)
		ret = -errno;
	g_length = 0;
	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);
		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);
	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);
		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);
	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 void send_report(int cfd, int ret)
{
	struct guest_packet gpkt;

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

int main(int argc, char *argv[])
{
	struct guest_packet gpkt;
	struct pollfd pollfd[1];
	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);

	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));
	if (ret < 0)
		error(-ret, -ret, "write control port");

	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_STATUS_OK: */
		/* 	gpkt.key = KEY_STATUS_OK; */
		/* 	gpkt.value = 1; */
		/* 	write(cfd, &gpkt, sizeof(gpkt)); */
		/* 	break; */
		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:
			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;
		default:
			send_report(cfd, -ERANGE);
			break;
		}
	}
	return 0;
}