--- libaitio/src/exec.c 2013/12/06 01:30:22 1.1.2.12 +++ libaitio/src/exec.c 2013/12/08 20:43:22 1.1.2.14 @@ -71,6 +71,7 @@ io_progDestroy(prog_t ** __restrict pprg) e_free((*pprg)->prog_used); array_Destroy(&(*pprg)->prog_fds); pthread_mutex_destroy(&(*pprg)->prog_mtx); + signal(SIGPIPE, SIG_DFL); e_free(*pprg); *pprg = NULL; @@ -88,6 +89,7 @@ io_progClose(prog_t * __restrict prg, u_int closeNum) { register int i; int ret = 0; + struct tagPIOPID *p; if (!prg) return 0; @@ -99,8 +101,17 @@ io_progClose(prog_t * __restrict prg, u_int closeNum) pthread_mutex_lock(&prg->prog_mtx); for (i = array_Size(prg->prog_fds) - 1; (closeNum ? ret < closeNum : 42) && i > -1; i--) - if (array_Get(prg->prog_fds, i)) { + if (array_Get(prg->prog_fds, i) && #ifdef POPEN_STREAM + (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) { +#else + (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) { +#endif + kill(p->pid, SIGTERM); + usleep(1000); + if (waitpid(p->pid, &p->stat, WNOHANG) > 0) + kill(p->pid, SIGKILL); +#ifdef POPEN_STREAM e_pclose(array(prg->prog_fds, i, FILE*)); #else e_pclose((int) array(prg->prog_fds, i, intptr_t)); @@ -116,6 +127,52 @@ io_progClose(prog_t * __restrict prg, u_int closeNum) } /* + * io_progCloseAt() - Close program at pool of certain position + * + * @prg = program pool + * @idx = index at pool + * return: 0 error or !=0 closed program + */ +int +io_progCloseAt(prog_t * __restrict prg, u_int idx) +{ + int ret = 0; + struct tagPIOPID *p; + + if (!prg) + return 0; + if (idx > prg->prog_maxn) { + io_SetErr(EINVAL, "Requested number for close program is over pool's limit"); + return 0; + } + + pthread_mutex_lock(&prg->prog_mtx); + if (array_Get(prg->prog_fds, idx) && +#ifdef POPEN_STREAM + (p = pio_pgetpid(array(prg->prog_fds, idx, FILE*)))) { +#else + (p = pio_pgetpid((int) array(prg->prog_fds, idx, intptr_t)))) { +#endif + kill(p->pid, SIGTERM); + usleep(1000); + if (waitpid(p->pid, &p->stat, WNOHANG) > 0) + kill(p->pid, SIGKILL); +#ifdef POPEN_STREAM + e_pclose(array(prg->prog_fds, idx, FILE*)); +#else + e_pclose((int) array(prg->prog_fds, idx, intptr_t)); +#endif + array_Del(prg->prog_fds, idx, 0); + clrbit(prg->prog_used, idx); + prg->prog_cnum--; + ret++; + } + pthread_mutex_unlock(&prg->prog_mtx); + + return ret; +} + +/* * io_progOpen() - Execute number of program(s) * * @prg = program pool @@ -202,6 +259,7 @@ io_progVacuum(prog_t * __restrict prg, u_int toNum) { register int i; int ret = 0; + struct tagPIOPID *p; if (!prg) return 0; @@ -214,10 +272,20 @@ io_progVacuum(prog_t * __restrict prg, u_int toNum) pthread_mutex_lock(&prg->prog_mtx); for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--) - if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i)) { + if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i) && #ifdef POPEN_STREAM + (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) { + kill(p->pid, SIGTERM); + usleep(1000); + if (waitpid(p->pid, &p->stat, WNOHANG) > 0) + kill(p->pid, SIGKILL); e_pclose(array(prg->prog_fds, i, FILE*)); #else + (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) { + kill(p->pid, SIGTERM); + usleep(1000); + if (waitpid(p->pid, &p->stat, WNOHANG) > 0) + kill(p->pid, SIGKILL); e_pclose((int) array(prg->prog_fds, i, intptr_t)); #endif array_Del(prg->prog_fds, i, 0);