version 1.1.2.14, 2013/12/08 20:43:22
|
version 1.1.2.23, 2013/12/15 22:48:04
|
Line 173 io_progCloseAt(prog_t * __restrict prg, u_int idx)
|
Line 173 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 |
|
* return: -1 error, >-1 reside at slot |
|
*/ |
|
int |
|
io_progOpen2(prog_t * __restrict prg) |
|
{ |
|
#ifdef POPEN_STREAM |
|
FILE *f = NULL; |
|
#else |
|
int f = -1; |
|
#endif |
|
int stat, ret = -1; |
|
register int i; |
|
pid_t pid; |
|
|
|
if (!prg) |
|
return -1; |
|
if (prg->prog_cnum + 1 > prg->prog_maxn) { |
|
io_SetErr(EINVAL, "Requested number for program execution is over pool's limit"); |
|
return -1; |
|
} |
|
|
|
pthread_mutex_lock(&prg->prog_mtx); |
|
for (i = 0; i < array_Size(prg->prog_fds); i++) |
|
if (!array_Get(prg->prog_fds, i)) { |
|
f = e_popen(prg->prog_name, "r+", &pid); |
|
#ifdef POPEN_STREAM |
|
if (!f) { |
|
#else |
|
if (f == -1) { |
|
#endif |
|
LOGERR; |
|
break; |
|
} else if (waitpid(pid, &stat, WNOHANG)) { |
|
io_SetErr(ECHILD, "Program with pid=%d exit with status %d", |
|
pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1); |
|
e_pclose(f); |
|
break; |
|
} else |
|
array_Set(prg->prog_fds, i, f); |
|
clrbit(prg->prog_used, i); |
|
prg->prog_cnum++; |
|
ret = i; |
|
break; |
|
} |
|
pthread_mutex_unlock(&prg->prog_mtx); |
|
|
|
return ret; |
|
} |
|
|
|
/* |
* io_progOpen() - Execute number of program(s) |
* io_progOpen() - Execute number of program(s) |
* |
* |
* @prg = program pool |
* @prg = program pool |
* @execNum = execute program(s) (0 max) |
* @execNum = execute program(s) (0 max) |
* return: 0 error, >0 executed programs and abs(<0) executed programs with logged error | * return: -1 error, >0 executed programs |
*/ |
*/ |
int |
int |
io_progOpen(prog_t * __restrict prg, u_int execNum) |
io_progOpen(prog_t * __restrict prg, u_int execNum) |
Line 192 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
Line 302 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
pid_t pid; |
pid_t pid; |
|
|
if (!prg) |
if (!prg) |
return 0; | return -1; |
if (prg->prog_cnum + execNum > prg->prog_maxn) { |
if (prg->prog_cnum + execNum > prg->prog_maxn) { |
io_SetErr(EINVAL, "Requested number for program execution is over pool's limit"); |
io_SetErr(EINVAL, "Requested number for program execution is over pool's limit"); |
return 0; | return -1; |
} |
} |
|
|
pthread_mutex_lock(&prg->prog_mtx); |
pthread_mutex_lock(&prg->prog_mtx); |
Line 213 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
Line 323 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
} else if (waitpid(pid, &stat, WNOHANG)) { |
} else if (waitpid(pid, &stat, WNOHANG)) { |
io_SetErr(ECHILD, "Program with pid=%d exit with status %d", |
io_SetErr(ECHILD, "Program with pid=%d exit with status %d", |
pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1); |
pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1); |
|
e_pclose(f); |
ret = -1; |
ret = -1; |
break; |
break; |
} else |
} else |
array_Set(prg->prog_fds, i, f); |
array_Set(prg->prog_fds, i, f); |
|
clrbit(prg->prog_used, i); |
prg->prog_cnum++; |
prg->prog_cnum++; |
ret++; |
ret++; |
} |
} |
Line 230 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
Line 342 io_progOpen(prog_t * __restrict prg, u_int execNum)
|
* |
* |
* @prg = program pool |
* @prg = program pool |
* @toNum = execute to number of programs (0 max) |
* @toNum = execute to number of programs (0 max) |
* return: 0 error, >0 executed programs and abs(<0) executed programs with logged error | * return: 0 error or nothing to do, |
| * >0 executed programs and abs(<0) executed programs with logged error |
*/ |
*/ |
int |
int |
io_progGrow(prog_t * __restrict prg, u_int toNum) |
io_progGrow(prog_t * __restrict prg, u_int toNum) |
Line 243 io_progGrow(prog_t * __restrict prg, u_int toNum)
|
Line 356 io_progGrow(prog_t * __restrict prg, u_int toNum)
|
} |
} |
if (!toNum) |
if (!toNum) |
toNum = prg->prog_maxn; |
toNum = prg->prog_maxn; |
|
if (toNum < prg->prog_inin) |
|
toNum = prg->prog_inin; |
|
|
|
if ((int) (toNum - prg->prog_cnum) < 1) |
|
return 0; |
|
|
return io_progOpen(prg, toNum - prg->prog_cnum); |
return io_progOpen(prg, toNum - prg->prog_cnum); |
} |
} |
|
|
Line 301 io_progVacuum(prog_t * __restrict prg, u_int toNum)
|
Line 419 io_progVacuum(prog_t * __restrict prg, u_int toNum)
|
* io_progCheck() - Check exit status of program pool |
* io_progCheck() - Check exit status of program pool |
* |
* |
* @prg = program pool |
* @prg = program pool |
|
* @re = resurrect program to init number |
* return: -1 error or >-1 exited programs |
* return: -1 error or >-1 exited programs |
*/ |
*/ |
int |
int |
io_progCheck(prog_t * __restrict prg) | io_progCheck(prog_t * __restrict prg, int re) |
{ |
{ |
int ret = 0; |
int ret = 0; |
struct tagPIOPID *p; |
struct tagPIOPID *p; |
Line 317 io_progCheck(prog_t * __restrict prg)
|
Line 436 io_progCheck(prog_t * __restrict prg)
|
for (i = 0; i < array_Size(prg->prog_fds); i++) |
for (i = 0; i < array_Size(prg->prog_fds); i++) |
if (array_Get(prg->prog_fds, i) && |
if (array_Get(prg->prog_fds, i) && |
#ifdef POPEN_STREAM |
#ifdef POPEN_STREAM |
(p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) | (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) { |
#else |
#else |
(p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) | (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) { |
#endif |
#endif |
if (waitpid(p->pid, &p->stat, WNOHANG) > 0) { | if (waitpid(p->pid, &p->stat, WNOHANG)) { |
clrbit(prg->prog_used, i); |
clrbit(prg->prog_used, i); |
|
#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); |
|
prg->prog_cnum--; |
ret++; |
ret++; |
} |
} |
|
} |
pthread_mutex_unlock(&prg->prog_mtx); |
pthread_mutex_unlock(&prg->prog_mtx); |
|
|
|
/* resurrect to init number */ |
|
if (re && ((int) (prg->prog_inin - prg->prog_cnum) > 0)) |
|
io_progOpen(prg, prg->prog_inin - prg->prog_cnum); |
|
|
return ret; |
return ret; |
} |
} |
|
|
Line 334 io_progCheck(prog_t * __restrict prg)
|
Line 465 io_progCheck(prog_t * __restrict prg)
|
* io_progAttach() - Attach to open program |
* io_progAttach() - Attach to open program |
* |
* |
* @prg = program pool |
* @prg = program pool |
|
* @newOne = Execute new one program after attach |
* return: NULL error or !=NULL attached program handle |
* return: NULL error or !=NULL attached program handle |
*/ |
*/ |
#ifdef POPEN_STREAM |
#ifdef POPEN_STREAM |
Line 341 FILE *
|
Line 473 FILE *
|
#else |
#else |
int |
int |
#endif |
#endif |
io_progAttach(prog_t * __restrict prg) | io_progAttach(prog_t * __restrict prg, int newOne) |
{ |
{ |
#ifdef POPEN_STREAM |
#ifdef POPEN_STREAM |
FILE *f = NULL; |
FILE *f = NULL; |
Line 369 io_progAttach(prog_t * __restrict prg)
|
Line 501 io_progAttach(prog_t * __restrict prg)
|
break; |
break; |
} |
} |
pthread_mutex_unlock(&prg->prog_mtx); |
pthread_mutex_unlock(&prg->prog_mtx); |
|
|
|
/* execute new one program */ |
|
if (newOne) { |
|
if (f) |
|
io_progOpen(prg, 1); |
|
else if ((i = io_progOpen2(prg)) > -1) |
|
/* not found free program */ |
|
#ifdef POPEN_STREAM |
|
f = array(prg->prog_fds, i, FILE*); |
|
#else |
|
f = array(prg->prog_fds, i, intptr_t); |
|
#endif |
|
} |
|
|
return f; |
return f; |
} |
} |