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