Skip to content

Commit dc829d6

Browse files
Steven MorelandGerrit Code Review
authored andcommitted
Merge "servicemanager: include caller info in logs" into main
2 parents 1be3153 + 5759db0 commit dc829d6

3 files changed

Lines changed: 59 additions & 39 deletions

File tree

cmds/servicemanager/Access.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <selinux/android.h>
2323
#include <selinux/avc.h>
2424

25+
#include <sstream>
26+
2527
namespace android {
2628

2729
#ifdef VENDORSERVICEMANAGER
@@ -80,6 +82,12 @@ static int auditCallback(void *data, security_class_t /*cls*/, char *buf, size_t
8082
}
8183
#endif
8284

85+
std::string Access::CallingContext::toDebugString() const {
86+
std::stringstream ss;
87+
ss << "Caller(pid=" << debugPid << ",uid=" << uid << ",sid=" << sid << ")";
88+
return ss.str();
89+
}
90+
8391
Access::Access() {
8492
#ifdef __ANDROID__
8593
union selinux_callback cb;

cmds/servicemanager/Access.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Access {
3636
pid_t debugPid;
3737
uid_t uid;
3838
std::string sid;
39+
40+
std::string toDebugString() const;
3941
};
4042

4143
virtual CallingContext getCallingContext();

cmds/servicemanager/ServiceManager.cpp

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,20 @@ static std::string getAidlInstanceName(const vintf::ManifestInstance& instance)
115115
return instance.package() + "." + instance.interface() + "/" + instance.instance();
116116
}
117117

118-
static bool isVintfDeclared(const std::string& name) {
118+
static bool isVintfDeclared(const Access::CallingContext& ctx, const std::string& name) {
119119
NativeName nname;
120120
if (NativeName::fill(name, &nname)) {
121121
bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
122122
if (mwd.manifest->hasNativeInstance(nname.package, nname.instance)) {
123-
ALOGI("Found %s in %s VINTF manifest.", name.c_str(), mwd.description);
123+
ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(),
124+
name.c_str(), mwd.description);
124125
return true; // break
125126
}
126127
return false; // continue
127128
});
128129
if (!found) {
129-
ALOGI("Could not find %s in the VINTF manifest.", name.c_str());
130+
ALOGI("%s Could not find %s in the VINTF manifest.", ctx.toDebugString().c_str(),
131+
name.c_str());
130132
}
131133
return found;
132134
}
@@ -136,7 +138,8 @@ static bool isVintfDeclared(const std::string& name) {
136138

137139
bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
138140
if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) {
139-
ALOGI("Found %s in %s VINTF manifest.", name.c_str(), mwd.description);
141+
ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(), name.c_str(),
142+
mwd.description);
140143
return true; // break
141144
}
142145
return false; // continue
@@ -161,8 +164,9 @@ static bool isVintfDeclared(const std::string& name) {
161164
}
162165
// Although it is tested, explicitly rebuilding qualified name, in case it
163166
// becomes something unexpected.
164-
ALOGI("Could not find %s.%s/%s in the VINTF manifest. %s.", aname.package.c_str(),
165-
aname.iface.c_str(), aname.instance.c_str(), available.c_str());
167+
ALOGI("%s Could not find %s.%s/%s in the VINTF manifest. %s.", ctx.toDebugString().c_str(),
168+
aname.package.c_str(), aname.iface.c_str(), aname.instance.c_str(),
169+
available.c_str());
166170
}
167171

168172
return found;
@@ -290,12 +294,13 @@ static std::vector<std::string> getVintfInstances(const std::string& interface)
290294
return ret;
291295
}
292296

