Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed SYS___pthread_chdir value for FreeBSD when making call to syscall #109

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/com/zaxxer/nuprocess/internal/LibC.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@
@SuppressWarnings("WeakerAccess")
public class LibC
{

public static int SYS___pthread_chdir = 348;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem like this should be mutable. This is clearly intended to be a constant.

Rather than initializing this here and changing it in the static block, which requires this to be mutable, leave it uninitialized here and update the static block to set the "right" value:

public static final int SYS___pthread_chdir;

static {
   // O_NONBLOCK initialization as now

   SYS___pthread_chdir = System.getProperty("os.name").toLowerCase().contains("freebsd") ? 12 : 348;

   // Register Native
}

If this constant is going to move to LibC, should it be further down, with the others (like O_NONBLOCK)? Alternatively, perhaps the constant should just stay where it was, paired with a new static block inside SyscallLibrary to initialize it?


static {
if (OS == OperatingSystem.MAC) {
O_NONBLOCK = 0x0004; // MacOS X, Freebsd
}
else {
O_NONBLOCK = 2048; // Linux
}


// changing the value based on https://github.com/freebsd/freebsd/blob/master/sys/sys/syscall.h
String osname = System.getProperty("os.name").toLowerCase();
if ( osname.contains("freebsd") ) {
SYS___pthread_chdir = 12;
}

Native.register(NativeLibrary.getProcess());
}

Expand Down Expand Up @@ -81,8 +90,7 @@ public static native int posix_spawnp(IntByReference restrict_pid, String restri
// We can't use JNA direct mapping for syscall(), since it takes varargs.
public interface SyscallLibrary extends Library
{
public static final int SYS___pthread_chdir = 348;


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't put the chdir constant back here, should this line be removed?

int syscall(int syscall_number, Object... args);
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/zaxxer/nuprocess/osx/OsxProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.zaxxer.nuprocess.internal.BasePosixProcess;
import com.zaxxer.nuprocess.internal.IEventProcessor;
import com.zaxxer.nuprocess.internal.LibC;
import com.zaxxer.nuprocess.internal.LibC.SyscallLibrary;

import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -181,14 +180,14 @@ private int spawnWithCwd(final IntByReference restrict_pid,
Pointer oldCwd = new Pointer(peer);
LibC.getcwd(oldCwd, cwdBufSize);
String newCwd = cwd.toAbsolutePath().toString();
int rc = LibC.SYSCALL.syscall(SyscallLibrary.SYS___pthread_chdir, newCwd);
int rc = LibC.SYSCALL.syscall(LibC.SYS___pthread_chdir, newCwd);
checkReturnCode(rc, "syscall(SYS__pthread_chdir) failed to set current directory");

try {
return LibC.posix_spawnp(restrict_pid, restrict_path, file_actions, restrict_attrp, argv, envp);
}
finally {
rc = LibC.SYSCALL.syscall(SyscallLibrary.SYS___pthread_chdir, oldCwd);
rc = LibC.SYSCALL.syscall(LibC.SYS___pthread_chdir, oldCwd);
brettwooldridge marked this conversation as resolved.
Show resolved Hide resolved
Native.free(Pointer.nativeValue(oldCwd));
checkReturnCode(rc, "syscall(SYS__pthread_chdir) failed to restore current directory");
}
Expand Down