1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2011 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#include <vector>
#ifdef _WIN32
#include "diagnostics.h"
#ifdef __STDWX_H__
#include "stdwx.h"
#else
#include "boinc_win.h"
#include "win_util.h"
#endif
#else
#include "config.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#if HAVE_CSIGNAL
#include <csignal>
#elif HAVE_SYS_SIGNAL_H
#include <sys/signal.h>
#elif HAVE_SIGNAL_H
#include <signal.h>
#endif
#endif
#include "common_defs.h"
#include "procinfo.h"
#include "str_util.h"
#include "util.h"
#include "proc_control.h"
using std::vector;
//#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#endif
static void get_descendants_aux(PROC_MAP& pm, int pid, vector<int>& pids) {
PROC_MAP::iterator i = pm.find(pid);
if (i == pm.end()) return;
PROCINFO& p = i->second;
if (p.scanned) return; // avoid infinite recursion
p.scanned = true;
for (unsigned int j=0; j<p.children.size(); j++) {
int child_pid = p.children[j];
pids.push_back(child_pid);
get_descendants_aux(pm, child_pid, pids);
}
}
// return a list of all descendants of the given process
//
void get_descendants(int pid, vector<int>& pids) {
int retval;
PROC_MAP pm;
pids.clear();
retval = procinfo_setup(pm);
if (retval) return;
get_descendants_aux(pm, pid, pids);
#ifdef DEBUG
fprintf(stderr, "descendants of %d:\n", pid);
for (unsigned int i=0; i<pids.size(); i++) {
fprintf(stderr, " %d\n", pids[i]);
}
#endif
}
#ifdef _WIN32
// Suspend or resume the threads in a set of processes,
// but don't suspend 'calling_thread'.
//
// The only way to do this on Windows is to enumerate
// all the threads in the entire system,
// and find those belonging to one of the process (ugh!!)
//
int suspend_or_resume_threads(
vector<int>pids, DWORD calling_thread_id, bool resume, bool check_exempt
) {
HANDLE threads, thread;
THREADENTRY32 te = {0};
int retval = 0;
DWORD n;
#ifdef DEBUG
fprintf(stderr, "start: check_exempt %d %s\n", check_exempt, precision_time_to_string(dtime()));
fprintf(stderr, "%s processes", resume?"resume":"suspend");
for (unsigned int i=0; i<pids.size(); i++) {
fprintf(stderr, " %d", pids[i]);
}
fprintf(stderr, "\n");
#endif
threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (threads == INVALID_HANDLE_VALUE) {
fprintf(stderr, "CreateToolhelp32Snapshot failed\n");
return -1;
}
te.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(threads, &te)) {
fprintf(stderr, "Thread32First failed\n");
CloseHandle(threads);
return -1;
}
do {
if (check_exempt && !diagnostics_is_thread_exempt_suspend(te.th32ThreadID)) {
#ifdef DEBUG
fprintf(stderr, "thread is exempt\n");
#endif
continue;
}
//fprintf(stderr, "thread %d PID %d %s\n", te.th32ThreadID, te.th32OwnerProcessID, precision_time_to_string(dtime()));
if (te.th32ThreadID == calling_thread_id) continue;
if (!in_vector(te.th32OwnerProcessID, pids)) continue;
thread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
if (resume) {
n = ResumeThread(thread);
#ifdef DEBUG
fprintf(stderr, "ResumeThread returns %d\n", n);
#endif
} else {
n = SuspendThread(thread);
#ifdef DEBUG
fprintf(stderr, "SuspendThread returns %d\n", n);
#endif
}
if (n == -1) retval = -1;
CloseHandle(thread);
} while (Thread32Next(threads, &te));
CloseHandle (threads);
#ifdef DEBUG
fprintf(stderr, "end: %s\n", precision_time_to_string(dtime()));
#endif
return retval;
}
#else
bool any_process_exists(vector<int>& pids) {
int status;
for (unsigned int i=0; i<pids.size(); i++) {
if (waitpid(pids[i], &status, WNOHANG) >= 0) {
return true;
}
}
return false;
}
#endif
void kill_all(vector<int>& pids) {
for (unsigned int i=0; i<pids.size(); i++) {
#ifdef _WIN32
HANDLE h = OpenProcess(READ_CONTROL | PROCESS_TERMINATE, false, pids[i]);
if (h == NULL) continue;
TerminateProcess(h, 0);
CloseHandle(h);
#else
kill(pids[i], SIGTERM);
#endif
}
}
// Kill the descendants of the calling process.
//
#ifdef _WIN32
void kill_descendants() {
vector<int> descendants;
// on Win, kill descendants directly
//
get_descendants(GetCurrentProcessId(), descendants);
kill_all(descendants);
}
#else
// Same, but if child_pid is nonzero,
// give it a chance to exit gracefully on Unix
//
void kill_descendants(int child_pid) {
vector<int> descendants;
// on Unix, ask main process nicely.
// it descendants still exist after 10 sec, use the nuclear option
//
get_descendants(getpid(), descendants);
if (child_pid) {
::kill(child_pid, SIGTERM);
for (int i=0; i<10; i++) {
if (!any_process_exists(descendants)) {
return;
}
sleep(1);
}
kill_all(descendants);
// kill any processes that might have been created
// in the last 10 secs
get_descendants(getpid(), descendants);
}
kill_all(descendants);
}
#endif
// suspend/resume the descendants of the calling process
// (or if pid==0, the calling process)
//
void suspend_or_resume_descendants(bool resume) {
vector<int> descendants;
#ifdef _WIN32
int pid = GetCurrentProcessId();
get_descendants(pid, descendants);
suspend_or_resume_threads(descendants, 0, resume, false);
#else
int pid = getpid();
get_descendants(pid, descendants);
for (unsigned int i=0; i<descendants.size(); i++) {
kill(descendants[i], resume?SIGCONT:SIGSTOP);
}
#endif
}
// used by the wrapper
//
void suspend_or_resume_process(int pid, bool resume) {
#ifdef _WIN32
vector<int> pids;
pids.push_back(pid);
suspend_or_resume_threads(pids, 0, resume, false);
#else
::kill(pid, resume?SIGCONT:SIGSTOP);
#endif
}
// return OS-specific value associated with priority code
//
int process_priority_value(int priority) {
#ifdef _WIN32
switch (priority) {
case PROCESS_PRIORITY_LOWEST: return IDLE_PRIORITY_CLASS;
case PROCESS_PRIORITY_LOW: return BELOW_NORMAL_PRIORITY_CLASS;
case PROCESS_PRIORITY_NORMAL: return NORMAL_PRIORITY_CLASS;
case PROCESS_PRIORITY_HIGH: return ABOVE_NORMAL_PRIORITY_CLASS;
case PROCESS_PRIORITY_HIGHEST: return HIGH_PRIORITY_CLASS;
}
return 0;
#else
switch (priority) {
case PROCESS_PRIORITY_LOWEST: return 19;
case PROCESS_PRIORITY_LOW: return 10;
case PROCESS_PRIORITY_NORMAL: return 0;
case PROCESS_PRIORITY_HIGH: return -10;
case PROCESS_PRIORITY_HIGHEST: return -16;
}
return 0;
#endif
}
|