add killProcessGroupOnce

- killProcessGroup retries if it fails while
  killProcessGroupOnce only tries once.
- useful for sending signal like SIGTERM once.

bug: 36696408
Test: will add usage to init

(cherry picked from commit a88e425e0f5502062c7f3088d53b1b0ce8ef803e)

Change-Id: Ie8eb338ee919b499948baf2a220e584b54293a89
This commit is contained in:
Keun-young Park 2017-03-28 17:25:24 -07:00
parent 54df8375ba
commit e1adb869da
2 changed files with 13 additions and 6 deletions

View File

@ -24,6 +24,8 @@ __BEGIN_DECLS
int killProcessGroup(uid_t uid, int initialPid, int signal);
int killProcessGroupOnce(uid_t uid, int initialPid, int signal);
int createProcessGroup(uid_t uid, int initialPid);
void removeAllProcessGroups(void);

View File

@ -252,8 +252,7 @@ void removeAllProcessGroups()
}
}
static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
{
static int doKillProcessGroupOnce(uid_t uid, int initialPid, int signal) {
int processes = 0;
struct ctx ctx;
pid_t pid;
@ -282,13 +281,11 @@ static int killProcessGroupOnce(uid_t uid, int initialPid, int signal)
return processes;
}
int killProcessGroup(uid_t uid, int initialPid, int signal)
{
static int killProcessGroup(uid_t uid, int initialPid, int signal, int retry) {
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
int retry = 40;
int processes;
while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
while ((processes = doKillProcessGroupOnce(uid, initialPid, signal)) > 0) {
LOG(VERBOSE) << "killed " << processes << " processes for processgroup " << initialPid;
if (retry > 0) {
std::this_thread::sleep_for(5ms);
@ -313,6 +310,14 @@ int killProcessGroup(uid_t uid, int initialPid, int signal)
}
}
int killProcessGroup(uid_t uid, int initialPid, int signal) {
return killProcessGroup(uid, initialPid, signal, 40 /*maxRetry*/);
}
int killProcessGroupOnce(uid_t uid, int initialPid, int signal) {
return killProcessGroup(uid, initialPid, signal, 0 /*maxRetry*/);
}
static bool mkdirAndChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
{
if (mkdir(path, mode) == -1 && errno != EEXIST) {