Skip to content

Commit 5759db0

Browse files
author
Steven Moreland
committed
servicemanager: include caller info in logs
In cases like b/329039146, it can be difficult to understand where spam is coming from, because we only have the logs. This adds caller information to servicemanager logs, so that we can easily identify what is causing an issue, even if we don't have logs. Btw - passing around a calling context like this, instead of referencing thread locals, is how I think we should do all ACL with binder, but that's another story. Example log output: 03-27 00:32:02.242 0 0 I servicemanager: Caller(pid=2145,uid=1001,sid=u:r:radio:s0) Found android.hardware.radio.data.IRadioData/slot1 in device VINTF manifest. Bug: N/A Test: N/A Change-Id: Iea45e4d22f28f28592eb73a129117204c6c8e814
1 parent 315a0f1 commit 5759db0

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

@@ -421,25 +426,26 @@ Status ServiceManager::addService(const std::string& name, const sp<IBinder>& bi
421426
}
422427

423428
if (!isValidServiceName(name)) {
424-
ALOGE("Invalid service name: %s", name.c_str());
429+
ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
425430
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
426431
}
427432

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

435440
if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) {
436-
ALOGW("Dump flag priority is not set when adding %s", name.c_str());
441+
ALOGW("%s Dump flag priority is not set when adding %s", ctx.toDebugString().c_str(),
442+
name.c_str());
437443
}
438444

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

@@ -541,7 +547,7 @@ Status ServiceManager::registerForNotifications(
541547
}
542548

543549
if (!isValidServiceName(name)) {
544-
ALOGE("Invalid service name: %s", name.c_str());
550+
ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
545551
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
546552
}
547553

@@ -552,7 +558,7 @@ Status ServiceManager::registerForNotifications(
552558
if (OK !=
553559
IInterface::asBinder(callback)->linkToDeath(
554560
sp<ServiceManager>::fromExisting(this))) {
555-
ALOGE("Could not linkToDeath when adding %s", name.c_str());
561+
ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
556562
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death.");
557563
}
558564

@@ -584,7 +590,8 @@ Status ServiceManager::unregisterForNotifications(
584590
}
585591

586592
if (!found) {
587-
ALOGE("Trying to unregister callback, but none exists %s", name.c_str());
593+
ALOGE("%s Trying to unregister callback, but none exists %s", ctx.toDebugString().c_str(),
594+
name.c_str());
588595
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister.");
589596
}
590597

@@ -601,7 +608,7 @@ Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
601608
*outReturn = false;
602609

603610
#ifndef VENDORSERVICEMANAGER
604-
*outReturn = isVintfDeclared(name);
611+
*outReturn = isVintfDeclared(ctx, name);
605612
#endif
606613
return Status::ok();
607614
}
@@ -733,18 +740,16 @@ void ServiceManager::binderDied(const wp<IBinder>& who) {
733740
}
734741

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

741747
std::thread([=] {
742748
if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
743-
ALOGI("Tried to start aidl service %s as a lazy service, but was unable to. Usually "
744-
"this happens when a "
745-
"service is not installed, but if the service is intended to be used as a "
746-
"lazy service, then it may be configured incorrectly.",
747-
name.c_str());
749+
ALOGI("%s Tried to start aidl service %s as a lazy service, but was unable to. Usually "
750+
"this happens when a service is not installed, but if the service is intended to "
751+
"be used as a lazy service, then it may be configured incorrectly.",
752+
ctx.toDebugString().c_str(), name.c_str());
748753
}
749754
}).detach();
750755
}
@@ -762,26 +767,28 @@ Status ServiceManager::registerClientCallback(const std::string& name, const sp<
762767

763768
auto serviceIt = mNameToService.find(name);
764769
if (serviceIt == mNameToService.end()) {
765-
ALOGE("Could not add callback for nonexistent service: %s", name.c_str());
770+
ALOGE("%s Could not add callback for nonexistent service: %s", ctx.toDebugString().c_str(),
771+
name.c_str());
766772
return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist.");
767773
}
768774

769775
if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
770-
ALOGW("Only a server can register for client callbacks (for %s)", name.c_str());
776+
ALOGW("%s Only a server can register for client callbacks (for %s)",
777+
ctx.toDebugString().c_str(), name.c_str());
771778
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
772779
"Only service can register client callback for itself.");
773780
}
774781

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

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

@@ -919,30 +926,32 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
919926

920927
auto serviceIt = mNameToService.find(name);
921928
if (serviceIt == mNameToService.end()) {
922-
ALOGW("Tried to unregister %s, but that service wasn't registered to begin with.",
923-
name.c_str());
929+
ALOGW("%s Tried to unregister %s, but that service wasn't registered to begin with.",
930+
ctx.toDebugString().c_str(), name.c_str());
924931
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered.");
925932
}
926933

927934
if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
928-
ALOGW("Only a server can unregister itself (for %s)", name.c_str());
935+
ALOGW("%s Only a server can unregister itself (for %s)", ctx.toDebugString().c_str(),
936+
name.c_str());
929937
return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
930938
"Service can only unregister itself.");
931939
}
932940

933941
sp<IBinder> storedBinder = serviceIt->second.binder;
934942

935943
if (binder != storedBinder) {
936-
ALOGW("Tried to unregister %s, but a different service is registered under this name.",
937-
name.c_str());
944+
ALOGW("%s Tried to unregister %s, but a different service is registered under this name.",
945+
ctx.toDebugString().c_str(), name.c_str());
938946
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
939947
"Different service registered under this name.");
940948
}
941949

942950
// important because we don't have timer-based guarantees, we don't want to clear
943951
// this
944952
if (serviceIt->second.guaranteeClient) {
945-
ALOGI("Tried to unregister %s, but there is about to be a client.", name.c_str());
953+
ALOGI("%s Tried to unregister %s, but there is about to be a client.",
954+
ctx.toDebugString().c_str(), name.c_str());
946955
return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
947956
"Can't unregister, pending client.");
948957
}
@@ -952,7 +961,8 @@ Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IB
952961
constexpr size_t kKnownClients = 2;
953962

954963
if (handleServiceClientCallback(kKnownClients, name, false)) {
955-
ALOGI("Tried to unregister %s, but there are clients.", name.c_str());
964+
ALOGI("%s Tried to unregister %s, but there are clients.", ctx.toDebugString().c_str(),
965+
name.c_str());
956966

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

966-
ALOGI("Unregistering %s", name.c_str());
976+
ALOGI("%s Unregistering %s", ctx.toDebugString().c_str(), name.c_str());
967977
mNameToService.erase(name);
968978

969979
return Status::ok();

0 commit comments

Comments
 (0)