Skip to content

Commit

Permalink
Cleanup of Posix C header file imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Wimmer committed Nov 20, 2019
1 parent 00c20d0 commit 6cda312
Show file tree
Hide file tree
Showing 154 changed files with 2,356 additions and 1,569 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@

import java.io.FileDescriptor;

import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.LogHandler;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.impl.InternalPlatform;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.word.UnsignedWord;

import com.oracle.svm.core.CErrorNumber;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.headers.Errno;
import com.oracle.svm.core.posix.headers.LibC;

@AutomaticFeature
@Platforms({InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class, InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class})
class PosixLogHandlerFeature implements Feature {
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
Expand All @@ -60,7 +57,7 @@ public class PosixLogHandler implements LogHandler {
@Override
public void log(CCharPointer bytes, UnsignedWord length) {
/* Save and restore errno around calls that would otherwise change errno. */
final int savedErrno = Errno.errno();
final int savedErrno = CErrorNumber.getCErrorNumber();
try {
if (!PosixUtils.writeBytes(getOutputFile(), bytes, length)) {
/*
Expand All @@ -70,19 +67,19 @@ public void log(CCharPointer bytes, UnsignedWord length) {
fatalError();
}
} finally {
Errno.set_errno(savedErrno);
CErrorNumber.setCErrorNumber(savedErrno);
}
}

@Override
public void flush() {
/* Save and restore errno around calls that would otherwise change errno. */
final int savedErrno = Errno.errno();
final int savedErrno = CErrorNumber.getCErrorNumber();
try {
PosixUtils.flush(getOutputFile());
/* ignore error -- they're benign */
} finally {
Errno.set_errno(savedErrno);
CErrorNumber.setCErrorNumber(savedErrno);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.CErrorNumber;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.headers.Errno;
import com.oracle.svm.core.jdk.JNIPlatformNativeLibrarySupport;
import com.oracle.svm.core.jdk.Jvm;
import com.oracle.svm.core.jdk.NativeLibrarySupport;
Expand All @@ -51,7 +51,6 @@
import com.oracle.svm.core.posix.headers.darwin.DarwinSyslimits;

@AutomaticFeature
@Platforms({InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class, InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class})
class PosixNativeLibraryFeature implements Feature {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
Expand Down Expand Up @@ -89,10 +88,10 @@ public boolean initializeBuiltinLibraries() {
}
rlp.set_rlim_cur(newValue);
if (Resource.setrlimit(Resource.RLIMIT_NOFILE(), rlp) != 0) {
Log.log().string("setrlimit to increase file descriptor limit failed, errno ").signed(Errno.errno()).newline();
Log.log().string("setrlimit to increase file descriptor limit failed, errno ").signed(CErrorNumber.getCErrorNumber()).newline();
}
} else {
Log.log().string("getrlimit failed, errno ").signed(Errno.errno()).newline();
Log.log().string("getrlimit failed, errno ").signed(CErrorNumber.getCErrorNumber()).newline();
}
}

Expand Down Expand Up @@ -203,14 +202,11 @@ public PointerBase findSymbol(String name) {
return findBuiltinSymbol(name);
}
assert dlhandle.isNonNull();
try (CCharPointerHolder symbol = CTypeConversion.toCString(name)) {
return Dlfcn.dlsym(dlhandle, symbol.get());
}
return PosixUtils.dlsym(dlhandle, name);
}
}
}

@Platforms({Platform.LINUX.class, Platform.DARWIN.class})
@TargetClass(className = "java.io.UnixFileSystem")
final class Target_java_io_UnixFileSystem_JNI {
@Alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,27 @@
*/
package com.oracle.svm.core.posix;

import static com.oracle.svm.core.posix.headers.Resource.RLIMIT_NOFILE;
import static com.oracle.svm.core.posix.headers.Resource.getrlimit;

import java.lang.management.OperatingSystemMXBean;
import java.util.concurrent.TimeUnit;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.impl.InternalPlatform;

import com.oracle.svm.core.CErrorNumber;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.headers.Errno;
import com.oracle.svm.core.jdk.SubstrateOperatingSystemMXBean;
import com.oracle.svm.core.posix.headers.Resource.rlimit;
import com.oracle.svm.core.posix.headers.Stat;
import com.oracle.svm.core.posix.headers.Stat.stat;
import com.oracle.svm.core.posix.headers.Errno;
import com.oracle.svm.core.posix.headers.Resource;
import com.oracle.svm.core.posix.headers.Times;
import com.oracle.svm.core.posix.headers.Unistd;
import com.oracle.svm.core.posix.headers.darwin.DarwinStat;
import com.oracle.svm.core.posix.headers.linux.LinuxStat;
import com.oracle.svm.core.util.VMError;
import com.sun.management.UnixOperatingSystemMXBean;

@Platforms({InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class, InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class})
class PosixSubstrateOperatingSystemMXBean extends SubstrateOperatingSystemMXBean implements UnixOperatingSystemMXBean {

/**
Expand All @@ -72,8 +70,8 @@ public long getProcessCpuTime() {

@Override
public long getMaxFileDescriptorCount() {
rlimit rlp = StackValue.get(rlimit.class);
if (getrlimit(RLIMIT_NOFILE(), rlp) < 0) {
Resource.rlimit rlp = StackValue.get(Resource.rlimit.class);
if (Resource.getrlimit(Resource.RLIMIT_NOFILE(), rlp) < 0) {
throwUnchecked(PosixUtils.newIOExceptionWithLastError("getrlimit failed"));
}
return rlp.rlim_cur().rawValue();
Expand All @@ -84,21 +82,31 @@ public long getOpenFileDescriptorCount() {
int maxFileDescriptor = Unistd.getdtablesize();
long count = 0;
for (int i = 0; i <= maxFileDescriptor; i++) {
stat stat = StackValue.get(stat.class);
if (Stat.fstat(i, stat) == 0 || Errno.errno() != Errno.EBADF()) {
if (fstat(i) == 0 || CErrorNumber.getCErrorNumber() != Errno.EBADF()) {
count++;
}
}
return count;
}

private static int fstat(int fd) {
if (Platform.includedIn(InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class)) {
LinuxStat.stat64 stat = StackValue.get(LinuxStat.stat64.class);
return LinuxStat.fstat64(fd, stat);
} else if (Platform.includedIn(InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class)) {
DarwinStat.stat stat = StackValue.get(DarwinStat.stat.class);
return DarwinStat.fstat(fd, stat);
} else {
throw VMError.shouldNotReachHere("Unsupported platform");
}
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void throwUnchecked(Throwable exception) throws T {
throw (T) exception;
}
}

@Platforms({InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class, InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class})
@AutomaticFeature
class PosixSubstrateOperatingSystemMXBeanFeature implements Feature {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,16 @@
*/
package com.oracle.svm.core.posix;

import static com.oracle.svm.core.posix.headers.Limits.MAXPATHLEN;
import static com.oracle.svm.core.posix.headers.Pwd.getpwuid;

import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.impl.InternalPlatform;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.jdk.SystemPropertiesSupport;
import com.oracle.svm.core.posix.headers.Pwd.passwd;
import com.oracle.svm.core.posix.headers.Limits;
import com.oracle.svm.core.posix.headers.Pwd;
import com.oracle.svm.core.posix.headers.Unistd;

@Platforms({InternalPlatform.LINUX_JNI_AND_SUBSTITUTIONS.class, InternalPlatform.DARWIN_JNI_AND_SUBSTITUTIONS.class})
public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSupport {

/*
Expand All @@ -48,19 +43,19 @@ public abstract class PosixSystemPropertiesSupport extends SystemPropertiesSuppo

@Override
protected String userNameValue() {
passwd pwent = getpwuid(Unistd.getuid());
Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid());
return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_name());
}

@Override
protected String userHomeValue() {
passwd pwent = getpwuid(Unistd.getuid());
Pwd.passwd pwent = Pwd.getpwuid(Unistd.getuid());
return pwent.isNull() ? "?" : CTypeConversion.toJavaString(pwent.pw_dir());
}

@Override
protected String userDirValue() {
int bufSize = MAXPATHLEN();
int bufSize = Limits.MAXPATHLEN();
CCharPointer buf = StackValue.get(bufSize);
if (Unistd.getcwd(buf, WordFactory.unsigned(bufSize)).isNonNull()) {
return CTypeConversion.toJavaString(buf);
Expand Down
Loading

0 comments on commit 6cda312

Please sign in to comment.