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