293-
static bool meetsDeclarationRequirements(const sp<IBinder>& binder, const std::string& name) {
297+
static bool meetsDeclarationRequirements(const Access::CallingContext& ctx,
298+
const sp<IBinder>& binder, const std::string& name) {
294299
if (!Stability::requiresVintfDeclaration(binder)) {
295300
return true;
296301
}
297302

298-
return isVintfDeclared(name);
303+
return isVintfDeclared(ctx, name);
299304
}
300305
#endif // !VENDORSERVICEMANAGER
301306

@@ -307,7 +312,7 @@ ServiceManager::Service::~Service() {
307312
// clear this bit so that we can abort in other cases, where it would
308313
// mean inconsistent logic in servicemanager (unexpected and tested, but
309314
// the original lazy service impl here had that bug).
310-
LOG(WARNING) << "a service was removed when there are clients";
315+
ALOGW("A service was removed when there are clients");
311316
}
312317
}
313318

@@ -423,25 +428,26 @@ Status ServiceManager::addService(const std::string& name, const sp<IBinder>& bi
423428
}
424429

425430
if (!isValidServiceName(name)) {
426-
ALOGE("Invalid service name: %s", name.c_str());
431+
ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
427432
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
428433
}
429434

430435
#ifndef VENDORSERVICEMANAGER
431-
if (!meetsDeclarationRequirements(binder, name)) {
436+
if (!meetsDeclarationRequirements(ctx, binder, name)) {
432437
// already logged
433438
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error.");
434439
}
435440
#endif // !VENDORSERVICEMANAGER
436441

437442
if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) {
438-
ALOGW("Dump flag priority is not set when adding %s", name.c_str());
443+
ALOGW("%s Dump flag priority is not set when adding %s", ctx.toDebugString().c_str(),
444+
name.c_str());
439445
}
440446

441447
// implicitly unlinked when the binder is removed
442448
if (binder->remoteBinder() != nullptr &&
443449
binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
444-
ALOGE("Could not linkToDeath when adding %s", name.c_str());
450+
ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
445451
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
446452
}
447453

@@ -543,7 +549,7 @@ Status ServiceManager::registerForNotifications(
543549
}
544550

545551
if (!isValidServiceName(name)) {
546-
ALOGE("Invalid service name: %s", name.c_str());
552+
ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
547553
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
548554
}
549555

@@ -554,7 +560,7 @@ Status ServiceManager::registerForNotifications(
554560
if (OK !=
555561
IInterface::asBinder(callback)->linkToDeath(
556562
sp<ServiceManager>::fromExisting(this))) {
557-
ALOGE("Could not linkToDeath when adding %s", name.c_str());
563+
ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
558564
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death.");
559565
}
560566

@@ -586,7 +592,8 @@ Status ServiceManager::unregisterForNotifications(
586592
}
587593

588594
if (!found) {
589-
ALOGE("Trying to unregister callback, but none exists %s", name.c_str());
595+
ALOGE("%s Trying to unregister callback, but none exists %s", ctx.toDebugString().c_str(),
596+
name.c_str());
590597
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister.");
591598
}
592599

@@ -603,7 +610,7 @@ Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
603610
*outReturn = false;
604611

605612
#ifndef VENDORSERVICEMANAGER
606-
*outReturn = isVintfDeclared(name);
613+
*outReturn = isVintfDeclared(ctx, name);
607614
#endif
608615
return Status::ok();
609616
}
@@ -735,18 +742,16 @@ void ServiceManager::binderDied(const wp<IBinder>& who) {
735742
}
736743

737744
void ServiceManager::tryStartService(const Access::CallingContext& ctx, const std::string& name) {
738-
ALOGI("Since '%s' could not be found (requested by debug pid %d), trying to start it as a lazy "
739-
"AIDL service. (if it's not configured to be a lazy service, it may be stuck starting or "
740-
"still starting).",
741-
name.c_str(), ctx.debugPid);
745+
ALOGI("%s Since '%s' could not be found trying to start it as a lazy AIDL service. (if it's "
746+
"not configured to be a lazy service, it may be stuck starting or still starting).",
747+
ctx.toDebugString().c_str(), name.c_str());
742748

743749
std::thread([=] {
744750
if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
745-
ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually "
746-
"this happens when a "
747-
"service is not installed, but if the service is intended to be used as a "
748-
"lazy service, then it may be configured incorrectly.",
749-
name.c_str());
751+
ALOGI("%s Tried to start aidl service %s as a lazy service, but was unable to. Usually "
752+
"this happens when a service is not installed, but if the service is intended to "
753+
"be used as a lazy service, then it may be configured incorrectly.",
754+
ctx.toDebugString().c_str(), name.c_str());
750755
}
751756
}).detach();
752757
}
@@ -764,26 +769,28 @@ Status ServiceManager::registerClientCallback(const std::string& name, const sp<
764769

765770
auto serviceIt = mNameToService.find(name);
766771
if (serviceIt == mNameToService.end()) {
767-
ALOGE("Could not add callback for nonexistent service: %s", name.c_str());
772+
ALOGE("%s Could not add callback for nonexistent service: %s", ctx.toDebugString().c_str(),
773+
name.c_str());
768774
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist.");
769775
}
770776

771777
if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
772-
ALOGW("Only a server can register for client callbacks (for %s)", name.c_str());
778+
ALOGW("%s Only a server can register for client callbacks (for %s)",
779+
ctx.toDebugString().c_str(), name.c_str());
773780
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
774781
"Only service can register client callback for itself.");
775782
}
776783

777784
if (serviceIt->second.binder != service) {
778-
ALOGW("Tried to register client callback for %s but a different service is registered "
785+
ALOGW("%s Tried to register client callback for %s but a different service is registered "
779786
"under this name.",
780-
name.c_str());
787+
ctx.toDebugString().c_str(), name.c_str());
781788
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch.");
782789
}
783790

784791
if (OK !=
785792
IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
786-
ALOGE("Could not linkToDeath when adding client callback for %s", name.c_str());
793+
ALOGE("%s Could not linkToDeath when adding client callback for %s", name.c_str());
787794
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
788795
}
789796

@@ -921,30 +928,32 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
921928

922929
auto serviceIt = mNameToService.find(name);
923930
if (serviceIt == mNameToService.end()) {
924-
ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.",
925-
name.c_str());
931+
ALOGW("%s Tried to unregister %s, but that service wasn't registered to begin with.",
932+
ctx.toDebugString().c_str(), name.c_str());
926933
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered.");
927934
}
928935

929936
if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
930-
ALOGW("Only a server can unregister itself (for %s)", name.c_str());
937+
ALOGW("%s Only a server can unregister itself (for %s)", ctx.toDebugString().c_str(),
938+
name.c_str());
931939
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
932940
"Service can only unregister itself.");
933941
}
934942

935943
sp<IBinder> storedBinder = serviceIt->second.binder;
936944

937945
if (binder != storedBinder) {
938-
ALOGW("Tried to unregister %s, but a different service is registered under this name.",
939-
name.c_str());
946+
ALOGW("%s Tried to unregister %s, but a different service is registered under this name.",
947+
ctx.toDebugString().c_str(), name.c_str());
940948
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
941949
"Different service registered under this name.");
942950
}
943951

944952
// important because we don't have timer-based guarantees, we don't want to clear
945953
// this
946954
if (serviceIt->second.guaranteeClient) {
947-
ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str());
955+
ALOGI("%s Tried to unregister %s, but there is about to be a client.",
956+
ctx.toDebugString().c_str(), name.c_str());
948957
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
949958
"Can't unregister, pending client.");
950959
}
@@ -954,7 +963,8 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
954963
constexpr size_t kKnownClients = 2;
955964

956965
if (handleServiceClientCallback(kKnownClients, name, false)) {
957-
ALOGI("Tried to unregister %s, but there are clients.", name.c_str());
966+
ALOGI("%s Tried to unregister %s, but there are clients.", ctx.toDebugString().c_str(),
967+
name.c_str());
958968

959969
// Since we had a failed registration attempt, and the HIDL implementation of
960970
// delaying service shutdown for multiple periods wasn't ported here... this may
@@ -965,7 +975,7 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
965975
"Can't unregister, known client.");
966976
}
967977

968-
ALOGI("Unregistering %s", name.c_str());
978+
ALOGI("%s Unregistering %s", ctx.toDebugString().c_str(), name.c_str());
969979
mNameToService.erase(name);
970980

971981
return Status::ok();

0 commit comments

Comments
 (0)