blob: 8d341b603eb48127405427399ca1535dfe5526f7 [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;
Steven Moreland14b9eb62019-01-11 10:19:51 -080023import android.annotation.TestApi;
Nirav Atre9850dd92018-07-24 17:03:44 -070024import android.system.ErrnoException;
25import android.system.Os;
26
27import java.io.Closeable;
28import java.io.FileDescriptor;
29
30/**
31 * Collection representing a set of open file descriptors and an opaque data stream.
32 *
33 * @hide
34 */
35@SystemApi
Steven Moreland14b9eb62019-01-11 10:19:51 -080036@TestApi
Nirav Atre9850dd92018-07-24 17:03:44 -070037public final class NativeHandle implements Closeable {
38 // whether this object owns mFds
39 private boolean mOwn = false;
40 private FileDescriptor[] mFds;
41 private int[] mInts;
42
43 /**
44 * Constructs a {@link NativeHandle} object containing
45 * zero file descriptors and an empty data stream.
46 */
47 public NativeHandle() {
48 this(new FileDescriptor[0], new int[0], false);
49 }
50
51 /**
52 * Constructs a {@link NativeHandle} object containing the given
53 * {@link FileDescriptor} object and an empty data stream.
54 */
55 public NativeHandle(@NonNull FileDescriptor descriptor, boolean own) {
56 this(new FileDescriptor[] {descriptor}, new int[0], own);
57 }
58
59 /**
60 * Convenience method for creating a list of file descriptors.
61 *
62 * @hide
63 */
64 private static FileDescriptor[] createFileDescriptorArray(@NonNull int[] fds) {
65 FileDescriptor[] list = new FileDescriptor[fds.length];
66 for (int i = 0; i < fds.length; i++) {
67 FileDescriptor descriptor = new FileDescriptor();
68 descriptor.setInt$(fds[i]);
69 list[i] = descriptor;
70 }
71 return list;
72 }
73
74 /**
75 * Convenience method for instantiating a {@link NativeHandle} from JNI. It does
76 * not take ownership of the int[] params. It does not dupe the FileDescriptors.
77 *
78 * @hide
79 */
80 private NativeHandle(@NonNull int[] fds, @NonNull int[] ints, boolean own) {
81 this(createFileDescriptorArray(fds), ints, own);
82 }
83
84 /**
85 * Instantiate an opaque {@link NativeHandle} from fds and integers.
86 *
87 * @param own whether the fds are owned by this object and should be closed
88 */
89 public NativeHandle(@NonNull FileDescriptor[] fds, @NonNull int[] ints, boolean own) {
90 mFds = fds.clone();
91 mInts = ints.clone();
92 mOwn = own;
93 }
94
95 /**
96 * Returns whether this {@link NativeHandle} object contains a single file
97 * descriptor and nothing else.
98 *
99 * @return a boolean value
100 */
101 public boolean hasSingleFileDescriptor() {
Steven Moreland0ff061a2019-03-04 17:56:30 -0800102 checkOpen();
103
Nirav Atre9850dd92018-07-24 17:03:44 -0700104 return mFds.length == 1 && mInts.length == 0;
105 }
106
107 /**
108 * Explicitly duplicate NativeHandle (this dups all file descritptors).
Steven Moreland12a2c0a2018-08-13 13:53:39 -0700109 *
110 * If this method is called, this must also be explicitly closed with
111 * {@link #close()}.
Nirav Atre9850dd92018-07-24 17:03:44 -0700112 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800113 public @NonNull NativeHandle dup() throws java.io.IOException {
Nirav Atre9850dd92018-07-24 17:03:44 -0700114 FileDescriptor[] fds = new FileDescriptor[mFds.length];
115 try {
116 for (int i = 0; i < mFds.length; i++) {
Nick Kralevich1de00962018-12-17 14:41:59 -0800117 FileDescriptor newFd = new FileDescriptor();
118 int fdint = Os.fcntlInt(mFds[i], F_DUPFD_CLOEXEC, 0);
119 newFd.setInt$(fdint);
120 fds[i] = newFd;
Nirav Atre9850dd92018-07-24 17:03:44 -0700121 }
122 } catch (ErrnoException e) {
123 e.rethrowAsIOException();
124 }
125 return new NativeHandle(fds, mInts, true /*own*/);
126 }
127
Steven Moreland0ff061a2019-03-04 17:56:30 -0800128 private void checkOpen() {
129 if (mFds == null) {
130 throw new IllegalStateException("NativeHandle is invalidated after close.");
131 }
132 }
133
Nirav Atre9850dd92018-07-24 17:03:44 -0700134 /**
135 * Closes the file descriptors if they are owned by this object.
136 *
137 * This also invalidates the object.
138 */
139 @Override
140 public void close() throws java.io.IOException {
Steven Moreland0ff061a2019-03-04 17:56:30 -0800141 checkOpen();
Nirav Atre9850dd92018-07-24 17:03:44 -0700142
Steven Moreland0ff061a2019-03-04 17:56:30 -0800143 if (mOwn) {
144 try {
145 for (FileDescriptor fd : mFds) {
146 Os.close(fd);
147 }
148 } catch (ErrnoException e) {
149 e.rethrowAsIOException();
Nirav Atre9850dd92018-07-24 17:03:44 -0700150 }
Steven Moreland0ff061a2019-03-04 17:56:30 -0800151
152 mOwn = false;
Nirav Atre9850dd92018-07-24 17:03:44 -0700153 }
154
Nirav Atre9850dd92018-07-24 17:03:44 -0700155 mFds = null;
156 mInts = null;
157 }
158
159 /**
160 * Returns the underlying lone file descriptor.
161 *
162 * @return a {@link FileDescriptor} object
163 * @throws IllegalStateException if this object contains either zero or
164 * more than one file descriptor, or a non-empty data stream.
165 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800166 public @NonNull FileDescriptor getFileDescriptor() {
167 checkOpen();
168
Nirav Atre9850dd92018-07-24 17:03:44 -0700169 if (!hasSingleFileDescriptor()) {
170 throw new IllegalStateException(
171 "NativeHandle is not single file descriptor. Contents must"
172 + " be retreived through getFileDescriptors and getInts.");
173 }
174
175 return mFds[0];
176 }
177
178 /**
179 * Convenience method for fetching this object's file descriptors from JNI.
180 * @return a mutable copy of the underlying file descriptors (as an int[])
181 *
182 * @hide
183 */
184 private int[] getFdsAsIntArray() {
Steven Moreland0ff061a2019-03-04 17:56:30 -0800185 checkOpen();
186
Nirav Atre9850dd92018-07-24 17:03:44 -0700187 int numFds = mFds.length;
188 int[] fds = new int[numFds];
189
190 for (int i = 0; i < numFds; i++) {
191 fds[i] = mFds[i].getInt$();
192 }
193
194 return fds;
195 }
196
197 /**
Steven Moreland0ff061a2019-03-04 17:56:30 -0800198 * Fetch file descriptors
Nirav Atre9850dd92018-07-24 17:03:44 -0700199 *
200 * @return the fds.
201 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800202 public @NonNull FileDescriptor[] getFileDescriptors() {
203 checkOpen();
204
Nirav Atre9850dd92018-07-24 17:03:44 -0700205 return mFds;
206 }
207
208 /**
209 * Fetch opaque ints. Note: This object retains ownership of the data.
210 *
211 * @return the opaque data stream.
212 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800213 public @NonNull int[] getInts() {
214 checkOpen();
215
Nirav Atre9850dd92018-07-24 17:03:44 -0700216 return mInts;
217 }
218}