/* Device File Virtualization Guest Preload Library Test Program (c) Akhil Guliani and William Gross, 2015 Adapted from MPI module (c) 2012 Peter Dinda */ #include #include #include #include #include #include #include #include #include #include "devfile_hc.h" int read_all(int fd, char *data, int numbytes) { int i; int rc; for (i=0;i > output\n"); fprintf(stderr,"test_preload w < input\n"); return -1; } what = argv[1]; count = atoi(argv[2]); path = argv[3]; fprintf(stderr,"what: %s\n",what); fprintf(stderr,"path: %s\n",path); fprintf(stderr,"count: %d\n",count); if (*what=='w') { flags = O_RDWR; mode = 0; // we are not doing a file creation } else if (*what=='r') { flags = O_RDONLY; mode = 0; // we are not doing a file creation } else { fprintf(stderr,"Don't know how to %s\n",what); return -1; } fprintf(stderr,"flags: %d, mode: %d\n",flags,mode); if ((fd = open(path, flags, mode)) < 0) { fprintf(stderr,"Failed to open file %s\n",argv[3]); return -1; } fprintf(stderr,"Open Done, fd : %d\n",fd); buff = (char*)malloc(count); if (!buff) { perror("Can't allocate\n"); return -1; } if (*what=='r') { fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", fd, buff, count); bytes = read_all(fd,buff,count); if (bytes < 0) { fprintf(stderr,"Failed to read file %s\n",path); free(buff); close(fd); return -1; } fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", 1, buff, count); bytes = write_all(1,buff,count); if (bytes<0) { fprintf(stderr,"Failed to write stdout\n"); free(buff); close(fd); return -1; } } else if (*what=='w') { fprintf(stderr,"READ: fd: %d, buff: %p, bytes: %d \n", 0, buff, count); bytes = read_all(0,buff,count); if (bytes < 0) { fprintf(stderr,"Failed to read stdin\n"); free(buff); close(fd); return -1; } fprintf(stderr,"WRITE: fd: %d, buff: %p, bytes: %d \n", fd, buff, count); bytes = write_all(fd,buff,count); if (bytes<0) { fprintf(stderr,"Failed to write stdout\n"); free(buff); close(fd); return -1; } } free(buff); close(fd); fprintf(stderr,"Close done\n"); return 0; }