--- libaitio/src/exec.c 2013/12/12 23:06:45 1.1.2.20 +++ libaitio/src/exec.c 2013/12/18 12:40:21 1.3 @@ -1,3 +1,48 @@ +/************************************************************************* +* (C) 2013 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: exec.c,v 1.3 2013/12/18 12:40:21 misho Exp $ +* +************************************************************************** +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" @@ -173,6 +218,63 @@ io_progCloseAt(prog_t * __restrict prg, u_int idx) } /* + * io_progCloseOf() - Close program at pool with certain handle + * + * @prg = program pool + * @h = handle of program + * return: 0 error, >0 closed programs + */ +int +#ifdef POPEN_STREAM +io_progCloseOf(prog_t * __restrict prg, FILE *h) +#else +io_progCloseOf(prog_t * __restrict prg, int h) +#endif +{ + register int i; + int ret = 0; + struct tagPIOPID *p; +#ifdef POPEN_STREAM + FILE *f; +#else + int f; +#endif + + if (!prg) + return 0; + + pthread_mutex_lock(&prg->prog_mtx); + for (i = 0; i < array_Size(prg->prog_fds); i++) + if (array_Get(prg->prog_fds, i)) { +#ifdef POPEN_STREAM + f = array(prg->prog_fds, i, FILE*); + if (f == h && (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) { +#else + f = (int) array(prg->prog_fds, i, intptr_t); + if (f == h && (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)); +#endif + array_Del(prg->prog_fds, i, 0); + clrbit(prg->prog_used, i); + prg->prog_cnum--; + ret++; + break; + } + } + pthread_mutex_unlock(&prg->prog_mtx); + + return ret; +} + +/* * io_progOpen2() - Start program from pool on first unused slot * * @prg = program pool @@ -214,7 +316,7 @@ io_progOpen2(prog_t * __restrict prg) e_pclose(f); break; } else - array_Set(prg->prog_fds, i, f); + array_Set(prg->prog_fds, i, (intptr_t) f); clrbit(prg->prog_used, i); prg->prog_cnum++; ret = i; @@ -254,7 +356,6 @@ io_progOpen(prog_t * __restrict prg, u_int execNum) pthread_mutex_lock(&prg->prog_mtx); for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++) if (!array_Get(prg->prog_fds, i)) { - printf("%d) %s!!! %d ret=%d\n", i, __func__, execNum, ret); f = e_popen(prg->prog_name, "r+", &pid); #ifdef POPEN_STREAM if (!f) { @@ -271,7 +372,7 @@ io_progOpen(prog_t * __restrict prg, u_int execNum) ret = -1; break; } else - array_Set(prg->prog_fds, i, f); + array_Set(prg->prog_fds, i, (intptr_t) f); clrbit(prg->prog_used, i); prg->prog_cnum++; ret++; @@ -380,9 +481,9 @@ io_progCheck(prog_t * __restrict prg, int re) for (i = 0; i < array_Size(prg->prog_fds); i++) if (array_Get(prg->prog_fds, i) && #ifdef POPEN_STREAM - (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) + (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) { #else - (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) + (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) { #endif if (waitpid(p->pid, &p->stat, WNOHANG)) { clrbit(prg->prog_used, i); @@ -395,6 +496,7 @@ io_progCheck(prog_t * __restrict prg, int re) prg->prog_cnum--; ret++; } + } pthread_mutex_unlock(&prg->prog_mtx); /* resurrect to init number */