public static void rb_maygvl_fd_fix_cloexec(Ruby runtime, int fd) { PosixShim shim = new PosixShim(runtime); OpenFile.fdFixCloexec(shim, fd); } }
clear(); int ret = checkSharedExclusive(fd, lockMode); if (ret < 0) return ret; if (!lockStateChanges(fd.currentLock, lockMode)) return 0; if (!lockStateChanges(fd.currentLock, lockMode)) return 0; return unlock(fd); case LOCK_EX: return lock(fd, true); case LOCK_EX | LOCK_NB: return tryLock(fd, true); case LOCK_SH: return lock(fd, false); case LOCK_SH | LOCK_NB: return tryLock(fd, false); errmsg = "overlapping file locks"; return lockFailedReturn(lockMode); } else {
public int close(ChannelFD fd) { return close((Closeable)fd); }
public static void fdFixCloexec(PosixShim posix, int fd) { if (fd >= 0 && fd < FilenoUtil.FIRST_FAKE_FD) { int flags, flags2, ret; flags = posix.fcntlGetFD(fd); /* should not fail except EBADF. */ if (flags == -1) { throw new AssertionError(String.format("BUG: rb_maygvl_fd_fix_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, posix.errno.description())); } if (fd <= 2) flags2 = flags & ~FcntlLibrary.FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */ else flags2 = flags | FcntlLibrary.FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */ if (flags != flags2) { ret = posix.fcntlSetFD(fd, flags2); if (ret == -1) { throw new AssertionError(String.format("BUG: rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2, posix.errno.description())); } } } // otherwise JVM sets cloexec }
public int close(Closeable closeable) { clear(); try { closeable.close(); return 0; } catch (IOException ioe) { Errno errno = Helpers.errnoFromException(ioe); if (errno == null) { throw new RuntimeException("unknown IOException: " + ioe); } this.errno = errno; return -1; } }
public Channel[] pipe() { clear(); try { Pipe pipe = Pipe.open(); Channel source = pipe.source(), sink = pipe.sink(); if (posix.isNative() && !Platform.IS_WINDOWS) { // set cloexec if possible int read = FilenoUtil.filenoFrom(source); int write = FilenoUtil.filenoFrom(sink); setCloexec(read, true); setCloexec(write, true); } return new Channel[]{source, sink}; } catch (IOException ioe) { errno = Helpers.errnoFromException(ioe); return null; } }
public static ChannelFD cloexecOpen(Ruby runtime, Sysopen data) { Channel ret = null; if (OpenFlags.O_CLOEXEC.defined()) { data.oflags |= OpenFlags.O_CLOEXEC.intValue(); } else { // #elif defined O_NOINHERIT // flags |= O_NOINHERIT; } PosixShim shim = new PosixShim(runtime); ret = shim.open(runtime.getCurrentDirectory(), data.fname, data.oflags, data.perm); if (ret == null) { data.errno = shim.errno; return null; } ChannelFD fd = new ChannelFD(ret, runtime.getPosix(), runtime.getFilenoUtil()); if (fd.realFileno > 0 && runtime.getPosix().isNative()) { OpenFile.fdFixCloexec(shim, fd.realFileno); } return fd; }
private int lock(ChannelFD fd, boolean exclusive) throws IOException { if (fd.currentLock != null) fd.currentLock.release(); fd.currentLock = fd.chFile.lock(0L, Long.MAX_VALUE, !exclusive); if (fd.currentLock != null) { return 0; } return lockFailedReturn(exclusive ? LOCK_EX : LOCK_SH); }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject len) { Ruby runtime = context.runtime; OpenFile fptr; long pos; pos = RubyNumeric.num2int(len); fptr = getOpenFileChecked(); if (!fptr.isWritable()) { throw runtime.newIOError("not opened for writing"); } flushRaw(context, false); if (pos < 0) { throw runtime.newErrnoEINVALError(openFile.getPath()); } if (fptr.posix.ftruncate(fptr.fd(), pos) < 0) { throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } return RubyFixnum.zero(runtime); }
ret = posix.dup2(oldfd, newfd);
public int close(Closeable closeable) { clear(); try { closeable.close(); return 0; } catch (IOException ioe) { Errno errno = Helpers.errnoFromException(ioe); if (errno == null) { throw new RuntimeException("unknown IOException: " + ioe); } this.errno = errno; return -1; } }
public Channel[] pipe() { clear(); try { Pipe pipe = Pipe.open(); Channel source = pipe.source(), sink = pipe.sink(); if (posix.isNative() && !Platform.IS_WINDOWS) { // set cloexec if possible int read = FilenoUtil.filenoFrom(source); int write = FilenoUtil.filenoFrom(sink); setCloexec(read, true); setCloexec(write, true); } return new Channel[]{source, sink}; } catch (IOException ioe) { errno = Helpers.errnoFromException(ioe); return null; } }
public static void fdFixCloexec(PosixShim posix, int fd) { if (fd >= 0 && fd < FilenoUtil.FIRST_FAKE_FD) { int flags, flags2, ret; flags = posix.fcntlGetFD(fd); /* should not fail except EBADF. */ if (flags == -1) { throw new AssertionError(String.format("BUG: rb_maygvl_fd_fix_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, posix.errno.description())); } if (fd <= 2) flags2 = flags & ~FcntlLibrary.FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */ else flags2 = flags | FcntlLibrary.FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */ if (flags != flags2) { ret = posix.fcntlSetFD(fd, flags2); if (ret == -1) { throw new AssertionError(String.format("BUG: rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2, posix.errno.description())); } } } // otherwise JVM sets cloexec }
public static ChannelFD cloexecOpen(Ruby runtime, Sysopen data) { Channel ret = null; if (OpenFlags.O_CLOEXEC.defined()) { data.oflags |= OpenFlags.O_CLOEXEC.intValue(); } else { // #elif defined O_NOINHERIT // flags |= O_NOINHERIT; } PosixShim shim = new PosixShim(runtime); ret = shim.open(runtime.getCurrentDirectory(), data.fname, data.oflags, data.perm); if (ret == null) { data.errno = shim.errno; return null; } ChannelFD fd = new ChannelFD(ret, runtime.getPosix(), runtime.getFilenoUtil()); if (fd.realFileno > 0 && runtime.getPosix().isNative()) { OpenFile.fdFixCloexec(shim, fd.realFileno); } return fd; }
private int tryLock(ChannelFD fd, boolean exclusive) throws IOException { if (fd.currentLock != null) fd.currentLock.release(); fd.currentLock = fd.chFile.tryLock(0L, Long.MAX_VALUE, !exclusive); if (fd.currentLock != null) { return 0; } return lockFailedReturn(exclusive ? LOCK_EX : LOCK_SH); }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject len) { Ruby runtime = context.runtime; OpenFile fptr; long pos; pos = RubyNumeric.num2int(len); fptr = getOpenFileChecked(); if (!fptr.isWritable()) { throw runtime.newIOError("not opened for writing"); } flushRaw(context, false); if (pos < 0) { throw runtime.newErrnoEINVALError(openFile.getPath()); } if (fptr.posix.ftruncate(fptr.fd(), pos) < 0) { throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } return RubyFixnum.zero(runtime); }
ret = posix.dup2(oldfd, newfd);
clear(); int ret = checkSharedExclusive(fd, lockMode); if (ret < 0) return ret; if (!lockStateChanges(fd.currentLock, lockMode)) return 0; if (!lockStateChanges(fd.currentLock, lockMode)) return 0; return unlock(fd); case LOCK_EX: return lock(fd, true); case LOCK_EX | LOCK_NB: return tryLock(fd, true); case LOCK_SH: return lock(fd, false); case LOCK_SH | LOCK_NB: return tryLock(fd, false); errmsg = "overlapping file locks"; return lockFailedReturn(lockMode); } else {