sfakeroot

manipulate files faking root privileges
git clone git://git.vx21.xyz/sfakeroot
Log | Files | Refs | README | LICENSE

fchownat.c (1243B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 #include <fcntl.h>
      7 #include <errno.h>
      8 
      9 int main(void)
     10 {
     11     char *template = "/tmp/sfakeroot.test.XXXXXX";
     12     char path[strlen(template) + 1];
     13     struct stat st_before;
     14     struct stat st_after;
     15     int fd;
     16 
     17     strcpy(path, template);
     18     printf("test making path %s\n", mktemp(path));
     19 
     20     if ((fd = creat(path, 777)) == -1) {
     21         fprintf(stderr, "creat '%s' failed: %s\n", path, strerror(errno));
     22         return 1;
     23     }
     24     if (stat(path, &st_before) == -1)
     25     {
     26         fprintf(stderr, "stat failed\n");
     27         return 1;
     28     }
     29     if (st_before.st_uid != 0 || st_before.st_gid != 0) {
     30         fprintf(stderr, "uid/gid not 0\n");
     31         return 1;
     32     }
     33 
     34     if (fchownat(AT_FDCWD, path, 42, 43, 0) == -1) {
     35         fprintf(stderr, "chown '%s' failed: %s\n", path, strerror(errno));
     36         return 1;
     37     }
     38 
     39     if (stat(path, &st_after) == -1)
     40     {
     41         fprintf(stderr, "stat failed\n");
     42         return 1;
     43     }
     44     if (st_after.st_uid != 42) {
     45         fprintf(stderr, "uid not 42\n");
     46         return 1;
     47     }
     48     if (st_after.st_gid != 43) {
     49         fprintf(stderr, "gid not 43\n");
     50         return 1;
     51     }
     52 
     53     return 0;
     54 }