#include #include #include #include #include #include #include #define LOG_ERR(func, err) \ do { \ fprintf(stderr, "%s : returned error (%s)\n", func, strerror(err)); \ exit(err); \ } while (0) int fd; struct flock lock; char *buf = "ten bytes!"; char *fname = "/mnt/glusterfs/0/mand.lock"; int open_flags, child, err, status, blocked = 0; int do_child(char *argv[]) { /* Initialize file open flags */ if (strcmp(argv[2], "BLOCK") == 0) open_flags = O_RDWR; else if (strcmp(argv[2], "TRUNC") == 0) open_flags = O_RDWR | O_TRUNC | O_NONBLOCK; else if (strcmp(argv[2], "NONE") == 0) open_flags = O_RDWR | O_NONBLOCK; else LOG_ERR("Invalid option:", EINVAL); /* Open the file */ fd = open(fname, open_flags); if (fd == -1) LOG_ERR("Child open", errno); /* Perform the file operation*/ if (strcmp(argv[3], "READ") == 0) { buf = NULL; err = read(fd, buf, 10); if (err == -1) LOG_ERR("Child read", errno); } else if (strcmp(argv[3], "WRITE") == 0) { err = write(fd, buf, 10); if (err == -1) LOG_ERR("Child write", errno); } else if (strcmp(argv[3], "FTRUNCATE") == 0) { err = ftruncate(fd, 5); if (err) LOG_ERR("Child ftruncate", errno); } else LOG_ERR("Invalid operation:", EINVAL); /* Close child fd */ err = close(fd); if (err) LOG_ERR("Child close", errno); /* Exit success */ exit(0); } int main(int argc, char *argv[]) { if (argc < 4) { fprintf(stderr, "Wrong usage: Use as ./mandatory-lock " " " "