blob: f13bf5fccd38ed62016593162d074eace0106742 [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 Morelandcbba4c72019-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 Morelandcbba4c72019-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() {
102 return mFds.length == 1 && mInts.length == 0;
103 }
104
105 /**
106 * Explicitly duplicate NativeHandle (this dups all file descritptors).
Steven Moreland12a2c0a2018-08-13 13:53:39 -0700107 *
108 * If this method is called, this must also be explicitly closed with
109 * {@link #close()}.
Nirav Atre9850dd92018-07-24 17:03:44 -0700110 */
111 public NativeHandle dup() throws java.io.IOException {
112 FileDescriptor[] fds = new FileDescriptor[mFds.length];
113 try {
114 for (int i = 0; i < mFds.length; i++) {
Nick Kralevich1de00962018-12-17 14:41:59 -0800115 FileDescriptor newFd = new FileDescriptor();
116 int fdint = Os.fcntlInt(mFds[i], F_DUPFD_CLOEXEC, 0);
117 newFd.setInt$(fdint);
118 fds[i] = newFd;
Nirav Atre9850dd92018-07-24 17:03:44 -0700119 }
120 } catch (ErrnoException e) {
121 e.rethrowAsIOException();
122 }
123 return new NativeHandle(fds, mInts, true /*own*/);
124 }
125
126 /**
127 * Closes the file descriptors if they are owned by this object.
128 *
129 * This also invalidates the object.
130 */
131 @Override
132 public void close() throws java.io.IOException {
133 if (!mOwn) {
134 return;
135 }
136
137 try {
138 for (FileDescriptor fd : mFds) {
139 Os.close(fd);
140 }
141 } catch (ErrnoException e) {
142 e.rethrowAsIOException();
143 }
144
145 mOwn = false;
146 mFds = null;
147 mInts = null;
148 }
149
150 /**
151 * Returns the underlying lone file descriptor.
152 *
153 * @return a {@link FileDescriptor} object
154 * @throws IllegalStateException if this object contains either zero or
155 * more than one file descriptor, or a non-empty data stream.
156 */
157 public FileDescriptor getFileDescriptor() {
158 if (!hasSingleFileDescriptor()) {
159 throw new IllegalStateException(
160 "NativeHandle is not single file descriptor. Contents must"
161 + " be retreived through getFileDescriptors and getInts.");
162 }
163
164 return mFds[0];
165 }
166
167 /**
168 * Convenience method for fetching this object's file descriptors from JNI.
169 * @return a mutable copy of the underlying file descriptors (as an int[])
170 *
171 * @hide
172 */
173 private int[] getFdsAsIntArray() {
174 int numFds = mFds.length;
175 int[] fds = new int[numFds];
176
177 for (int i = 0; i < numFds; i++) {
178 fds[i] = mFds[i].getInt$();
179 }
180
181 return fds;
182 }
183
184 /**
185 * Fetch file descriptors.
186 *
187 * @return the fds.
188 */
189 public FileDescriptor[] getFileDescriptors() {
190 return mFds;
191 }
192
193 /**
194 * Fetch opaque ints. Note: This object retains ownership of the data.
195 *
196 * @return the opaque data stream.
197 */
198 public int[] getInts() {
199 return mInts;
200 }
201}