enabled.c revision 685f4aeeadc0b60f3770404d4f149610d656e3c8
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 <stdio_ext.h>
1013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "policy.h"
1113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
1213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindleint is_selinux_enabled(void)
1313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
1413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* init_selinuxmnt() gets called before this function. We
1513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle 	 * will assume that if a selinux file system is mounted, then
1613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle 	 * selinux is enabled. */
17685f4aeeadc0b60f3770404d4f149610d656e3c8Stephen Smalley	return (selinux_mnt ? 1 : 0);
1813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
1913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlehidden_def(is_selinux_enabled)
2113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle/*
2313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle * Function: is_selinux_mls_enabled()
2413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle * Return:   1 on success
2513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle *	     0 on failure
2613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle */
2713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindleint is_selinux_mls_enabled(void)
2813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
2913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	char buf[20], path[PATH_MAX];
3013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	int fd, ret, enabled = 0;
3113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (!selinux_mnt)
3313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
3413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	snprintf(path, sizeof path, "%s/mls", selinux_mnt);
3613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	fd = open(path, O_RDONLY);
3713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (fd < 0)
3813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
3913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	memset(buf, 0, sizeof buf);
4113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	do {
4313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		ret = read(fd, buf, sizeof buf - 1);
4413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	} while (ret < 0 && errno == EINTR);
4513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	close(fd);
4613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (ret < 0)
4713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return enabled;
4813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (!strcmp(buf, "1"))
5013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		enabled = 1;
5113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return enabled;
5313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
5413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlehidden_def(is_selinux_mls_enabled)
56