blob: 5c252dd1327f5570d9dc8e0e5feb2904b2ad1d41 [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>
9#include <stdio_ext.h>
10#include "policy.h"
11
12int is_selinux_enabled(void)
13{
Joshua Brindle13cd4c82008-08-19 15:30:36 -040014 int enabled = 0;
Stephen Smalley9eb9c932014-02-19 09:16:17 -050015 char * con;
Joshua Brindle13cd4c82008-08-19 15:30:36 -040016
17 /* init_selinuxmnt() gets called before this function. We
18 * will assume that if a selinux file system is mounted, then
19 * selinux is enabled. */
20 if (selinux_mnt) {
21
22 /* Since a file system is mounted, we consider selinux
23 * enabled. If getcon_raw fails, selinux is still enabled.
24 * We only consider it disabled if no policy is loaded. */
25 enabled = 1;
26 if (getcon_raw(&con) == 0) {
27 if (!strcmp(con, "kernel"))
28 enabled = 0;
29 freecon(con);
30 }
Joshua Brindle13cd4c82008-08-19 15:30:36 -040031 }
32
Joshua Brindle13cd4c82008-08-19 15:30:36 -040033 return enabled;
34}
35
36hidden_def(is_selinux_enabled)
37
38/*
39 * Function: is_selinux_mls_enabled()
40 * Return: 1 on success
41 * 0 on failure
42 */
43int is_selinux_mls_enabled(void)
44{
45 char buf[20], path[PATH_MAX];
46 int fd, ret, enabled = 0;
47
48 if (!selinux_mnt)
49 return enabled;
50
51 snprintf(path, sizeof path, "%s/mls", selinux_mnt);
52 fd = open(path, O_RDONLY);
53 if (fd < 0)
54 return enabled;
55
56 memset(buf, 0, sizeof buf);
57
58 do {
59 ret = read(fd, buf, sizeof buf - 1);
60 } while (ret < 0 && errno == EINTR);
61 close(fd);
62 if (ret < 0)
63 return enabled;
64
65 if (!strcmp(buf, "1"))
66 enabled = 1;
67
68 return enabled;
69}
70
71hidden_def(is_selinux_mls_enabled)