NetdNativeService.cpp revision e4d626ea35b7a402388b524e2feafc81e6387697
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
30using android::base::StringPrintf;
31
32namespace android {
33namespace net {
34
35namespace {
36
37const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
38
39binder::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
65binder::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
74