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