Merge "DO NOT MERGE Add event log tag for cert pin failures." into jb-mr1-dev
diff --git a/include/cutils/fs.h b/include/cutils/fs.h
index 04c8839..fd5296b 100644
--- a/include/cutils/fs.h
+++ b/include/cutils/fs.h
@@ -19,6 +19,21 @@
 
 #include <sys/types.h>
 
+/*
+ * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
+ * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
+ * not already defined, then define it here.
+ */
+#ifndef TEMP_FAILURE_RETRY
+/* Used to retry syscalls that can return EINTR. */
+#define TEMP_FAILURE_RETRY(exp) ({         \
+    typeof (exp) _rc;                      \
+    do {                                   \
+        _rc = (exp);                       \
+    } while (_rc == -1 && errno == EINTR); \
+    _rc; })
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 6521cbe..33ecd9a 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -87,6 +87,9 @@
 
 #define AID_USER        100000  /* offset for uid ranges for each user */
 
+#define AID_SHARED_GID_START 50000 /* start of gids for apps in each user to share */
+#define AID_SHARED_GID_END   59999 /* start of gids for apps in each user to share */
+
 #if !defined(EXCLUDE_FS_CONFIG_STRUCTURES)
 struct android_id_info {
     const char *name;
diff --git a/libcutils/fs.c b/libcutils/fs.c
index 1788eca..a9889b2 100644
--- a/libcutils/fs.c
+++ b/libcutils/fs.c
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "cutils"
+
 #include <cutils/fs.h>
 #include <cutils/log.h>
 
@@ -31,11 +33,11 @@
 int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
     // Check if path needs to be created
     struct stat sb;
-    if (lstat(path, &sb) == -1) {
+    if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
         if (errno == ENOENT) {
             goto create;
         } else {
-            ALOGE("Failed to stat(%s): %s", path, strerror(errno));
+            ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
             return -1;
         }
     }
@@ -52,17 +54,17 @@
     }
 
 create:
-    if (mkdir(path, mode) == -1) {
+    if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
         ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
         return -1;
     }
 
 fixup:
-    if (chmod(path, mode) == -1) {
-        ALOGE("Failed to chown(%s, %d): %s", path, mode, strerror(errno));
+    if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
+        ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
         return -1;
     }
-    if (chown(path, uid, gid) == -1) {
+    if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
         ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
         return -1;
     }
@@ -71,14 +73,14 @@
 }
 
 int fs_read_atomic_int(const char* path, int* out_value) {
-    int fd = open(path, O_RDONLY);
+    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
     if (fd == -1) {
         ALOGE("Failed to read %s: %s", path, strerror(errno));
         return -1;
     }
 
     char buf[BUF_SIZE];
-    if (read(fd, buf, BUF_SIZE) == -1) {
+    if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
         ALOGE("Failed to read %s: %s", path, strerror(errno));
         goto fail;
     }
@@ -102,7 +104,7 @@
         return -1;
     }
 
-    int fd = mkstemp(temp);
+    int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
     if (fd == -1) {
         ALOGE("Failed to open %s: %s", temp, strerror(errno));
         return -1;
@@ -114,7 +116,7 @@
         ALOGE("Value %d too large: %s", value, strerror(errno));
         goto fail;
     }
-    if (write(fd, buf, len) < len) {
+    if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
         ALOGE("Failed to write %s: %s", temp, strerror(errno));
         goto fail;
     }