Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit f7e79b9

Browse files
Collin Mullinerenh-google
authored andcommitted
use process groups for processes started by init
Put every service into a process group, kill the process group and all child processes created within the group when killing the service. Removed libutil dependency in libprocessgroup. Bug: 25355957 Change-Id: Ieed60ec41579f638ab9b1e66a7e6330ed578ab05 Signed-off-by: Collin Mulliner <collinrm@squareup.com>
1 parent 605628d commit f7e79b9

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

init/Android.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ LOCAL_SRC_FILES:= \
5252
service.cpp \
5353
util.cpp \
5454

55-
LOCAL_STATIC_LIBRARIES := libbase libselinux
55+
LOCAL_STATIC_LIBRARIES := libbase libselinux liblog libprocessgroup
5656
LOCAL_MODULE := libinit
5757
LOCAL_SANITIZE := integer
5858
LOCAL_CLANG := true
@@ -91,7 +91,6 @@ LOCAL_STATIC_LIBRARIES := \
9191
libcutils \
9292
libbase \
9393
libext4_utils_static \
94-
libutils \
9594
libc \
9695
libselinux \
9796
liblog \
@@ -100,7 +99,8 @@ LOCAL_STATIC_LIBRARIES := \
10099
libc++_static \
101100
libdl \
102101
libsparse_static \
103-
libz
102+
libz \
103+
libprocessgroup
104104

105105
# Create symlinks
106106
LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \

init/service.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <cutils/android_reboot.h>
3131
#include <cutils/sockets.h>
3232

33+
#include <processgroup/processgroup.h>
34+
3335
#include "action.h"
3436
#include "init.h"
3537
#include "init_parser.h"
@@ -97,7 +99,7 @@ bool Service::Reap() {
9799
if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
98100
NOTICE("Service '%s' (pid %d) killing any children in process group\n",
99101
name_.c_str(), pid_);
100-
kill(-pid_, SIGKILL);
102+
killProcessGroup(uid_, pid_, SIGKILL);
101103
}
102104

103105
// Remove any sockets we may have created.
@@ -490,6 +492,7 @@ bool Service::Start() {
490492
time_started_ = gettime();
491493
pid_ = pid;
492494
flags_ |= SVC_RUNNING;
495+
createProcessGroup(uid_, pid_);
493496

494497
if ((flags_ & SVC_EXEC) != 0) {
495498
INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
@@ -532,7 +535,7 @@ void Service::Terminate() {
532535
if (pid_) {
533536
NOTICE("Sending SIGTERM to service '%s' (pid %d)...\n", name_.c_str(),
534537
pid_);
535-
kill(-pid_, SIGTERM);
538+
killProcessGroup(uid_, pid_, SIGTERM);
536539
NotifyStateChange("stopping");
537540
}
538541
}
@@ -583,7 +586,7 @@ void Service::StopOrReset(int how) {
583586

584587
if (pid_) {
585588
NOTICE("Service '%s' is being killed...\n", name_.c_str());
586-
kill(-pid_, SIGKILL);
589+
killProcessGroup(uid_, pid_, SIGKILL);
587590
NotifyStateChange("stopping");
588591
} else {
589592
NotifyStateChange("stopped");

libprocessgroup/Android.mk

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ LOCAL_PATH := $(call my-dir)
33
include $(CLEAR_VARS)
44
LOCAL_SRC_FILES := processgroup.cpp
55
LOCAL_MODULE := libprocessgroup
6-
LOCAL_SHARED_LIBRARIES := liblog libutils
6+
LOCAL_STATIC_LIBRARIES := liblog
7+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
8+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
9+
LOCAL_CFLAGS := -Wall -Werror
10+
include $(BUILD_STATIC_LIBRARY)
11+
12+
include $(CLEAR_VARS)
13+
LOCAL_SRC_FILES := processgroup.cpp
14+
LOCAL_MODULE := libprocessgroup
15+
LOCAL_SHARED_LIBRARIES := liblog
716
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
817
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
918
LOCAL_CFLAGS := -Wall -Werror

libprocessgroup/processgroup.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@
2222
#include <errno.h>
2323
#include <fcntl.h>
2424
#include <inttypes.h>
25-
#include <stdbool.h>
2625
#include <stdio.h>
2726
#include <stdlib.h>
2827
#include <string.h>
2928
#include <sys/stat.h>
3029
#include <sys/types.h>
30+
31+
#include <chrono>
3132
#include <memory>
3233

3334
#include <log/log.h>
3435
#include <private/android_filesystem_config.h>
3536

36-
#include <utils/SystemClock.h>
37-
3837
#include <processgroup/processgroup.h>
3938
#include "processgroup_priv.h"
4039

@@ -250,25 +249,26 @@ static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
250249

251250
int killProcessGroup(uid_t uid, int initialPid, int signal)
252251
{
253-
int processes;
254-
const int sleep_us = 5 * 1000; // 5ms
255-
int64_t startTime = android::uptimeMillis();
256-
int retry = 40;
252+
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
257253

254+
int retry = 40;
255+
int processes;
258256
while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
259257
SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
260258
if (retry > 0) {
261-
usleep(sleep_us);
259+
usleep(5 * 1000); // 5ms
262260
--retry;
263261
} else {
264-
SLOGE("failed to kill %d processes for processgroup %d\n",
265-
processes, initialPid);
262+
SLOGE("failed to kill %d processes for processgroup %d\n", processes, initialPid);
266263
break;
267264
}
268265
}
269266

270-
SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
271-
android::uptimeMillis()-startTime, processes);
267+
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
268+
269+
SLOGV("Killed process group uid %d pid %d in %dms, %d procs remain", uid, initialPid,
270+
static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()),
271+
processes);
272272

273273
if (processes == 0) {
274274
return removeProcessGroup(uid, initialPid);

0 commit comments

Comments
 (0)