Skip to content

Commit 4f6cac2

Browse files
committed
[GR-53622] Remove getpwuid in favor of getpwuid_r.
PullRequest: graal/17582
2 parents 6311410 + a5af706 commit 4f6cac2

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSystemPropertiesSupport.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
3232
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
3333
import com.oracle.svm.core.posix.headers.Limits;
34-
import com.oracle.svm.core.posix.headers.Pwd;
3534
import com.oracle.svm.core.posix.headers.Unistd;
3635

3736
public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSupport {
@@ -43,14 +42,14 @@ public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSuppo
4342

4443
@Override
4544
protected String userNameValue() {
46-
Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid());
47-
return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_name());
45+
String name = PosixUtils.getUserName(Unistd.getuid());
46+
return name == null ? "?" : name;
4847
}
4948

5049
@Override
5150
protected String userHomeValue() {
52-
Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid());
53-
return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_dir());
51+
String dir = PosixUtils.getUserDir(Unistd.getuid());
52+
return dir == null ? "?" : dir;
5453
}
5554

5655
@Override

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
*/
2525
package com.oracle.svm.core.posix;
2626

27+
import static com.oracle.svm.core.posix.headers.Unistd._SC_GETPW_R_SIZE_MAX;
28+
2729
import java.io.FileDescriptor;
2830
import java.io.IOException;
2931

3032
import org.graalvm.nativeimage.Platform;
33+
import org.graalvm.nativeimage.StackValue;
3134
import org.graalvm.nativeimage.c.struct.SizeOf;
3235
import org.graalvm.nativeimage.c.type.CCharPointer;
3336
import org.graalvm.nativeimage.c.type.CIntPointer;
@@ -47,13 +50,18 @@
4750
import com.oracle.svm.core.c.libc.LibCBase;
4851
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
4952
import com.oracle.svm.core.headers.LibC;
53+
import com.oracle.svm.core.memory.NullableNativeMemory;
54+
import com.oracle.svm.core.nmt.NmtCategory;
5055
import com.oracle.svm.core.posix.headers.Dlfcn;
5156
import com.oracle.svm.core.posix.headers.Errno;
5257
import com.oracle.svm.core.posix.headers.Locale;
58+
import com.oracle.svm.core.posix.headers.Pwd;
5359
import com.oracle.svm.core.posix.headers.Signal;
5460
import com.oracle.svm.core.posix.headers.Time;
5561
import com.oracle.svm.core.posix.headers.Unistd;
5662
import com.oracle.svm.core.posix.headers.Wait;
63+
import com.oracle.svm.core.posix.headers.Pwd.passwd;
64+
import com.oracle.svm.core.posix.headers.Pwd.passwdPointer;
5765
import com.oracle.svm.core.posix.headers.darwin.DarwinTime;
5866
import com.oracle.svm.core.posix.headers.linux.LinuxTime;
5967
import com.oracle.svm.core.thread.VMOperation;
@@ -335,4 +343,49 @@ public static int clock_gettime(int clock_id, Time.timespec ts) {
335343
}
336344
}
337345
// Checkstyle: resume
346+
347+
public static String getUserName(int uid) {
348+
return getUserNameOrDir(uid, true);
349+
}
350+
351+
public static String getUserDir(int uid) {
352+
return getUserNameOrDir(uid, false);
353+
}
354+
355+
private static String getUserNameOrDir(int uid, boolean name) {
356+
/* Determine max. pwBuf size. */
357+
long bufSize = Unistd.sysconf(_SC_GETPW_R_SIZE_MAX());
358+
if (bufSize == -1) {
359+
bufSize = 1024;
360+
}
361+
362+
/* Retrieve the username and copy it to a String object. */
363+
CCharPointer pwBuf = NullableNativeMemory.malloc(WordFactory.unsigned(bufSize), NmtCategory.Internal);
364+
if (pwBuf.isNull()) {
365+
return null;
366+
}
367+
368+
try {
369+
passwd pwent = StackValue.get(passwd.class);
370+
passwdPointer p = StackValue.get(passwdPointer.class);
371+
int code = Pwd.getpwuid_r(uid, pwent, pwBuf, WordFactory.unsigned(bufSize), p);
372+
if (code != 0) {
373+
return null;
374+
}
375+
376+
passwd result = p.read();
377+
if (result.isNull()) {
378+
return null;
379+
}
380+
381+
CCharPointer pwName = name ? result.pw_name() : result.pw_dir();
382+
if (pwName.isNull() || pwName.read() == '\0') {
383+
return null;
384+
}
385+
386+
return CTypeConversion.toJavaString(pwName);
387+
} finally {
388+
NullableNativeMemory.free(pwBuf);
389+
}
390+
}
338391
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public interface passwdPointer extends Pointer {
5656
passwd read();
5757
}
5858

59-
@CFunction
60-
public static native passwd getpwuid(int __uid);
61-
6259
@CFunction
6360
public static native int getpwuid_r(int __uid, passwd pwd, CCharPointer buf, UnsignedWord buflen, passwdPointer result);
6461
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/jvmstat/PosixPerfMemoryProvider.java

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import static com.oracle.svm.core.posix.headers.Fcntl.O_NOFOLLOW;
3838
import static com.oracle.svm.core.posix.headers.Fcntl.O_RDONLY;
3939
import static com.oracle.svm.core.posix.headers.Fcntl.O_RDWR;
40-
import static com.oracle.svm.core.posix.headers.Unistd._SC_GETPW_R_SIZE_MAX;
4140

4241
import java.nio.ByteBuffer;
4342

@@ -61,21 +60,17 @@
6160
import com.oracle.svm.core.jvmstat.PerfManager;
6261
import com.oracle.svm.core.jvmstat.PerfMemoryPrologue;
6362
import com.oracle.svm.core.jvmstat.PerfMemoryProvider;
64-
import com.oracle.svm.core.memory.NullableNativeMemory;
65-
import com.oracle.svm.core.nmt.NmtCategory;
6663
import com.oracle.svm.core.os.RawFileOperationSupport;
6764
import com.oracle.svm.core.os.RawFileOperationSupport.RawFileDescriptor;
6865
import com.oracle.svm.core.os.VirtualMemoryProvider;
6966
import com.oracle.svm.core.posix.PosixStat;
67+
import com.oracle.svm.core.posix.PosixUtils;
7068
import com.oracle.svm.core.posix.headers.Dirent;
7169
import com.oracle.svm.core.posix.headers.Dirent.DIR;
7270
import com.oracle.svm.core.posix.headers.Dirent.dirent;
7371
import com.oracle.svm.core.posix.headers.Errno;
7472
import com.oracle.svm.core.posix.headers.Fcntl;
7573
import com.oracle.svm.core.posix.headers.Mman;
76-
import com.oracle.svm.core.posix.headers.Pwd;
77-
import com.oracle.svm.core.posix.headers.Pwd.passwd;
78-
import com.oracle.svm.core.posix.headers.Pwd.passwdPointer;
7974
import com.oracle.svm.core.posix.headers.Signal;
8075
import com.oracle.svm.core.posix.headers.Unistd;
8176

@@ -111,7 +106,7 @@ public ByteBuffer create() {
111106
}
112107

113108
int vmId = Unistd.getpid();
114-
String userName = getUserName(Unistd.NoTransitions.geteuid());
109+
String userName = PosixUtils.getUserName(Unistd.NoTransitions.geteuid());
115110
if (userName == null) {
116111
return null;
117112
}
@@ -150,43 +145,6 @@ public ByteBuffer create() {
150145
return DirectByteBufferUtil.allocate(mapAddress.rawValue(), size);
151146
}
152147

153-
private static String getUserName(int uid) {
154-
/* Determine max. pwBuf size. */
155-
long bufSize = Unistd.sysconf(_SC_GETPW_R_SIZE_MAX());
156-
if (bufSize == -1) {
157-
bufSize = 1024;
158-
}
159-
160-
/* Retrieve the username and copy it to a String object. */
161-
CCharPointer pwBuf = NullableNativeMemory.malloc(WordFactory.unsigned(bufSize), NmtCategory.JvmStat);
162-
if (pwBuf.isNull()) {
163-
return null;
164-
}
165-
166-
try {
167-
passwd pwent = StackValue.get(passwd.class);
168-
passwdPointer p = StackValue.get(passwdPointer.class);
169-
int code = Pwd.getpwuid_r(uid, pwent, pwBuf, WordFactory.unsigned(bufSize), p);
170-
if (code != 0) {
171-
return null;
172-
}
173-
174-
passwd result = p.read();
175-
if (result.isNull()) {
176-
return null;
177-
}
178-
179-
CCharPointer pwName = result.pw_name();
180-
if (pwName.isNull() || pwName.read() == '\0') {
181-
return null;
182-
}
183-
184-
return CTypeConversion.toJavaString(pwName);
185-
} finally {
186-
NullableNativeMemory.free(pwBuf);
187-
}
188-
}
189-
190148
private static String getUserTmpDir(String user, int vmId, int nsPid) {
191149
String tmpDir = Target_jdk_internal_vm_VMSupport.getVMTemporaryDirectory();
192150
if (Platform.includedIn(Platform.LINUX.class)) {

0 commit comments

Comments
 (0)