Annotation of libaitio/src/exec.c, revision 1.1.2.1
1.1.2.1 ! misho 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_fds = array_Init(prg->prog_maxn);
! 32: if (!prg->prog_fds) {
! 33: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
! 34: e_free(prg);
! 35: return NULL;
! 36: }
! 37:
! 38: pthread_mutex_init(&prg->prog_mtx, NULL);
! 39: return prg;
! 40: }
! 41:
! 42: /*
! 43: * io_progDestroy() - Destroy entire program pool
! 44: *
! 45: * @pprg = program pool
! 46: * return: none
! 47: */
! 48: void
! 49: io_progDestroy(prog_t ** __restrict pprg)
! 50: {
! 51: if (!pprg || !*pprg)
! 52: return;
! 53:
! 54: io_progClose(*pprg, 0);
! 55:
! 56: array_Destroy(&(*pprg)->prog_fds);
! 57: pthread_mutex_destroy(&(*pprg)->prog_mtx);
! 58:
! 59: e_free(*pprg);
! 60: *pprg = NULL;
! 61: }
! 62:
! 63: /*
! 64: * io_progClose() - Close all programs in pool
! 65: *
! 66: * @prg = program pool
! 67: * @closeNum = close program(s) (0 all)
! 68: * return: 0 error, >0 closed programs
! 69: */
! 70: int
! 71: io_progClose(prog_t * __restrict prg, u_int closeNum)
! 72: {
! 73: register int i;
! 74: int ret = 0;
! 75:
! 76: if (!prg)
! 77: return 0;
! 78: if (closeNum > prg->prog_maxn) {
! 79: io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
! 80: return 0;
! 81: }
! 82:
! 83: pthread_mutex_lock(&prg->prog_mtx);
! 84: for (i = array_Size(prg->prog_fds) - 1;
! 85: (closeNum ? ret < closeNum : 42) && i > -1; i--)
! 86: if (array_Get(prg->prog_fds, i)) {
! 87: pclose(array(prg->prog_fds, i, FILE*));
! 88: array_Del(prg->prog_fds, i, 0);
! 89: prg->prog_cnum--;
! 90: ret++;
! 91: }
! 92: pthread_mutex_unlock(&prg->prog_mtx);
! 93:
! 94: return ret;
! 95: }
! 96:
! 97: /*
! 98: * io_progOpen() - Execute number of program(s)
! 99: *
! 100: * @prg = program pool
! 101: * @execNum = execute program(s) (0 max)
! 102: * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error
! 103: */
! 104: int
! 105: io_progOpen(prog_t * __restrict prg, u_int execNum)
! 106: {
! 107: FILE *f;
! 108: int ret = 0;
! 109: register int i;
! 110:
! 111: if (!prg)
! 112: return 0;
! 113: if (execNum > prg->prog_maxn) {
! 114: io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
! 115: return 0;
! 116: }
! 117:
! 118: pthread_mutex_lock(&prg->prog_mtx);
! 119: for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++)
! 120: if (!array_Get(prg->prog_fds, i)) {
! 121: f = popen(prg->prog_name, "r+");
! 122: if (!f) {
! 123: LOGERR;
! 124: ret *= -1;
! 125: break;
! 126: } else
! 127: array_Set(prg->prog_fds, i, f);
! 128: prg->prog_cnum++;
! 129: ret++;
! 130: }
! 131: pthread_mutex_unlock(&prg->prog_mtx);
! 132:
! 133: return ret;
! 134: }
! 135:
! 136: /*
! 137: * io_progVacuum() - Vacuum pool to running number of programs
! 138: *
! 139: * @prg = program pool
! 140: * @toNum = vacuum to number of programs (0 to init number)
! 141: * return: 0 error or >0 closed programs
! 142: */
! 143: int
! 144: io_progVacuum(prog_t * __restrict prg, u_int toNum)
! 145: {
! 146: register int i;
! 147: int ret = 0;
! 148:
! 149: if (!prg)
! 150: return 0;
! 151: if (toNum > prg->prog_maxn) {
! 152: io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
! 153: return 0;
! 154: }
! 155: if (!toNum)
! 156: toNum = prg->prog_inin;
! 157:
! 158: pthread_mutex_lock(&prg->prog_mtx);
! 159: for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--)
! 160: if (array_Get(prg->prog_fds, i)) {
! 161: pclose(array(prg->prog_fds, i, FILE*));
! 162: array_Del(prg->prog_fds, i, 0);
! 163: prg->prog_cnum--;
! 164: ret++;
! 165: }
! 166: pthread_mutex_unlock(&prg->prog_mtx);
! 167:
! 168: return ret;
! 169: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>