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 java.io.FileDescriptor; 20 import java.net.InetAddress; 21 import java.net.InetSocketAddress; 22 import java.net.SocketAddress; 23 import java.nio.ByteBuffer; 24 import java.nio.NioUtils; 25 import libcore.util.MutableInt; 26 import libcore.util.MutableLong; 27 28 public final class Posix implements Os { Posix()29 Posix() { } 30 accept(FileDescriptor fd, InetSocketAddress peerAddress)31 public native FileDescriptor accept(FileDescriptor fd, InetSocketAddress peerAddress) throws ErrnoException; access(String path, int mode)32 public native boolean access(String path, int mode) throws ErrnoException; bind(FileDescriptor fd, InetAddress address, int port)33 public native void bind(FileDescriptor fd, InetAddress address, int port) throws ErrnoException; chmod(String path, int mode)34 public native void chmod(String path, int mode) throws ErrnoException; close(FileDescriptor fd)35 public native void close(FileDescriptor fd) throws ErrnoException; connect(FileDescriptor fd, InetAddress address, int port)36 public native void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException; dup(FileDescriptor oldFd)37 public native FileDescriptor dup(FileDescriptor oldFd) throws ErrnoException; dup2(FileDescriptor oldFd, int newFd)38 public native FileDescriptor dup2(FileDescriptor oldFd, int newFd) throws ErrnoException; environ()39 public native String[] environ(); fcntlVoid(FileDescriptor fd, int cmd)40 public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; fcntlLong(FileDescriptor fd, int cmd, long arg)41 public native int fcntlLong(FileDescriptor fd, int cmd, long arg) throws ErrnoException; fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg)42 public native int fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg) throws ErrnoException; fdatasync(FileDescriptor fd)43 public native void fdatasync(FileDescriptor fd) throws ErrnoException; fstat(FileDescriptor fd)44 public native StructStat fstat(FileDescriptor fd) throws ErrnoException; fstatfs(FileDescriptor fd)45 public native StructStatFs fstatfs(FileDescriptor fd) throws ErrnoException; fsync(FileDescriptor fd)46 public native void fsync(FileDescriptor fd) throws ErrnoException; ftruncate(FileDescriptor fd, long length)47 public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; gai_strerror(int error)48 public native String gai_strerror(int error); getaddrinfo(String node, StructAddrinfo hints)49 public native InetAddress[] getaddrinfo(String node, StructAddrinfo hints) throws GaiException; getegid()50 public native int getegid(); geteuid()51 public native int geteuid(); getgid()52 public native int getgid(); getenv(String name)53 public native String getenv(String name); getnameinfo(InetAddress address, int flags)54 public native String getnameinfo(InetAddress address, int flags) throws GaiException; getpid()55 public native int getpid(); getppid()56 public native int getppid(); getpwnam(String name)57 public native StructPasswd getpwnam(String name) throws ErrnoException; getpwuid(int uid)58 public native StructPasswd getpwuid(int uid) throws ErrnoException; getsockname(FileDescriptor fd)59 public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; getsockoptByte(FileDescriptor fd, int level, int option)60 public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInAddr(FileDescriptor fd, int level, int option)61 public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInt(FileDescriptor fd, int level, int option)62 public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptLinger(FileDescriptor fd, int level, int option)63 public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptTimeval(FileDescriptor fd, int level, int option)64 public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; getuid()65 public native int getuid(); if_indextoname(int index)66 public native String if_indextoname(int index); inet_pton(int family, String address)67 public native InetAddress inet_pton(int family, String address); ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName)68 public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException; ioctlInt(FileDescriptor fd, int cmd, MutableInt arg)69 public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException; isatty(FileDescriptor fd)70 public native boolean isatty(FileDescriptor fd); kill(int pid, int signal)71 public native void kill(int pid, int signal) throws ErrnoException; listen(FileDescriptor fd, int backlog)72 public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; lseek(FileDescriptor fd, long offset, int whence)73 public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; lstat(String path)74 public native StructStat lstat(String path) throws ErrnoException; mincore(long address, long byteCount, byte[] vector)75 public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; mkdir(String path, int mode)76 public native void mkdir(String path, int mode) throws ErrnoException; mlock(long address, long byteCount)77 public native void mlock(long address, long byteCount) throws ErrnoException; mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset)78 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)79 public native void msync(long address, long byteCount, int flags) throws ErrnoException; munlock(long address, long byteCount)80 public native void munlock(long address, long byteCount) throws ErrnoException; munmap(long address, long byteCount)81 public native void munmap(long address, long byteCount) throws ErrnoException; open(String path, int flags, int mode)82 public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; pipe()83 public native FileDescriptor[] pipe() throws ErrnoException; poll(StructPollfd[] fds, int timeoutMs)84 public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException; pread(FileDescriptor fd, ByteBuffer buffer, long offset)85 public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException { 86 if (buffer.isDirect()) { 87 return preadBytes(fd, buffer, buffer.position(), buffer.remaining(), offset); 88 } else { 89 return preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining(), offset); 90 } 91 } pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)92 public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException { 93 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 94 return preadBytes(fd, bytes, byteOffset, byteCount, offset); 95 } preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)96 private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException; pwrite(FileDescriptor fd, ByteBuffer buffer, long offset)97 public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException { 98 if (buffer.isDirect()) { 99 return pwriteBytes(fd, buffer, buffer.position(), buffer.remaining(), offset); 100 } else { 101 return pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining(), offset); 102 } 103 } pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)104 public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException { 105 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 106 return pwriteBytes(fd, bytes, byteOffset, byteCount, offset); 107 } pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)108 private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException; read(FileDescriptor fd, ByteBuffer buffer)109 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 110 if (buffer.isDirect()) { 111 return readBytes(fd, buffer, buffer.position(), buffer.remaining()); 112 } else { 113 return readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 114 } 115 } read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)116 public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 117 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 118 return readBytes(fd, bytes, byteOffset, byteCount); 119 } readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)120 private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException; readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)121 public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress)122 public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException { 123 if (buffer.isDirect()) { 124 return recvfromBytes(fd, buffer, buffer.position(), buffer.remaining(), flags, srcAddress); 125 } else { 126 return recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining(), flags, srcAddress); 127 } 128 } recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)129 public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException { 130 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 131 return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress); 132 } recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)133 private native int recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException; remove(String path)134 public native void remove(String path) throws ErrnoException; rename(String oldPath, String newPath)135 public native void rename(String oldPath, String newPath) throws ErrnoException; sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount)136 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException; sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)137 public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException { 138 if (buffer.isDirect()) { 139 return sendtoBytes(fd, buffer, buffer.position(), buffer.remaining(), flags, inetAddress, port); 140 } else { 141 return sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining(), flags, inetAddress, port); 142 } 143 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)144 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException { 145 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 146 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 147 } sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)148 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException; setegid(int egid)149 public native void setegid(int egid) throws ErrnoException; seteuid(int euid)150 public native void seteuid(int euid) throws ErrnoException; setgid(int gid)151 public native void setgid(int gid) throws ErrnoException; setsockoptByte(FileDescriptor fd, int level, int option, int value)152 public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIfreq(FileDescriptor fd, int level, int option, String value)153 public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; setsockoptInt(FileDescriptor fd, int level, int option, int value)154 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value)155 public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value)156 public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value)157 public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value)158 public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; setuid(int uid)159 public native void setuid(int uid) throws ErrnoException; shutdown(FileDescriptor fd, int how)160 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; socket(int domain, int type, int protocol)161 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; stat(String path)162 public native StructStat stat(String path) throws ErrnoException; statfs(String path)163 public native StructStatFs statfs(String path) throws ErrnoException; strerror(int errno)164 public native String strerror(int errno); symlink(String oldPath, String newPath)165 public native void symlink(String oldPath, String newPath) throws ErrnoException; sysconf(int name)166 public native long sysconf(int name); uname()167 public native StructUtsname uname(); waitpid(int pid, MutableInt status, int options)168 public native int waitpid(int pid, MutableInt status, int options) throws ErrnoException; write(FileDescriptor fd, ByteBuffer buffer)169 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 170 if (buffer.isDirect()) { 171 return writeBytes(fd, buffer, buffer.position(), buffer.remaining()); 172 } else { 173 return writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 174 } 175 } write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)176 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { 177 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 178 return writeBytes(fd, bytes, byteOffset, byteCount); 179 } writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)180 private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException; writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)181 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; 182 } 183