From 51f5988d94ab3488080c4b9350b9d37b2ea19140 Mon Sep 17 00:00:00 2001
Date: Fri, 19 Apr 2019 16:13:29 +0000
Subject: [PATCH] 8160300: aarch32: JNI layer to initialize native nio constants

Bug url: https://bugs.openjdk.java.net/browse/JDK-8160300

---
 jdk/make/gensrc/GensrcMisc.gmk                |   4 +
 jdk/make/lib/NioLibraries.gmk                 |   3 +-
 jdk/make/mapfiles/libnio/mapfile-linux        |   1 +
 .../genconstants/fs/genCrossUnixConstants.c   | 139 ++++++++++++++++++
 .../solaris/native/sun/nio/fs/UnixConstants.c | 114 ++++++++++++++
 5 files changed, 260 insertions(+), 1 deletion(-)
 create mode 100644 jdk/make/src/native/genconstants/fs/genCrossUnixConstants.c
 create mode 100644 jdk/src/solaris/native/sun/nio/fs/UnixConstants.c

diff --git a/jdk/make/gensrc/GensrcMisc.gmk b/jdk/make/gensrc/GensrcMisc.gmk
index 84a3c27e7d..02ca9ab7e0 100644
--- a/jdk/make/gensrc/GensrcMisc.gmk
+++ b/jdk/make/gensrc/GensrcMisc.gmk
@@ -102,7 +102,11 @@ ifneq ($(OPENJDK_TARGET_OS), windows)
   GENSRC_MISC += $(JDK_OUTPUTDIR)/gensrc/sun/nio/fs/UnixConstants.java
 
   GENSRC_UC_SRC := $(JDK_TOPDIR)/make/src/native/genconstants/fs
+ifneq ($(OPENJDK_TARGET_OS), linux)
   GENSRC_UC_SRC_FILE := genUnixConstants.c
+else
+  GENSRC_UC_SRC_FILE := genCrossUnixConstants.c
+endif
   GENSRC_UC_BIN := $(JDK_OUTPUTDIR)/btnative/genUnixConstants
 
   UC_COPYRIGHT_YEARS = $(shell $(CAT) $(GENSRC_UC_SRC)/$(GENSRC_UC_SRC_FILE) | \
diff --git a/jdk/make/lib/NioLibraries.gmk b/jdk/make/lib/NioLibraries.gmk
index 6c9c46a3f3..3d6c9ffa30 100644
--- a/jdk/make/lib/NioLibraries.gmk
+++ b/jdk/make/lib/NioLibraries.gmk
@@ -74,7 +74,8 @@ ifeq ($(OPENJDK_TARGET_OS), linux)
       LinuxNativeDispatcher.c \
       LinuxWatchService.c \
       UnixCopyFile.c \
-      UnixNativeDispatcher.c
+      UnixNativeDispatcher.c \
+      UnixConstants.c
 endif
 
 ifeq ($(OPENJDK_TARGET_OS), macosx)
diff --git a/jdk/make/mapfiles/libnio/mapfile-linux b/jdk/make/mapfiles/libnio/mapfile-linux
index 30f795f6f5..3373a45a82 100644
--- a/jdk/make/mapfiles/libnio/mapfile-linux
+++ b/jdk/make/mapfiles/libnio/mapfile-linux
@@ -201,6 +201,7 @@ SUNWprivate_1.1 {
 		Java_sun_nio_fs_UnixNativeDispatcher_getpwnam0;
 		Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0;
 		Java_sun_nio_fs_UnixCopyFile_transfer;
+        Java_sun_nio_fs_UnixConstants_init;
 		handleSocketError;
 
 	local:
diff --git a/jdk/make/src/native/genconstants/fs/genCrossUnixConstants.c b/jdk/make/src/native/genconstants/fs/genCrossUnixConstants.c
new file mode 100644
index 0000000000..64e14592b4
--- /dev/null
+++ b/jdk/make/src/native/genconstants/fs/genCrossUnixConstants.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+/**
+ * Generates sun.nio.fs.UnixConstants class
+ */
+static const char* cnames[]={
+    // open flags
+    "O_RDONLY",
+    "O_WRONLY",
+    "O_RDWR",
+    "O_APPEND",
+    "O_CREAT",
+    "O_EXCL",
+    "O_TRUNC",
+    "O_SYNC",
+    "O_DSYNC",
+    "O_NOFOLLOW",
+    // mode masks
+    "S_IRUSR",
+    "S_IWUSR",
+    "S_IXUSR",
+    "S_IRGRP",
+    "S_IWGRP",
+    "S_IXGRP",
+    "S_IROTH",
+    "S_IWOTH",
+    "S_IXOTH",
+    "S_IFMT",
+    "S_IFREG",
+    "S_IFDIR",
+    "S_IFLNK",
+    "S_IFCHR",
+    "S_IFBLK",
+    "S_IFIFO",
+    "S_IAMB",
+    // access modes
+    "R_OK",
+    "W_OK",
+    "X_OK",
+    "F_OK",
+
+    // errors
+    "ENOENT",
+    "EACCES",
+    "EEXIST",
+    "ENOTDIR",
+    "EINVAL",
+    "EXDEV",
+    "EISDIR",
+    "ENOTEMPTY",
+    "ENOSPC",
+    "EAGAIN",
+    "ENOSYS",
+    "ELOOP",
+    "EROFS",
+    "ENODATA",
+    "ERANGE",
+    "EMFILE",
+
+    // flags used with openat/unlinkat/etc.
+    "AT_SYMLINK_NOFOLLOW",
+    "AT_REMOVEDIR"
+};
+static void out(const char* s) {
+    printf("%s\n", s);
+}
+
+static void declTemp(const char* name) {
+    printf("    private static int p%s=0;\n",name);
+}
+
+static void declConst(const char* name) {
+    printf("    static final int %s = p%s;\n", name, name);
+}
+
+static void init() {
+    out("    private static native void init();");
+    out("    static {");
+    out("        AccessController.doPrivileged(new PrivilegedAction<Void>() {");
+    out("            public Void run() {");
+    out("                System.loadLibrary(\"nio\");");
+    out("                return null;");
+    out("        }});");
+    out("        init();");
+    out("    }");
+}
+
+int main(int argc, const char* argv[]) {
+    int i;
+    out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT                                  ");
+    out("package sun.nio.fs;                                                            ");
+    out("import java.security.AccessController;                                         ");
+    out("import java.security.PrivilegedAction;                                         ");
+    out("class UnixConstants {                                                          ");
+    out("    private UnixConstants() { }                                                ");
+
+    // define private intermediate constants
+    for(i=0; i<(int)sizeof(cnames)/sizeof(cnames[0]);i++)
+        declTemp(cnames[i]);
+
+    init();
+
+    // define real unix constants
+    for(i=0; i<(int)sizeof(cnames)/sizeof(cnames[0]);i++)
+        declConst(cnames[i]);
+
+    out("}                                                                              ");
+
+    return 0;
+}
diff --git a/jdk/src/solaris/native/sun/nio/fs/UnixConstants.c b/jdk/src/solaris/native/sun/nio/fs/UnixConstants.c
new file mode 100644
index 0000000000..95545405ce
--- /dev/null
+++ b/jdk/src/solaris/native/sun/nio/fs/UnixConstants.c
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include "jni.h"
+
+#include "sun_nio_fs_UnixConstants.h"
+
+#define INIT_CONST(ENV, CLS, VAL) INIT_CONST_NAME(ENV, CLS, VAL, p ## VAL)
+
+#define INIT_CONST_NAME(ENV, CLS, VAL, NAME)                                \
+{                                                                           \
+    jfieldID fID = (*(ENV))->GetStaticFieldID((ENV), (CLS), #NAME, "I");    \
+    if (fID != 0) {                                                          \
+        (*(ENV))->SetStaticIntField((ENV), (CLS), fID, (VAL));              \
+    }                                                                       \
+}                                                                           \
+
+/**
+ * Initialization of the UnixConstants fields:
+ * file open flags, modes and error codes
+ */
+JNIEXPORT void JNICALL
+Java_sun_nio_fs_UnixConstants_init(JNIEnv* env, jclass cls) {
+    // open flags
+    INIT_CONST(env, cls, O_RDONLY);
+    INIT_CONST(env, cls, O_WRONLY);
+    INIT_CONST(env, cls, O_RDWR);
+    INIT_CONST(env, cls, O_APPEND);
+    INIT_CONST(env, cls, O_CREAT);
+    INIT_CONST(env, cls, O_EXCL);
+    INIT_CONST(env, cls, O_TRUNC);
+    INIT_CONST(env, cls, O_SYNC);
+    INIT_CONST(env, cls, O_DSYNC);
+    INIT_CONST(env, cls, O_NOFOLLOW);
+
+    // mode masks
+    INIT_CONST(env, cls, S_IRUSR);
+    INIT_CONST(env, cls, S_IWUSR);
+    INIT_CONST(env, cls, S_IXUSR);
+    INIT_CONST(env, cls, S_IRGRP);
+    INIT_CONST(env, cls, S_IWGRP);
+    INIT_CONST(env, cls, S_IXGRP);
+    INIT_CONST(env, cls, S_IROTH);
+    INIT_CONST(env, cls, S_IWOTH);
+    INIT_CONST(env, cls, S_IXOTH);
+    INIT_CONST(env, cls, S_IFMT);
+    INIT_CONST(env, cls, S_IFREG);
+    INIT_CONST(env, cls, S_IFDIR);
+    INIT_CONST(env, cls, S_IFLNK);
+    INIT_CONST(env, cls, S_IFCHR);
+    INIT_CONST(env, cls, S_IFBLK);
+    INIT_CONST(env, cls, S_IFIFO);
+    INIT_CONST_NAME(env, cls, (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH), pS_IAMB);
+
+    // access modes
+    INIT_CONST(env, cls, R_OK);
+    INIT_CONST(env, cls, W_OK);
+    INIT_CONST(env, cls, X_OK);
+    INIT_CONST(env, cls, F_OK);
+
+    // errors
+    INIT_CONST(env, cls, ENOENT);
+    INIT_CONST(env, cls, EACCES);
+    INIT_CONST(env, cls, EEXIST);
+    INIT_CONST(env, cls, ENOTDIR);
+    INIT_CONST(env, cls, EINVAL);
+    INIT_CONST(env, cls, EXDEV);
+    INIT_CONST(env, cls, EISDIR);
+    INIT_CONST(env, cls, ENOTEMPTY);
+    INIT_CONST(env, cls, ENOSPC);
+    INIT_CONST(env, cls, EAGAIN);
+    INIT_CONST(env, cls, ENOSYS);
+    INIT_CONST(env, cls, ELOOP);
+    INIT_CONST(env, cls, EROFS);
+    INIT_CONST(env, cls, ERANGE);
+    INIT_CONST(env, cls, EMFILE);
+
+#if defined(ENODATA)
+    INIT_CONST(env, cls, ENODATA);
+#endif
+
+#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
+    INIT_CONST(env, cls, AT_SYMLINK_NOFOLLOW);
+    INIT_CONST(env, cls, AT_REMOVEDIR);
+#endif
+
+}
-- 
2.19.0

