113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <unistd.h>
213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <fcntl.h>
313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <string.h>
413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "selinux_internal.h"
513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <stdlib.h>
613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <errno.h>
713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <limits.h>
813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <stdio.h>
913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "policy.h"
1013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
1113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindleint is_selinux_enabled(void)
1213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
1313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* init_selinuxmnt() gets called before this function. We
1413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle 	 * will assume that if a selinux file system is mounted, then
1513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle 	 * selinux is enabled. */
16c08c4eacab8d55598b9e5caaef8a871a7a476cabStephen Smalley#ifdef ANDROID
17685f4aeeadc0b60f3770404d4f149610d656e3c8Stephen Smalley	return (selinux_mnt ? 1 : 0);
18c08c4eacab8d55598b9e5caaef8a871a7a476cabStephen Smalley#else
19c08c4eacab8d55598b9e5caaef8a871a7a476cabStephen Smalley	return (selinux_mnt && has_selinux_config);
20c08c4eacab8d55598b9e5caaef8a871a7a476cabStephen Smalley#endif
2113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
2213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlehidden_def(is_selinux_enabled)
2413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle/*
2613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle * Function: is_selinux_mls_enabled()
2713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle * Return:   1 on success
2813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle *	     0 on failure
2913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle */
3013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindleint is_selinux_mls_enabled(void)
3113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
3213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	char buf[20], path[PATH_MAX];
3313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	int fd, ret, enabled = 0;
3413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (!selinux_mnt)
3613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
3713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	snprintf(path, sizeof path, "%s/mls", selinux_mnt);
3913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	fd = open(path, O_RDONLY);
4013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (fd < 0)
4113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
4213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	memset(buf, 0, sizeof buf);
4413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	do {
4613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		ret = read(fd, buf, sizeof buf - 1);
4713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	} while (ret < 0 && errno == EINTR);
4813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	close(fd);
4913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (ret < 0)
5013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
5113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (!strcmp(buf, "1"))
5313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		enabled = 1;
5413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return enabled;
5613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
5713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlehidden_def(is_selinux_mls_enabled)
59