1 /*
2 * Copyright (C) 2015 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 <binder/Status.h>
18
19 namespace android {
20 namespace binder {
21
ok()22 Status Status::ok() {
23 return Status();
24 }
25
fromExceptionCode(int32_t exceptionCode)26 Status Status::fromExceptionCode(int32_t exceptionCode) {
27 return Status(exceptionCode, OK);
28 }
29
fromExceptionCode(int32_t exceptionCode,const String8 & message)30 Status Status::fromExceptionCode(int32_t exceptionCode,
31 const String8& message) {
32 return Status(exceptionCode, OK, message);
33 }
34
fromExceptionCode(int32_t exceptionCode,const char * message)35 Status Status::fromExceptionCode(int32_t exceptionCode,
36 const char* message) {
37 return fromExceptionCode(exceptionCode, String8(message));
38 }
39
fromServiceSpecificError(int32_t serviceSpecificErrorCode)40 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode) {
41 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode);
42 }
43
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const String8 & message)44 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
45 const String8& message) {
46 return Status(EX_SERVICE_SPECIFIC, serviceSpecificErrorCode, message);
47 }
48
fromServiceSpecificError(int32_t serviceSpecificErrorCode,const char * message)49 Status Status::fromServiceSpecificError(int32_t serviceSpecificErrorCode,
50 const char* message) {
51 return fromServiceSpecificError(serviceSpecificErrorCode, String8(message));
52 }
53
fromStatusT(status_t status)54 Status Status::fromStatusT(status_t status) {
55 Status ret;
56 ret.setFromStatusT(status);
57 return ret;
58 }
59
Status(int32_t exceptionCode,int32_t errorCode)60 Status::Status(int32_t exceptionCode, int32_t errorCode)
61 : mException(exceptionCode),
62 mErrorCode(errorCode) {}
63
Status(int32_t exceptionCode,int32_t errorCode,const String8 & message)64 Status::Status(int32_t exceptionCode, int32_t errorCode, const String8& message)
65 : mException(exceptionCode),
66 mErrorCode(errorCode),
67 mMessage(message) {}
68
readFromParcel(const Parcel & parcel)69 status_t Status::readFromParcel(const Parcel& parcel) {
70 status_t status = parcel.readInt32(&mException);
71 if (status != OK) {
72 setFromStatusT(status);
73 return status;
74 }
75
76 // Skip over fat response headers. Not used (or propagated) in native code.
77 if (mException == EX_HAS_REPLY_HEADER) {
78 // Note that the header size includes the 4 byte size field.
79 const size_t header_start = parcel.dataPosition();
80 // Get available size before reading more
81 const size_t header_avail = parcel.dataAvail();
82
83 int32_t header_size;
84 status = parcel.readInt32(&header_size);
85 if (status != OK) {
86 setFromStatusT(status);
87 return status;
88 }
89
90 if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
91 android_errorWriteLog(0x534e4554, "132650049");
92 setFromStatusT(UNKNOWN_ERROR);
93 return UNKNOWN_ERROR;
94 }
95
96 parcel.setDataPosition(header_start + header_size);
97 // And fat response headers are currently only used when there are no
98 // exceptions, so act like there was no error.
99 mException = EX_NONE;
100 }
101
102 if (mException == EX_NONE) {
103 return status;
104 }
105
106 // The remote threw an exception. Get the message back.
107 String16 message;
108 status = parcel.readString16(&message);
109 if (status != OK) {
110 setFromStatusT(status);
111 return status;
112 }
113 mMessage = String8(message);
114
115 // Skip over the remote stack trace data
116 int32_t remote_stack_trace_header_size;
117 status = parcel.readInt32(&remote_stack_trace_header_size);
118 if (status != OK) {
119 setFromStatusT(status);
120 return status;
121 }
122 if (remote_stack_trace_header_size < 0 ||
123 static_cast<size_t>(remote_stack_trace_header_size) > parcel.dataAvail()) {
124
125 android_errorWriteLog(0x534e4554, "132650049");
126 setFromStatusT(UNKNOWN_ERROR);
127 return UNKNOWN_ERROR;
128 }
129 parcel.setDataPosition(parcel.dataPosition() + remote_stack_trace_header_size);
130
131 if (mException == EX_SERVICE_SPECIFIC) {
132 status = parcel.readInt32(&mErrorCode);
133 } else if (mException == EX_PARCELABLE) {
134 // Skip over the blob of Parcelable data
135 const size_t header_start = parcel.dataPosition();
136 // Get available size before reading more
137 const size_t header_avail = parcel.dataAvail();
138
139 int32_t header_size;
140 status = parcel.readInt32(&header_size);
141 if (status != OK) {
142 setFromStatusT(status);
143 return status;
144 }
145
146 if (header_size < 0 || static_cast<size_t>(header_size) > header_avail) {
147 android_errorWriteLog(0x534e4554, "132650049");
148 setFromStatusT(UNKNOWN_ERROR);
149 return UNKNOWN_ERROR;
150 }
151
152 parcel.setDataPosition(header_start + header_size);
153 }
154 if (status != OK) {
155 setFromStatusT(status);
156 return status;
157 }
158
159 return status;
160 }
161
writeToParcel(Parcel * parcel) const162 status_t Status::writeToParcel(Parcel* parcel) const {
163 // Something really bad has happened, and we're not going to even
164 // try returning rich error data.
165 if (mException == EX_TRANSACTION_FAILED) {
166 return mErrorCode;
167 }
168
169 status_t status = parcel->writeInt32(mException);
170 if (status != OK) { return status; }
171 if (mException == EX_NONE) {
172 // We have no more information to write.
173 return status;
174 }
175 status = parcel->writeString16(String16(mMessage));
176 status = parcel->writeInt32(0); // Empty remote stack trace header
177 if (mException == EX_SERVICE_SPECIFIC) {
178 status = parcel->writeInt32(mErrorCode);
179 } else if (mException == EX_PARCELABLE) {
180 // Sending Parcelable blobs currently not supported
181 status = parcel->writeInt32(0);
182 }
183 return status;
184 }
185
setException(int32_t ex,const String8 & message)186 void Status::setException(int32_t ex, const String8& message) {
187 mException = ex;
188 mErrorCode = NO_ERROR; // an exception, not a transaction failure.
189 mMessage.setTo(message);
190 }
191
setServiceSpecificError(int32_t errorCode,const String8 & message)192 void Status::setServiceSpecificError(int32_t errorCode, const String8& message) {
193 setException(EX_SERVICE_SPECIFIC, message);
194 mErrorCode = errorCode;
195 }
196
setFromStatusT(status_t status)197 void Status::setFromStatusT(status_t status) {
198 mException = (status == NO_ERROR) ? EX_NONE : EX_TRANSACTION_FAILED;
199 mErrorCode = status;
200 mMessage.clear();
201 }
202
toString8() const203 String8 Status::toString8() const {
204 String8 ret;
205 if (mException == EX_NONE) {
206 ret.append("No error");
207 } else {
208 ret.appendFormat("Status(%d): '", mException);
209 if (mException == EX_SERVICE_SPECIFIC ||
210 mException == EX_TRANSACTION_FAILED) {
211 ret.appendFormat("%d: ", mErrorCode);
212 }
213 ret.append(String8(mMessage));
214 ret.append("'");
215 }
216 return ret;
217 }
218
operator <<(std::stringstream & stream,const Status & s)219 std::stringstream& operator<< (std::stringstream& stream, const Status& s) {
220 stream << s.toString8().string();
221 return stream;
222 }
223
224 } // namespace binder
225 } // namespace android
226