btrfs: add test for multiple fsync with adjacent preallocated extents
[xfstests-dev.git] / src / t_access_root.c
1 /* 
2  *  t_access_root.c - trivial test program to show permission bug.
3  *
4  *  Written by Michael Kerrisk - copyright ownership not pursued.
5  *  Sourced from: http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-10/6030.html  
6  */
7
8 #include <limits.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14
15 #define UID 500
16 #define GID 100
17 #define PERM 0
18 #define TESTPATH "/tmp/t_access"
19
20 static void
21 errExit(char *msg)
22 {
23     perror(msg);
24     exit(EXIT_FAILURE);
25 } /* errExit */
26
27 static void
28 accessTest(char *file, int mask, char *mstr)
29 {
30     printf("access(%s, %s) returns %d\n", file, mstr, access(file, mask));
31 } /* accessTest */
32
33 int
34 main(int argc, char *argv[])
35 {
36     int fd, perm, uid, gid;
37     char *testpath;
38     char cmd[PATH_MAX + 20];
39
40     testpath = (argc > 1) ? argv[1] : TESTPATH;
41     perm = (argc > 2) ? strtoul(argv[2], NULL, 8) : PERM;
42     uid = (argc > 3) ? atoi(argv[3]) : UID;
43     gid = (argc > 4) ? atoi(argv[4]) : GID;
44
45     unlink(testpath);
46
47     fd = open(testpath, O_RDWR | O_CREAT, 0);
48     if (fd == -1) errExit("open");
49
50     if (fchown(fd, uid, gid) == -1) errExit("fchown");
51     if (fchmod(fd, perm) == -1) errExit("fchmod");
52     close(fd);
53
54     snprintf(cmd, sizeof(cmd), "ls -l %s", testpath);
55     system(cmd);
56
57     if (seteuid(uid) == -1) errExit("seteuid");
58
59     accessTest(testpath, 0, "0");
60     accessTest(testpath, R_OK, "R_OK");
61     accessTest(testpath, W_OK, "W_OK");
62     accessTest(testpath, X_OK, "X_OK");
63     accessTest(testpath, R_OK | W_OK, "R_OK | W_OK");
64     accessTest(testpath, R_OK | X_OK, "R_OK | X_OK");
65     accessTest(testpath, W_OK | X_OK, "W_OK | X_OK");
66     accessTest(testpath, R_OK | W_OK | X_OK, "R_OK | W_OK | X_OK");
67
68     exit(EXIT_SUCCESS);
69 } /* main */