View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.unix;
17  
18  import io.netty.util.internal.ThrowableUtil;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  import java.nio.channels.ClosedChannelException;
24  import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
25  
26  import static io.netty.channel.unix.Errors.ioResult;
27  import static io.netty.channel.unix.Errors.newIOException;
28  import static io.netty.channel.unix.Limits.IOV_MAX;
29  import static io.netty.util.internal.ObjectUtil.checkNotNull;
30  import static java.lang.Math.min;
31  
32  /**
33   * Native {@link FileDescriptor} implementation which allows to wrap an {@code int} and provide a
34   * {@link FileDescriptor} for it.
35   */
36  public class FileDescriptor {
37      private static final ClosedChannelException WRITE_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
38              new ClosedChannelException(), FileDescriptor.class, "write(..)");
39      private static final ClosedChannelException WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION =
40              ThrowableUtil.unknownStackTrace(new ClosedChannelException(), FileDescriptor.class, "writeAddress(..)");
41      private static final ClosedChannelException WRITEV_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
42              new ClosedChannelException(), FileDescriptor.class, "writev(..)");
43      private static final ClosedChannelException WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION =
44              ThrowableUtil.unknownStackTrace(new ClosedChannelException(), FileDescriptor.class, "writevAddresses(..)");
45      private static final ClosedChannelException READ_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
46              new ClosedChannelException(), FileDescriptor.class, "read(..)");
47      private static final ClosedChannelException READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
48              new ClosedChannelException(), FileDescriptor.class, "readAddress(..)");
49      private static final Errors.NativeIoException WRITE_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
50              Errors.newConnectionResetException("syscall:write", Errors.ERRNO_EPIPE_NEGATIVE),
51              FileDescriptor.class, "write(..)");
52      private static final Errors.NativeIoException WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION =
53              ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:write",
54                      Errors.ERRNO_EPIPE_NEGATIVE), FileDescriptor.class, "writeAddress(..)");
55      private static final Errors.NativeIoException WRITEV_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
56              Errors.newConnectionResetException("syscall:writev", Errors.ERRNO_EPIPE_NEGATIVE),
57              FileDescriptor.class, "writev(..)");
58      private static final Errors.NativeIoException WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION =
59              ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:writev",
60                      Errors.ERRNO_EPIPE_NEGATIVE), FileDescriptor.class, "writeAddresses(..)");
61      private static final Errors.NativeIoException READ_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
62              Errors.newConnectionResetException("syscall:read", Errors.ERRNO_ECONNRESET_NEGATIVE),
63              FileDescriptor.class, "read(..)");
64      private static final Errors.NativeIoException READ_ADDRESS_CONNECTION_RESET_EXCEPTION =
65              ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:read",
66                      Errors.ERRNO_ECONNRESET_NEGATIVE), FileDescriptor.class, "readAddress(..)");
67  
68      private static final AtomicIntegerFieldUpdater<FileDescriptor> stateUpdater =
69              AtomicIntegerFieldUpdater.newUpdater(FileDescriptor.class, "state");
70  
71      private static final int STATE_CLOSED_MASK = 1;
72      private static final int STATE_INPUT_SHUTDOWN_MASK = 1 << 1;
73      private static final int STATE_OUTPUT_SHUTDOWN_MASK = 1 << 2;
74      private static final int STATE_ALL_MASK = STATE_CLOSED_MASK |
75                                                STATE_INPUT_SHUTDOWN_MASK |
76                                                STATE_OUTPUT_SHUTDOWN_MASK;
77  
78      /**
79       * Bit map = [Output Shutdown | Input Shutdown | Closed]
80       */
81      volatile int state;
82      final int fd;
83  
84      public FileDescriptor(int fd) {
85          if (fd < 0) {
86              throw new IllegalArgumentException("fd must be >= 0");
87          }
88          this.fd = fd;
89      }
90  
91      /**
92       * Return the int value of the filedescriptor.
93       */
94      public final int intValue() {
95          return fd;
96      }
97  
98      /**
99       * Close the file descriptor.
100      */
101     public void close() throws IOException {
102         for (;;) {
103             int state = this.state;
104             if (isClosed(state)) {
105                 return;
106             }
107             // Once a close operation happens, the channel is considered shutdown.
108             if (casState(state, state | STATE_ALL_MASK)) {
109                 break;
110             }
111         }
112         int res = close(fd);
113         if (res < 0) {
114             throw newIOException("close", res);
115         }
116     }
117 
118     /**
119      * Returns {@code true} if the file descriptor is open.
120      */
121     public boolean isOpen() {
122         return !isClosed(state);
123     }
124 
125     public final int write(ByteBuffer buf, int pos, int limit) throws IOException {
126         int res = write(fd, buf, pos, limit);
127         if (res >= 0) {
128             return res;
129         }
130         return ioResult("write", res, WRITE_CONNECTION_RESET_EXCEPTION, WRITE_CLOSED_CHANNEL_EXCEPTION);
131     }
132 
133     public final int writeAddress(long address, int pos, int limit) throws IOException {
134         int res = writeAddress(fd, address, pos, limit);
135         if (res >= 0) {
136             return res;
137         }
138         return ioResult("writeAddress", res,
139                 WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION, WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
140     }
141 
142     public final long writev(ByteBuffer[] buffers, int offset, int length, long maxBytesToWrite) throws IOException {
143         long res = writev(fd, buffers, offset, min(IOV_MAX, length), maxBytesToWrite);
144         if (res >= 0) {
145             return res;
146         }
147         return ioResult("writev", (int) res, WRITEV_CONNECTION_RESET_EXCEPTION, WRITEV_CLOSED_CHANNEL_EXCEPTION);
148     }
149 
150     public final long writevAddresses(long memoryAddress, int length) throws IOException {
151         long res = writevAddresses(fd, memoryAddress, length);
152         if (res >= 0) {
153             return res;
154         }
155         return ioResult("writevAddresses", (int) res,
156                 WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION, WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION);
157     }
158 
159     public final int read(ByteBuffer buf, int pos, int limit) throws IOException {
160         int res = read(fd, buf, pos, limit);
161         if (res > 0) {
162             return res;
163         }
164         if (res == 0) {
165             return -1;
166         }
167         return ioResult("read", res, READ_CONNECTION_RESET_EXCEPTION, READ_CLOSED_CHANNEL_EXCEPTION);
168     }
169 
170     public final int readAddress(long address, int pos, int limit) throws IOException {
171         int res = readAddress(fd, address, pos, limit);
172         if (res > 0) {
173             return res;
174         }
175         if (res == 0) {
176             return -1;
177         }
178         return ioResult("readAddress", res,
179                 READ_ADDRESS_CONNECTION_RESET_EXCEPTION, READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
180     }
181 
182     @Override
183     public String toString() {
184         return "FileDescriptor{" +
185                 "fd=" + fd +
186                 '}';
187     }
188 
189     @Override
190     public boolean equals(Object o) {
191         if (this == o) {
192             return true;
193         }
194         if (!(o instanceof FileDescriptor)) {
195             return false;
196         }
197 
198         return fd == ((FileDescriptor) o).fd;
199     }
200 
201     @Override
202     public int hashCode() {
203         return fd;
204     }
205 
206     /**
207      * Open a new {@link FileDescriptor} for the given path.
208      */
209     public static FileDescriptor from(String path) throws IOException {
210         checkNotNull(path, "path");
211         int res = open(path);
212         if (res < 0) {
213             throw newIOException("open", res);
214         }
215         return new FileDescriptor(res);
216     }
217 
218     /**
219      * Open a new {@link FileDescriptor} for the given {@link File}.
220      */
221     public static FileDescriptor from(File file) throws IOException {
222         return from(checkNotNull(file, "file").getPath());
223     }
224 
225     /**
226      * @return [0] = read end, [1] = write end
227      */
228     public static FileDescriptor[] pipe() throws IOException {
229         long res = newPipe();
230         if (res < 0) {
231             throw newIOException("newPipe", (int) res);
232         }
233         return new FileDescriptor[]{new FileDescriptor((int) (res >>> 32)), new FileDescriptor((int) res)};
234     }
235 
236     final boolean casState(int expected, int update) {
237         return stateUpdater.compareAndSet(this, expected, update);
238     }
239 
240     static boolean isClosed(int state) {
241         return (state & STATE_CLOSED_MASK) != 0;
242     }
243 
244     static boolean isInputShutdown(int state) {
245         return (state & STATE_INPUT_SHUTDOWN_MASK) != 0;
246     }
247 
248     static boolean isOutputShutdown(int state) {
249         return (state & STATE_OUTPUT_SHUTDOWN_MASK) != 0;
250     }
251 
252     static int inputShutdown(int state) {
253         return state | STATE_INPUT_SHUTDOWN_MASK;
254     }
255 
256     static int outputShutdown(int state) {
257         return state | STATE_OUTPUT_SHUTDOWN_MASK;
258     }
259 
260     private static native int open(String path);
261     private static native int close(int fd);
262 
263     private static native int write(int fd, ByteBuffer buf, int pos, int limit);
264     private static native int writeAddress(int fd, long address, int pos, int limit);
265     private static native long writev(int fd, ByteBuffer[] buffers, int offset, int length, long maxBytesToWrite);
266     private static native long writevAddresses(int fd, long memoryAddress, int length);
267 
268     private static native int read(int fd, ByteBuffer buf, int pos, int limit);
269     private static native int readAddress(int fd, long address, int pos, int limit);
270 
271     private static native long newPipe();
272 }