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; madvise(long address, long byteCount, int advice)163 public native void madvise(long address, long byteCount, int advice) throws ErrnoException; memfd_create(String name, int flags)164 public native FileDescriptor memfd_create(String name, int flags) throws ErrnoException; mincore(long address, long byteCount, byte[] vector)165 public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; mkdir(String path, int mode)166 public native void mkdir(String path, int mode) throws ErrnoException; mkfifo(String path, int mode)167 public native void mkfifo(String path, int mode) throws ErrnoException; mlock(long address, long byteCount)168 public native void mlock(long address, long byteCount) throws ErrnoException; mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset)169 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)170 public native void msync(long address, long byteCount, int flags) throws ErrnoException; munlock(long address, long byteCount)171 public native void munlock(long address, long byteCount) throws ErrnoException; munmap(long address, long byteCount)172 public native void munmap(long address, long byteCount) throws ErrnoException; open(String path, int flags, int mode)173 public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; pipe2(int flags)174 public native FileDescriptor[] pipe2(int flags) throws ErrnoException; poll(StructPollfd[] fds, int timeoutMs)175 public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException; posix_fallocate(FileDescriptor fd, long offset, long length)176 public native void posix_fallocate(FileDescriptor fd, long offset, long length) throws ErrnoException; prctl(int option, long arg2, long arg3, long arg4, long arg5)177 public native int prctl(int option, long arg2, long arg3, long arg4, long arg5) throws ErrnoException; pread(FileDescriptor fd, ByteBuffer buffer, long offset)178 public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 179 final int bytesRead; 180 final int position = buffer.position(); 181 182 if (buffer.isDirect()) { 183 bytesRead = preadBytes(fd, buffer, position, buffer.remaining(), offset); 184 } else { 185 bytesRead = preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 186 } 187 188 maybeUpdateBufferPosition(buffer, position, bytesRead); 189 return bytesRead; 190 } pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)191 public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 192 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 193 return preadBytes(fd, bytes, byteOffset, byteCount, offset); 194 } preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)195 private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; pwrite(FileDescriptor fd, ByteBuffer buffer, long offset)196 public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 197 final int bytesWritten; 198 final int position = buffer.position(); 199 200 if (buffer.isDirect()) { 201 bytesWritten = pwriteBytes(fd, buffer, position, buffer.remaining(), offset); 202 } else { 203 bytesWritten = pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 204 } 205 206 maybeUpdateBufferPosition(buffer, position, bytesWritten); 207 return bytesWritten; 208 } pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)209 public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 210 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 211 return pwriteBytes(fd, bytes, byteOffset, byteCount, offset); 212 } pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)213 private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; read(FileDescriptor fd, ByteBuffer buffer)214 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 215 final int bytesRead; 216 final int position = buffer.position(); 217 218 if (buffer.isDirect()) { 219 bytesRead = readBytes(fd, buffer, position, buffer.remaining()); 220 } else { 221 bytesRead = readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 222 } 223 224 maybeUpdateBufferPosition(buffer, position, bytesRead); 225 return bytesRead; 226 } read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)227 public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 228 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 229 return readBytes(fd, bytes, byteOffset, byteCount); 230 } readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)231 private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; readlink(String path)232 public native String readlink(String path) throws ErrnoException; realpath(String path)233 public native String realpath(String path) throws ErrnoException; readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)234 public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress)235 public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 236 final int bytesReceived; 237 final int position = buffer.position(); 238 239 if (buffer.isDirect()) { 240 bytesReceived = recvfromBytes(fd, buffer, position, buffer.remaining(), flags, srcAddress); 241 } else { 242 bytesReceived = recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, srcAddress); 243 } 244 245 maybeUpdateBufferPosition(buffer, position, bytesReceived); 246 return bytesReceived; 247 } recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)248 public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 249 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 250 return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress); 251 } recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)252 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)253 public native int recvmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; remove(String path)254 public native void remove(String path) throws ErrnoException; removexattr(String path, String name)255 public native void removexattr(String path, String name) throws ErrnoException; rename(String oldPath, String newPath)256 public native void rename(String oldPath, String newPath) throws ErrnoException; sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount)257 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount) throws ErrnoException; sendmsg(FileDescriptor fd, StructMsghdr msg, int flags)258 public native int sendmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)259 public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 260 final int bytesSent; 261 final int position = buffer.position(); 262 263 if (buffer.isDirect()) { 264 bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port); 265 } else { 266 bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port); 267 } 268 269 maybeUpdateBufferPosition(buffer, position, bytesSent); 270 return bytesSent; 271 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)272 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 273 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 274 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 275 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address)276 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { 277 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address); 278 } sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)279 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)280 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException; setegid(int egid)281 public native void setegid(int egid) throws ErrnoException; setenv(String name, String value, boolean overwrite)282 public native void setenv(String name, String value, boolean overwrite) throws ErrnoException; seteuid(int euid)283 public native void seteuid(int euid) throws ErrnoException; setgid(int gid)284 public native void setgid(int gid) throws ErrnoException; setpgid(int pid, int pgid)285 public native void setpgid(int pid, int pgid) throws ErrnoException; setregid(int rgid, int egid)286 public native void setregid(int rgid, int egid) throws ErrnoException; setreuid(int ruid, int euid)287 public native void setreuid(int ruid, int euid) throws ErrnoException; setsid()288 public native int setsid() throws ErrnoException; setsockoptByte(FileDescriptor fd, int level, int option, int value)289 public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIfreq(FileDescriptor fd, int level, int option, String value)290 public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; setsockoptInt(FileDescriptor fd, int level, int option, int value)291 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value)292 public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value)293 public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value)294 public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value)295 public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; setuid(int uid)296 public native void setuid(int uid) throws ErrnoException; setxattr(String path, String name, byte[] value, int flags)297 public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException; shutdown(FileDescriptor fd, int how)298 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; socket(int domain, int type, int protocol)299 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2)300 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)301 public native long splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags) throws ErrnoException; stat(String path)302 public native StructStat stat(String path) throws ErrnoException; statvfs(String path)303 public native StructStatVfs statvfs(String path) throws ErrnoException; strerror(int errno)304 public native String strerror(int errno); strsignal(int signal)305 public native String strsignal(int signal); symlink(String oldPath, String newPath)306 public native void symlink(String oldPath, String newPath) throws ErrnoException; 307 sysconf(int name)308 public native long sysconf(int name); // throws ErrnoException; tcdrain(FileDescriptor fd)309 public native void tcdrain(FileDescriptor fd) throws ErrnoException; tcsendbreak(FileDescriptor fd, int duration)310 public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException; umask(int mask)311 public int umask(int mask) { 312 if ((mask & 0777) != mask) { 313 throw new IllegalArgumentException("Invalid umask: " + mask); 314 } 315 return umaskImpl(mask); 316 } umaskImpl(int mask)317 private native int umaskImpl(int mask); uname()318 public native StructUtsname uname(); unlink(String pathname)319 public native void unlink(String pathname) throws ErrnoException; unsetenv(String name)320 public native void unsetenv(String name) throws ErrnoException; waitpid(int pid, Int32Ref status, int options)321 public native int waitpid(int pid, Int32Ref status, int options) throws ErrnoException; write(FileDescriptor fd, ByteBuffer buffer)322 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 323 final int bytesWritten; 324 final int position = buffer.position(); 325 if (buffer.isDirect()) { 326 bytesWritten = writeBytes(fd, buffer, position, buffer.remaining()); 327 } else { 328 bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 329 } 330 331 maybeUpdateBufferPosition(buffer, position, bytesWritten); 332 return bytesWritten; 333 } write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)334 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 335 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 336 return writeBytes(fd, bytes, byteOffset, byteCount); 337 } writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)338 private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)339 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 340 maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten)341 private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) { 342 if (bytesReadOrWritten > 0) { 343 buffer.position(bytesReadOrWritten + originalPosition); 344 } 345 } 346 } 347