Merge "Mutex: add timedLock() method"
diff --git a/adb/adb.h b/adb/adb.h
index 35d8a4d..a37fd5b 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -329,7 +329,7 @@
 #if !ADB_HOST
 void framebuffer_service(int fd, void *cookie);
 // Allow enable-verity to write to system and vendor block devices
-int make_system_and_vendor_block_devices_writable();
+int make_block_device_writable(const char* dev);
 void remount_service(int fd, void *cookie);
 void set_verity_enabled_state_service(int fd, void* cookie);
 #endif
diff --git a/adb/remount_service.c b/adb/remount_service.c
index 9746f9a..d7b0dd1 100644
--- a/adb/remount_service.c
+++ b/adb/remount_service.c
@@ -18,6 +18,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <mntent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -36,38 +37,21 @@
 /* Returns the device used to mount a directory in /proc/mounts */
 static char *find_mount(const char *dir)
 {
-    int fd;
-    int res;
-    char *token = NULL;
-    const char delims[] = "\n";
-    char buf[4096];
+    FILE* fp;
+    struct mntent* mentry;
+    char* device = NULL;
 
-    fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC);
-    if (fd < 0)
+    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
         return NULL;
-
-    buf[sizeof(buf) - 1] = '\0';
-    adb_read(fd, buf, sizeof(buf) - 1);
-    adb_close(fd);
-
-    token = strtok(buf, delims);
-
-    while (token) {
-        char mount_dev[256];
-        char mount_dir[256];
-        int mount_freq;
-        int mount_passno;
-
-        res = sscanf(token, "%255s %255s %*s %*s %d %d\n",
-                     mount_dev, mount_dir, &mount_freq, &mount_passno);
-        mount_dev[255] = 0;
-        mount_dir[255] = 0;
-        if (res == 4 && (strcmp(dir, mount_dir) == 0))
-            return strdup(mount_dev);
-
-        token = strtok(NULL, delims);
     }
-    return NULL;
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (strcmp(dir, mentry->mnt_dir) == 0) {
+            device = strdup(mentry->mnt_fsname);
+            break;
+        }
+    }
+    endmntent(fp);
+    return device;
 }
 
 static int hasVendorPartition()
@@ -79,14 +63,12 @@
     return false;
 }
 
-static int make_block_device_writable(const char* dir)
+int make_block_device_writable(const char* dev)
 {
-    char *dev = 0;
     int fd = -1;
     int OFF = 0;
     int rc = -1;
 
-    dev = find_mount(dir);
     if (!dev)
         goto errout;
 
@@ -104,36 +86,27 @@
     if (fd >= 0) {
         adb_close(fd);
     }
-
-    if (dev) {
-        free(dev);
-    }
     return rc;
 }
 
 /* Init mounts /system as read only, remount to enable writes. */
 static int remount(const char* dir, int* dir_ro)
 {
-    char *dev;
-
-    if (dir_ro == 0) {
-        return 0;
-    }
-
-    if (make_block_device_writable(dir)) {
-        return -1;
-    }
+    char *dev = 0;
+    int rc = -1;
 
     dev = find_mount(dir);
 
-    if (!dev)
-        return -1;
+    if (!dev || make_block_device_writable(dev)) {
+        goto errout;
+    }
 
-    *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL);
+    rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
+    *dir_ro = rc;
 
+errout:
     free(dev);
-
-    return *dir_ro;
+    return rc;
 }
 
 static void write_string(int fd, const char* str)
@@ -141,28 +114,6 @@
     writex(fd, str, strlen(str));
 }
 
