18290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <stdio.h>
28290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <stdlib.h>
38290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <string.h>
48290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <unistd.h>
58290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <fcntl.h>
68290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <sys/stat.h>
78290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <sys/mman.h>
88290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <errno.h>
98290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <selinux/selinux.h>
108290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
118290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalleyint load_policy_main(int argc, char **argv)
128290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley{
138290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    int fd, rc, vers;
148290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    struct stat sb;
158290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    void *map;
168290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    const char *path;
178290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
188290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (argc != 2) {
198290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        fprintf(stderr, "usage:  %s policy-file\n", argv[0]);
208290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        exit(1);
218290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
228290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
238290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    path = argv[1];
248290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    fd = open(path, O_RDONLY);
258290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (fd < 0) {
268290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        fprintf(stderr, "Could not open %s:  %s\n", path, strerror(errno));
278290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        exit(2);
288290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
298290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
308290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (fstat(fd, &sb) < 0) {
318290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        fprintf(stderr, "Could not stat %s:  %s\n", path, strerror(errno));
328290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        exit(3);
338290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
348290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
358290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
368290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (map == MAP_FAILED) {
378290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        fprintf(stderr, "Could not mmap %s:  %s\n", path, strerror(errno));
388290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        exit(4);
398290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
408290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
418290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    rc = security_load_policy(map, sb.st_size);
428290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (rc < 0) {
438290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        fprintf(stderr, "Could not load %s:  %s\n", path, strerror(errno));
448290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        exit(5);
458290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
468290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    munmap(map, sb.st_size);
478290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    close(fd);
488290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    exit(0);
498290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley}
50