Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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 <stdio_ext.h> |
| 10 | #include "policy.h" |
| 11 | |
| 12 | int is_selinux_enabled(void) |
| 13 | { |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 14 | int enabled = 0; |
Stephen Smalley | 9eb9c93 | 2014-02-19 09:16:17 -0500 | [diff] [blame] | 15 | char * con; |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 16 | |
| 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 Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 33 | return enabled; |
| 34 | } |
| 35 | |
| 36 | hidden_def(is_selinux_enabled) |
| 37 | |
| 38 | /* |
| 39 | * Function: is_selinux_mls_enabled() |
| 40 | * Return: 1 on success |
| 41 | * 0 on failure |
| 42 | */ |
| 43 | int 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 | |
| 71 | hidden_def(is_selinux_mls_enabled) |