/Users/deen/code/yugabyte-db/src/yb/util/errno.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_UTIL_ERRNO_H |
33 | | #define YB_UTIL_ERRNO_H |
34 | | |
35 | | #include <string> |
36 | | |
37 | | #include "yb/util/status_fwd.h" |
38 | | #include "yb/util/status_ec.h" |
39 | | |
40 | | DECLARE_bool(suicide_on_eio); |
41 | | |
42 | | namespace yb { |
43 | | |
44 | | void ErrnoToCString(int err, char *buf, size_t buf_len); |
45 | | |
46 | | // Return a string representing an errno. |
47 | 1.42M | inline std::string ErrnoToString(int err) { |
48 | 1.42M | char buf[512]; |
49 | 1.42M | ErrnoToCString(err, buf, sizeof(buf)); |
50 | 1.42M | return std::string(buf); |
51 | 1.42M | } |
52 | | |
53 | | struct ErrnoTag : IntegralErrorTag<int32_t> { |
54 | | // This category id is part of the wire protocol and should not be changed once released. |
55 | | static constexpr uint8_t kCategory = 1; |
56 | | |
57 | 1.42M | static std::string ToMessage(Value value) { |
58 | 1.42M | return ErrnoToString(value); |
59 | 1.42M | } |
60 | | }; |
61 | | |
62 | | typedef StatusErrorCodeImpl<ErrnoTag> Errno; |
63 | | |
64 | | namespace internal { |
65 | | |
66 | | Status StatusFromErrno(const std::string& context, int err_number, const char* file, int line); |
67 | | |
68 | | Status StatusFromErrnoSpecialEioHandling( |
69 | | const std::string& context, int err_number, const char* file, int line); |
70 | | |
71 | | // A lot of C library functions return zero on success and non-zero on failure, with the actual |
72 | | // error code stored in errno. This helper constructs a Status based on errno but only if the return |
73 | | // value (the rv parameter) is non-zero. |
74 | | Status StatusFromErrnoIfNonZero(const std::string& context, int rv, const char* file, int line); |
75 | | |
76 | | } // namespace internal |
77 | | |
78 | | #define STATUS_FROM_ERRNO(context, err_number) \ |
79 | 0 | ::yb::internal::StatusFromErrno(context, err_number, __FILE__, __LINE__) |
80 | | |
81 | | #define STATUS_FROM_ERRNO_SPECIAL_EIO_HANDLING(context, err_number) \ |
82 | 1.07M | ::yb::internal::StatusFromErrnoSpecialEioHandling(context, err_number, __FILE__, __LINE__) |
83 | | |
84 | | // A convenient way to invoke a function that returns an errno-like value, and automatically create |
85 | | // an error status that includes the function name in case it fails. Note that we are not looking at |
86 | | // the errno variable here, but at the function's return value. |
87 | | #define STATUS_FROM_ERRNO_RV_FN_CALL(fn_name, ...) \ |
88 | 0 | STATUS_FROM_ERRNO(BOOST_PP_STRINGIZE(fn_name), fn_name(__VA_ARGS__)) |
89 | | |
90 | | // Evaluates the given expression's value (typically a call to a C standard library function) and if |
91 | | // its result is nonzero, returns a status based on errno. |
92 | | // |
93 | | // Important: the expression's value is not treated as an error code, it is only compared with zero. |
94 | | #define STATUS_FROM_ERRNO_IF_NONZERO_RV(context, expr) \ |
95 | | ::yb::internal::StatusFromErrnoIfNonZero(context, (expr), __FILE__, __LINE__) |
96 | | |
97 | | #define RETURN_ON_ERRNO_RV_FN_CALL(...) \ |
98 | 0 | RETURN_NOT_OK(STATUS_FROM_ERRNO_RV_FN_CALL(__VA_ARGS__)) |
99 | | |
100 | | } // namespace yb |
101 | | |
102 | | #endif // YB_UTIL_ERRNO_H |