2011-09-02 22:06:30 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
2020-08-26 08:10:14 +00:00
|
|
|
// Copyright (C) 2020 University of California
|
2011-09-02 22:06:30 +00:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#include "win_util.h"
|
|
|
|
#else
|
2011-10-04 18:19:57 +00:00
|
|
|
#include "config.h"
|
2011-09-02 22:06:30 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-09-28 17:33:26 +00:00
|
|
|
#if HAVE_CSIGNAL
|
2011-09-02 22:06:30 +00:00
|
|
|
#include <csignal>
|
2011-09-28 17:33:26 +00:00
|
|
|
#elif HAVE_SYS_SIGNAL_H
|
2011-09-02 22:06:30 +00:00
|
|
|
#include <sys/signal.h>
|
2011-09-28 17:33:26 +00:00
|
|
|
#elif HAVE_SIGNAL_H
|
2011-09-02 22:06:30 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-10-13 18:35:16 +00:00
|
|
|
#include "common_defs.h"
|
2011-09-02 22:06:30 +00:00
|
|
|
#include "procinfo.h"
|
2012-12-12 00:05:40 +00:00
|
|
|
#include "str_util.h"
|
|
|
|
#include "util.h"
|
2011-09-02 22:06:30 +00:00
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "proc_control.h"
|
|
|
|
|
2011-09-02 22:06:30 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
//#define DEBUG
|
|
|
|
|
2014-06-24 11:52:49 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-02 22:06:30 +00:00
|
|
|
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;
|
2011-10-19 07:49:23 +00:00
|
|
|
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];
|
2011-09-02 22:06:30 +00:00
|
|
|
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);
|
2012-12-12 00:05:40 +00:00
|
|
|
#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
|
2011-09-02 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
// Suspend or resume the threads in a set of processes,
|
2011-09-02 22:06:30 +00:00
|
|
|
// but don't suspend 'calling_thread'.
|
|
|
|
//
|
2018-11-05 20:54:59 +00:00
|
|
|
// Called from:
|
|
|
|
// API (processes = self)
|
|
|
|
// This handles a) throttling; b) suspend/resume by user
|
|
|
|
// wrapper (via suspend_or_resume_process()); process = child
|
|
|
|
// wrapper, MP case (via suspend_or_resume_decendants());
|
|
|
|
// processes = descendants
|
|
|
|
//
|
2011-09-02 22:06:30 +00:00
|
|
|
// The only way to do this on Windows is to enumerate
|
|
|
|
// all the threads in the entire system,
|
2018-11-05 20:54:59 +00:00
|
|
|
// and identify those belonging to one of the processes (ugh!!)
|
|
|
|
//
|
2011-09-02 22:06:30 +00:00
|
|
|
int suspend_or_resume_threads(
|
2012-12-12 00:05:40 +00:00
|
|
|
vector<int>pids, DWORD calling_thread_id, bool resume, bool check_exempt
|
2011-09-02 22:06:30 +00:00
|
|
|
) {
|
|
|
|
HANDLE threads, thread;
|
|
|
|
THREADENTRY32 te = {0};
|
2013-11-11 18:42:03 +00:00
|
|
|
int retval = 0;
|
|
|
|
DWORD n;
|
2018-11-05 20:54:59 +00:00
|
|
|
static vector<DWORD> suspended_threads;
|
2011-09-02 22:06:30 +00:00
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
#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]);
|
2011-09-02 22:06:30 +00:00
|
|
|
}
|
2012-12-12 00:05:40 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
2011-09-02 22:06:30 +00:00
|
|
|
|
|
|
|
threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
|
2012-12-12 00:05:40 +00:00
|
|
|
if (threads == INVALID_HANDLE_VALUE) {
|
|
|
|
fprintf(stderr, "CreateToolhelp32Snapshot failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-02 22:06:30 +00:00
|
|
|
|
|
|
|
te.dwSize = sizeof(THREADENTRY32);
|
|
|
|
if (!Thread32First(threads, &te)) {
|
2012-12-12 00:05:40 +00:00
|
|
|
fprintf(stderr, "Thread32First failed\n");
|
2011-09-02 22:06:30 +00:00
|
|
|
CloseHandle(threads);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-05 20:54:59 +00:00
|
|
|
if (!resume) {
|
|
|
|
suspended_threads.clear();
|
|
|
|
}
|
|
|
|
|
2011-09-02 22:06:30 +00:00
|
|
|
do {
|
2012-12-12 00:05:40 +00:00
|
|
|
if (check_exempt && !diagnostics_is_thread_exempt_suspend(te.th32ThreadID)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "thread is exempt\n");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-05 20:54:59 +00:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "thread %d PID %d %s\n",
|
|
|
|
te.th32ThreadID, te.th32OwnerProcessID,
|
|
|
|
precision_time_to_string(dtime())
|
|
|
|
);
|
|
|
|
#endif
|
2011-09-02 22:06:30 +00:00
|
|
|
if (te.th32ThreadID == calling_thread_id) continue;
|
2012-12-12 00:05:40 +00:00
|
|
|
if (!in_vector(te.th32OwnerProcessID, pids)) continue;
|
|
|
|
thread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
|
|
|
|
if (resume) {
|
2018-11-05 20:54:59 +00:00
|
|
|
// check whether we suspended this thread earlier
|
|
|
|
//
|
|
|
|
if (std::find(
|
|
|
|
suspended_threads.begin(), suspended_threads.end(),
|
|
|
|
te.th32ThreadID
|
|
|
|
) != suspended_threads.end()) {
|
|
|
|
n = ResumeThread(thread);
|
2012-12-12 00:05:40 +00:00
|
|
|
#ifdef DEBUG
|
2018-11-05 20:54:59 +00:00
|
|
|
fprintf(stderr, "ResumeThread returns %d\n", n);
|
2012-12-12 00:05:40 +00:00
|
|
|
#endif
|
2018-11-05 21:09:43 +00:00
|
|
|
} else {
|
|
|
|
n = 0;
|
2018-11-05 20:54:59 +00:00
|
|
|
}
|
2012-12-12 00:05:40 +00:00
|
|
|
} else {
|
2013-11-11 18:42:03 +00:00
|
|
|
n = SuspendThread(thread);
|
2018-11-05 20:54:59 +00:00
|
|
|
suspended_threads.push_back(te.th32ThreadID);
|
2012-12-12 00:05:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "SuspendThread returns %d\n", n);
|
|
|
|
#endif
|
|
|
|
}
|
2013-11-11 18:42:03 +00:00
|
|
|
if (n == -1) retval = -1;
|
2012-12-12 00:05:40 +00:00
|
|
|
CloseHandle(thread);
|
2011-09-02 22:06:30 +00:00
|
|
|
} while (Thread32Next(threads, &te));
|
|
|
|
|
|
|
|
CloseHandle (threads);
|
2012-12-12 00:05:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "end: %s\n", precision_time_to_string(dtime()));
|
|
|
|
#endif
|
2013-11-11 18:42:03 +00:00
|
|
|
return retval;
|
2011-09-02 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
2012-10-11 16:41:31 +00:00
|
|
|
// Same, but if child_pid is nonzero,
|
|
|
|
// give it a chance to exit gracefully on Unix
|
2011-09-02 22:06:30 +00:00
|
|
|
//
|
|
|
|
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
|
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
// suspend/resume the descendants of the calling process
|
2011-09-02 22:06:30 +00:00
|
|
|
// (or if pid==0, the calling process)
|
|
|
|
//
|
2012-12-12 00:05:40 +00:00
|
|
|
void suspend_or_resume_descendants(bool resume) {
|
2011-09-02 22:06:30 +00:00
|
|
|
vector<int> descendants;
|
|
|
|
#ifdef _WIN32
|
2012-12-12 00:05:40 +00:00
|
|
|
int pid = GetCurrentProcessId();
|
|
|
|
get_descendants(pid, descendants);
|
|
|
|
suspend_or_resume_threads(descendants, 0, resume, false);
|
2011-09-02 22:06:30 +00:00
|
|
|
#else
|
2012-12-12 00:05:40 +00:00
|
|
|
int pid = getpid();
|
2011-09-02 22:06:30 +00:00
|
|
|
get_descendants(pid, descendants);
|
2012-12-12 00:05:40 +00:00
|
|
|
for (unsigned int i=0; i<descendants.size(); i++) {
|
2019-03-06 16:03:13 +00:00
|
|
|
kill(descendants[i], resume?SIGCONT:SIGTSTP);
|
2012-12-12 00:05:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-02 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
// used by the wrapper
|
|
|
|
//
|
2011-09-02 22:06:30 +00:00
|
|
|
void suspend_or_resume_process(int pid, bool resume) {
|
|
|
|
#ifdef _WIN32
|
2012-12-12 00:05:40 +00:00
|
|
|
vector<int> pids;
|
|
|
|
pids.push_back(pid);
|
|
|
|
suspend_or_resume_threads(pids, 0, resume, false);
|
2011-09-02 22:06:30 +00:00
|
|
|
#else
|
2019-03-06 16:03:13 +00:00
|
|
|
::kill(pid, resume?SIGCONT:SIGTSTP);
|
2011-09-02 22:06:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-10-13 18:35:16 +00:00
|
|
|
|
|
|
|
// 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) {
|
2020-08-28 06:20:12 +00:00
|
|
|
case PROCESS_PRIORITY_LOWEST: return PROCESS_IDLE_PRIORITY;
|
|
|
|
case PROCESS_PRIORITY_LOW: return PROCESS_MEDIUM_PRIORITY;
|
|
|
|
case PROCESS_PRIORITY_NORMAL: return PROCESS_NORMAL_PRIORITY;
|
|
|
|
case PROCESS_PRIORITY_HIGH: return PROCESS_ABOVE_NORMAL_PRIORITY;
|
|
|
|
case PROCESS_PRIORITY_HIGHEST: return PROCESS_HIGH_PRIORITY;
|
2014-10-13 18:35:16 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|