blob: f7ffc37f085ffc6b66490d697c2d01ab7dc5d320 [file] [log] [blame]
Nirav Atre9850dd92018-07-24 17:03:44 -07001/*
2 * Copyright (C) 2018 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 android.os;
18
Nick Kralevich1de00962018-12-17 14:41:59 -080019import static android.system.OsConstants.F_DUPFD_CLOEXEC;
20
Nirav Atre9850dd92018-07-24 17:03:44 -070021import android.annotation.NonNull;
22import android.annotation.SystemApi;
23import android.system.ErrnoException;
24import android.system.Os;
25
26import java.io.Closeable;
27import java.io.FileDescriptor;
28
29/**
30 * Collection representing a set of open file descriptors and an opaque data stream.
31 *
32 * @hide
33 */
34@SystemApi
35public final class NativeHandle implements Closeable {
36 // whether this object owns mFds
37 private boolean mOwn = false;
38 private FileDescriptor[] mFds;
39 private int[] mInts;
40
41 /**
42 * Constructs a {@link NativeHandle} object containing
43 * zero file descriptors and an empty data stream.
44 */
45 public NativeHandle() {
46 this(new FileDescriptor[0], new int[0], false);
47 }
48
49 /**
50 * Constructs a {@link NativeHandle} object containing the given
51 * {@link FileDescriptor} object and an empty data stream.
52 */
53 public NativeHandle(@NonNull FileDescriptor descriptor, boolean own) {
54 this(new FileDescriptor[] {descriptor}, new int[0], own);
55 }
56
57 /**
58 * Convenience method for creating a list of file descriptors.
59 *
60 * @hide
61 */
62 private static FileDescriptor[] createFileDescriptorArray(@NonNull int[] fds) {
63 FileDescriptor[] list = new FileDescriptor[fds.length];
64 for (int i = 0; i < fds.length; i++) {
65 FileDescriptor descriptor = new FileDescriptor();
66 descriptor.setInt$(fds[i]);
67 list[i] = descriptor;
68 }
69 return list;
70 }
71
72 /**
73 * Convenience method for instantiating a {@link NativeHandle} from JNI. It does
74 * not take ownership of the int[] params. It does not dupe the FileDescriptors.
75 *
76 * @hide
77 */
78 private NativeHandle(@NonNull int[] fds, @NonNull int[] ints, boolean own) {
79 this(createFileDescriptorArray(fds), ints, own);
80 }
81
82 /**
83 * Instantiate an opaque {@link NativeHandle} from fds and integers.
84 *
85 * @param own whether the fds are owned by this object and should be closed
86 */
87 public NativeHandle(@NonNull FileDescriptor[] fds, @NonNull int[] ints, boolean own) {
88 mFds = fds.clone();
89 mInts = ints.clone();
90 mOwn = own;
91 }
92
93 /**
94 * Returns whether this {@link NativeHandle} object contains a single file
95 * descriptor and nothing else.
96 *
97 * @return a boolean value
98 */
99 public boolean hasSingleFileDescriptor() {
100 return mFds.length == 1 && mInts.length == 0;
101 }
102
103 /**
104 * Explicitly duplicate NativeHandle (this dups all file descritptors).
Steven Moreland12a2c0a2018-08-13 13:53:39 -0700105 *
106 * If this method is called, this must also be explicitly closed with
107 * {@link #close()}.
Nirav Atre9850dd92018-07-24 17:03:44 -0700108 */
109 public NativeHandle dup() throws java.io.IOException {
110 FileDescriptor[] fds = new FileDescriptor[mFds.length];
111 try {
112 for (int i = 0; i < mFds.length; i++) {
Nick Kralevich1de00962018-12-17 14:41:59 -0800113 FileDescriptor newFd = new FileDescriptor();
114 int fdint = Os.fcntlInt(mFds[i], F_DUPFD_CLOEXEC, 0);
115 newFd.setInt$(fdint);
116 fds[i] = newFd;
Nirav Atre9850dd92018-07-24 17:03:44 -0700117 }
118 } catch (ErrnoException e) {
119 e.rethrowAsIOException();
120 }
121 return new NativeHandle(fds, mInts, true /*own*/);
122 }
123
124 /**
125 * Closes the file descriptors if they are owned by this object.
126 *
127 * This also invalidates the object.
128 */
129 @Override
130 public void close() throws java.io.IOException {
131 if (!mOwn) {
132 return;
133 }
134
135 try {
136 for (FileDescriptor fd : mFds) {
137 Os.close(fd);
138 }
139 } catch (ErrnoException e) {
140 e.rethrowAsIOException();
141 }
142
143 mOwn = false;
144 mFds = null;
145 mInts = null;
146 }
147
148 /**
149 * Returns the underlying lone file descriptor.
150 *
151 * @return a {@link FileDescriptor} object
152 * @throws IllegalStateException if this object contains either zero or
153 * more than one file descriptor, or a non-empty data stream.
154 */
155 public FileDescriptor getFileDescriptor() {
156 if (!hasSingleFileDescriptor()) {
157 throw new IllegalStateException(
158 "NativeHandle is not single file descriptor. Contents must"
159 + " be retreived through getFileDescriptors and getInts.");
160 }
161
162 return mFds[0];
163 }
164
165 /**
166 * Convenience method for fetching this object's file descriptors from JNI.
167 * @return a mutable copy of the underlying file descriptors (as an int[])
168 *
169 * @hide
170 */
171 private int[] getFdsAsIntArray() {
172 int numFds = mFds.length;
173 int[] fds = new int[numFds];
174
175 for (int i = 0; i < numFds; i++) {
176 fds[i] = mFds[i].getInt$();
177 }
178
179 return fds;
180 }
181
182 /**
183 * Fetch file descriptors.
184 *
185 * @return the fds.
186 */
187 public FileDescriptor[] getFileDescriptors() {
188 return mFds;
189 }
190
191 /**
192 * Fetch opaque ints. Note: This object retains ownership of the data.
193 *
194 * @return the opaque data stream.
195 */
196 public int[] getInts() {
197 return mInts;
198 }
199}