6 static inline void selinux_setfilecon(char *file, unsigned int mode) {}
7 static inline void selinux_setfscreatecon(char *file, unsigned int mode) {}
8 static inline void selinux_init(void) {}
9 static inline void selinux_restore(void) {}
13 #include <selinux/selinux.h>
18 static int selinux_enabled=-1;
19 static security_context_t prev_scontext=NULL;
21 static inline int is_selinux_running(void)
23 if (selinux_enabled == -1)
24 return selinux_enabled = is_selinux_enabled() > 0;
25 return selinux_enabled;
28 static inline int selinux_get_media(char *path, int mode, char **media)
32 char mediabuf[PATH_MAX];
35 if (!(mode && S_IFBLK)) {
38 snprintf(buf,sizeof(buf), "/proc/ide/%s/media", basename(path));
41 if (fgets(mediabuf,sizeof(mediabuf), fp)) {
42 int size = strlen(mediabuf);
44 if (isspace(mediabuf[size])) {
50 *media = strdup(mediabuf);
51 info("selinux_get_media(%s)->%s \n", path, *media);
60 static inline void selinux_setfilecon(char *file, unsigned int mode)
62 if (is_selinux_running()) {
63 security_context_t scontext=NULL;
65 int ret=selinux_get_media(file, mode, &media);
67 ret = matchmediacon(media, &scontext);
71 if (matchpathcon(file, mode, &scontext) < 0) {
72 dbg("matchpathcon(%s) failed\n", file);
75 if (setfilecon(file, scontext) < 0)
76 dbg("setfiles %s failed with error '%s'",
77 file, strerror(errno));
82 static inline void selinux_setfscreatecon(char *file, unsigned int mode)
85 security_context_t scontext=NULL;
87 if (is_selinux_running()) {
89 int ret = selinux_get_media(file, mode, &media);
92 ret = matchmediacon(media, &scontext);
97 if (matchpathcon(file, mode, &scontext) < 0) {
98 dbg("matchpathcon(%s) failed\n", file);
102 retval = setfscreatecon(scontext);
104 dbg("setfiles %s failed with error '%s'",
105 file, strerror(errno));
110 static inline void selinux_init(void)
113 * record the present security context, for file-creation
114 * restoration creation purposes.
116 if (is_selinux_running()) {
117 if (getfscreatecon(&prev_scontext) < 0) {
118 dbg("getfscreatecon failed\n");
120 prev_scontext = NULL;
124 static inline void selinux_restore(void)
126 if (is_selinux_running()) {
127 /* reset the file create context to its former glory */
128 if (setfscreatecon(prev_scontext) < 0)
129 dbg("setfscreatecon failed\n");
131 freecon(prev_scontext);
132 prev_scontext = NULL;
137 #endif /* USE_SELINUX */
139 #endif /* SELINUX_H */