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