Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 #include <fcntl.h>
      6 #include <sys/stat.h>
      7 #include <sys/mman.h>
      8 #include <errno.h>
      9 #include <selinux/selinux.h>
     10 
     11 int load_policy_main(int argc, char **argv)
     12 {
     13     int fd, rc, vers;
     14     struct stat sb;
     15     void *map;
     16     const char *path;
     17 
     18     if (argc != 2) {
     19         fprintf(stderr, "usage:  %s policy-file\n", argv[0]);
     20         exit(1);
     21     }
     22 
     23     path = argv[1];
     24     fd = open(path, O_RDONLY);
     25     if (fd < 0) {
     26         fprintf(stderr, "Could not open %s:  %s\n", path, strerror(errno));
     27         exit(2);
     28     }
     29 
     30     if (fstat(fd, &sb) < 0) {
     31         fprintf(stderr, "Could not stat %s:  %s\n", path, strerror(errno));
     32         exit(3);
     33     }
     34 
     35     map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
     36     if (map == MAP_FAILED) {
     37         fprintf(stderr, "Could not mmap %s:  %s\n", path, strerror(errno));
     38         exit(4);
     39     }
     40 
     41     rc = security_load_policy(map, sb.st_size);
     42     if (rc < 0) {
     43         fprintf(stderr, "Could not load %s:  %s\n", path, strerror(errno));
     44         exit(5);
     45     }
     46     munmap(map, sb.st_size);
     47     close(fd);
     48     exit(0);
     49 }
     50