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

/* 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;

static char *get_port_dev(unsigned int nr)
{
	char *buf;
	buf = malloc(strlen("/dev/vcon12") + 1);
	if (!buf)
		return NULL;
	sprintf(buf, "/dev/vcon%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(get_port_dev(nr), O_RDWR);
	if (fd == -1)
		return -errno;
	g_fd = fd;
	return 0;
}

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

	pollfds[0].fd = g_fd;
	pollfds[0].events = POLLIN | POLLOUT;
	ret = poll(pollfds, 1, timeout);
	if (ret == -1)
		return -errno;
	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 = read(g_fd, buf, g_length);
	free(buf);
	if (ret == -1)
		ret = -errno;

	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 = write(g_fd, buf, g_length);
	free(buf);
	if (ret == -1)
		ret = -errno;

	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 = 0;
	if (val)
		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 };

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

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

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

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

	while((ret = read(g_fd, buf, g_length))) {
		ret = write(g_bigfile_fd, buf, ret);
		if (ret < 0)
			break;
	}
	free(buf);
	return ret;
}

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

	buf = malloc(g_length);

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

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

	ret = write(g_fd, buf, ret);
	if (ret < 0)
		ret = -errno;
	free(buf);
	return ret;
}

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

	gpkt.key = KEY_RESULT;
	gpkt.value = ret;
	write(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 = write(cfd, &gpkt, sizeof(gpkt));
	if (ret == -1)
		error(errno, errno, "write control port");

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

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

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

		ret = read(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_BIGFILE:
			ret = open_bigfile(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_BYTESTREAM:
			ret = save_bytestream(gpkt.value);
			send_report(cfd, ret);
			break;
		case KEY_CSUM:
			ret = send_csum(gpkt.value);
			send_report(cfd, ret);
			break;
		}
	}
	return 0;
}