]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/errors.h
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / errors.h
1 /*
2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // Error hierarchy
27 //
28 #ifndef _H_UTILITIES_ERROR
29 #define _H_UTILITIES_ERROR
30
31 #include <AvailabilityMacros.h>
32 #include <exception>
33 #include <errno.h>
34 #include <Security/SecBase.h>
35 #include <set>
36 #undef check
37
38
39 namespace Security {
40
41
42 //
43 // Common base of Security exceptions that represent error conditions.
44 // All can yield Unix or OSStatus error codes as needed, though *how*
45 // is up to the subclass implementation.
46 // CSSM_RETURN conversions are done externally in (???).
47 //
48 class CommonError : public std::exception {
49 protected:
50 CommonError();
51 CommonError(const CommonError &source);
52 public:
53 virtual ~CommonError() _NOEXCEPT;
54
55 virtual OSStatus osStatus() const = 0;
56 virtual int unixError() const = 0;
57
58 char whatBuffer[128];
59 const size_t whatBufferSize = sizeof(whatBuffer);
60
61 static void LogBacktrace();
62 };
63
64
65 //
66 // Genuine Unix-originated errors identified by an errno value.
67 // This includes secondary sources such as pthreads.
68 //
69 class UnixError : public CommonError {
70 protected:
71 UnixError();
72 UnixError(int err, bool suppresslogging);
73 public:
74 const int error;
75 virtual OSStatus osStatus() const;
76 virtual int unixError() const;
77 virtual const char *what () const _NOEXCEPT;
78
79 static void check(int result) { if (result == -1) throwMe(); }
80 static void throwMe(int err = errno) __attribute__((noreturn));
81 static void throwMeNoLogging(int err = errno) __attribute__((noreturn));
82
83 // @@@ This is a hack for the Network protocol state machine
84 static UnixError make(int err = errno) DEPRECATED_ATTRIBUTE;
85 };
86
87
88 //
89 // Genuine MacOS (X) errors identified by an OSStatus value.
90 // Don't even think of working with OSErr values; use OSStatus.
91 //
92 class MacOSError : public CommonError {
93 protected:
94 MacOSError(int err);
95 public:
96 const int error;
97 virtual OSStatus osStatus() const;
98 virtual int unixError() const;
99 virtual const char *what () const _NOEXCEPT;
100
101 static void check(OSStatus status) { if (status != errSecSuccess) throwMe(status); }
102 static void throwMe(int err) __attribute__((noreturn));
103 static void throwMe(int err, char const *message, ...) __attribute__((noreturn));
104
105 static MacOSError make(int err);
106 };
107
108 typedef std::set<OSStatus> MacOSErrorSet;
109
110
111 //
112 // CoreFoundation errors.
113 // Since CF prefers not to tell us *why* something didn't work, this
114 // is not very useful - but it's better than faking it into one of the other
115 // error spaces.
116 //
117 class CFError : public CommonError {
118 protected:
119 CFError();
120 public:
121 virtual OSStatus osStatus() const;
122 virtual int unixError() const;
123 virtual const char *what () const _NOEXCEPT;
124
125 template <class T>
126 static void check(const T &p) { if (!p) throwMe(); }
127
128 static void throwMe() __attribute__((noreturn));
129 };
130
131
132 // Something that gets thrown when ModuleNexus creation fails
133 class ModuleNexusError : public CommonError {
134 protected:
135 ModuleNexusError() {}
136
137 public:
138 virtual OSStatus osStatus() const;
139 virtual int unixError() const;
140 static void throwMe() __attribute__((noreturn));
141 };
142
143 } // end namespace Security
144
145
146 #endif //_H_UTILITIES_ERROR