OSDN Git Service

init: fix write_file checkreqprot logic error
authorNick Kralevich <nnk@google.com>
Sat, 25 Apr 2015 21:10:03 +0000 (14:10 -0700)
committerNick Kralevich <nnk@google.com>
Sat, 25 Apr 2015 22:36:13 +0000 (15:36 -0700)
write_file() returned -errno on error, not -1. Callers who check for
-1 would falsely believe that the write was successful when it wasn't.
Fixup write_file so that it return -1 on error consistent
with other functions.

Change-Id: Ic51aaf8678d8d97b2606bd171f11b3b11f642e39

init/init.cpp
init/util.cpp

index 377b89c..b79da89 100644 (file)
@@ -947,12 +947,6 @@ static void selinux_initialize(bool in_kernel_domain) {
     }
 
     if (in_kernel_domain) {
-        if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
-            ERROR("couldn't write to /sys/fs/selinux/checkreqprot: %s\n",
-                  strerror(errno));
-            security_failure();
-        }
-
         INFO("Loading SELinux policy...\n");
         if (selinux_android_load_policy() < 0) {
             ERROR("failed to load policy: %s\n", strerror(errno));
@@ -962,6 +956,10 @@ static void selinux_initialize(bool in_kernel_domain) {
         bool is_enforcing = selinux_is_enforcing();
         security_setenforce(is_enforcing);
 
+        if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
+            security_failure();
+        }
+
         NOTICE("(Initializing SELinux %s took %.2fs.)\n",
                is_enforcing ? "enforcing" : "non-enforcing", t.duration());
     } else {
index 3b49b30..332aa2a 100644 (file)
@@ -179,9 +179,13 @@ bool read_file(const char* path, std::string* content) {
 int write_file(const char* path, const char* content) {
     int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
     if (fd == -1) {
-        return -errno;
+        NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
+        return -1;
+    }
+    int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
+    if (result == -1) {
+        NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
     }
-    int result = android::base::WriteStringToFd(content, fd) ? 0 : -errno;
     TEMP_FAILURE_RETRY(close(fd));
     return result;
 }