blob: dd628fba86a81c124cae6d2caae1220d95d16a46 [file] [log] [blame]
Joshua Brindle13cd4c82008-08-19 15:30:36 -04001#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>
Joshua Brindle13cd4c82008-08-19 15:30:36 -04009#include "policy.h"
10
11int is_selinux_enabled(void)
12{
Joshua Brindle13cd4c82008-08-19 15:30:36 -040013 /* init_selinuxmnt() gets called before this function. We
14 * will assume that if a selinux file system is mounted, then
15 * selinux is enabled. */
Stephen Smalleyc08c4ea2015-05-11 10:18:16 -040016#ifdef ANDROID
Stephen Smalley685f4ae2015-04-17 09:25:51 -040017 return (selinux_mnt ? 1 : 0);
Stephen Smalleyc08c4ea2015-05-11 10:18:16 -040018#else
19 return (selinux_mnt && has_selinux_config);
20#endif
Joshua Brindle13cd4c82008-08-19 15:30:36 -040021}
22
23hidden_def(is_selinux_enabled)
24
25/*
26 * Function: is_selinux_mls_enabled()
27 * Return: 1 on success
28 * 0 on failure
29 */
30int is_selinux_mls_enabled(void)
31{
32 char buf[20], path[PATH_MAX];
33 int fd, ret, enabled = 0;
34
35 if (!selinux_mnt)
36 return enabled;
37
38 snprintf(path, sizeof path, "%s/mls", selinux_mnt);
Nick Kralevich64afa1a2016-12-11 09:30:16 -080039 fd = open(path, O_RDONLY | O_CLOEXEC);
Joshua Brindle13cd4c82008-08-19 15:30:36 -040040 if (fd < 0)
41 return enabled;
42
43 memset(buf, 0, sizeof buf);
44
45 do {
46 ret = read(fd, buf, sizeof buf - 1);
47 } while (ret < 0 && errno == EINTR);
48 close(fd);
49 if (ret < 0)
50 return enabled;
51
52 if (!strcmp(buf, "1"))
53 enabled = 1;
54
55 return enabled;
56}
57
58hidden_def(is_selinux_mls_enabled)