Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Device File Virtualization Proof of Concept (Kernel+Preload)
[palacios.git] / gears / services / devfile / test_preload.c
1 /* 
2    Device File Virtualization Guest Preload Library 
3    Test Program
4
5    (c) Akhil Guliani and William Gross, 2015
6      
7    Adapted from MPI module (c) 2012 Peter Dinda
8
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <malloc.h>
19
20 #include <sys/mman.h>
21 #include "devfile_hc.h"
22
23
24 int read_all(int fd, char *data, int numbytes) 
25 {
26     int i;
27     int rc;
28
29     for (i=0;i<numbytes;) {
30         rc = read(fd,data+i,(numbytes-i));
31         if (rc<=0) {
32             return -1;
33         } else {
34             i+=rc;
35         }
36     }
37     return numbytes;
38 }
39
40 int write_all(int fd, char *data, int numbytes) 
41 {
42     int i;
43     int rc;
44
45     for (i=0;i<numbytes;) {
46         rc = write(fd,data+i,(numbytes-i));
47         if (rc<=0) {
48             return -1;
49         } else {
50             i+=rc;
51         }
52     }
53     return numbytes;
54 }
55
56
57 int main(int argc, char * argv[])
58 {
59     int fd, bytes, count, flags, mode;
60     char *buff;
61     char *path;
62     char *what;
63         
64     if (argc!=4) {
65         fprintf(stderr,"test_preload r <count> <path-to-file> > output\n");
66         fprintf(stderr,"test_preload w <count> <path-to-file> < input\n");
67         return -1;
68     }
69
70     what = argv[1];
71     count = atoi(argv[2]);
72     path = argv[3];
73     
74     fprintf(stderr,"what:  %s\n",what);
75     fprintf(stderr,"path:  %s\n",path);
76     fprintf(stderr,"count: %d\n",count);
77     
78     if (*what=='w') {
79         flags = O_RDWR;
80         mode = 0; // we are not doing a file creation
81     } else if (*what=='r') {
82         flags = O_RDONLY;
83         mode = 0; // we are not doing a file creation
84     } else {
85         fprintf(stderr,"Don't know how to %s\n",what);
86         return -1;
87     }
88     
89     fprintf(stderr,"flags: %d, mode: %d\n",flags,mode);
90     
91     if ((fd = open(path, flags, mode)) < 0) {
92         fprintf(stderr,"Failed to open file %s\n",argv[3]);
93         return -1;
94     }
95     
96     fprintf(stderr,"Open Done, fd : %d\n",fd);
97
98     buff = (char*)malloc(count);
99     
100     if (!buff) {
101         perror("Can't allocate\n");
102         return -1;
103     }
104     
105     if (*what=='r') { 
106         fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", fd, buff, count);
107     
108         bytes = read_all(fd,buff,count);
109
110         if (bytes < 0) {
111             fprintf(stderr,"Failed to read file %s\n",path);
112             free(buff);
113             close(fd);
114             return -1;
115         }
116
117         fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", 1, buff, count);
118     
119         bytes = write_all(1,buff,count);
120
121         if (bytes<0) { 
122             fprintf(stderr,"Failed to write stdout\n");
123             free(buff);
124             close(fd);
125             return -1;
126         }
127
128     } else if (*what=='w') {
129
130         fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", 0, buff, count);
131     
132         bytes = read_all(0,buff,count);
133
134         if (bytes < 0) {
135             fprintf(stderr,"Failed to read stdin\n");
136             free(buff);
137             close(fd);
138             return -1;
139         }
140
141         fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", fd, buff, count);
142     
143         bytes = write_all(fd,buff,count);
144         
145         if (bytes<0) { 
146             fprintf(stderr,"Failed to write stdout\n");
147             free(buff);
148             close(fd);
149             return -1;
150         }
151
152     }
153
154     free(buff);
155     close(fd);
156     fprintf(stderr,"Close done\n");
157     return 0;
158 }