/** * Load the JNR implementation of the POSIX APIs for the current platform. * Runtime exceptions will be of type {@link PosixException}. * {@link IllegalStateException} will be thrown for methods not implemented on this platform. * @return some implementation (even on Windows or unsupported Unix) * @since 1.518 */ public static synchronized POSIX jnr() { if (posix == null) { posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler() { @Override public void error(Errno error, String extraData) { throw new PosixException("native error " + error.description() + " " + extraData, convert(error)); } @Override public void error(Errno error, String methodName, String extraData) { throw new PosixException("native error calling " + methodName + ": " + error.description() + " " + extraData, convert(error)); } private org.jruby.ext.posix.POSIX.ERRORS convert(Errno error) { try { return org.jruby.ext.posix.POSIX.ERRORS.valueOf(error.name()); } catch (IllegalArgumentException x) { return org.jruby.ext.posix.POSIX.ERRORS.EIO; // PosixException.message has real error anyway } } }, true); } return posix; }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
public int setrlimit(int resource, long rlimCur, long rlimMax) { RLimit rlim = new DefaultNativeRLimit(getRuntime()); rlim.init(rlimCur, rlimMax); return libc().setrlimit(resource, rlim); }
public void init() throws URISyntaxException { String path = new File( WindowsService.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(); POSIXFactory.getPOSIX().chdir(path); System.setProperty("user.dir", path); serviceMain = new ServiceMain(); SERVICE_TABLE_ENTRY entry = new SERVICE_TABLE_ENTRY(); entry.lpServiceName = serviceName; entry.lpServiceProc = serviceMain; Advapi32.INSTANCE.StartServiceCtrlDispatcher((SERVICE_TABLE_ENTRY[]) entry.toArray(2)); }
@Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; }
/** * Gets the mode of a file/directory, if appropriate. Only includes read, write, and * execute permissions for the owner, group, and others, i.e. the max return value * is 0777. Consider using {@link Files#getPosixFilePermissions} instead if you only * care about access permissions. * <p>If the file is symlink, the mode is that of the link target, not the link itself. * @return a file mode, or -1 if not on Unix * @throws PosixException if the file could not be statted, e.g. broken symlink */ public static int mode(File f) throws PosixException { if(Functions.isWindows()) return -1; try { if (Util.NATIVE_CHMOD_MODE) { return PosixAPI.jnr().stat(f.getPath()).mode(); } else { return Util.permissionsToMode(Files.getPosixFilePermissions(fileToPath(f))); } } catch (IOException cause) { PosixException e = new PosixException("Unable to get file permissions", null); e.initCause(cause); throw e; } }
private void sendSignalToProcess(Process process, Signal signal) { int processPid = getProcessPid(process); if (processPid != -1) { LOGGER.info("Sending signal {} to process {}", signal.intValue(), processPid); POSIXFactory.getNativePOSIX().kill(processPid, signal.intValue()); } }
/** * Returns the JVM's process identifier (PID) via a system call to {@code getpid}. * * @return the JVM's process identifier (PID). * @throws UnsupportedOperationException if JNR POSIX library is not loaded or {@code getpid} is * not available. */ public static int processId() { if (!isGetpidAvailable()) throw new UnsupportedOperationException( "JNR POSIX library not loaded or getpid not available"); return PosixLoader.POSIX.getpid(); } }
/** * Change permissions via NIO. */ private static void _chmod(File f, int mask) throws IOException { // TODO WindowsPosix actually does something here (WindowsLibC._wchmod); should we let it? // Anyway the existing calls already skip this method if on Windows. if (File.pathSeparatorChar==';') return; // noop if (Util.NATIVE_CHMOD_MODE) { PosixAPI.jnr().chmod(f.getAbsolutePath(), mask); } else { Files.setPosixFilePermissions(fileToPath(f), Util.modeToPermissions(mask)); } }
public FileStat stat(String path) { FileStat stat = allocateStat(); if (helper.stat(path, stat) < 0) handler.error(ENOENT, "stat", path); return stat; }
public int ioprio_get(int which, int who) { Syscall.ABI abi = Syscall.abi(); if (abi == null) { handler.unimplementedError("ioprio_get"); return -1; } return libc().syscall(abi.__NR_ioprio_get(), which, who); }
public int ioprio_set(int which, int who, int ioprio) { Syscall.ABI abi = Syscall.abi(); if (abi == null) { handler.unimplementedError("ioprio_set"); return -1; } return libc().syscall(abi.__NR_ioprio_set(), which, who, ioprio); } }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
public int setrlimit(int resource, long rlimCur, long rlimMax) { RLimit rlim = new DefaultNativeRLimit(getRuntime()); rlim.init(rlimCur, rlimMax); return libc().setrlimit(resource, rlim); }
@Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
public int setrlimit(int resource, long rlimCur, long rlimMax) { RLimit rlim = new DefaultNativeRLimit(getRuntime()); rlim.init(rlimCur, rlimMax); return libc().setrlimit(resource, rlim); }