forked from libos-nuse/linux-libos-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuse-vif-pipe.c
108 lines (89 loc) · 1.81 KB
/
nuse-vif-pipe.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "nuse-hostcalls.h"
#include "nuse-vif.h"
#include "nuse.h"
#define NUSE_DEFAULT_PIPE_PRIV 0666
static int
named_pipe_alloc(const char *path)
{
int fd;
if (mkfifo(path, NUSE_DEFAULT_PIPE_PRIV) < 0) {
perror ("mkfifo");
return -1;
}
if ((fd = host_open(path, O_RDWR)) < 0) {
perror ("open");
return -1;
}
return fd;
}
void
nuse_vif_pipe_read(struct nuse_vif *vif, struct SimDevice *dev)
{
int sock = vif->sock;
char buf[8192];
ssize_t size;
while (1) {
size = host_read(sock, buf, sizeof(buf));
if (size < 0) {
perror("read");
host_close(sock);
return;
} else if (size == 0) {
host_close(sock);
return;
}
nuse_dev_rx(dev, buf, size);
}
}
void
nuse_vif_pipe_write(struct nuse_vif *vif, struct SimDevice *dev,
unsigned char *data, int len)
{
int sock = vif->sock;
int ret = host_write(sock, data, len);
if (ret == -1)
perror ("write");
}
void *
nuse_vif_pipe_create(const char *pipepath)
{
int sock;
struct nuse_vif *vif;
sock = named_pipe_alloc(pipepath);
if (sock < 0) {
printf ("failed to create named pipe \"%s\"\n", pipepath);
return NULL;
}
vif = malloc (sizeof(struct nuse_vif));
vif->sock = sock;
vif->type = NUSE_VIF_PIPE;
return vif;
}
void
nuse_vif_pipe_delete(struct nuse_vif *vif)
{
int sock = vif->sock;
free(vif);
host_close(sock);
}
static struct nuse_vif_impl nuse_vif_pipe = {
.read = nuse_vif_pipe_read,
.write = nuse_vif_pipe_write,
.create = nuse_vif_pipe_create,
.delete = nuse_vif_pipe_delete,
};
extern struct nuse_vif_impl *nuse_vif[NUSE_VIF_MAX];
int __attribute__((constructor))
nuse_vif_pipe_init(void)
{
nuse_vif[NUSE_VIF_PIPE] = &nuse_vif_pipe;
return 0;
}