6307456: UnixFileSystem_md.c use of chmod() and access() should handle EINTR signal appropriately (unix)
authorigerasim
Sat, 16 Mar 2019 13:44:30 -0700
changeset 54156 56e9781e6044
parent 54155 b5a73f22b2bd
child 54157 1a18b8d56d73
6307456: UnixFileSystem_md.c use of chmod() and access() should handle EINTR signal appropriately (unix) Reviewed-by: bpb, dholmes, alanb
src/java.base/unix/native/libjava/UnixFileSystem_md.c
src/java.base/unix/native/libjava/io_util_md.h
--- a/src/java.base/unix/native/libjava/UnixFileSystem_md.c	Sat Mar 16 19:44:12 2019 +0000
+++ b/src/java.base/unix/native/libjava/UnixFileSystem_md.c	Sat Mar 16 13:44:30 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -168,7 +168,9 @@
     default: assert(0);
     }
     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
-        if (access(path, mode) == 0) {
+        int res;
+        RESTARTABLE(access(path, mode), res);
+        if (res == 0) {
             rv = JNI_TRUE;
         }
     } END_PLATFORM_STRING(env, path);
@@ -188,6 +190,7 @@
     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
         int amode = 0;
         int mode;
+        int res;
         switch (access) {
         case java_io_FileSystem_ACCESS_READ:
             if (owneronly)
@@ -215,7 +218,8 @@
                 mode |= amode;
             else
                 mode &= ~amode;
-            if (chmod(path, mode) >= 0) {
+            RESTARTABLE(chmod(path, mode), res);
+            if (res == 0) {
                 rv = JNI_TRUE;
             }
         }
@@ -355,13 +359,15 @@
     closedir(dir);
 
     /* Copy the final results into an appropriately-sized array */
-    old = rv;
-    rv = (*env)->NewObjectArray(env, len, str_class, NULL);
-    if (rv == NULL) {
-        return NULL;
-    }
-    if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
-        return NULL;
+    if (len < maxlen) {
+        old = rv;
+        rv = (*env)->NewObjectArray(env, len, str_class, NULL);
+        if (rv == NULL) {
+            return NULL;
+        }
+        if (JNU_CopyObjectArray(env, rv, old, len) < 0) {
+            return NULL;
+        }
     }
     return rv;
 
@@ -446,8 +452,10 @@
 
     WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
         int mode;
+        int res;
         if (statMode(path, &mode)) {
-            if (chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)) >= 0) {
+            RESTARTABLE(chmod(path, mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)), res);
+            if (res == 0) {
                 rv = JNI_TRUE;
             }
         }
@@ -466,6 +474,7 @@
         struct statfs fsstat;
 #else
         struct statvfs64 fsstat;
+        int res;
 #endif
         memset(&fsstat, 0, sizeof(fsstat));
 #ifdef MACOSX
@@ -488,7 +497,8 @@
             }
         }
 #else
-        if (statvfs64(path, &fsstat) == 0) {
+        RESTARTABLE(statvfs64(path, &fsstat), res);
+        if (res == 0) {
             switch(t) {
             case java_io_FileSystem_SPACE_TOTAL:
                 rv = jlong_mul(long_to_jlong(fsstat.f_frsize),
--- a/src/java.base/unix/native/libjava/io_util_md.h	Sat Mar 16 19:44:12 2019 +0000
+++ b/src/java.base/unix/native/libjava/io_util_md.h	Sat Mar 16 13:44:30 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -93,11 +93,10 @@
 /*
  * Retry the operation if it is interrupted
  */
-#define RESTARTABLE(_cmd, _result) do { \
-    do { \
-        _result = _cmd; \
-    } while((_result == -1) && (errno == EINTR)); \
-} while(0)
+#define RESTARTABLE(_cmd, _result)                \
+    do {                                          \
+        _result = _cmd;                           \
+    } while ((_result == -1) && (errno == EINTR))
 
 void fileDescriptorClose(JNIEnv *env, jobject this);