1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include "selinux_internal.h"
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include "policy.h"
10
is_selinux_enabled(void)11 int is_selinux_enabled(void)
12 {
13 /* init_selinuxmnt() gets called before this function. We
14 * will assume that if a selinux file system is mounted, then
15 * selinux is enabled. */
16 #ifdef ANDROID
17 return (selinux_mnt ? 1 : 0);
18 #else
19 return (selinux_mnt && has_selinux_config);
20 #endif
21 }
22
23
24 /*
25 * Function: is_selinux_mls_enabled()
26 * Return: 1 on success
27 * 0 on failure
28 */
is_selinux_mls_enabled(void)29 int is_selinux_mls_enabled(void)
30 {
31 char buf[20], path[PATH_MAX];
32 int fd, ret, enabled = 0;
33
34 if (!selinux_mnt)
35 return enabled;
36
37 snprintf(path, sizeof path, "%s/mls", selinux_mnt);
38 fd = open(path, O_RDONLY | O_CLOEXEC);
39 if (fd < 0)
40 return enabled;
41
42 memset(buf, 0, sizeof buf);
43
44 do {
45 ret = read(fd, buf, sizeof buf - 1);
46 } while (ret < 0 && errno == EINTR);
47 close(fd);
48 if (ret < 0)
49 return enabled;
50
51 if (!strcmp(buf, "1"))
52 enabled = 1;
53
54 return enabled;
55 }
56
57