Skip to content

Commit c29c2ba

Browse files
init: Add support for native service registration with lmkd
init should be able to register native services with lmkd so that they can be killed when needed. Only processes with oom_score_adjust not equal to the default -1000 will be registered with lmkd because with the score that low the process is unkillable anyway. Inform lmkd when a registered process is killed so that the record can be removed. Change init.rc to start lmkd during init phase so that it is there to register other services. Replace hardcoded oom_score_adj values with appropriate definitions. Bug: 129011369 Test: boot and verify native service registration Change-Id: Ie5ed62203395120d86dc1c8250fae01aa0b3c511 Signed-off-by: Suren Baghdasaryan <[email protected]>
1 parent e353d86 commit c29c2ba

File tree

8 files changed

+207
-8
lines changed

8 files changed

+207
-8
lines changed

init/Android.bp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ cc_defaults {
6565
"libavb",
6666
"libc++fs",
6767
"libcgrouprc_format",
68+
"liblmkd_utils",
6869
"libmodprobe",
6970
"libprotobuf-cpp-lite",
7071
"libpropertyinfoserializer",
@@ -118,6 +119,7 @@ cc_library_static {
118119
"init.cpp",
119120
"interface_utils.cpp",
120121
"keychords.cpp",
122+
"lmkd_service.cpp",
121123
"modalias_handler.cpp",
122124
"mount_handler.cpp",
123125
"mount_namespace.cpp",

init/init.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "first_stage_mount.h"
6060
#include "import_parser.h"
6161
#include "keychords.h"
62+
#include "lmkd_service.h"
6263
#include "mount_handler.h"
6364
#include "mount_namespace.h"
6465
#include "property_service.h"
@@ -684,9 +685,15 @@ int SecondStageMain(int argc, char** argv) {
684685
InitKernelLogging(argv);
685686
LOG(INFO) << "init second stage started!";
686687

688+
// Will handle EPIPE at the time of write by checking the errno
689+
signal(SIGPIPE, SIG_IGN);
690+
687691
// Set init and its forked children's oom_adj.
688-
if (auto result = WriteFile("/proc/1/oom_score_adj", "-1000"); !result) {
689-
LOG(ERROR) << "Unable to write -1000 to /proc/1/oom_score_adj: " << result.error();
692+
if (auto result =
693+
WriteFile("/proc/1/oom_score_adj", StringPrintf("%d", DEFAULT_OOM_SCORE_ADJUST));
694+
!result) {
695+
LOG(ERROR) << "Unable to write " << DEFAULT_OOM_SCORE_ADJUST
696+
<< " to /proc/1/oom_score_adj: " << result.error();
690697
}
691698

692699
// Set up a session keyring that all processes will have access to. It

init/lmkd_service.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (C) 2019 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+
#include "lmkd_service.h"
18+
19+
#include <errno.h>
20+
21+
#include <android-base/logging.h>
22+
#include <liblmkd_utils.h>
23+
24+
#include "service_list.h"
25+
26+
namespace android {
27+
namespace init {
28+
29+
enum LmkdRegistrationResult {
30+
LMKD_REG_SUCCESS,
31+
LMKD_CONN_FAILED,
32+
LMKD_REG_FAILED,
33+
};
34+
35+
static int lmkd_socket = -1;
36+
37+
static LmkdRegistrationResult RegisterProcess(uid_t uid, pid_t pid, int oom_score_adjust) {
38+
// connect to lmkd if not already connected
39+
if (lmkd_socket < 0) {
40+
lmkd_socket = lmkd_connect();
41+
if (lmkd_socket < 0) {
42+
return LMKD_CONN_FAILED;
43+
}
44+
}
45+
46+
// register service with lmkd
47+
struct lmk_procprio params;
48+
params.pid = pid;
49+
params.uid = uid;
50+
params.oomadj = oom_score_adjust;
51+
params.ptype = PROC_TYPE_SERVICE;
52+
if (lmkd_register_proc(lmkd_socket, &params) != 0) {
53+
// data transfer failed, reset the connection
54+
close(lmkd_socket);
55+
lmkd_socket = -1;
56+
return LMKD_REG_FAILED;
57+
}
58+
59+
return LMKD_REG_SUCCESS;
60+
}
61+
62+
static bool UnregisterProcess(pid_t pid) {
63+
if (lmkd_socket < 0) {
64+
// no connection or it was lost, no need to unregister
65+
return false;
66+
}
67+
68+
// unregister service
69+
struct lmk_procremove params;
70+
params.pid = pid;
71+
if (lmkd_unregister_proc(lmkd_socket, &params) != 0) {
72+
// data transfer failed, reset the connection
73+
close(lmkd_socket);
74+
lmkd_socket = -1;
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
static void RegisterServices(pid_t exclude_pid) {
82+
for (const auto& service : ServiceList::GetInstance().services()) {
83+
auto svc = service.get();
84+
if (svc->oom_score_adjust() != DEFAULT_OOM_SCORE_ADJUST) {
85+
// skip if process is excluded or not yet forked (pid==0)
86+
if (svc->pid() == exclude_pid || svc->pid() == 0) {
87+
continue;
88+
}
89+
if (RegisterProcess(svc->uid(), svc->pid(), svc->oom_score_adjust()) !=
90+
LMKD_REG_SUCCESS) {
91+
// a failure here resets the connection, will retry during next registration
92+
break;
93+
}
94+
}
95+
}
96+
}
97+
98+
void LmkdRegister(const std::string& name, uid_t uid, pid_t pid, int oom_score_adjust) {
99+
bool new_connection = lmkd_socket == -1;
100+
LmkdRegistrationResult result;
101+
102+
result = RegisterProcess(uid, pid, oom_score_adjust);
103+
if (result == LMKD_REG_FAILED) {
104+
// retry one time if connection to lmkd was lost
105+
result = RegisterProcess(uid, pid, oom_score_adjust);
106+
new_connection = result == LMKD_REG_SUCCESS;
107+
}
108+
switch (result) {
109+
case LMKD_REG_SUCCESS:
110+
// register existing services once new connection is established
111+
if (new_connection) {
112+
RegisterServices(pid);
113+
}
114+
break;
115+
case LMKD_CONN_FAILED:
116+
PLOG(ERROR) << "lmkd connection failed when " << name << " process got started";
117+
break;
118+
case LMKD_REG_FAILED:
119+
PLOG(ERROR) << "lmkd failed to register " << name << " process";
120+
break;
121+
}
122+
}
123+
124+
void LmkdUnregister(const std::string& name, pid_t pid) {
125+
if (!UnregisterProcess(pid)) {
126+
PLOG(ERROR) << "lmkd failed to unregister " << name << " process";
127+
}
128+
}
129+
130+
} // namespace init
131+
} // namespace android

init/lmkd_service.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2019 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 <sys/types.h>
20+
21+
#include <string>
22+
23+
namespace android {
24+
namespace init {
25+
26+
static const int MIN_OOM_SCORE_ADJUST = -1000;
27+
static const int MAX_OOM_SCORE_ADJUST = 1000;
28+
// service with default score is unkillable
29+
static const int DEFAULT_OOM_SCORE_ADJUST = MIN_OOM_SCORE_ADJUST;
30+
31+
#if defined(__ANDROID__)
32+
33+
void LmkdRegister(const std::string& name, uid_t uid, pid_t pid, int oom_score_adjust);
34+
void LmkdUnregister(const std::string& name, pid_t pid);
35+
36+
#else // defined(__ANDROID__)
37+
38+
static inline void LmkdRegister(const std::string&, uid_t, pid_t, int) {}
39+
static inline void LmkdUnregister(const std::string&, pid_t) {}
40+
41+
#endif // defined(__ANDROID__)
42+
43+
} // namespace init
44+
} // namespace android

init/service.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <processgroup/processgroup.h>
3737
#include <selinux/selinux.h>
3838

39+
#include "lmkd_service.h"
3940
#include "service_list.h"
4041
#include "util.h"
4142

@@ -151,7 +152,7 @@ Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid,
151152
seclabel_(seclabel),
152153
onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
153154
"onrestart", {}),
154-
oom_score_adjust_(-1000),
155+
oom_score_adjust_(DEFAULT_OOM_SCORE_ADJUST),
155156
start_order_(0),
156157
args_(args) {}
157158

@@ -199,6 +200,10 @@ void Service::KillProcessGroup(int signal) {
199200

200201
if (r == 0) process_cgroup_empty_ = true;
201202
}
203+
204+
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
205+
LmkdUnregister(name_, pid_);
206+
}
202207
}
203208

204209
void Service::SetProcessAttributesAndCaps() {
@@ -502,7 +507,7 @@ Result<void> Service::Start() {
502507
return ErrnoError() << "Failed to fork";
503508
}
504509

505-
if (oom_score_adjust_ != -1000) {
510+
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
506511
std::string oom_str = std::to_string(oom_score_adjust_);
507512
std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid);
508513
if (!WriteStringToFile(oom_str, oom_file)) {
@@ -563,6 +568,10 @@ Result<void> Service::Start() {
563568
}
564569
}
565570

571+
if (oom_score_adjust_ != DEFAULT_OOM_SCORE_ADJUST) {
572+
LmkdRegister(name_, proc_attr_.uid, pid_, oom_score_adjust_);
573+
}
574+
566575
NotifyStateChange("running");
567576
reboot_on_failure.Disable();
568577
return {};

init/service_parser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <hidl-util/FQName.h>
3030
#include <system/thread_defs.h>
3131

32+
#include "lmkd_service.h"
3233
#include "rlimit_parser.h"
3334
#include "service_utils.h"
3435
#include "util.h"
@@ -261,8 +262,10 @@ Result<void> ServiceParser::ParseNamespace(std::vector<std::string>&& args) {
261262
}
262263

263264
Result<void> ServiceParser::ParseOomScoreAdjust(std::vector<std::string>&& args) {
264-
if (!ParseInt(args[1], &service_->oom_score_adjust_, -1000, 1000)) {
265-
return Error() << "oom_score_adjust value must be in range -1000 - +1000";
265+
if (!ParseInt(args[1], &service_->oom_score_adjust_, MIN_OOM_SCORE_ADJUST,
266+
MAX_OOM_SCORE_ADJUST)) {
267+
return Error() << "oom_score_adjust value must be in range " << MIN_OOM_SCORE_ADJUST
268+
<< " - +" << MAX_OOM_SCORE_ADJUST;
266269
}
267270
return {};
268271
}

init/service_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <gtest/gtest.h>
2525

26+
#include "lmkd_service.h"
2627
#include "util.h"
2728

2829
namespace android {
@@ -49,7 +50,7 @@ TEST(service, pod_initialized) {
4950
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class());
5051
EXPECT_EQ(0, service_in_old_memory->ioprio_pri());
5152
EXPECT_EQ(0, service_in_old_memory->priority());
52-
EXPECT_EQ(-1000, service_in_old_memory->oom_score_adjust());
53+
EXPECT_EQ(DEFAULT_OOM_SCORE_ADJUST, service_in_old_memory->oom_score_adjust());
5354
EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
5455

5556
for (std::size_t i = 0; i < memory_size; ++i) {
@@ -68,7 +69,7 @@ TEST(service, pod_initialized) {
6869
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class());
6970
EXPECT_EQ(0, service_in_old_memory2->ioprio_pri());
7071
EXPECT_EQ(0, service_in_old_memory2->priority());
71-
EXPECT_EQ(-1000, service_in_old_memory2->oom_score_adjust());
72+
EXPECT_EQ(DEFAULT_OOM_SCORE_ADJUST, service_in_old_memory2->oom_score_adjust());
7273
EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
7374
}
7475

rootdir/init.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ on init
347347

348348
# Start logd before any other services run to ensure we capture all of their logs.
349349
start logd
350+
# Start lmkd before any other services run so that it can register them
351+
start lmkd
350352

351353
# Start essential services.
352354
start servicemanager

0 commit comments

Comments
 (0)