Skip to content

Commit

Permalink
remove getpwuid in favor of getpwuid_r
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasstadler committed Apr 24, 2024
1 parent 62dce15 commit a5af706
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit a5af706

Please sign in to comment.