aboutsummaryrefslogtreecommitdiff
path: root/libs/kdbinder/binder/IServiceManager.cpp
blob: d701d39fe491eaa6f2083fb2e3a2c5c14f6e042b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "ServiceManager"

#include <binder/IBinder.h>
#include <binder/Binder.h>
#include <binder/BpBinder.h>
#include <binder/IInterface.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/Parcel.h>

#include <kdbus/KDBinder.h>

#include <private/binder/Static.h>

#include <utils/String8.h>
#include <utils/String16.h>
#include <string>

namespace android {

bool checkCallingPermission(const String16& /*permission*/, int32_t* /*outPid*/,
                            int32_t* /*outUid*/) {
  // TODO: unimplemented
  return true;
}

bool checkPermission(const String16& /*permission*/, pid_t /*pid*/,
                     uid_t /*uid*/) {
  // TODO: unimplemented
  return true;
}

class BpServiceManager : public BpInterface<IServiceManager> {
 public:
  explicit BpServiceManager(const sp<IBinder>& impl)
      : BpInterface<IServiceManager>(impl) {
  }

  sp<IBinder> getService(const String16& name) const override {
    unsigned n;
    for (n = 0; n < 5; n++) {
      sp<IBinder> svc = checkService(name);
      if (svc != NULL) return svc;
      ALOGI("Waiting for service %s...\n", String8(name).string());
      sleep(1);
    }
    return NULL;
  }

  sp<IBinder> checkService(const String16& name) const override {
    // Service names are prepended with "service." because KDBUS forces
    // you to have a hierarchy.
    std::string real_name = "service." + std::string(String8(name).string());

    // Probe the bus with all connections with well-known names.
    auto connection = kdbus::Connection::hello("0-services", "checkService");
    auto list = connection->name_list(kdbus::Connection::Names);

    // Find the service in the list of names.
    auto it = std::find_if(list.cbegin(), list.cend(),
                           [&real_name](const kdbus::NameInfo& n) {
      return n.name == real_name;
    });

    if (it == list.cend()) {
      return NULL;
    } else {
      // A remote Binder object contains the connection ID as its
      // handle.
      return new BpBinder(it->owner_id);
    }
  }

  status_t addService(const String16& name, const sp<IBinder>& service,
                      bool /*allowIsolated*/) override {
    // Prepend the name with "service." to make sure we have at least
    // one level of hierarchy.  Otherwise KDBUS will reject it.
    String16 real_name = String16("service.");
    real_name.append(name);

    // Register the service with the calling process.
    ProcessState::self()->registerService(service, real_name);

    return NO_ERROR;
  }

  Vector<String16> listServices() override {
    auto connection = kdbus::Connection::hello("0-services", "listServices");

    // Probe the bus for the list of well-known names.
    auto list = connection->name_list(kdbus::Connection::Names);

    Vector<String16> result;

    for (const auto& n : list) {
      // Service names are all prefixed with "service.".
      auto pos = n.name.find(".");
      std::string name = n.name.substr(pos + 1);

      result.push(String16(name.c_str()));
    }

    return result;
  }
};

sp<IServiceManager> defaultServiceManager() {
  if (gDefaultServiceManager != NULL) return gDefaultServiceManager;

  {
    AutoMutex _l(gDefaultServiceManagerLock);
    // The service manager is nothing more than a dummy Binder object
    // implementing the IServiceManager interface.
    gDefaultServiceManager = interface_cast<IServiceManager>(new BBinder());
  }

  return gDefaultServiceManager;
}

IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");

}  // namespace android