/* * fdescfs-test.c * * Compile with: * cc -o t t.c -I/usr/local/include -L/usr/local/lib -lsunacl */ #include #include #include #include #include #include #include #include #include #include int f_stat = 0; int f_dir = 0; char * mode2type(mode_t m) { if (S_ISDIR(m)) return "dir"; if (S_ISLNK(m)) return "symlink"; if (S_ISBLK(m)) return "block"; if (S_ISCHR(m)) return "char"; if (S_ISREG(m)) return "file"; return "other"; } int main(int argc, char *argv[]) { int fd, rfd; char buf[2048]; int na, rc; struct stat sb; int i, j; int dfd; char path[2048]; int uid = 0; for (i = 1; i < argc && argv[i][0] == '-'; i++) for (j = 1; argv[i][j]; j++) switch (argv[i][j]) { case 's': f_stat++; break; case 'd': f_dir++; break; default: fprintf(stderr, "%s: Error: -%c: Invalid switch\n", argv[0], argv[i][j]); exit(1); } if (i+1 >= argc) { fprintf(stderr, "%s: Error: Missing required arguments\n", argv[0]); exit(1); } dfd = open(argv[i], O_PATH); if (dfd < 0) { fprintf(stderr, "%s: Error: open(\"%s\", O_PATH): %s\n", argv[0], argv[i], strerror(errno)); exit(1); } else printf("open(\"%s\", O_PATH) -> %d\n", argv[i], dfd); na = facl(dfd, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" facl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", dfd, na, errno, strerror(errno)); } else printf(" facl(%d, ACE_GETACLCNT) -> %d\n", dfd, na); fd = openat(dfd, argv[i+1], O_RDONLY); if (fd < 0) { printf(" openat(%d, \"%s\", O_RDONLY) -> %d [errno=%d (%s)]\n", dfd, argv[i+1], fd, errno, strerror(errno)); } else { printf(" openat(%d, \"%s\", O_RDONLY) -> %d\n", dfd, argv[i+1], fd); na = facl(fd, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" facl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", fd, na, errno, strerror(errno)); } else printf(" facl(%d, ACE_GETACLCNT) -> %d\n", fd, na); sprintf(buf, "/compat/linux/dev/fd/%d", fd); if (f_stat) { for (i = 0; i < f_stat; i++) { if ((rc = stat(buf, &sb)) < 0) { printf(" stat(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); } else { printf(" stat(\"%s\") -> %d [type=%s, size=%lu, uid=%u, gid=%u]\n", buf, rc, mode2type(sb.st_mode), sb.st_size, sb.st_uid, sb.st_gid); if (S_ISLNK(sb.st_mode)) { rc = readlink(buf, path, sizeof(path)); if (rc < 0) { printf(" readlink(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); } else { path[rc] = '\0'; printf(" readlink(\"%s\") -> %d [path=%s]\n", buf, rc, path); } } } } } na = acl(buf, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" acl(\"%s\", ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", buf, na, errno, strerror(errno)); } else printf(" acl(\"%s\", ACE_GETACLCNT) -> %d\n", buf, na); close(fd); } fd = openat(dfd, argv[i+1], O_PATH); if (fd < 0) { printf(" openat(%d, \"%s\", O_PATH) -> %d [errno=%d (%s)]\n", dfd, argv[i+1], fd, errno, strerror(errno)); } else { printf(" openat(%d, \"%s\", O_PATH) -> %d\n", dfd, argv[i+1], fd); na = facl(fd, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" facl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", fd, na, errno, strerror(errno)); } else printf(" facl(%d, ACE_GETACLCNT) -> %d\n", fd, na); sprintf(buf, "/compat/linux/dev/fd/%d", fd); if (f_stat) { for (i = 0; i < f_stat; i++) { if ((rc = stat(buf, &sb)) < 0) { printf(" stat(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); } else { printf(" stat(\"%s\") -> %d [type=%s, size=%lu, uid=%u, gid=%u]\n", buf, rc, mode2type(sb.st_mode), sb.st_size, sb.st_uid, sb.st_gid); if (S_ISLNK(sb.st_mode)) { rc = readlink(buf, path, sizeof(path)); if (rc < 0) { printf(" readlink(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); } else { path[rc] = '\0'; printf(" readlink(\"%s\") -> %d [path=%s]\n", buf, rc, path); } } } } } na = acl(buf, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" acl(\"%s\", ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", buf, na, errno, strerror(errno)); } else printf(" acl(\"%s\", ACE_GETACLCNT) -> %d\n", buf, na); rfd = openat(fd, "", O_EMPTY_PATH); if (rfd < 0) { printf(" openat(%d, \"\", O_EMPTY_PATH) -> %d [errno=%d (%s)]\n", fd, rfd, errno, strerror(errno)); } else { printf(" openat(%d, \"\", O_EMPTY_PATH) -> %d\n", fd, rfd); na = facl(rfd, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" facl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", rfd, na, errno, strerror(errno)); } else printf(" facl(%d, ACE_GETACLCNT) -> %d\n", rfd, na); close(rfd); } rfd = open(buf, O_RDONLY|(f_dir ? O_DIRECTORY : 0)); if (rfd < 0) { printf(" open(\"%s\", O_RDONLY) -> %d [errno=%d (%s)]\n", buf, rfd, errno, strerror(errno)); } else { printf(" open(\"%s\", O_RDONLY) -> %d\n", buf, rfd); na = facl(rfd, ACE_GETACLCNT, 0, NULL); if (na < 0) { printf(" facl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", rfd, na, errno, strerror(errno)); } else printf(" facl(%d, ACE_GETACLCNT) -> %d\n", rfd, na); close(rfd); } close(fd); } exit(0); }