/
sink.c
109 lines (100 loc) · 2.66 KB
/
sink.c
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
/**
* @file
* @section AUTHORS
*
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
*
* Authors:
* Rafal Wojtczuk <rafal@invisiblethingslab.com>
* Daniel De Graaf <dgdegra@tycho.nsa.gov>
*
* @section LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @section DESCRIPTION
*
* This is a test program for libxenvchan. Communications are in one direction,
* either server (grant offeror) to client or vice versa.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <libxenvchan.h>
void usage(char** argv)
{
fprintf(stderr, "usage:\n"
"%s <bufsize> <my domid> <remote domid>\n", argv[0]);
exit(1);
}
char *buf;
int bufsize;
void reader(struct libxenvchan *ctrl)
{
uint64_t nbytes = 0;
int size;
time_t start;
time_t end;
double nsecs;
for (;;) {
size = libxenvchan_read(ctrl, buf, bufsize);
if (nbytes == 0)
start = time(NULL);
/* fprintf(stderr, "# %d\n", size); */
if (size < 0) {
end = time(NULL);
nsecs = difftime(end, start);
fprintf(stderr, "%Ld bytes in %.1f seconds\n", nbytes, nsecs);
fprintf(stderr, "%d MiB/sec\n", (int) ( (double)(nbytes / (1024 * 1024)) /nsecs ));
perror("read vchan");
libxenvchan_close(ctrl);
exit(1);
}
nbytes += size;
}
}
/**
Listens for connections and writes everything to /dev/null
*/
int main(int argc, char **argv)
{
int my_domid = -1;
int remote_domid = -1;
char path[128];
struct libxenvchan *ctrl = 0;
if (argc < 4)
usage(argv);
bufsize = atoi(argv[1]);
my_domid = atoi(argv[2]);
buf = (char*)malloc(bufsize);
if (!buf) {
perror("malloc");
exit(1);
}
remote_domid = atoi(argv[3]);
sprintf(path, "/local/domain/%d/data/%d/0", my_domid, my_domid);
fprintf(stderr, "Path: %s\n", path);
ctrl = libxenvchan_server_init(NULL, remote_domid, path, 1024, 1024);
if (!ctrl) {
perror("libxenvchan_*_init");
exit(1);
}
ctrl->blocking = 1;
reader(ctrl);
libxenvchan_close(ctrl);
return 0;
}