Skip to content

Commit

Permalink
openbsd support based on recent freebsd one
Browse files Browse the repository at this point in the history
  • Loading branch information
jirib committed Feb 17, 2016
1 parent 1384223 commit f844d0e
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 2 deletions.
7 changes: 7 additions & 0 deletions native/Makefile_openbsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include Makefile_linux

CC=cc
LINUX=openbsd
ARCH_FLAG_X86_64=
ARCH_FLAG_X86=-m32
CFLAGS +=-shared -fPIC -fpic -D_REENTRANT -D_GNU_SOURCE
3 changes: 3 additions & 0 deletions src/com/pty4j/unix/PtyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ public static interface OSFacade {
else if (Platform.isFreeBSD()) {
myOsFacade = new com.pty4j.unix.freebsd.OSFacadeImpl();
}
else if (Platform.isOpenBSD()) {
myOsFacade = new com.pty4j.unix.openbsd.OSFacadeImpl();
}
else if (Platform.isLinux()) {
myOsFacade = new com.pty4j.unix.linux.OSFacadeImpl();
}
Expand Down
255 changes: 255 additions & 0 deletions src/com/pty4j/unix/openbsd/OSFacadeImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* JPty - A small PTY interface for Java.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.pty4j.unix.openbsd;


import com.pty4j.WinSize;
import com.pty4j.unix.PtyHelpers;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.StringArray;
import com.sun.jna.ptr.IntByReference;
import jtermios.JTermios;


/**
* Provides a {@link PtyHelpers.OSFacade} implementation for OpenBSD.
*/
public class OSFacadeImpl implements PtyHelpers.OSFacade {
// INNER TYPES

public interface OpenBSD_C_lib extends Library {
int posix_openpt(int oflag);

int execv(String command, StringArray argv);

int execve(String command, StringArray argv, StringArray env);

int ioctl(int fd, NativeLong cmd, PtyHelpers.winsize data);

int kill(int pid, int signal);

int waitpid(int pid, int[] stat, int options);

int sigprocmask(int how, IntByReference set, IntByReference oldset);

String strerror(int errno);

int grantpt(int fdm);

int unlockpt(int fdm);

int close(int fd);

String ptsname(int fd);

int open(String pts_name, int o_rdwr);

int killpg(int pid, int sig);

int fork();

int setsid();

int getpid();

int setpgid(int pid, int pgid);

void dup2(int fd, int fileno);

int getppid();

void unsetenv(String s);

int login_tty(int fd);

void chdir(String dirpath);
}

public interface OpenBSD_Util_lib extends Library {
int login_tty(int fd);
}

// CONSTANTS

private static final long TIOCGWINSZ = 0x40087468L;
private static final long TIOCSWINSZ = 0x80087467L;

// VARIABLES

private static OpenBSD_C_lib m_Clib = (OpenBSD_C_lib) Native.loadLibrary("c", OpenBSD_C_lib.class);
private static OpenBSD_Util_lib m_Utillib = (OpenBSD_Util_lib)Native.loadLibrary("util", OpenBSD_Util_lib.class);

// CONSTUCTORS

/**
* Creates a new {@link OSFacadeImpl} instance.
*/
public OSFacadeImpl() {
PtyHelpers.ONLCR = 0x02;

PtyHelpers.VERASE = 3;
PtyHelpers.VWERASE = 4;
PtyHelpers.VKILL = 5;
PtyHelpers.VREPRINT = 6;
PtyHelpers.VINTR = 8;
PtyHelpers.VQUIT = 9;
PtyHelpers.VSUSP = 10;

PtyHelpers.ECHOKE = 0x01;
PtyHelpers.ECHOCTL = 0x40;
}

// METHODS

@Override
public int execve(String command, String[] argv, String[] env) {
StringArray argvp = (argv == null) ? new StringArray(new String[]{command}) : new StringArray(argv);
StringArray envp = (env == null) ? null : new StringArray(env);
return m_Clib.execve(command, argvp, envp);
}

@Override
public int getWinSize(int fd, WinSize winSize) {
int r;

PtyHelpers.winsize ws = new PtyHelpers.winsize();
if ((r = m_Clib.ioctl(fd, new NativeLong(TIOCGWINSZ), ws)) < 0) {
return r;
}
ws.update(winSize);

return r;
}

@Override
public int kill(int pid, int signal) {
return m_Clib.kill(pid, signal);
}

@Override
public int setWinSize(int fd, WinSize winSize) {
PtyHelpers.winsize ws = new PtyHelpers.winsize(winSize);
return m_Clib.ioctl(fd, new NativeLong(TIOCSWINSZ), ws);
}

@Override
public int waitpid(int pid, int[] stat, int options) {
return m_Clib.waitpid(pid, stat, options);
}

@Override
public int sigprocmask(int how, IntByReference set, IntByReference oldset) {
return m_Clib.sigprocmask(how, set, oldset);
}

@Override
public String strerror(int errno) {
return m_Clib.strerror(errno);
}

@Override
public int getpt() {
return m_Clib.posix_openpt(JTermios.O_RDWR | JTermios.O_NOCTTY);
}

@Override
public int grantpt(int fd) {
return m_Clib.grantpt(fd);
}

@Override
public int unlockpt(int fd) {
return m_Clib.unlockpt(fd);
}

@Override
public int close(int fd) {
return m_Clib.close(fd);
}

@Override
public String ptsname(int fd) {
return m_Clib.ptsname(fd);
}

@Override
public int killpg(int pid, int sig) {
return m_Clib.killpg(pid, sig);
}

@Override
public int fork() {
return m_Clib.fork();
}

@Override
public int pipe(int[] pipe2) {
return JTermios.pipe(pipe2);
}

@Override
public int setsid() {
return m_Clib.setsid();
}

@Override
public void execv(String path, String[] argv) {
StringArray argvp = (argv == null) ? new StringArray(new String[]{path}) : new StringArray(argv);
m_Clib.execv(path, argvp);
}

@Override
public int getpid() {
return m_Clib.getpid();
}

@Override
public int setpgid(int pid, int pgid) {
return m_Clib.setpgid(pid, pgid);
}

@Override
public void dup2(int fds, int fileno) {
m_Clib.dup2(fds, fileno);
}

@Override
public int getppid() {
return m_Clib.getppid();
}

@Override
public void unsetenv(String s) {
m_Clib.unsetenv(s);
}

@Override
public int login_tty(int fd) {
return m_Utillib.login_tty(fd);
}

@Override
public void chdir(String dirpath) {
m_Clib.chdir(dirpath);
}
}
4 changes: 3 additions & 1 deletion src/com/pty4j/util/PtyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ private static String getPlatformFolder() {
result = "linux";
} else if (Platform.isFreeBSD()) {
result = "freebsd";
} else if (Platform.isOpenBSD()) {
result = "openbsd";
} else {
throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
}
Expand All @@ -132,7 +134,7 @@ private static String getNativeLibraryName() {
result = "libpty.dylib";
} else if (Platform.isWindows()) {
result = "libwinpty.dll";
} else if (Platform.isLinux() || Platform.isFreeBSD()) {
} else if (Platform.isLinux() || Platform.isFreeBSD() || Platform.isOpenBSD()) {
result = "libpty.so";
} else {
throw new IllegalStateException("Platform " + Platform.getOSType() + " is not supported");
Expand Down
2 changes: 1 addition & 1 deletion test/com/pty4j/PtyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private String[] preparePingCommand(int count) {
return new String[]{"ping", "-n", value, "127.0.0.1"};
} else if (Platform.isSolaris()) {
return new String[]{"/usr/sbin/ping", "-s", "127.0.0.1", "64", value};
} else if (Platform.isMac() || Platform.isFreeBSD()) {
} else if (Platform.isMac() || Platform.isFreeBSD() || Platform.isOpenBSD()) {
return new String[]{"/sbin/ping", "-c", value, "127.0.0.1"};
} else if (Platform.isLinux()) {
return new String[]{"/bin/ping", "-c", value, "127.0.0.1"};
Expand Down

0 comments on commit f844d0e

Please sign in to comment.