summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--init/Android.mk6
-rw-r--r--init/service.cpp9
-rw-r--r--libprocessgroup/Android.mk11
-rw-r--r--libprocessgroup/processgroup.cpp26
4 files changed, 32 insertions, 20 deletions
diff --git a/init/Android.mk b/init/Android.mk
index 2525b009b..f83924e9b 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -52,7 +52,7 @@ LOCAL_SRC_FILES:= \
52 service.cpp \ 52 service.cpp \
53 util.cpp \ 53 util.cpp \
54 54
55LOCAL_STATIC_LIBRARIES := libbase libselinux 55LOCAL_STATIC_LIBRARIES := libbase libselinux liblog libprocessgroup
56LOCAL_MODULE := libinit 56LOCAL_MODULE := libinit
57LOCAL_SANITIZE := integer 57LOCAL_SANITIZE := integer
58LOCAL_CLANG := true 58LOCAL_CLANG := true
@@ -92,7 +92,6 @@ LOCAL_STATIC_LIBRARIES := \
92 libcutils \ 92 libcutils \
93 libext4_utils_static \ 93 libext4_utils_static \
94 libbase \ 94 libbase \
95 libutils \
96 libc \ 95 libc \
97 libselinux \ 96 libselinux \
98 liblog \ 97 liblog \
@@ -101,7 +100,8 @@ LOCAL_STATIC_LIBRARIES := \
101 libc++_static \ 100 libc++_static \
102 libdl \ 101 libdl \
103 libsparse_static \ 102 libsparse_static \
104 libz 103 libz \
104 libprocessgroup
105 105
106# Create symlinks 106# Create symlinks
107LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \ 107LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
diff --git a/init/service.cpp b/init/service.cpp
index f5b8b00a2..4175d054f 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -30,6 +30,8 @@
30#include <cutils/android_reboot.h> 30#include <cutils/android_reboot.h>
31#include <cutils/sockets.h> 31#include <cutils/sockets.h>
32 32
33#include <processgroup/processgroup.h>
34
33#include "action.h" 35#include "action.h"
34#include "init.h" 36#include "init.h"
35#include "init_parser.h" 37#include "init_parser.h"
@@ -97,7 +99,7 @@ bool Service::Reap() {
97 if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) { 99 if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
98 NOTICE("Service '%s' (pid %d) killing any children in process group\n", 100 NOTICE("Service '%s' (pid %d) killing any children in process group\n",
99 name_.c_str(), pid_); 101 name_.c_str(), pid_);
100 kill(-pid_, SIGKILL); 102 killProcessGroup(uid_, pid_, SIGKILL);
101 } 103 }
102 104
103 // Remove any sockets we may have created. 105 // Remove any sockets we may have created.
@@ -490,6 +492,7 @@ bool Service::Start() {
490 time_started_ = gettime(); 492 time_started_ = gettime();
491 pid_ = pid; 493 pid_ = pid;
492 flags_ |= SVC_RUNNING; 494 flags_ |= SVC_RUNNING;
495 createProcessGroup(uid_, pid_);
493 496
494 if ((flags_ & SVC_EXEC) != 0) { 497 if ((flags_ & SVC_EXEC) != 0) {
495 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n", 498 INFO("SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...\n",
@@ -532,7 +535,7 @@ void Service::Terminate() {
532 if (pid_) { 535 if (pid_) {
533 NOTICE("Sending SIGTERM to service '%s' (pid %d)...\n", name_.c_str(), 536 NOTICE("Sending SIGTERM to service '%s' (pid %d)...\n", name_.c_str(),
534 pid_); 537 pid_);
535 kill(-pid_, SIGTERM); 538 killProcessGroup(uid_, pid_, SIGTERM);
536 NotifyStateChange("stopping"); 539 NotifyStateChange("stopping");
537 } 540 }
538} 541}
@@ -583,7 +586,7 @@ void Service::StopOrReset(int how) {
583 586
584 if (pid_) { 587 if (pid_) {
585 NOTICE("Service '%s' is being killed...\n", name_.c_str()); 588 NOTICE("Service '%s' is being killed...\n", name_.c_str());
586 kill(-pid_, SIGKILL); 589 killProcessGroup(uid_, pid_, SIGKILL);
587 NotifyStateChange("stopping"); 590 NotifyStateChange("stopping");
588 } else { 591 } else {
589 NotifyStateChange("stopped"); 592 NotifyStateChange("stopped");
diff --git a/libprocessgroup/Android.mk b/libprocessgroup/Android.mk
index 1885fa5dd..9620ebdd6 100644
--- a/libprocessgroup/Android.mk
+++ b/libprocessgroup/Android.mk
@@ -3,7 +3,16 @@ LOCAL_PATH := $(call my-dir)
3include $(CLEAR_VARS) 3include $(CLEAR_VARS)
4LOCAL_SRC_FILES := processgroup.cpp 4LOCAL_SRC_FILES := processgroup.cpp
5LOCAL_MODULE := libprocessgroup 5LOCAL_MODULE := libprocessgroup
6LOCAL_SHARED_LIBRARIES := liblog libutils 6LOCAL_STATIC_LIBRARIES := liblog
7LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
8LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
9LOCAL_CFLAGS := -Wall -Werror
10include $(BUILD_STATIC_LIBRARY)
11
12include $(CLEAR_VARS)
13LOCAL_SRC_FILES := processgroup.cpp
14LOCAL_MODULE := libprocessgroup
15LOCAL_SHARED_LIBRARIES := liblog
7LOCAL_C_INCLUDES := $(LOCAL_PATH)/include 16LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
8LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include 17LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
9LOCAL_CFLAGS := -Wall -Werror 18LOCAL_CFLAGS := -Wall -Werror
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index cfc9ae2dc..c4672c48a 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -22,20 +22,19 @@
22#include <errno.h> 22#include <errno.h>
23#include <fcntl.h> 23#include <fcntl.h>
24#include <inttypes.h> 24#include <inttypes.h>
25#include <mutex>
26#include <stdbool.h>
27#include <stdio.h> 25#include <stdio.h>
28#include <stdlib.h> 26#include <stdlib.h>
29#include <string.h> 27#include <string.h>
30#include <sys/stat.h> 28#include <sys/stat.h>
31#include <sys/types.h> 29#include <sys/types.h>
30
31#include <chrono>
32#include <memory> 32#include <memory>
33#include <mutex>
33 34
34#include <log/log.h> 35#include <log/log.h>
35#include <private/android_filesystem_config.h> 36#include <private/android_filesystem_config.h>
36 37
37#include <utils/SystemClock.h>
38
39#include <processgroup/processgroup.h> 38#include <processgroup/processgroup.h>
40 39
41// Uncomment line below use memory cgroups for keeping track of (forked) PIDs 40// Uncomment line below use memory cgroups for keeping track of (forked) PIDs
@@ -290,25 +289,26 @@ static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
290 289
291int killProcessGroup(uid_t uid, int initialPid, int signal) 290int killProcessGroup(uid_t uid, int initialPid, int signal)
292{ 291{
293 int processes; 292 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
294 const int sleep_us = 5 * 1000; // 5ms
295 int64_t startTime = android::uptimeMillis();
296 int retry = 40;
297 293
294 int retry = 40;
295 int processes;
298 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) { 296 while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
299 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid); 297 SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
300 if (retry > 0) { 298 if (retry > 0) {
301 usleep(sleep_us); 299 usleep(5 * 1000); // 5ms
302 --retry; 300 --retry;
303 } else { 301 } else {
304 SLOGE("failed to kill %d processes for processgroup %d\n", 302 SLOGE("failed to kill %d processes for processgroup %d\n", processes, initialPid);
305 processes, initialPid);
306 break; 303 break;
307 } 304 }
308 } 305 }
309 306
310 SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid, 307 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
311 android::uptimeMillis()-startTime, processes); 308
309 SLOGV("Killed process group uid %d pid %d in %dms, %d procs remain", uid, initialPid,
310 static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()),
311 processes);
312 312
313 if (processes == 0) { 313 if (processes == 0) {
314 return removeProcessGroup(uid, initialPid); 314 return removeProcessGroup(uid, initialPid);