1 /*
2 * Copyright (C) 2016 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 "sysdeps/errno.h"
18
19 #include <errno.h>
20
21 #include <thread>
22 #include <unordered_map>
23 #include <utility>
24
25 #include "adb.h"
26
27 // Use the linux asm-generic values for errno (which are used on all android archs but mips).
28 #define ERRNO_VALUES() \
29 ERRNO_VALUE(EACCES, 13); \
30 ERRNO_VALUE(EEXIST, 17); \
31 ERRNO_VALUE(EFAULT, 14); \
32 ERRNO_VALUE(EFBIG, 27); \
33 ERRNO_VALUE(EINTR, 4); \
34 ERRNO_VALUE(EINVAL, 22); \
35 ERRNO_VALUE(EIO, 5); \
36 ERRNO_VALUE(EISDIR, 21); \
37 ERRNO_VALUE(ELOOP, 40); \
38 ERRNO_VALUE(EMFILE, 24); \
39 ERRNO_VALUE(ENAMETOOLONG, 36); \
40 ERRNO_VALUE(ENFILE, 23); \
41 ERRNO_VALUE(ENOENT, 2); \
42 ERRNO_VALUE(ENOMEM, 12); \
43 ERRNO_VALUE(ENOSPC, 28); \
44 ERRNO_VALUE(ENOTDIR, 20); \
45 ERRNO_VALUE(EOVERFLOW, 75); \
46 ERRNO_VALUE(EPERM, 1); \
47 ERRNO_VALUE(EROFS, 30); \
48 ERRNO_VALUE(ETXTBSY, 26)
49
50 // Make sure these values are actually correct.
51 #if defined(__linux__) && !defined(__mips__)
52 #define ERRNO_VALUE(error_name, wire_value) static_assert((error_name) == (wire_value), "")
53 ERRNO_VALUES();
54 #undef ERRNO_VALUE
55 #endif
56
generate_host_to_wire()57 static std::unordered_map<int, int>* generate_host_to_wire() {
58 auto result = new std::unordered_map<int, int>();
59 #define ERRNO_VALUE(error_name, wire_value) \
60 result->insert(std::make_pair((error_name), (wire_value)))
61 ERRNO_VALUES();
62 #undef ERRNO_VALUE
63 return result;
64 }
65
generate_wire_to_host()66 static std::unordered_map<int, int>* generate_wire_to_host() {
67 auto result = new std::unordered_map<int, int>();
68 #define ERRNO_VALUE(error_name, wire_value) \
69 result->insert(std::make_pair((wire_value), (error_name)))
70 ERRNO_VALUES();
71 #undef ERRNO_VALUE
72 return result;
73 }
74
75 static std::unordered_map<int, int>& host_to_wire = *generate_host_to_wire();
76 static std::unordered_map<int, int>& wire_to_host = *generate_wire_to_host();
77
errno_to_wire(int error)78 int errno_to_wire(int error) {
79 auto it = host_to_wire.find(error);
80 if (it == host_to_wire.end()) {
81 LOG(ERROR) << "failed to convert errno " << error << " (" << strerror(error) << ") to wire";
82
83 // Return EIO;
84 return 5;
85 }
86 return it->second;
87 }
88
errno_from_wire(int error)89 int errno_from_wire(int error) {
90 auto it = host_to_wire.find(error);
91 if (it == host_to_wire.end()) {
92 LOG(ERROR) << "failed to convert errno " << error << " from wire";
93 return EIO;
94 }
95 return it->second;
96 }
97