summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-05-25 00:59:58 -0500
committerGerrit Code Review2018-05-25 00:59:58 -0500
commite82401e592c6c45eca854525c91530ef8422db40 (patch)
treed48a98b4e800a88a3da46e45a31ca6dbca0a8b66 /adb/adb_unique_fd.cpp
parent6c9bb058c5801bd2fd52ceabd0aecd96776f0e71 (diff)
parent6e1246c234bdfc41ff80b6d23599c56284d297ad (diff)
downloadplatform-system-core-e82401e592c6c45eca854525c91530ef8422db40.tar.gz
platform-system-core-e82401e592c6c45eca854525c91530ef8422db40.tar.xz
platform-system-core-e82401e592c6c45eca854525c91530ef8422db40.zip
Merge "adb: really fix the mac build."
Diffstat (limited to 'adb/adb_unique_fd.cpp')
-rw-r--r--adb/adb_unique_fd.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/adb/adb_unique_fd.cpp b/adb/adb_unique_fd.cpp
new file mode 100644
index 000000000..2079be152
--- /dev/null
+++ b/adb/adb_unique_fd.cpp
@@ -0,0 +1,65 @@
1/*
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
17#include "adb_unique_fd.h"
18
19#include <errno.h>
20#include <unistd.h>
21
22#include "sysdeps.h"
23
24#if !defined(_WIN32)
25bool Pipe(unique_fd* read, unique_fd* write, int flags) {
26 int pipefd[2];
27#if !defined(__APPLE__)
28 if (pipe2(pipefd, flags) != 0) {
29 return false;
30 }
31#else
32 // Darwin doesn't have pipe2. Implement it ourselves.
33 if (flags != 0 && (flags & ~(O_CLOEXEC | O_NONBLOCK)) != 0) {
34 errno = EINVAL;
35 return false;
36 }
37
38 if (pipe(pipefd) != 0) {
39 return false;
40 }
41
42 if (flags & O_CLOEXEC) {
43 if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 ||
44 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) {
45 adb_close(pipefd[0]);
46 adb_close(pipefd[1]);
47 return false;
48 }
49 }
50
51 if (flags & O_NONBLOCK) {
52 if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0 ||
53 fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) {
54 adb_close(pipefd[0]);
55 adb_close(pipefd[1]);
56 return false;
57 }
58 }
59#endif
60
61 read->reset(pipefd[0]);
62 write->reset(pipefd[1]);
63 return true;
64}
65#endif