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 17package libcore.io; 18 19import android.system.ErrnoException; 20import android.system.GaiException; 21import android.system.StructAddrinfo; 22import android.system.StructFlock; 23import android.system.StructGroupReq; 24import android.system.StructGroupSourceReq; 25import android.system.StructLinger; 26import android.system.StructPasswd; 27import android.system.StructPollfd; 28import android.system.StructStat; 29import android.system.StructStatVfs; 30import android.system.StructTimeval; 31import android.system.StructUcred; 32import android.system.StructUtsname; 33import android.util.MutableInt; 34import android.util.MutableLong; 35import java.io.FileDescriptor; 36import java.io.InterruptedIOException; 37import java.net.InetAddress; 38import java.net.InetSocketAddress; 39import java.net.SocketAddress; 40import java.net.SocketException; 41import java.nio.ByteBuffer; 42import java.nio.NioUtils; 43 44public final class Posix implements Os { 45 Posix() { } 46 47 public native FileDescriptor accept(FileDescriptor fd, InetSocketAddress peerAddress) throws ErrnoException, SocketException; 48 public native boolean access(String path, int mode) throws ErrnoException; 49 public native InetAddress[] android_getaddrinfo(String node, StructAddrinfo hints, int netId) throws GaiException; 50 public native void bind(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; 51 public native void bind(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; 52 public native void chmod(String path, int mode) throws ErrnoException; 53 public native void chown(String path, int uid, int gid) throws ErrnoException; 54 public native void close(FileDescriptor fd) throws ErrnoException; 55 public native void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; 56 public native void connect(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; 57 public native FileDescriptor dup(FileDescriptor oldFd) throws ErrnoException; 58 public native FileDescriptor dup2(FileDescriptor oldFd, int newFd) throws ErrnoException; 59 public native String[] environ(); 60 public native void execv(String filename, String[] argv) throws ErrnoException; 61 public native void execve(String filename, String[] argv, String[] envp) throws ErrnoException; 62 public native void fchmod(FileDescriptor fd, int mode) throws ErrnoException; 63 public native void fchown(FileDescriptor fd, int uid, int gid) throws ErrnoException; 64 public native int fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg) throws ErrnoException, InterruptedIOException; 65 public native int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException; 66 public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; 67 public native void fdatasync(FileDescriptor fd) throws ErrnoException; 68 public native StructStat fstat(FileDescriptor fd) throws ErrnoException; 69 public native StructStatVfs fstatvfs(FileDescriptor fd) throws ErrnoException; 70 public native void fsync(FileDescriptor fd) throws ErrnoException; 71 public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; 72 public native String gai_strerror(int error); 73 public native int getegid(); 74 public native int geteuid(); 75 public native int getgid(); 76 public native String getenv(String name); 77 public native String getnameinfo(InetAddress address, int flags) throws GaiException; 78 public native SocketAddress getpeername(FileDescriptor fd) throws ErrnoException; 79 public native int getpgid(int pid); 80 public native int getpid(); 81 public native int getppid(); 82 public native StructPasswd getpwnam(String name) throws ErrnoException; 83 public native StructPasswd getpwuid(int uid) throws ErrnoException; 84 public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; 85 public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; 86 public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; 87 public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; 88 public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; 89 public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; 90 public native StructUcred getsockoptUcred(FileDescriptor fd, int level, int option) throws ErrnoException; 91 public native int gettid(); 92 public native int getuid(); 93 public native int getxattr(String path, String name, byte[] outValue) throws ErrnoException; 94 public native String if_indextoname(int index); 95 public native InetAddress inet_pton(int family, String address); 96 public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException; 97 public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException; 98 public native boolean isatty(FileDescriptor fd); 99 public native void kill(int pid, int signal) throws ErrnoException; 100 public native void lchown(String path, int uid, int gid) throws ErrnoException; 101 public native void link(String oldPath, String newPath) throws ErrnoException; 102 public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; 103 public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; 104 public native StructStat lstat(String path) throws ErrnoException; 105 public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; 106 public native void mkdir(String path, int mode) throws ErrnoException; 107 public native void mkfifo(String path, int mode) throws ErrnoException; 108 public native void mlock(long address, long byteCount) throws ErrnoException; 109 public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException; 110 public native void msync(long address, long byteCount, int flags) throws ErrnoException; 111 public native void munlock(long address, long byteCount) throws ErrnoException; 112 public native void munmap(long address, long byteCount) throws ErrnoException; 113 public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; 114 public native FileDescriptor[] pipe2(int flags) throws ErrnoException; 115 public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException; 116 public native void posix_fallocate(FileDescriptor fd, long offset, long length) throws ErrnoException; 117 public native int prctl(int option, long arg2, long arg3, long arg4, long arg5) throws ErrnoException; 118 public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 119 final int bytesRead; 120 final int position = buffer.position(); 121 122 if (buffer.isDirect()) { 123 bytesRead = preadBytes(fd, buffer, position, buffer.remaining(), offset); 124 } else { 125 bytesRead = preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 126 } 127 128 maybeUpdateBufferPosition(buffer, position, bytesRead); 129 return bytesRead; 130 } 131 public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 132 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 133 return preadBytes(fd, bytes, byteOffset, byteCount, offset); 134 } 135 private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; 136 public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 137 final int bytesWritten; 138 final int position = buffer.position(); 139 140 if (buffer.isDirect()) { 141 bytesWritten = pwriteBytes(fd, buffer, position, buffer.remaining(), offset); 142 } else { 143 bytesWritten = pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 144 } 145 146 maybeUpdateBufferPosition(buffer, position, bytesWritten); 147 return bytesWritten; 148 } 149 public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 150 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 151 return pwriteBytes(fd, bytes, byteOffset, byteCount, offset); 152 } 153 private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; 154 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 155 final int bytesRead; 156 final int position = buffer.position(); 157 158 if (buffer.isDirect()) { 159 bytesRead = readBytes(fd, buffer, position, buffer.remaining()); 160 } else { 161 bytesRead = readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 162 } 163 164 maybeUpdateBufferPosition(buffer, position, bytesRead); 165 return bytesRead; 166 } 167 public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 168 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 169 return readBytes(fd, bytes, byteOffset, byteCount); 170 } 171 private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; 172 public native String readlink(String path) throws ErrnoException; 173 public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 174 public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 175 final int bytesReceived; 176 final int position = buffer.position(); 177 178 if (buffer.isDirect()) { 179 bytesReceived = recvfromBytes(fd, buffer, position, buffer.remaining(), flags, srcAddress); 180 } else { 181 bytesReceived = recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, srcAddress); 182 } 183 184 maybeUpdateBufferPosition(buffer, position, bytesReceived); 185 return bytesReceived; 186 } 187 public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 188 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 189 return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress); 190 } 191 private native int recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException; 192 public native void remove(String path) throws ErrnoException; 193 public native void removexattr(String path, String name) throws ErrnoException; 194 public native void rename(String oldPath, String newPath) throws ErrnoException; 195 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException; 196 public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 197 final int bytesSent; 198 final int position = buffer.position(); 199 200 if (buffer.isDirect()) { 201 bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port); 202 } else { 203 bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port); 204 } 205 206 maybeUpdateBufferPosition(buffer, position, bytesSent); 207 return bytesSent; 208 } 209 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 210 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 211 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 212 } 213 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { 214 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address); 215 } 216 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException; 217 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException; 218 public native void setegid(int egid) throws ErrnoException; 219 public native void setenv(String name, String value, boolean overwrite) throws ErrnoException; 220 public native void seteuid(int euid) throws ErrnoException; 221 public native void setgid(int gid) throws ErrnoException; 222 public native void setpgid(int pid, int pgid) throws ErrnoException; 223 public native void setregid(int rgid, int egid) throws ErrnoException; 224 public native void setreuid(int ruid, int euid) throws ErrnoException; 225 public native int setsid() throws ErrnoException; 226 public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 227 public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; 228 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 229 public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 230 public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; 231 public native void setsockoptGroupSourceReq(FileDescriptor fd, int level, int option, StructGroupSourceReq value) throws ErrnoException; 232 public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; 233 public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; 234 public native void setuid(int uid) throws ErrnoException; 235 public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException; 236 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; 237 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; 238 public native void socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2) throws ErrnoException; 239 public native StructStat stat(String path) throws ErrnoException; 240 public native StructStatVfs statvfs(String path) throws ErrnoException; 241 public native String strerror(int errno); 242 public native String strsignal(int signal); 243 public native void symlink(String oldPath, String newPath) throws ErrnoException; 244 public native long sysconf(int name); 245 public native void tcdrain(FileDescriptor fd) throws ErrnoException; 246 public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException; 247 public int umask(int mask) { 248 if ((mask & 0777) != mask) { 249 throw new IllegalArgumentException("Invalid umask: " + mask); 250 } 251 return umaskImpl(mask); 252 } 253 private native int umaskImpl(int mask); 254 public native StructUtsname uname(); 255 public native void unsetenv(String name) throws ErrnoException; 256 public native int waitpid(int pid, MutableInt status, int options) throws ErrnoException; 257 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 258 final int bytesWritten; 259 final int position = buffer.position(); 260 if (buffer.isDirect()) { 261 bytesWritten = writeBytes(fd, buffer, position, buffer.remaining()); 262 } else { 263 bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 264 } 265 266 maybeUpdateBufferPosition(buffer, position, bytesWritten); 267 return bytesWritten; 268 } 269 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 270 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 271 return writeBytes(fd, bytes, byteOffset, byteCount); 272 } 273 private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; 274 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 275 276 private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) { 277 if (bytesReadOrWritten > 0) { 278 buffer.position(bytesReadOrWritten + originalPosition); 279 } 280 } 281} 282