Merge "use process groups for processes started by init"
This commit is contained in:
commit
ebe636e5ce
4 changed files with 31 additions and 19 deletions
|
|
@ -52,7 +52,7 @@ LOCAL_SRC_FILES:= \
|
||||||
service.cpp \
|
service.cpp \
|
||||||
util.cpp \
|
util.cpp \
|
||||||
|
|
||||||
LOCAL_STATIC_LIBRARIES := libbase libselinux
|
LOCAL_STATIC_LIBRARIES := libbase libselinux liblog libprocessgroup
|
||||||
LOCAL_MODULE := libinit
|
LOCAL_MODULE := libinit
|
||||||
LOCAL_SANITIZE := integer
|
LOCAL_SANITIZE := integer
|
||||||
LOCAL_CLANG := true
|
LOCAL_CLANG := true
|
||||||
|
|
@ -91,7 +91,6 @@ LOCAL_STATIC_LIBRARIES := \
|
||||||
libcutils \
|
libcutils \
|
||||||
libbase \
|
libbase \
|
||||||
libext4_utils_static \
|
libext4_utils_static \
|
||||||
libutils \
|
|
||||||
libc \
|
libc \
|
||||||
libselinux \
|
libselinux \
|
||||||
liblog \
|
liblog \
|
||||||
|
|
@ -100,7 +99,8 @@ LOCAL_STATIC_LIBRARIES := \
|
||||||
libc++_static \
|
libc++_static \
|
||||||
libdl \
|
libdl \
|
||||||
libsparse_static \
|
libsparse_static \
|
||||||
libz
|
libz \
|
||||||
|
libprocessgroup
|
||||||
|
|
||||||
# Create symlinks
|
# Create symlinks
|
||||||
LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
|
LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@
|
||||||
#include <cutils/android_reboot.h>
|
#include <cutils/android_reboot.h>
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
|
|
||||||
|
#include <processgroup/processgroup.h>
|
||||||
|
|
||||||
#include "action.h"
|
#include "action.h"
|
||||||
#include "init.h"
|
#include "init.h"
|
||||||
#include "init_parser.h"
|
#include "init_parser.h"
|
||||||
|
|
@ -97,7 +99,7 @@ bool Service::Reap() {
|
||||||
if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
|
if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
|
||||||
NOTICE("Service '%s' (pid %d) killing any children in process group\n",
|
NOTICE("Service '%s' (pid %d) killing any children in process group\n",
|
||||||
name_.c_str(), pid_);
|
name_.c_str(), pid_);
|
||||||
kill(-pid_, SIGKILL);
|
killProcessGroup(uid_, pid_, SIGKILL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any sockets we may have created.
|
// Remove any sockets we may have created.
|
||||||
|
|
@ -490,6 +492,7 @@ bool Service::Start() {
|
||||||
time_started_ = gettime();
|
time_started_ = gettime();
|
||||||
pid_ = pid;
|
pid_ = pid;
|
||||||
flags_ |= SVC_RUNNING;
|
flags_ |= SVC_RUNNING;
|
||||||
|
createProcessGroup(uid_, pid_);
|
||||||
|
|
||||||
if ((flags_ & SVC_EXEC) != 0) {
|
if ((flags_ & SVC_EXEC) != 0) {
|
||||||
INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
|
INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
|
||||||
|
|
@ -532,7 +535,7 @@ void Service::Terminate() {
|
||||||
if (pid_) {
|
if (pid_) {
|
||||||
NOTICE("Sending SIGTERM to service '%s' (pid %d)...\n", name_.c_str(),
|
NOTICE("Sending SIGTERM to service '%s' (pid %d)...\n", name_.c_str(),
|
||||||
pid_);
|
pid_);
|
||||||
kill(-pid_, SIGTERM);
|
killProcessGroup(uid_, pid_, SIGTERM);
|
||||||
NotifyStateChange("stopping");
|
NotifyStateChange("stopping");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -583,7 +586,7 @@ void Service::StopOrReset(int how) {
|
||||||
|
|
||||||
if (pid_) {
|
if (pid_) {
|
||||||
NOTICE("Service '%s' is being killed...\n", name_.c_str());
|
NOTICE("Service '%s' is being killed...\n", name_.c_str());
|
||||||
kill(-pid_, SIGKILL);
|
killProcessGroup(uid_, pid_, SIGKILL);
|
||||||
NotifyStateChange("stopping");
|
NotifyStateChange("stopping");
|
||||||
} else {
|
} else {
|
||||||
NotifyStateChange("stopped");
|
NotifyStateChange("stopped");
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,16 @@ LOCAL_PATH := $(call my-dir)
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
LOCAL_SRC_FILES := processgroup.cpp
|
LOCAL_SRC_FILES := processgroup.cpp
|
||||||
LOCAL_MODULE := libprocessgroup
|
LOCAL_MODULE := libprocessgroup
|
||||||
LOCAL_SHARED_LIBRARIES := liblog libutils
|
LOCAL_STATIC_LIBRARIES := liblog
|
||||||
|
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
|
||||||
|
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
|
||||||
|
LOCAL_CFLAGS := -Wall -Werror
|
||||||
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
LOCAL_SRC_FILES := processgroup.cpp
|
||||||
|
LOCAL_MODULE := libprocessgroup
|
||||||
|
LOCAL_SHARED_LIBRARIES := liblog
|
||||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
|
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
|
||||||
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
|
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
|
||||||
LOCAL_CFLAGS := -Wall -Werror
|
LOCAL_CFLAGS := -Wall -Werror
|
||||||
|
|
|
||||||
|
|
@ -22,19 +22,18 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <log/log.h>
|
#include <log/log.h>
|
||||||
#include <private/android_filesystem_config.h>
|
#include <private/android_filesystem_config.h>
|
||||||
|
|
||||||
#include <utils/SystemClock.h>
|
|
||||||
|
|
||||||
#include <processgroup/processgroup.h>
|
#include <processgroup/processgroup.h>
|
||||||
#include "processgroup_priv.h"
|
#include "processgroup_priv.h"
|
||||||
|
|
||||||
|
|
@ -250,25 +249,26 @@ static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
|
||||||
|
|
||||||
int killProcessGroup(uid_t uid, int initialPid, int signal)
|
int killProcessGroup(uid_t uid, int initialPid, int signal)
|
||||||
{
|
{
|
||||||
int processes;
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||||
const int sleep_us = 5 * 1000; // 5ms
|
|
||||||
int64_t startTime = android::uptimeMillis();
|
|
||||||
int retry = 40;
|
|
||||||
|
|
||||||
|
int retry = 40;
|
||||||
|
int processes;
|
||||||
while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
|
while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
|
||||||
SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
|
SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
|
||||||
if (retry > 0) {
|
if (retry > 0) {
|
||||||
usleep(sleep_us);
|
usleep(5 * 1000); // 5ms
|
||||||
--retry;
|
--retry;
|
||||||
} else {
|
} else {
|
||||||
SLOGE("failed to kill %d processes for processgroup %d\n",
|
SLOGE("failed to kill %d processes for processgroup %d\n", processes, initialPid);
|
||||||
processes, initialPid);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
|
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
||||||
android::uptimeMillis()-startTime, processes);
|
|
||||||
|
SLOGV("Killed process group uid %d pid %d in %dms, %d procs remain", uid, initialPid,
|
||||||
|
static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()),
|
||||||
|
processes);
|
||||||
|
|
||||||
if (processes == 0) {
|
if (processes == 0) {
|
||||||
return removeProcessGroup(uid, initialPid);
|
return removeProcessGroup(uid, initialPid);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue