diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSystemPropertiesSupport.java index 0de2a84f767a..a93cd35ff9f0 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSystemPropertiesSupport.java @@ -31,7 +31,6 @@ import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue; import com.oracle.svm.core.jdk.SystemPropertiesSupport; import com.oracle.svm.core.posix.headers.Limits; -import com.oracle.svm.core.posix.headers.Pwd; import com.oracle.svm.core.posix.headers.Unistd; public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSupport { @@ -43,14 +42,14 @@ public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSuppo @Override protected String userNameValue() { - Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid()); - return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_name()); + String name = PosixUtils.getUserName(Unistd.getuid()); + return name == null ? "?" : name; } @Override protected String userHomeValue() { - Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid()); - return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_dir()); + String dir = PosixUtils.getUserDir(Unistd.getuid()); + return dir == null ? "?" : dir; } @Override diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java index c3480e594c31..b81462d2b43d 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java @@ -24,10 +24,13 @@ */ package com.oracle.svm.core.posix; +import static com.oracle.svm.core.posix.headers.Unistd._SC_GETPW_R_SIZE_MAX; + import java.io.FileDescriptor; import java.io.IOException; import org.graalvm.nativeimage.Platform; +import org.graalvm.nativeimage.StackValue; import org.graalvm.nativeimage.c.struct.SizeOf; import org.graalvm.nativeimage.c.type.CCharPointer; import org.graalvm.nativeimage.c.type.CIntPointer; @@ -47,13 +50,18 @@ import com.oracle.svm.core.c.libc.LibCBase; import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue; import com.oracle.svm.core.headers.LibC; +import com.oracle.svm.core.memory.NullableNativeMemory; +import com.oracle.svm.core.nmt.NmtCategory; import com.oracle.svm.core.posix.headers.Dlfcn; import com.oracle.svm.core.posix.headers.Errno; import com.oracle.svm.core.posix.headers.Locale; +import com.oracle.svm.core.posix.headers.Pwd; import com.oracle.svm.core.posix.headers.Signal; import com.oracle.svm.core.posix.headers.Time; import com.oracle.svm.core.posix.headers.Unistd; import com.oracle.svm.core.posix.headers.Wait; +import com.oracle.svm.core.posix.headers.Pwd.passwd; +import com.oracle.svm.core.posix.headers.Pwd.passwdPointer; import com.oracle.svm.core.posix.headers.darwin.DarwinTime; import com.oracle.svm.core.posix.headers.linux.LinuxTime; import com.oracle.svm.core.thread.VMOperation; @@ -335,4 +343,49 @@ public static int clock_gettime(int clock_id, Time.timespec ts) { } } // Checkstyle: resume + + public static String getUserName(int uid) { + return getUserNameOrDir(uid, true); + } + + public static String getUserDir(int uid) { + return getUserNameOrDir(uid, false); + } + + private static String getUserNameOrDir(int uid, boolean name) { + /* Determine max. pwBuf size. */ + long bufSize = Unistd.sysconf(_SC_GETPW_R_SIZE_MAX()); + if (bufSize == -1) { + bufSize = 1024; + } + + /* Retrieve the username and copy it to a String object. */ + CCharPointer pwBuf = NullableNativeMemory.malloc(WordFactory.unsigned(bufSize), NmtCategory.Internal); + if (pwBuf.isNull()) { + return null; + } + + try { + passwd pwent = StackValue.get(passwd.class); + passwdPointer p = StackValue.get(passwdPointer.class); + int code = Pwd.getpwuid_r(uid, pwent, pwBuf, WordFactory.unsigned(bufSize), p); + if (code != 0) { + return null; + } + + passwd result = p.read(); + if (result.isNull()) { + return null; + } + + CCharPointer pwName = name ? result.pw_name() : result.pw_dir(); + if (pwName.isNull() || pwName.read() == '\0') { + return null; + } + + return CTypeConversion.toJavaString(pwName); + } finally { + NullableNativeMemory.free(pwBuf); + } + } } diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java index 9d627b263243..8a685fdc4979 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java @@ -56,9 +56,6 @@ public interface passwdPointer extends Pointer { passwd read(); } - @CFunction - public static native passwd getpwuid(int __uid); - @CFunction public static native int getpwuid_r(int __uid, passwd pwd, CCharPointer buf, UnsignedWord buflen, passwdPointer result); } diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java index fffe6ebe1034..8398ce162f37 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java @@ -37,7 +37,6 @@ import static com.oracle.svm.core.posix.headers.Fcntl.O_NOFOLLOW; import static com.oracle.svm.core.posix.headers.Fcntl.O_RDONLY; import static com.oracle.svm.core.posix.headers.Fcntl.O_RDWR; -import static com.oracle.svm.core.posix.headers.Unistd._SC_GETPW_R_SIZE_MAX; import java.nio.ByteBuffer; @@ -61,21 +60,17 @@ import com.oracle.svm.core.jvmstat.PerfManager; import com.oracle.svm.core.jvmstat.PerfMemoryPrologue; import com.oracle.svm.core.jvmstat.PerfMemoryProvider; -import com.oracle.svm.core.memory.NullableNativeMemory; -import com.oracle.svm.core.nmt.NmtCategory; import com.oracle.svm.core.os.RawFileOperationSupport; import com.oracle.svm.core.os.RawFileOperationSupport.RawFileDescriptor; import com.oracle.svm.core.os.VirtualMemoryProvider; import com.oracle.svm.core.posix.PosixStat; +import com.oracle.svm.core.posix.PosixUtils; import com.oracle.svm.core.posix.headers.Dirent; import com.oracle.svm.core.posix.headers.Dirent.DIR; import com.oracle.svm.core.posix.headers.Dirent.dirent; import com.oracle.svm.core.posix.headers.Errno; import com.oracle.svm.core.posix.headers.Fcntl; import com.oracle.svm.core.posix.headers.Mman; -import com.oracle.svm.core.posix.headers.Pwd; -import com.oracle.svm.core.posix.headers.Pwd.passwd; -import com.oracle.svm.core.posix.headers.Pwd.passwdPointer; import com.oracle.svm.core.posix.headers.Signal; import com.oracle.svm.core.posix.headers.Unistd; @@ -111,7 +106,7 @@ public ByteBuffer create() { } int vmId = Unistd.getpid(); - String userName = getUserName(Unistd.NoTransitions.geteuid()); + String userName = PosixUtils.getUserName(Unistd.NoTransitions.geteuid()); if (userName == null) { return null; } @@ -150,43 +145,6 @@ public ByteBuffer create() { return DirectByteBufferUtil.allocate(mapAddress.rawValue(), size); } - private static String getUserName(int uid) { - /* Determine max. pwBuf size. */ - long bufSize = Unistd.sysconf(_SC_GETPW_R_SIZE_MAX()); - if (bufSize == -1) { - bufSize = 1024; - } - - /* Retrieve the username and copy it to a String object. */ - CCharPointer pwBuf = NullableNativeMemory.malloc(WordFactory.unsigned(bufSize), NmtCategory.JvmStat); - if (pwBuf.isNull()) { - return null; - } - - try { - passwd pwent = StackValue.get(passwd.class); - passwdPointer p = StackValue.get(passwdPointer.class); - int code = Pwd.getpwuid_r(uid, pwent, pwBuf, WordFactory.unsigned(bufSize), p); - if (code != 0) { - return null; - } - - passwd result = p.read(); - if (result.isNull()) { - return null; - } - - CCharPointer pwName = result.pw_name(); - if (pwName.isNull() || pwName.read() == '\0') { - return null; - } - - return CTypeConversion.toJavaString(pwName); - } finally { - NullableNativeMemory.free(pwBuf); - } - } - private static String getUserTmpDir(String user, int vmId, int nsPid) { String tmpDir = Target_jdk_internal_vm_VMSupport.getVMTemporaryDirectory(); if (Platform.includedIn(Platform.LINUX.class)) {