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