File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / exec.c
Revision 1.1.2.16: download - view: text, annotated - select for diffs - revision graph
Sun Dec 8 21:11:54 2013 UTC (10 years, 6 months ago) by misho
Branches: io6_7
add new arg

    1: #include "global.h"
    2: 
    3: 
    4: /*
    5:  * io_progInit() - Init program pool
    6:  *
    7:  * @progName = program name for execution
    8:  * @initNum = initial started programs
    9:  * @maxNum = maximum started programs
   10:  * return: NULL error or !=NULL allocated pool (must destroied with io_progDestroy())
   11:  */
   12: prog_t *
   13: io_progInit(const char *progName, u_int initNum, u_int maxNum)
   14: {
   15: 	prog_t *prg = NULL;
   16: 
   17: 	if (initNum > maxNum)
   18: 		return NULL;
   19: 
   20: 	prg = e_malloc(sizeof(prog_t));
   21: 	if (!prg) {
   22: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
   23: 		return NULL;
   24: 	} else
   25: 		memset(prg, 0, sizeof(prog_t));
   26: 
   27: 	prg->prog_inin = initNum;
   28: 	prg->prog_maxn = maxNum;
   29: 	strlcpy(prg->prog_name, progName, sizeof prg->prog_name);
   30: 
   31: 	prg->prog_used = e_malloc(E_ALIGN(prg->prog_maxn, sizeof *prg->prog_used) / 
   32: 			sizeof *prg->prog_used);
   33: 	if (!prg->prog_used) {
   34: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
   35: 		e_free(prg);
   36: 		return NULL;
   37: 	}
   38: 
   39: 	prg->prog_fds = array_Init(prg->prog_maxn);
   40: 	if (!prg->prog_fds) {
   41: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
   42: 		e_free(prg->prog_used);
   43: 		e_free(prg);
   44: 		return NULL;
   45: 	}
   46: 
   47: 	pthread_mutex_init(&prg->prog_mtx, NULL);
   48: 	signal(SIGPIPE, SIG_IGN);
   49: 
   50: 	if (io_progOpen(prg, prg->prog_inin) < 0) {
   51: 		io_progDestroy(&prg);
   52: 		prg = NULL;
   53: 	}
   54: 	return prg;
   55: }
   56: 
   57: /*
   58:  * io_progDestroy() - Destroy entire program pool
   59:  *
   60:  * @pprg = program pool
   61:  * return: none
   62:  */
   63: void
   64: io_progDestroy(prog_t ** __restrict pprg)
   65: {
   66: 	if (!pprg || !*pprg)
   67: 		return;
   68: 
   69: 	io_progClose(*pprg, 0);
   70: 
   71: 	e_free((*pprg)->prog_used);
   72: 	array_Destroy(&(*pprg)->prog_fds);
   73: 	pthread_mutex_destroy(&(*pprg)->prog_mtx);
   74: 	signal(SIGPIPE, SIG_DFL);
   75: 
   76: 	e_free(*pprg);
   77: 	*pprg = NULL;
   78: }
   79: 
   80: /*
   81:  * io_progClose() - Close all programs in pool
   82:  *
   83:  * @prg = program pool
   84:  * @closeNum = close program(s) (0 all)
   85:  * return: 0 error, >0 closed programs
   86:  */
   87: int
   88: io_progClose(prog_t * __restrict prg, u_int closeNum)
   89: {
   90: 	register int i;
   91: 	int ret = 0;
   92: 	struct tagPIOPID *p;
   93: 
   94: 	if (!prg)
   95: 		return 0;
   96: 	if (closeNum > prg->prog_maxn) {
   97: 		io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
   98: 		return 0;
   99: 	}
  100: 
  101: 	pthread_mutex_lock(&prg->prog_mtx);
  102: 	for (i = array_Size(prg->prog_fds) - 1; 
  103: 			(closeNum ? ret < closeNum : 42) && i > -1; i--)
  104: 		if (array_Get(prg->prog_fds, i) && 
  105: #ifdef POPEN_STREAM
  106: 				(p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) {
  107: #else
  108: 				(p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) {
  109: #endif
  110: 			kill(p->pid, SIGTERM);
  111: 			usleep(1000);
  112: 			if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
  113: 				kill(p->pid, SIGKILL);
  114: #ifdef POPEN_STREAM
  115: 			e_pclose(array(prg->prog_fds, i, FILE*));
  116: #else
  117: 			e_pclose((int) array(prg->prog_fds, i, intptr_t));
  118: #endif
  119: 			array_Del(prg->prog_fds, i, 0);
  120: 			clrbit(prg->prog_used, i);
  121: 			prg->prog_cnum--;
  122: 			ret++;
  123: 		}
  124: 	pthread_mutex_unlock(&prg->prog_mtx);
  125: 
  126: 	return ret;
  127: }
  128: 
  129: /*
  130:  * io_progCloseAt() - Close program at pool of certain position
  131:  *
  132:  * @prg = program pool
  133:  * @idx = index at pool
  134:  * return: 0 error or !=0 closed program
  135:  */
  136: int
  137: io_progCloseAt(prog_t * __restrict prg, u_int idx)
  138: {
  139: 	int ret = 0;
  140: 	struct tagPIOPID *p;
  141: 
  142: 	if (!prg)
  143: 		return 0;
  144: 	if (idx > prg->prog_maxn) {
  145: 		io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
  146: 		return 0;
  147: 	}
  148: 
  149: 	pthread_mutex_lock(&prg->prog_mtx);
  150: 	if (array_Get(prg->prog_fds, idx) && 
  151: #ifdef POPEN_STREAM
  152: 			(p = pio_pgetpid(array(prg->prog_fds, idx, FILE*)))) {
  153: #else
  154: 			(p = pio_pgetpid((int) array(prg->prog_fds, idx, intptr_t)))) {
  155: #endif
  156: 		kill(p->pid, SIGTERM);
  157: 		usleep(1000);
  158: 		if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
  159: 			kill(p->pid, SIGKILL);
  160: #ifdef POPEN_STREAM
  161: 		e_pclose(array(prg->prog_fds, idx, FILE*));
  162: #else
  163: 		e_pclose((int) array(prg->prog_fds, idx, intptr_t));
  164: #endif
  165: 		array_Del(prg->prog_fds, idx, 0);
  166: 		clrbit(prg->prog_used, idx);
  167: 		prg->prog_cnum--;
  168: 		ret++;
  169: 	}
  170: 	pthread_mutex_unlock(&prg->prog_mtx);
  171: 
  172: 	return ret;
  173: }
  174: 
  175: /*
  176:  * io_progOpen() - Execute number of program(s)
  177:  *
  178:  * @prg = program pool
  179:  * @execNum = execute program(s) (0 max)
  180:  * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error
  181:  */
  182: int
  183: io_progOpen(prog_t * __restrict prg, u_int execNum)
  184: {
  185: #ifdef POPEN_STREAM
  186: 	FILE *f;
  187: #else
  188: 	int f;
  189: #endif
  190: 	int stat, ret = 0;
  191: 	register int i;
  192: 	pid_t pid;
  193: 
  194: 	if (!prg)
  195: 		return 0;
  196: 	if (prg->prog_cnum + execNum > prg->prog_maxn) {
  197: 		io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
  198: 		return 0;
  199: 	}
  200: 
  201: 	pthread_mutex_lock(&prg->prog_mtx);
  202: 	for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++)
  203: 		if (!array_Get(prg->prog_fds, i)) {
  204: 			f = e_popen(prg->prog_name, "r+", &pid);
  205: #ifdef POPEN_STREAM
  206: 			if (!f) {
  207: #else
  208: 			if (f == -1) {
  209: #endif
  210: 				LOGERR;
  211: 				ret = -1;
  212: 				break;
  213: 			} else if (waitpid(pid, &stat, WNOHANG)) {
  214: 				io_SetErr(ECHILD, "Program with pid=%d exit with status %d", 
  215: 						pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1);
  216: 				ret = -1;
  217: 				break;
  218: 			} else
  219: 				array_Set(prg->prog_fds, i, f);
  220: 			clrbit(prg->prog_used, i);
  221: 			prg->prog_cnum++;
  222: 			ret++;
  223: 		}
  224: 	pthread_mutex_unlock(&prg->prog_mtx);
  225: 
  226: 	return ret;
  227: }
  228: 
  229: /*
  230:  * io_progGrow() - Execute to number of programs in pool
  231:  *
  232:  * @prg = program pool
  233:  * @toNum = execute to number of programs (0 max)
  234:  * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error
  235:  */
  236: int
  237: io_progGrow(prog_t * __restrict prg, u_int toNum)
  238: {
  239: 	if (!prg)
  240: 		return 0;
  241: 	if (toNum > prg->prog_maxn) {
  242: 		io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
  243: 		return 0;
  244: 	}
  245: 	if (!toNum)
  246: 		toNum = prg->prog_maxn;
  247: 
  248: 	return io_progOpen(prg, toNum - prg->prog_cnum);
  249: }
  250: 
  251: /*
  252:  * io_progVacuum() - Vacuum pool to running number of programs
  253:  *
  254:  * @prg = program pool
  255:  * @toNum = vacuum to number of programs (0 to init number)
  256:  * return: 0 error or >0 closed programs
  257:  */
  258: int
  259: io_progVacuum(prog_t * __restrict prg, u_int toNum)
  260: {
  261: 	register int i;
  262: 	int ret = 0;
  263: 	struct tagPIOPID *p;
  264: 
  265: 	if (!prg)
  266: 		return 0;
  267: 	if (toNum > prg->prog_maxn) {
  268: 		io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
  269: 		return 0;
  270: 	}
  271: 	if (!toNum)
  272: 		toNum = prg->prog_inin;
  273: 
  274: 	pthread_mutex_lock(&prg->prog_mtx);
  275: 	for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--)
  276: 		if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i) && 
  277: #ifdef POPEN_STREAM
  278: 				(p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) {
  279: 			kill(p->pid, SIGTERM);
  280: 			usleep(1000);
  281: 			if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
  282: 				kill(p->pid, SIGKILL);
  283: 			e_pclose(array(prg->prog_fds, i, FILE*));
  284: #else
  285: 				(p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) {
  286: 			kill(p->pid, SIGTERM);
  287: 			usleep(1000);
  288: 			if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
  289: 				kill(p->pid, SIGKILL);
  290: 			e_pclose((int) array(prg->prog_fds, i, intptr_t));
  291: #endif
  292: 			array_Del(prg->prog_fds, i, 0);
  293: 			prg->prog_cnum--;
  294: 			ret++;
  295: 		}
  296: 	pthread_mutex_unlock(&prg->prog_mtx);
  297: 
  298: 	return ret;
  299: }
  300: 
  301: /*
  302:  * io_progCheck() - Check exit status of program pool
  303:  *
  304:  * @prg = program pool
  305:  * @re = resurrect program
  306:  * return: -1 error or >-1 exited programs
  307:  */
  308: int
  309: io_progCheck(prog_t * __restrict prg, int re)
  310: {
  311: 	int ret = 0;
  312: 	struct tagPIOPID *p;
  313: 	register int i;
  314: 
  315: 	if (!prg)
  316: 		return -1;
  317: 
  318: 	pthread_mutex_lock(&prg->prog_mtx);
  319: 	for (i = 0; i < array_Size(prg->prog_fds); i++)
  320: 		if (array_Get(prg->prog_fds, i) && 
  321: #ifdef POPEN_STREAM
  322: 				(p = pio_pgetpid(array(prg->prog_fds, i, FILE*))))
  323: #else
  324: 				(p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t))))
  325: #endif
  326: 			if (waitpid(p->pid, &p->stat, WNOHANG) > 0) {
  327: 				clrbit(prg->prog_used, i);
  328: #ifdef POPEN_STREAM
  329: 				e_pclose(array(prg->prog_fds, i, FILE*));
  330: #else
  331: 				e_pclose((int) array(prg->prog_fds, i, intptr_t));
  332: #endif
  333: 				array_Del(prg->prog_fds, i, 0);
  334: 				prg->prog_cnum--;
  335: 				ret++;
  336: 			}
  337: 	pthread_mutex_unlock(&prg->prog_mtx);
  338: 
  339: 	/* resurrect */
  340: 	if (re && ret > 0)
  341: 		io_progOpen(prg, ret);
  342: 
  343: 	return ret;
  344: }
  345: 
  346: /*
  347:  * io_progAttach() - Attach to open program
  348:  *
  349:  * @prg = program pool
  350:  * @newOne = Execute new one program after attach
  351:  * return: NULL error or !=NULL attached program handle
  352:  */
  353: #ifdef POPEN_STREAM
  354: FILE *
  355: #else
  356: int
  357: #endif
  358: io_progAttach(prog_t * __restrict prg, int newOne)
  359: {
  360: #ifdef POPEN_STREAM
  361: 	FILE *f = NULL;
  362: #else
  363: 	int f = -1;
  364: #endif
  365: 	register int i;
  366: 
  367: 	if (!prg)
  368: #ifdef POPEN_STREAM
  369: 		return NULL;
  370: #else
  371: 		return -1;
  372: #endif
  373: 
  374: 	pthread_mutex_lock(&prg->prog_mtx);
  375: 	for (i = 0; i < array_Size(prg->prog_fds); i++)
  376: 		if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i)) {
  377: 			setbit(prg->prog_used, i);
  378: #ifdef POPEN_STREAM
  379: 			f = array(prg->prog_fds, i, FILE*);
  380: #else
  381: 			f = array(prg->prog_fds, i, intptr_t);
  382: #endif
  383: 			break;
  384: 		}
  385: 	pthread_mutex_unlock(&prg->prog_mtx);
  386: 
  387: 	/* execute new one program */
  388: 	if (newOne && f)
  389: 		io_progOpen(prg, 1);
  390: 
  391: 	return f;
  392: }
  393: 
  394: /*
  395:  * io_progDetach() - Detch from open program
  396:  *
  397:  * @prg= program pool
  398:  * @pfd = attached program handle
  399:  * return: none
  400:  */
  401: void
  402: #ifdef POPEN_STREAM
  403: io_progDetach(prog_t * __restrict prg, FILE *pfd)
  404: #else
  405: io_progDetach(prog_t * __restrict prg, int pfd)
  406: #endif
  407: {
  408: 	register int i;
  409: 
  410: 	if (!prg || !pfd)
  411: 		return;
  412: 
  413: 	pthread_mutex_lock(&prg->prog_mtx);
  414: 	for (i = 0; i < array_Size(prg->prog_fds); i++)
  415: #ifdef POPEN_STREAM
  416: 		if (array(prg->prog_fds, i, FILE*) == pfd) {
  417: #else
  418: 		if (array(prg->prog_fds, i, intptr_t) == pfd) {
  419: #endif
  420: 			clrbit(prg->prog_used, i);
  421: 			break;
  422: 		}
  423: 	pthread_mutex_unlock(&prg->prog_mtx);
  424: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>