• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005 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 #pragma once
18 
19 #include <binder/IInterface.h>
20 #include <utils/Vector.h>
21 #include <utils/String16.h>
22 
23 #include <optional>
24 
25 namespace android {
26 
27 // ----------------------------------------------------------------------
28 
29 /**
30  * Service manager for C++ services.
31  *
32  * IInterface is only for legacy ABI compatibility
33  */
34 class IServiceManager : public IInterface
35 {
36 public:
37     // for ABI compatibility
38     virtual const String16& getInterfaceDescriptor() const;
39 
40     IServiceManager();
41     virtual ~IServiceManager();
42 
43     /**
44      * Must match values in IServiceManager.aidl
45      */
46     /* Allows services to dump sections according to priorities. */
47     static const int DUMP_FLAG_PRIORITY_CRITICAL = 1 << 0;
48     static const int DUMP_FLAG_PRIORITY_HIGH = 1 << 1;
49     static const int DUMP_FLAG_PRIORITY_NORMAL = 1 << 2;
50     /**
51      * Services are by default registered with a DEFAULT dump priority. DEFAULT priority has the
52      * same priority as NORMAL priority but the services are not called with dump priority
53      * arguments.
54      */
55     static const int DUMP_FLAG_PRIORITY_DEFAULT = 1 << 3;
56     static const int DUMP_FLAG_PRIORITY_ALL = DUMP_FLAG_PRIORITY_CRITICAL |
57             DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;
58     static const int DUMP_FLAG_PROTO = 1 << 4;
59 
60     /**
61      * Retrieve an existing service, blocking for a few seconds
62      * if it doesn't yet exist.
63      */
64     virtual sp<IBinder>         getService( const String16& name) const = 0;
65 
66     /**
67      * Retrieve an existing service, non-blocking.
68      */
69     virtual sp<IBinder>         checkService( const String16& name) const = 0;
70 
71     /**
72      * Register a service.
73      */
74     // NOLINTNEXTLINE(google-default-arguments)
75     virtual status_t addService(const String16& name, const sp<IBinder>& service,
76                                 bool allowIsolated = false,
77                                 int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) = 0;
78 
79     /**
80      * Return list of all existing services.
81      */
82     // NOLINTNEXTLINE(google-default-arguments)
83     virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) = 0;
84 
85     /**
86      * Efficiently wait for a service.
87      *
88      * Returns nullptr only for permission problem or fatal error.
89      */
90     virtual sp<IBinder> waitForService(const String16& name) = 0;
91 
92     /**
93      * Check if a service is declared (e.g. VINTF manifest).
94      *
95      * If this returns true, waitForService should always be able to return the
96      * service.
97      */
98     virtual bool isDeclared(const String16& name) = 0;
99 
100     /**
101      * Get all instances of a service as declared in the VINTF manifest
102      */
103     virtual Vector<String16> getDeclaredInstances(const String16& interface) = 0;
104 
105     /**
106      * If this instance is updatable via an APEX, returns the APEX with which
107      * this can be updated.
108      */
109     virtual std::optional<String16> updatableViaApex(const String16& name) = 0;
110 };
111 
112 sp<IServiceManager> defaultServiceManager();
113 
114 /**
115  * Directly set the default service manager. Only used for testing.
116  * Note that the caller is responsible for caling this method
117  * *before* any call to defaultServiceManager(); if the latter is
118  * called first, setDefaultServiceManager() will abort.
119  */
120 void setDefaultServiceManager(const sp<IServiceManager>& sm);
121 
122 template<typename INTERFACE>
waitForService(const String16 & name)123 sp<INTERFACE> waitForService(const String16& name) {
124     const sp<IServiceManager> sm = defaultServiceManager();
125     return interface_cast<INTERFACE>(sm->waitForService(name));
126 }
127 
128 template<typename INTERFACE>
waitForDeclaredService(const String16 & name)129 sp<INTERFACE> waitForDeclaredService(const String16& name) {
130     const sp<IServiceManager> sm = defaultServiceManager();
131     if (!sm->isDeclared(name)) return nullptr;
132     return interface_cast<INTERFACE>(sm->waitForService(name));
133 }
134 
135 template <typename INTERFACE>
checkDeclaredService(const String16 & name)136 sp<INTERFACE> checkDeclaredService(const String16& name) {
137     const sp<IServiceManager> sm = defaultServiceManager();
138     if (!sm->isDeclared(name)) return nullptr;
139     return interface_cast<INTERFACE>(sm->checkService(name));
140 }
141 
142 template<typename INTERFACE>
143 sp<INTERFACE> waitForVintfService(
144         const String16& instance = String16("default")) {
145     return waitForDeclaredService<INTERFACE>(
146         INTERFACE::descriptor + String16("/") + instance);
147 }
148 
149 template<typename INTERFACE>
150 sp<INTERFACE> checkVintfService(
151         const String16& instance = String16("default")) {
152     return checkDeclaredService<INTERFACE>(
153         INTERFACE::descriptor + String16("/") + instance);
154 }
155 
156 template<typename INTERFACE>
getService(const String16 & name,sp<INTERFACE> * outService)157 status_t getService(const String16& name, sp<INTERFACE>* outService)
158 {
159     const sp<IServiceManager> sm = defaultServiceManager();
160     if (sm != nullptr) {
161         *outService = interface_cast<INTERFACE>(sm->getService(name));
162         if ((*outService) != nullptr) return NO_ERROR;
163     }
164     return NAME_NOT_FOUND;
165 }
166 
167 bool checkCallingPermission(const String16& permission);
168 bool checkCallingPermission(const String16& permission,
169                             int32_t* outPid, int32_t* outUid);
170 bool checkPermission(const String16& permission, pid_t pid, uid_t uid);
171 
172 } // namespace android
173