Lorenzo Colitti | e4d626e | 2016-02-02 17:19:04 +0900 | [diff] [blame] | 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 | #define LOG_TAG "Netd" |
| 18 | |
| 19 | #include <android-base/stringprintf.h> |
| 20 | #include <cutils/log.h> |
| 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include "android/net/BnNetd.h" |
| 26 | |
| 27 | #include "NetdConstants.h" |
| 28 | #include "NetdNativeService.h" |
| 29 | |
| 30 | using android::base::StringPrintf; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL"; |
| 38 | |
| 39 | binder::Status checkPermission(const char *permission) { |
| 40 | pid_t pid; |
| 41 | uid_t uid; |
| 42 | |
| 43 | if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) { |
| 44 | return binder::Status::ok(); |
| 45 | } else { |
| 46 | auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission); |
| 47 | return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str())); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #define ENFORCE_PERMISSION(permission) { \ |
| 52 | binder::Status status = checkPermission((permission)); \ |
| 53 | if (!status.isOk()) { \ |
| 54 | return status; \ |
| 55 | } \ |
| 56 | } |
| 57 | |
| 58 | #define NETD_LOCKING_RPC(permission) \ |
| 59 | ENFORCE_PERMISSION(permission); \ |
| 60 | android::RWLock::AutoWLock lock(gBigNetdLock); |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | |
| 65 | binder::Status NetdNativeService::isAlive(bool *alive) { |
| 66 | NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL); |
| 67 | |
| 68 | *alive = true; |
| 69 | return binder::Status::ok(); |
| 70 | } |
| 71 | |
| 72 | } // namespace net |
| 73 | } // namespace android |