2 Device File Virtualization Guest Preload Library
5 (c) Akhil Guliani and William Gross, 2015
7 Adapted from MPI module (c) 2012 Peter Dinda
15 #include <sys/types.h>
21 #include "devfile_hc.h"
24 int read_all(int fd, char *data, int numbytes)
29 for (i=0;i<numbytes;) {
30 rc = read(fd,data+i,(numbytes-i));
40 int write_all(int fd, char *data, int numbytes)
45 for (i=0;i<numbytes;) {
46 rc = write(fd,data+i,(numbytes-i));
57 int main(int argc, char * argv[])
59 int fd, bytes, count, flags, mode;
65 fprintf(stderr,"test_preload r <count> <path-to-file> > output\n");
66 fprintf(stderr,"test_preload w <count> <path-to-file> < input\n");
71 count = atoi(argv[2]);
74 fprintf(stderr,"what: %s\n",what);
75 fprintf(stderr,"path: %s\n",path);
76 fprintf(stderr,"count: %d\n",count);
80 mode = 0; // we are not doing a file creation
81 } else if (*what=='r') {
83 mode = 0; // we are not doing a file creation
85 fprintf(stderr,"Don't know how to %s\n",what);
89 fprintf(stderr,"flags: %d, mode: %d\n",flags,mode);
91 if ((fd = open(path, flags, mode)) < 0) {
92 fprintf(stderr,"Failed to open file %s\n",argv[3]);
96 fprintf(stderr,"Open Done, fd : %d\n",fd);
98 buff = (char*)malloc(count);
101 perror("Can't allocate\n");
106 fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", fd, buff, count);
108 bytes = read_all(fd,buff,count);
111 fprintf(stderr,"Failed to read file %s\n",path);
117 fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", 1, buff, count);
119 bytes = write_all(1,buff,count);
122 fprintf(stderr,"Failed to write stdout\n");
128 } else if (*what=='w') {
130 fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", 0, buff, count);
132 bytes = read_all(0,buff,count);
135 fprintf(stderr,"Failed to read stdin\n");
141 fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", fd, buff, count);
143 bytes = write_all(fd,buff,count);
146 fprintf(stderr,"Failed to write stdout\n");
156 fprintf(stderr,"Close done\n");