-int make_system_and_vendor_block_devices_writable(int fd)
-{
-    char buffer[200];
-    if (make_block_device_writable("/system")) {
-        snprintf(buffer, sizeof(buffer),
-                 "Failed to make system block device writable %s\n",
-                 strerror(errno));
-        write_string(fd, buffer);
-        return -1;
-    }
-
-    if (hasVendorPartition() && make_block_device_writable("/vendor")) {
-        snprintf(buffer, sizeof(buffer),
-                 "Failed to make vendor block device writable: %s\n",
-                 strerror(errno));
-        write_string(fd, buffer);
-        return -1;
-    }
-
-    return 0;
-}
-
 void remount_service(int fd, void *cookie)
 {
     char buffer[200];
diff --git a/adb/set_verity_enable_state_service.c b/adb/set_verity_enable_state_service.c
index 09e2eb9..2660ddd 100644
--- a/adb/set_verity_enable_state_service.c
+++ b/adb/set_verity_enable_state_service.c
@@ -87,9 +87,15 @@
     const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
                                       : VERITY_METADATA_MAGIC_DISABLE;
     uint64_t device_length;
-    int device;
+    int device = -1;
     int retval = -1;
 
+    if (make_block_device_writable(block_device)) {
+        write_console(fd, "Could not make block device %s writable (%s).\n",
+                      block_device, strerror(errno));
+        goto errout;
+    }
+
     device = adb_open(block_device, O_RDWR | O_CLOEXEC);
     if (device == -1) {
         write_console(fd, "Could not open block device %s (%s).\n",
@@ -191,10 +197,6 @@
         goto errout;
     }
 
-    if (enable && make_system_and_vendor_block_devices_writable(fd)) {
-        goto errout;
-    }
-
     /* Loop through entries looking for ones that vold manages */
     for (i = 0; i < fstab->num_entries; i++) {
         if(fs_mgr_is_verified(&fstab->recs[i])) {
diff --git a/include/log/log_read.h b/include/log/log_read.h
index 946711a..1b70aff 100644
--- a/include/log/log_read.h
+++ b/include/log/log_read.h
@@ -100,6 +100,12 @@
         log_time local(*this);
         return local -= T;
     }
+    log_time operator+= (const timespec &T);
+    log_time operator+ (const timespec &T) const
+    {
+        log_time local(*this);
+        return local += T;
+    }
 
     // log_time
     bool operator== (const log_time &T) const
@@ -134,6 +140,12 @@
         log_time local(*this);
         return local -= T;
     }
+    log_time operator+= (const log_time &T);
+    log_time operator+ (const log_time &T) const
+    {
+        log_time local(*this);
+        return local += T;
+    }
 
     uint64_t nsec() const
     {
diff --git a/libcutils/android_reboot.c b/libcutils/android_reboot.c
index 5d98295..aa86206 100644
--- a/libcutils/android_reboot.c
+++ b/libcutils/android_reboot.c
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <mntent.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -33,37 +34,21 @@
  */
 static int remount_ro_done(void)
 {
-    FILE *f;
-    char mount_dev[256];
-    char mount_dir[256];
-    char mount_type[256];
-    char mount_opts[256];
-    int mount_freq;
-    int mount_passno;
-    int match;
+    FILE* fp;
+    struct mntent* mentry;
     int found_rw_fs = 0;
 
-    f = fopen("/proc/mounts", "r");
-    if (! f) {
-        /* If we can't read /proc/mounts, just give up */
+    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
+        /* If we can't read /proc/mounts, just give up. */
         return 1;
     }
-
-    do {
-        match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
-                       mount_dev, mount_dir, mount_type,
-                       mount_opts, &mount_freq, &mount_passno);
-        mount_dev[255] = 0;
-        mount_dir[255] = 0;
-        mount_type[255] = 0;
-        mount_opts[255] = 0;
-        if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw,")) {
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
             found_rw_fs = 1;
             break;
         }
-    } while (match != EOF);
-
-    fclose(f);
+    }
+    endmntent(fp);
 
     return !found_rw_fs;
 }
diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp
index 209a9da..50742df 100644
--- a/liblog/log_time.cpp
+++ b/liblog/log_time.cpp
@@ -150,6 +150,17 @@
     return *this;
 }
 
+log_time log_time::operator+= (const timespec &T) {
+    this->tv_nsec += (unsigned long int)T.tv_nsec;
+    if (this->tv_nsec >= NS_PER_SEC) {
+        this->tv_nsec -= NS_PER_SEC;
+        ++this->tv_sec;
+    }
+    this->tv_sec += T.tv_sec;
+
+    return *this;
+}
+
 log_time log_time::operator-= (const log_time &T) {
     // No concept of negative time, clamp to EPOCH
     if (*this <= T) {
@@ -166,3 +177,14 @@
 
     return *this;
 }
+
+log_time log_time::operator+= (const log_time &T) {
+    this->tv_nsec += T.tv_nsec;
+    if (this->tv_nsec >= NS_PER_SEC) {
+        this->tv_nsec -= NS_PER_SEC;
+        ++this->tv_sec;
+    }
+    this->tv_sec += T.tv_sec;
+
+    return *this;
+}