aboutsummaryrefslogtreecommitdiffstats
path: root/init/util.c
diff options
context:
space:
mode:
authorJean-Baptiste Queru <jbq@google.com>2012-08-08 13:42:20 -0700
committerandroid code review <noreply-gerritcodereview@google.com>2012-08-08 13:42:20 -0700
commitef3f7fa32b451bd6f180c4c1586a98cee41aa296 (patch)
treefe2f59ac3da17fb714f345541c77fa621586afbe /init/util.c
parent8261d27115746f58a6bbf37e6dcf18a093ee8c85 (diff)
parente096e36e50b4b66638ebc4d3c09c2ee35f538dfa (diff)
downloadsystem_core-ef3f7fa32b451bd6f180c4c1586a98cee41aa296.tar.gz
system_core-ef3f7fa32b451bd6f180c4c1586a98cee41aa296.tar.bz2
system_core-ef3f7fa32b451bd6f180c4c1586a98cee41aa296.zip
Merge "Set the SELinux security label on new directories."
Diffstat (limited to 'init/util.c')
-rwxr-xr-xinit/util.c53
1 files changed, 51 insertions, 2 deletions
diff --git a/init/util.c b/init/util.c
index 7d79f398..f994ab9f 100755
--- a/init/util.c
+++ b/init/util.c
@@ -302,12 +302,12 @@ int mkdir_recursive(const char *pathname, mode_t mode)
memcpy(buf, pathname, width);
buf[width] = 0;
if (stat(buf, &info) != 0) {
- ret = mkdir(buf, mode);
+ ret = make_dir(buf, mode);
if (ret && errno != EEXIST)
return ret;
}
}
- ret = mkdir(pathname, mode);
+ ret = make_dir(pathname, mode);
if (ret && errno != EEXIST)
return ret;
return 0;
@@ -463,3 +463,52 @@ void import_kernel_cmdline(int in_qemu,
ptr = x;
}
}
+
+int make_dir(const char *path, mode_t mode)
+{
+ int rc;
+
+#ifdef HAVE_SELINUX
+ char *secontext = NULL;
+
+ if (sehandle) {
+ selabel_lookup(sehandle, &secontext, path, mode);
+ setfscreatecon(secontext);
+ }
+#endif
+
+ rc = mkdir(path, mode);
+
+#ifdef HAVE_SELINUX
+ if (secontext) {
+ int save_errno = errno;
+ freecon(secontext);
+ setfscreatecon(NULL);
+ errno = save_errno;
+ }
+#endif
+ return rc;
+}
+
+int restorecon(const char *pathname)
+{
+#ifdef HAVE_SELINUX
+ char *secontext = NULL;
+ struct stat sb;
+ int i;
+
+ if (is_selinux_enabled() <= 0 || !sehandle)
+ return 0;
+
+ if (lstat(pathname, &sb) < 0)
+ return -errno;
+ if (selabel_lookup(sehandle, &secontext, pathname, sb.st_mode) < 0)
+ return -errno;
+ if (lsetfilecon(pathname, secontext) < 0) {
+ freecon(secontext);
+ return -errno;
+ }
+ freecon(secontext);
+#endif
+ return 0;
+}