6 #include <sys/select.h>
14 #define sint64_t int64_t
16 #include "v3_user_keyed_stream.h"
20 fprintf(stderr,"v3_user_keyed_stream_file /dev/v3-vm0 user:file:stream\n");
25 int dir_setup(char *dir)
31 char buf[strlen(dir)+strlen("/.palacios_keyed_stream_user_file")+1];
35 strcat(buf,"/.palacios_keyed_stream_user_file");
39 // does the directory exist or can we create it
43 if (mkdir(dir,0700)<0) {
44 perror("cannot create directory");
51 // can we write to it?
53 fd = open(buf,O_RDWR | O_CREAT,0600);
56 perror("cannot write directory");
72 int handle_open_key(struct palacios_user_keyed_stream_op *req,
73 struct palacios_user_keyed_stream_op **resp,
77 char fn[strlen(dir)+req->buf_len+1];
84 fd = open(fn,O_RDWR | O_CREAT,0600);
86 (*resp) = malloc(sizeof(struct palacios_user_keyed_stream_op)+0);
92 (*resp)->len=sizeof(struct palacios_user_keyed_stream_op);
93 (*resp)->type=req->type;
95 (*resp)->user_key=(void*)fd;
102 int handle_close_key(struct palacios_user_keyed_stream_op *req,
103 struct palacios_user_keyed_stream_op **resp,
109 fd = (int) (req->user_key);
113 (*resp) = malloc(sizeof(struct palacios_user_keyed_stream_op)+0);
119 (*resp)->len=sizeof(struct palacios_user_keyed_stream_op);
120 (*resp)->type=req->type;
122 (*resp)->user_key=(void*)fd;
129 int read_all(int fd, char *buf, int len)
137 xfer=read(fd, buf+len-left,left);
139 perror("cannot read file");
148 int write_all(int fd, char *buf, int len)
156 xfer=write(fd, buf+len-left,left);
158 perror("cannot write file");
168 int handle_write_key(struct palacios_user_keyed_stream_op *req,
169 struct palacios_user_keyed_stream_op **resp,
175 fd = (int) (req->user_key);
177 rc = write_all(fd,req->buf,req->xfer);
179 (*resp) = malloc(sizeof(struct palacios_user_keyed_stream_op)+0);
185 (*resp)->len=sizeof(struct palacios_user_keyed_stream_op);
186 (*resp)->type=req->type;
188 (*resp)->user_key=(void*)fd;
196 int handle_read_key(struct palacios_user_keyed_stream_op *req,
197 struct palacios_user_keyed_stream_op **resp,
203 fd = (int) (req->user_key);
205 (*resp) = malloc(sizeof(struct palacios_user_keyed_stream_op)+req->xfer);
211 rc = read_all(fd,(*resp)->buf,req->xfer);
213 (*resp)->len=sizeof(struct palacios_user_keyed_stream_op) + (rc>0 ? rc : 0);
214 (*resp)->type=req->type;
216 (*resp)->user_key=(void*)fd;
217 (*resp)->buf_len=rc>0 ? rc : 0;
227 int handle_request(struct palacios_user_keyed_stream_op *req,
228 struct palacios_user_keyed_stream_op **resp,
234 case PALACIOS_KSTREAM_OPEN:
235 case PALACIOS_KSTREAM_CLOSE:
236 fprintf(stderr,"unsupported stream open or close\n");
240 case PALACIOS_KSTREAM_OPEN_KEY:
241 return handle_open_key(req,resp,dir);
243 case PALACIOS_KSTREAM_CLOSE_KEY:
244 return handle_close_key(req,resp,dir);
246 case PALACIOS_KSTREAM_READ_KEY:
247 return handle_read_key(req,resp,dir);
249 case PALACIOS_KSTREAM_WRITE_KEY:
250 return handle_write_key(req,resp,dir);
253 fprintf(stderr,"unknown request type\n");
262 int run(int devfd, char *dir)
264 struct palacios_user_keyed_stream_op *req;
265 struct palacios_user_keyed_stream_op *resp;
271 FD_SET(devfd,&readset);
273 rc = select(devfd+1, &readset, 0, 0, 0);
276 if (FD_ISSET(devfd,&readset)) {
280 if (v3_user_keyed_stream_pull_request(devfd, &req)) {
281 fprintf(stderr, "could not get request\n");
286 err=handle_request(req, &resp, dir);
288 if (v3_user_keyed_stream_push_response(devfd, resp)) {
289 fprintf(stderr,"could not send response\n");
296 fprintf(stderr, "request handling resulted in an error, continuing\n");
310 int main(int argc, char *argv[])
324 if (strncmp(url,"user:file:",10)) {
325 fprintf(stderr, "Url %s is not a user:file: url\n",url);
331 if (dir_setup(dir)) {
332 fprintf(stderr,"Unable to open or create directory %s\n",dir);
336 fprintf(stderr,"Attempting to attach to vm %s as url %s\n", vm, url);
338 if ((devfd = v3_user_keyed_stream_attach(vm,url))<0) {
339 perror("failed to attach");
343 fprintf(stderr,"Attached and running\n");
347 v3_user_keyed_stream_detach(devfd);