init.c revision f074036424618c130dacb3464465a8b40bffef58
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <stdlib.h>
5#include <errno.h>
6#include <ctype.h>
7#include <stdio.h>
8#include <dlfcn.h>
9#include <sys/vfs.h>
10#include <stdint.h>
11#include <limits.h>
12
13#include "dso.h"
14#include "policy.h"
15#include "selinux_internal.h"
16
17char *selinux_mnt = NULL;
18int selinux_page_size = 0;
19
20static void init_selinuxmnt(void)
21{
22	char buf[BUFSIZ], *p;
23	FILE *fp=NULL;
24	struct statfs sfbuf;
25	int rc;
26	char *bufp;
27	int exists = 0;
28
29	if (selinux_mnt)
30		return;
31
32	/* We check to see if the preferred mount point for selinux file
33	 * system has a selinuxfs. */
34	do {
35		rc = statfs(SELINUXMNT, &sfbuf);
36	} while (rc < 0 && errno == EINTR);
37	if (rc == 0) {
38		if ((uint32_t)sfbuf.f_type == (uint32_t)SELINUX_MAGIC) {
39			selinux_mnt = strdup(SELINUXMNT);
40			return;
41		}
42	}
43
44	/* Drop back to detecting it the long way. */
45	fp = fopen("/proc/filesystems", "r");
46	if (!fp)
47		return;
48
49	while ((bufp = fgets(buf, sizeof buf - 1, fp)) != NULL) {
50		if (strstr(buf, "selinuxfs")) {
51			exists = 1;
52			break;
53		}
54	}
55
56	if (!exists)
57		goto out;
58
59	fclose(fp);
60
61	/* At this point, the usual spot doesn't have an selinuxfs so
62	 * we look around for it */
63	fp = fopen("/proc/mounts", "r");
64	if (!fp)
65		goto out;
66
67	while ((bufp = fgets(buf, sizeof buf - 1, fp)) != NULL) {
68		char *tmp;
69		p = strchr(buf, ' ');
70		if (!p)
71			goto out;
72		p++;
73		tmp = strchr(p, ' ');
74		if (!tmp)
75			goto out;
76		if (!strncmp(tmp + 1, "selinuxfs ", 10)) {
77			*tmp = '\0';
78			break;
79		}
80	}
81
82	/* If we found something, dup it */
83	if (bufp)
84		selinux_mnt = strdup(p);
85
86      out:
87	if (fp)
88		fclose(fp);
89	return;
90}
91
92void fini_selinuxmnt(void)
93{
94	free(selinux_mnt);
95	selinux_mnt = NULL;
96}
97
98void set_selinuxmnt(char *mnt)
99{
100	selinux_mnt = strdup(mnt);
101}
102
103hidden_def(set_selinuxmnt)
104
105static void init_lib(void) __attribute__ ((constructor));
106static void init_lib(void)
107{
108	selinux_page_size = sysconf(_SC_PAGE_SIZE);
109	init_selinuxmnt();
110}
111
112static void fini_lib(void) __attribute__ ((destructor));
113static void fini_lib(void)
114{
115	fini_selinuxmnt();
116}
117