1#include <unistd.h>
2#include <sys/types.h>
3#include <fcntl.h>
4#include <stdlib.h>
5#include <errno.h>
6#include <string.h>
7#include "selinux_internal.h"
8#include <stdio.h>
9#include "policy.h"
10#include <limits.h>
11
12int security_policyvers(void)
13{
14	int fd, ret;
15	char path[PATH_MAX];
16	char buf[20];
17	unsigned vers = DEFAULT_POLICY_VERSION;
18
19	if (!selinux_mnt) {
20		errno = ENOENT;
21		return -1;
22	}
23
24	snprintf(path, sizeof path, "%s/policyvers", selinux_mnt);
25	fd = open(path, O_RDONLY | O_CLOEXEC);
26	if (fd < 0) {
27		if (errno == ENOENT)
28			return vers;
29		else
30			return -1;
31	}
32	memset(buf, 0, sizeof buf);
33	ret = read(fd, buf, sizeof buf - 1);
34	close(fd);
35	if (ret < 0)
36		return -1;
37
38	if (sscanf(buf, "%u", &vers) != 1)
39		return -1;
40
41	return vers;
42}
43
44