mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-09-25 00:45:46 +00:00
85 lines
2.1 KiB
C
85 lines
2.1 KiB
C
|
/*
|
||
|
* virprocess.c: interaction with processes
|
||
|
*
|
||
|
* Copyright (C) 2010-2012 Red Hat, Inc.
|
||
|
*
|
||
|
* This library 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 2.1 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* This library 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 this library. If not, see
|
||
|
* <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
|
#include <config.h>
|
||
|
|
||
|
#include <signal.h>
|
||
|
#include <errno.h>
|
||
|
|
||
|
#include "virprocess.h"
|
||
|
|
||
|
/* send signal to a single process */
|
||
|
int virProcessKill(pid_t pid, int sig)
|
||
|
{
|
||
|
if (pid <= 1) {
|
||
|
errno = ESRCH;
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
#ifdef WIN32
|
||
|
/* Mingw / Windows don't have many signals (AFAIK) */
|
||
|
switch (sig) {
|
||
|
case SIGINT:
|
||
|
/* This does a Ctrl+C equiv */
|
||
|
if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)) {
|
||
|
errno = ESRCH;
|
||
|
return -1;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case SIGTERM:
|
||
|
/* Since TerminateProcess is closer to SIG_KILL, we do
|
||
|
* a Ctrl+Break equiv which is more pleasant like the
|
||
|
* good old unix SIGTERM/HUP
|
||
|
*/
|
||
|
if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)) {
|
||
|
errno = ESRCH;
|
||
|
return -1;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
{
|
||
|
HANDLE proc;
|
||
|
proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
||
|
if (!proc) {
|
||
|
errno = ESRCH; /* Not entirely accurate, but close enough */
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* TerminateProcess is more or less equiv to SIG_KILL, in that
|
||
|
* a process can't trap / block it
|
||
|
*/
|
||
|
if (sig != 0 && !TerminateProcess(proc, sig)) {
|
||
|
errno = ESRCH;
|
||
|
return -1;
|
||
|
}
|
||
|
CloseHandle(proc);
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
#else
|
||
|
return kill(pid, sig);
|
||
|
#endif
|
||
|
}
|