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