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