1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.io; 18 19 import android.system.ErrnoException; 20 import android.system.GaiException; 21 import android.system.Int32Ref; 22 import android.system.Int64Ref; 23 import android.system.StructAddrinfo; 24 import android.system.StructCapUserData; 25 import android.system.StructCapUserHeader; 26 import android.system.StructGroupReq; 27 import android.system.StructIfaddrs; 28 import android.system.StructLinger; 29 import android.system.StructMsghdr; 30 import android.system.StructPasswd; 31 import android.system.StructPollfd; 32 import android.system.StructRlimit; 33 import android.system.StructStat; 34 import android.system.StructStatVfs; 35 import android.system.StructTimeval; 36 import android.system.StructUcred; 37 import android.system.StructUtsname; 38 39 import dalvik.annotation.optimization.CriticalNative; 40 41 import java.io.FileDescriptor; 42 import java.io.InterruptedIOException; 43 import java.net.InetAddress; 44 import java.net.InetSocketAddress; 45 import java.net.SocketAddress; 46 import java.net.SocketException; 47 import java.nio.ByteBuffer; 48 import java.nio.NioUtils; 49 50 public final class Linux implements Os { Linux()51 Linux() { } 52 accept(FileDescriptor fd, SocketAddress peerAddress)53 public native FileDescriptor accept(FileDescriptor fd, SocketAddress peerAddress) throws ErrnoException, SocketException; access(String path, int mode)54 public native boolean access(String path, int mode) throws ErrnoException; android_getaddrinfo(String node, StructAddrinfo hints, int netId)55 public native InetAddress[] android_getaddrinfo(String node, StructAddrinfo hints, int netId) throws GaiException; bind(FileDescriptor fd, InetAddress address, int port)56 public native void bind(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; bind(FileDescriptor fd, SocketAddress address)57 public native void bind(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; 58 @Override capget(StructCapUserHeader hdr)59 public native StructCapUserData[] capget(StructCapUserHeader hdr) throws ErrnoException; 60 @Override capset(StructCapUserHeader hdr, StructCapUserData[] data)61 public native void capset(StructCapUserHeader hdr, StructCapUserData[] data) 62 throws ErrnoException; chmod(String path, int mode)63 public native void chmod(String path, int mode) throws ErrnoException; chown(String path, int uid, int gid)64 public native void chown(String path, int uid, int gid) throws ErrnoException; 65 close(FileDescriptor fd)66 public native void close(FileDescriptor fd) throws ErrnoException; android_fdsan_exchange_owner_tag(FileDescriptor fd, long previousOwnerId, long newOwnerId)67 public native void android_fdsan_exchange_owner_tag(FileDescriptor fd, long previousOwnerId, long newOwnerId); android_fdsan_get_owner_tag(FileDescriptor fd)68 public native long android_fdsan_get_owner_tag(FileDescriptor fd); android_fdsan_get_tag_type(long tag)69 public native String android_fdsan_get_tag_type(long tag); android_fdsan_get_tag_value(long tag)70 public native long android_fdsan_get_tag_value(long tag); 71 connect(FileDescriptor fd, InetAddress address, int port)72 public native void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; connect(FileDescriptor fd, SocketAddress address)73 public native void connect(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; dup(FileDescriptor oldFd)74 public native FileDescriptor dup(FileDescriptor oldFd) throws ErrnoException; dup2(FileDescriptor oldFd, int newFd)75 public native FileDescriptor dup2(FileDescriptor oldFd, int newFd) throws ErrnoException; environ()76 public native String[] environ(); execv(String filename, String[] argv)77 public native void execv(String filename, String[] argv) throws ErrnoException; execve(String filename, String[] argv, String[] envp)78 public native void execve(String filename, String[] argv, String[] envp) throws ErrnoException; fchmod(FileDescriptor fd, int mode)79 public native void fchmod(FileDescriptor fd, int mode) throws ErrnoException; fchown(FileDescriptor fd, int uid, int gid)80 public native void fchown(FileDescriptor fd, int uid, int gid) throws ErrnoException; fcntlInt(FileDescriptor fd, int cmd, int arg)81 public native int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException; fcntlVoid(FileDescriptor fd, int cmd)82 public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; fdatasync(FileDescriptor fd)83 public native void fdatasync(FileDescriptor fd) throws ErrnoException; fstat(FileDescriptor fd)84 public native StructStat fstat(FileDescriptor fd) throws ErrnoException; fstatvfs(FileDescriptor fd)85 public native StructStatVfs fstatvfs(FileDescriptor fd) throws ErrnoException; fsync(FileDescriptor fd)86 public native void fsync(FileDescriptor fd) throws ErrnoException; ftruncate(FileDescriptor fd, long length)87 public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; gai_strerror(int error)88 public native String gai_strerror(int error); 89 @CriticalNative nativeGetegid()90 private static native int nativeGetegid(); 91 @Override getegid()92 public int getegid() { 93 return nativeGetegid(); 94 } 95 @CriticalNative nativeGeteuid()96 private static native int nativeGeteuid(); 97 @Override geteuid()98 public int geteuid() { 99 return nativeGeteuid(); 100 } 101 @CriticalNative nativeGetgid()102 private static native int nativeGetgid(); 103 @Override getgid()104 public int getgid() { 105 return nativeGetgid(); 106 } getenv(String name)107 public native String getenv(String name); getnameinfo(InetAddress address, int flags)108 public native String getnameinfo(InetAddress address, int flags) throws GaiException; getpeername(FileDescriptor fd)109 public native SocketAddress getpeername(FileDescriptor fd) throws ErrnoException; 110 getpgid(int pid)111 public native int getpgid(int pid) throws ErrnoException; 112 @CriticalNative nativeGetpid()113 private static native int nativeGetpid(); 114 @Override getpid()115 public int getpid() { 116 return nativeGetpid(); 117 } 118 @CriticalNative nativeGetppid()119 private static native int nativeGetppid(); 120 @Override getppid()121 public int getppid() { 122 return nativeGetppid(); 123 } getpwnam(String name)124 public native StructPasswd getpwnam(String name) throws ErrnoException; getpwuid(int uid)125 public native StructPasswd getpwuid(int uid) throws ErrnoException; getrlimit(int resource)126 public native StructRlimit getrlimit(int resource) throws ErrnoException; getsockname(FileDescriptor fd)127 public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; getsockoptByte(FileDescriptor fd, int level, int option)128 public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInAddr(FileDescriptor fd, int level, int option)129 public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInt(FileDescriptor fd, int level, int option)130 public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptLinger(FileDescriptor fd, int level, int option)131 public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptTimeval(FileDescriptor fd, int level, int option)132 public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptUcred(FileDescriptor fd, int level, int option)133 public native StructUcred getsockoptUcred(FileDescriptor fd, int level, int option) throws ErrnoException; 134 @CriticalNative nativeGettid()135 private static native int nativeGettid(); 136 @Override gettid()137 public int gettid() { 138 return nativeGettid(); 139 } 140 @CriticalNative nativeGetuid()141 private static native int nativeGetuid(); 142 @Override getuid()143 public int getuid() { 144 return nativeGetuid(); 145 } getxattr(String path, String name)146 public native byte[] getxattr(String path, String name) throws ErrnoException; getifaddrs()147 public native StructIfaddrs[] getifaddrs() throws ErrnoException; if_indextoname(int index)148 public native String if_indextoname(int index); if_nametoindex(String name)149 public native int if_nametoindex(String name); inet_pton(int family, String address)150 public native InetAddress inet_pton(int family, String address); ioctlFlags(FileDescriptor fd, String interfaceName)151 public native int ioctlFlags(FileDescriptor fd, String interfaceName) throws ErrnoException; ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName)152 public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException; ioctlInt(FileDescriptor fd, int cmd)153 public native int ioctlInt(FileDescriptor fd, int cmd) throws ErrnoException; ioctlMTU(FileDescriptor fd, String interfaceName)154 public native int ioctlMTU(FileDescriptor fd, String interfaceName) throws ErrnoException; isatty(FileDescriptor fd)155 public native boolean isatty(FileDescriptor fd); kill(int pid, int signal)156 public native void kill(int pid, int signal) throws ErrnoException; lchown(String path, int uid, int gid)157 public native void lchown(String path, int uid, int gid) throws ErrnoException; link(String oldPath, String newPath)158 public native void link(String oldPath, String newPath) throws ErrnoException; listen(FileDescriptor fd, int backlog)159 public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; listxattr(String path)160 public native String[] listxattr(String path) throws ErrnoException; lseek(FileDescriptor fd, long offset, int whence)161 public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; lstat(String path)162 public native StructStat lstat(String path) throws ErrnoException; memfd_create(String name, int flags)163 public native FileDescriptor memfd_create(String name, int flags) throws ErrnoException; mincore(long address, long byteCount, byte[] vector)164 public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; mkdir(String path, int mode)165 public native void mkdir(String path, int mode) throws ErrnoException; mkfifo(String path, int mode)166 public native void mkfifo(String path, int mode) throws ErrnoException; mlock(long address, long byteCount)167 public native void mlock(long address, long byteCount) throws ErrnoException; mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset)168 public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException; msync(long address, long byteCount, int flags)169 public native void msync(long address, long byteCount, int flags) throws ErrnoException; munlock(long address, long byteCount)170 public native void munlock(long address, long byteCount) throws ErrnoException; munmap(long address, long byteCount)171 public native void munmap(long address, long byteCount) throws ErrnoException; open(String path, int flags, int mode)172 public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; pipe2(int flags)173 public native FileDescriptor[] pipe2(int flags) throws ErrnoException; poll(StructPollfd[] fds, int timeoutMs)174 public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException; posix_fallocate(FileDescriptor fd, long offset, long length)175 public native void posix_fallocate(FileDescriptor fd, long offset, long length) throws ErrnoException; prctl(int option, long arg2, long arg3, long arg4, long arg5)176 public native int prctl(int option, long arg2, long arg3, long arg4, long arg5) throws ErrnoException; pread(FileDescriptor fd, ByteBuffer buffer, long offset)177 public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 178 final int bytesRead; 179 final int position = buffer.position(); 180 181 if (buffer.isDirect()) { 182 bytesRead = preadBytes(fd, buffer, position, buffer.remaining(), offset); 183 } else { 184 bytesRead = preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 185 } 186 187 maybeUpdateBufferPosition(buffer, position, bytesRead); 188 return bytesRead; 189 } pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)190 public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 191 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 192 return preadBytes(fd, bytes, byteOffset, byteCount, offset); 193 } preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)194 private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; pwrite(FileDescriptor fd, ByteBuffer buffer, long offset)195 public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 196 final int bytesWritten; 197 final int position = buffer.position(); 198 199 if (buffer.isDirect()) { 200 bytesWritten = pwriteBytes(fd, buffer, position, buffer.remaining(), offset); 201 } else { 202 bytesWritten = pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 203 } 204 205 maybeUpdateBufferPosition(buffer, position, bytesWritten); 206 return bytesWritten; 207 } pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)208 public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 209 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 210 return pwriteBytes(fd, bytes, byteOffset, byteCount, offset); 211 } pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)212 private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; read(FileDescriptor fd, ByteBuffer buffer)213 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 214 final int bytesRead; 215 final int position = buffer.position(); 216 217 if (buffer.isDirect()) { 218 bytesRead = readBytes(fd, buffer, position, buffer.remaining()); 219 } else { 220 bytesRead = readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 221 } 222 223 maybeUpdateBufferPosition(buffer, position, bytesRead); 224 return bytesRead; 225 } read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)226 public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 227 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 228 return readBytes(fd, bytes, byteOffset, byteCount); 229 } readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)230 private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; readlink(String path)231 public native String readlink(String path) throws ErrnoException; realpath(String path)232 public native String realpath(String path) throws ErrnoException; readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)233 public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress)234 public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 235 final int bytesReceived; 236 final int position = buffer.position(); 237 238 if (buffer.isDirect()) { 239 bytesReceived = recvfromBytes(fd, buffer, position, buffer.remaining(), flags, srcAddress); 240 } else { 241 bytesReceived = recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, srcAddress); 242 } 243 244 maybeUpdateBufferPosition(buffer, position, bytesReceived); 245 return bytesReceived; 246 } recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)247 public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 248 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 249 return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress); 250 } recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)251 private native int recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException; recvmsg(FileDescriptor fd, StructMsghdr msg, int flags)252 public native int recvmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; remove(String path)253 public native void remove(String path) throws ErrnoException; removexattr(String path, String name)254 public native void removexattr(String path, String name) throws ErrnoException; rename(String oldPath, String newPath)255 public native void rename(String oldPath, String newPath) throws ErrnoException; sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount)256 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount) throws ErrnoException; sendmsg(FileDescriptor fd, StructMsghdr msg, int flags)257 public native int sendmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)258 public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 259 final int bytesSent; 260 final int position = buffer.position(); 261 262 if (buffer.isDirect()) { 263 bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port); 264 } else { 265 bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port); 266 } 267 268 maybeUpdateBufferPosition(buffer, position, bytesSent); 269 return bytesSent; 270 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)271 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 272 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 273 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 274 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address)275 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { 276 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address); 277 } sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)278 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException; sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address)279 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException; setegid(int egid)280 public native void setegid(int egid) throws ErrnoException; setenv(String name, String value, boolean overwrite)281 public native void setenv(String name, String value, boolean overwrite) throws ErrnoException; seteuid(int euid)282 public native void seteuid(int euid) throws ErrnoException; setgid(int gid)283 public native void setgid(int gid) throws ErrnoException; setpgid(int pid, int pgid)284 public native void setpgid(int pid, int pgid) throws ErrnoException; setregid(int rgid, int egid)285 public native void setregid(int rgid, int egid) throws ErrnoException; setreuid(int ruid, int euid)286 public native void setreuid(int ruid, int euid) throws ErrnoException; setsid()287 public native int setsid() throws ErrnoException; setsockoptByte(FileDescriptor fd, int level, int option, int value)288 public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIfreq(FileDescriptor fd, int level, int option, String value)289 public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; setsockoptInt(FileDescriptor fd, int level, int option, int value)290 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value)291 public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value)292 public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value)293 public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value)294 public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; setuid(int uid)295 public native void setuid(int uid) throws ErrnoException; setxattr(String path, String name, byte[] value, int flags)296 public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException; shutdown(FileDescriptor fd, int how)297 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; socket(int domain, int type, int protocol)298 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2)299 public native void socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2) throws ErrnoException; splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags)300 public native long splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags) throws ErrnoException; stat(String path)301 public native StructStat stat(String path) throws ErrnoException; statvfs(String path)302 public native StructStatVfs statvfs(String path) throws ErrnoException; strerror(int errno)303 public native String strerror(int errno); strsignal(int signal)304 public native String strsignal(int signal); symlink(String oldPath, String newPath)305 public native void symlink(String oldPath, String newPath) throws ErrnoException; 306 sysconf(int name)307 public native long sysconf(int name); // throws ErrnoException; tcdrain(FileDescriptor fd)308 public native void tcdrain(FileDescriptor fd) throws ErrnoException; tcsendbreak(FileDescriptor fd, int duration)309 public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException; umask(int mask)310 public int umask(int mask) { 311 if ((mask & 0777) != mask) { 312 throw new IllegalArgumentException("Invalid umask: " + mask); 313 } 314 return umaskImpl(mask); 315 } umaskImpl(int mask)316 private native int umaskImpl(int mask); uname()317 public native StructUtsname uname(); unlink(String pathname)318 public native void unlink(String pathname) throws ErrnoException; unsetenv(String name)319 public native void unsetenv(String name) throws ErrnoException; waitpid(int pid, Int32Ref status, int options)320 public native int waitpid(int pid, Int32Ref status, int options) throws ErrnoException; write(FileDescriptor fd, ByteBuffer buffer)321 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 322 final int bytesWritten; 323 final int position = buffer.position(); 324 if (buffer.isDirect()) { 325 bytesWritten = writeBytes(fd, buffer, position, buffer.remaining()); 326 } else { 327 bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 328 } 329 330 maybeUpdateBufferPosition(buffer, position, bytesWritten); 331 return bytesWritten; 332 } write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)333 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 334 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 335 return writeBytes(fd, bytes, byteOffset, byteCount); 336 } writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)337 private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)338 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 339 maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten)340 private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) { 341 if (bytesReadOrWritten > 0) { 342 buffer.position(bytesReadOrWritten + originalPosition); 343 } 344 } 345 } 346