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> |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 9 | #include "policy.h" |
| 10 | |
| 11 | int is_selinux_enabled(void) |
| 12 | { |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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. */ |
Stephen Smalley | c08c4ea | 2015-05-11 10:18:16 -0400 | [diff] [blame] | 16 | #ifdef ANDROID |
Stephen Smalley | 685f4ae | 2015-04-17 09:25:51 -0400 | [diff] [blame] | 17 | return (selinux_mnt ? 1 : 0); |
Stephen Smalley | c08c4ea | 2015-05-11 10:18:16 -0400 | [diff] [blame] | 18 | #else |
| 19 | return (selinux_mnt && has_selinux_config); |
| 20 | #endif |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | hidden_def(is_selinux_enabled) |
| 24 | |
| 25 | /* |
| 26 | * Function: is_selinux_mls_enabled() |
| 27 | * Return: 1 on success |
| 28 | * 0 on failure |
| 29 | */ |
| 30 | int 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 Kralevich | 64afa1a | 2016-12-11 09:30:16 -0800 | [diff] [blame] | 39 | fd = open(path, O_RDONLY | O_CLOEXEC); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 40 | 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 | |
| 58 | hidden_def(is_selinux_mls_enabled) |