1 #include <unistd.h> 2 #include <sys/types.h> 3 #include <fcntl.h> 4 #include <stdlib.h> 5 #include <errno.h> 6 #include <string.h> 7 #include "selinux_internal.h" 8 #include "policy.h" 9 #include <stdio.h> 10 #include <limits.h> 11 12 int security_getenforce(void) 13 { 14 int fd, ret, enforce = 0; 15 char path[PATH_MAX]; 16 char buf[20]; 17 18 if (!selinux_mnt) { 19 errno = ENOENT; 20 return -1; 21 } 22 23 snprintf(path, sizeof path, "%s/enforce", selinux_mnt); 24 fd = open(path, O_RDONLY | O_CLOEXEC); 25 if (fd < 0) 26 return -1; 27 28 memset(buf, 0, sizeof buf); 29 ret = read(fd, buf, sizeof buf - 1); 30 close(fd); 31 if (ret < 0) 32 return -1; 33 34 if (sscanf(buf, "%d", &enforce) != 1) 35 return -1; 36 37 return !!enforce; 38 } 39 40 hidden_def(security_getenforce) 41