Commit bb9f7a93 authored by joreland@mysql.com's avatar joreland@mysql.com

Stop entire process group (if child has forked)

Impl. to handle "angle" process of ndb
parent b8027810
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <pwd.h> #include <pwd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
void void
CPCD::Process::print(FILE * f){ CPCD::Process::print(FILE * f){
...@@ -103,7 +105,7 @@ bool ...@@ -103,7 +105,7 @@ bool
CPCD::Process::isRunning() { CPCD::Process::isRunning() {
if(m_pid <= 1){ if(m_pid <= 1){
logger.critical("isRunning(%d) invalid pid: %d", m_id, m_pid); //logger.critical("isRunning(%d) invalid pid: %d", m_id, m_pid);
return false; return false;
} }
/* Check if there actually exists a process with such a pid */ /* Check if there actually exists a process with such a pid */
...@@ -355,7 +357,7 @@ CPCD::Process::start() { ...@@ -355,7 +357,7 @@ CPCD::Process::start() {
*/ */
switch(pid = fork()) { switch(pid = fork()) {
case 0: /* Child */ case 0: /* Child */
setsid();
writePid(getpid()); writePid(getpid());
if(runas(m_runas.c_str()) == 0){ if(runas(m_runas.c_str()) == 0){
do_exec(); do_exec();
...@@ -384,11 +386,11 @@ CPCD::Process::start() { ...@@ -384,11 +386,11 @@ CPCD::Process::start() {
pid_t pid; pid_t pid;
switch(pid = fork()) { switch(pid = fork()) {
case 0: /* Child */ case 0: /* Child */
setsid();
writePid(getpid()); writePid(getpid());
if(runas(m_runas.c_str()) != 0){ if(runas(m_runas.c_str()) != 0){
_exit(1); _exit(1);
} }
setsid();
do_exec(); do_exec();
_exit(1); _exit(1);
/* NOTREACHED */ /* NOTREACHED */
...@@ -423,11 +425,11 @@ CPCD::Process::start() { ...@@ -423,11 +425,11 @@ CPCD::Process::start() {
while(readPid() < 0){ while(readPid() < 0){
sched_yield(); sched_yield();
} }
if(pid != -1 && pid != m_pid){ if(pid != -1 && pid != m_pid){
logger.error("pid and m_pid don't match: %d %d", pid, m_pid); logger.error("pid and m_pid don't match: %d %d", pid, m_pid);
} }
if(isRunning()){ if(isRunning()){
m_status = RUNNING; m_status = RUNNING;
return 0; return 0;
...@@ -449,28 +451,32 @@ CPCD::Process::stop() { ...@@ -449,28 +451,32 @@ CPCD::Process::stop() {
} }
m_status = STOPPING; m_status = STOPPING;
int ret = kill((pid_t)m_pid, SIGTERM); const pid_t pgid = - getpgid(m_pid);
int ret = kill(pgid, SIGTERM);
switch(ret) { switch(ret) {
case 0: case 0:
logger.debug("Sent SIGTERM to pid %d", (int)m_pid); logger.debug("Sent SIGTERM to pid %d", (int)pgid);
break; break;
default: default:
logger.debug("kill pid: %d : %s", (int)m_pid, strerror(errno)); logger.debug("kill pid: %d : %s", (int)pgid, strerror(errno));
break; break;
} }
if(isRunning()){ errno = 0;
ret = kill((pid_t)m_pid, SIGKILL); ret = kill(pgid, 0);
if(ret == 0) {
errno = 0;
ret = kill(pgid, SIGKILL);
switch(ret) { switch(ret) {
case 0: case 0:
logger.debug("Sent SIGKILL to pid %d", (int)m_pid); logger.debug("Sent SIGKILL to pid %d", (int)pgid);
break; break;
default: default:
logger.debug("kill pid: %d : %s\n", (int)m_pid, strerror(errno)); logger.debug("kill pid: %d : %s\n", (int)pgid, strerror(errno));
break; break;
} }
} }
m_pid = -1; m_pid = -1;
m_status = STOPPED; m_status = STOPPED;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment