Annotation of libaitio/src/exec.c, revision 1.1.2.21
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:
1.1.2.9 misho 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:
1.1.2.1 misho 39: prg->prog_fds = array_Init(prg->prog_maxn);
40: if (!prg->prog_fds) {
41: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.1.2.9 misho 42: e_free(prg->prog_used);
1.1.2.1 misho 43: e_free(prg);
44: return NULL;
45: }
46:
47: pthread_mutex_init(&prg->prog_mtx, NULL);
1.1.2.12 misho 48: signal(SIGPIPE, SIG_IGN);
1.1.2.2 misho 49:
1.1.2.6 misho 50: if (io_progOpen(prg, prg->prog_inin) < 0) {
1.1.2.2 misho 51: io_progDestroy(&prg);
1.1.2.6 misho 52: prg = NULL;
53: }
1.1.2.1 misho 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:
1.1.2.9 misho 71: e_free((*pprg)->prog_used);
1.1.2.1 misho 72: array_Destroy(&(*pprg)->prog_fds);
73: pthread_mutex_destroy(&(*pprg)->prog_mtx);
1.1.2.13 misho 74: signal(SIGPIPE, SIG_DFL);
1.1.2.1 misho 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;
1.1.2.14 misho 92: struct tagPIOPID *p;
1.1.2.1 misho 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--)
1.1.2.14 misho 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);
1.1.2.11 misho 114: #ifdef POPEN_STREAM
1.1.2.6 misho 115: e_pclose(array(prg->prog_fds, i, FILE*));
1.1.2.11 misho 116: #else
117: e_pclose((int) array(prg->prog_fds, i, intptr_t));
118: #endif
1.1.2.1 misho 119: array_Del(prg->prog_fds, i, 0);
1.1.2.9 misho 120: clrbit(prg->prog_used, i);
1.1.2.1 misho 121: prg->prog_cnum--;
122: ret++;
123: }
124: pthread_mutex_unlock(&prg->prog_mtx);
125:
126: return ret;
127: }
128:
129: /*
1.1.2.14 misho 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: /*
1.1.2.18 misho 176: * io_progOpen2() - Start program from pool on first unused slot
177: *
178: * @prg = program pool
179: * return: -1 error, >-1 reside at slot
180: */
181: int
182: io_progOpen2(prog_t * __restrict prg)
183: {
184: #ifdef POPEN_STREAM
185: FILE *f = NULL;
186: #else
187: int f = -1;
188: #endif
189: int stat, ret = -1;
190: register int i;
191: pid_t pid;
192:
193: if (!prg)
194: return -1;
195: if (prg->prog_cnum + 1 > prg->prog_maxn) {
196: io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
197: return -1;
198: }
199:
200: pthread_mutex_lock(&prg->prog_mtx);
201: for (i = 0; i < array_Size(prg->prog_fds); i++)
202: if (!array_Get(prg->prog_fds, i)) {
203: f = e_popen(prg->prog_name, "r+", &pid);
204: #ifdef POPEN_STREAM
205: if (!f) {
206: #else
207: if (f == -1) {
208: #endif
209: LOGERR;
210: break;
211: } else if (waitpid(pid, &stat, WNOHANG)) {
212: io_SetErr(ECHILD, "Program with pid=%d exit with status %d",
213: pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1);
214: e_pclose(f);
215: break;
216: } else
217: array_Set(prg->prog_fds, i, f);
218: clrbit(prg->prog_used, i);
219: prg->prog_cnum++;
220: ret = i;
221: break;
222: }
223: pthread_mutex_unlock(&prg->prog_mtx);
224:
225: return ret;
226: }
227:
228: /*
1.1.2.1 misho 229: * io_progOpen() - Execute number of program(s)
230: *
231: * @prg = program pool
232: * @execNum = execute program(s) (0 max)
1.1.2.18 misho 233: * return: -1 error, >0 executed programs
1.1.2.1 misho 234: */
235: int
236: io_progOpen(prog_t * __restrict prg, u_int execNum)
237: {
1.1.2.11 misho 238: #ifdef POPEN_STREAM
1.1.2.1 misho 239: FILE *f;
1.1.2.11 misho 240: #else
241: int f;
242: #endif
1.1.2.4 misho 243: int stat, ret = 0;
1.1.2.1 misho 244: register int i;
1.1.2.4 misho 245: pid_t pid;
1.1.2.1 misho 246:
247: if (!prg)
1.1.2.18 misho 248: return -1;
1.1.2.8 misho 249: if (prg->prog_cnum + execNum > prg->prog_maxn) {
1.1.2.1 misho 250: io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
1.1.2.18 misho 251: return -1;
1.1.2.1 misho 252: }
253:
254: pthread_mutex_lock(&prg->prog_mtx);
255: for (i = 0; (execNum ? ret < execNum : 42) && i < array_Size(prg->prog_fds); i++)
256: if (!array_Get(prg->prog_fds, i)) {
1.1.2.6 misho 257: f = e_popen(prg->prog_name, "r+", &pid);
1.1.2.11 misho 258: #ifdef POPEN_STREAM
1.1.2.1 misho 259: if (!f) {
1.1.2.11 misho 260: #else
261: if (f == -1) {
262: #endif
1.1.2.1 misho 263: LOGERR;
1.1.2.5 misho 264: ret = -1;
1.1.2.1 misho 265: break;
1.1.2.6 misho 266: } else if (waitpid(pid, &stat, WNOHANG)) {
267: io_SetErr(ECHILD, "Program with pid=%d exit with status %d",
268: pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1);
1.1.2.18 misho 269: e_pclose(f);
1.1.2.5 misho 270: ret = -1;
1.1.2.4 misho 271: break;
1.1.2.1 misho 272: } else
273: array_Set(prg->prog_fds, i, f);
1.1.2.15 misho 274: clrbit(prg->prog_used, i);
1.1.2.1 misho 275: prg->prog_cnum++;
276: ret++;
277: }
278: pthread_mutex_unlock(&prg->prog_mtx);
279:
280: return ret;
281: }
282:
283: /*
1.1.2.8 misho 284: * io_progGrow() - Execute to number of programs in pool
285: *
286: * @prg = program pool
287: * @toNum = execute to number of programs (0 max)
1.1.2.17 misho 288: * return: 0 error or nothing to do,
289: * >0 executed programs and abs(<0) executed programs with logged error
1.1.2.8 misho 290: */
291: int
292: io_progGrow(prog_t * __restrict prg, u_int toNum)
293: {
294: if (!prg)
295: return 0;
296: if (toNum > prg->prog_maxn) {
297: io_SetErr(EINVAL, "Requested number for program execution is over pool's limit");
298: return 0;
299: }
300: if (!toNum)
301: toNum = prg->prog_maxn;
1.1.2.17 misho 302: if (toNum < prg->prog_inin)
303: toNum = prg->prog_inin;
304:
1.1.2.19 misho 305: if ((int) (toNum - prg->prog_cnum) < 1)
1.1.2.17 misho 306: return 0;
1.1.2.8 misho 307:
308: return io_progOpen(prg, toNum - prg->prog_cnum);
309: }
310:
311: /*
1.1.2.1 misho 312: * io_progVacuum() - Vacuum pool to running number of programs
313: *
314: * @prg = program pool
315: * @toNum = vacuum to number of programs (0 to init number)
316: * return: 0 error or >0 closed programs
317: */
318: int
319: io_progVacuum(prog_t * __restrict prg, u_int toNum)
320: {
321: register int i;
322: int ret = 0;
1.1.2.14 misho 323: struct tagPIOPID *p;
1.1.2.1 misho 324:
325: if (!prg)
326: return 0;
327: if (toNum > prg->prog_maxn) {
328: io_SetErr(EINVAL, "Requested number for close program is over pool's limit");
329: return 0;
330: }
331: if (!toNum)
332: toNum = prg->prog_inin;
333:
334: pthread_mutex_lock(&prg->prog_mtx);
335: for (i = array_Size(prg->prog_fds) - 1; prg->prog_cnum > toNum && i > -1; i--)
1.1.2.14 misho 336: if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i) &&
1.1.2.11 misho 337: #ifdef POPEN_STREAM
1.1.2.14 misho 338: (p = pio_pgetpid(array(prg->prog_fds, i, FILE*)))) {
339: kill(p->pid, SIGTERM);
340: usleep(1000);
341: if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
342: kill(p->pid, SIGKILL);
1.1.2.6 misho 343: e_pclose(array(prg->prog_fds, i, FILE*));
1.1.2.11 misho 344: #else
1.1.2.14 misho 345: (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t)))) {
346: kill(p->pid, SIGTERM);
347: usleep(1000);
348: if (waitpid(p->pid, &p->stat, WNOHANG) > 0)
349: kill(p->pid, SIGKILL);
1.1.2.11 misho 350: e_pclose((int) array(prg->prog_fds, i, intptr_t));
351: #endif
1.1.2.1 misho 352: array_Del(prg->prog_fds, i, 0);
353: prg->prog_cnum--;
354: ret++;
355: }
356: pthread_mutex_unlock(&prg->prog_mtx);
357:
358: return ret;
359: }
1.1.2.7 misho 360:
361: /*
362: * io_progCheck() - Check exit status of program pool
363: *
364: * @prg = program pool
1.1.2.18 misho 365: * @re = resurrect program to init number
1.1.2.7 misho 366: * return: -1 error or >-1 exited programs
367: */
368: int
1.1.2.15 misho 369: io_progCheck(prog_t * __restrict prg, int re)
1.1.2.7 misho 370: {
371: int ret = 0;
372: struct tagPIOPID *p;
373: register int i;
374:
375: if (!prg)
376: return -1;
377:
1.1.2.9 misho 378: pthread_mutex_lock(&prg->prog_mtx);
1.1.2.7 misho 379: for (i = 0; i < array_Size(prg->prog_fds); i++)
380: if (array_Get(prg->prog_fds, i) &&
1.1.2.11 misho 381: #ifdef POPEN_STREAM
1.1.2.7 misho 382: (p = pio_pgetpid(array(prg->prog_fds, i, FILE*))))
1.1.2.11 misho 383: #else
384: (p = pio_pgetpid((int) array(prg->prog_fds, i, intptr_t))))
385: #endif
1.1.2.20 misho 386: if (waitpid(p->pid, &p->stat, WNOHANG)) {
1.1.2.9 misho 387: clrbit(prg->prog_used, i);
1.1.2.15 misho 388: #ifdef POPEN_STREAM
389: e_pclose(array(prg->prog_fds, i, FILE*));
390: #else
391: e_pclose((int) array(prg->prog_fds, i, intptr_t));
392: #endif
393: array_Del(prg->prog_fds, i, 0);
394: prg->prog_cnum--;
1.1.2.7 misho 395: ret++;
1.1.2.9 misho 396: }
397: pthread_mutex_unlock(&prg->prog_mtx);
1.1.2.7 misho 398:
1.1.2.18 misho 399: /* resurrect to init number */
1.1.2.19 misho 400: if (re && ((int) (prg->prog_inin - prg->prog_cnum) > 0))
1.1.2.17 misho 401: io_progOpen(prg, prg->prog_inin - prg->prog_cnum);
1.1.2.15 misho 402:
1.1.2.7 misho 403: return ret;
404: }
1.1.2.9 misho 405:
406: /*
407: * io_progAttach() - Attach to open program
408: *
409: * @prg = program pool
1.1.2.16 misho 410: * @newOne = Execute new one program after attach
1.1.2.9 misho 411: * return: NULL error or !=NULL attached program handle
412: */
1.1.2.11 misho 413: #ifdef POPEN_STREAM
1.1.2.9 misho 414: FILE *
1.1.2.11 misho 415: #else
416: int
417: #endif
1.1.2.16 misho 418: io_progAttach(prog_t * __restrict prg, int newOne)
1.1.2.9 misho 419: {
1.1.2.11 misho 420: #ifdef POPEN_STREAM
1.1.2.9 misho 421: FILE *f = NULL;
1.1.2.11 misho 422: #else
423: int f = -1;
424: #endif
1.1.2.9 misho 425: register int i;
426:
427: if (!prg)
1.1.2.11 misho 428: #ifdef POPEN_STREAM
1.1.2.9 misho 429: return NULL;
1.1.2.11 misho 430: #else
431: return -1;
432: #endif
1.1.2.9 misho 433:
434: pthread_mutex_lock(&prg->prog_mtx);
435: for (i = 0; i < array_Size(prg->prog_fds); i++)
436: if (array_Get(prg->prog_fds, i) && isclr(prg->prog_used, i)) {
437: setbit(prg->prog_used, i);
1.1.2.11 misho 438: #ifdef POPEN_STREAM
1.1.2.9 misho 439: f = array(prg->prog_fds, i, FILE*);
1.1.2.11 misho 440: #else
441: f = array(prg->prog_fds, i, intptr_t);
442: #endif
1.1.2.9 misho 443: break;
444: }
445: pthread_mutex_unlock(&prg->prog_mtx);
446:
1.1.2.20 misho 447: /* execute new one program */
448: if (newOne) {
449: if (f)
450: io_progOpen(prg, 1);
451: else if ((i = io_progOpen2(prg)) > -1)
452: /* not found free program */
1.1.2.18 misho 453: #ifdef POPEN_STREAM
1.1.2.20 misho 454: f = array(prg->prog_fds, i, FILE*);
1.1.2.18 misho 455: #else
1.1.2.20 misho 456: f = array(prg->prog_fds, i, intptr_t);
1.1.2.18 misho 457: #endif
458: }
459:
1.1.2.9 misho 460: return f;
461: }
462:
463: /*
464: * io_progDetach() - Detch from open program
465: *
466: * @prg= program pool
467: * @pfd = attached program handle
468: * return: none
469: */
470: void
1.1.2.11 misho 471: #ifdef POPEN_STREAM
1.1.2.9 misho 472: io_progDetach(prog_t * __restrict prg, FILE *pfd)
1.1.2.11 misho 473: #else
474: io_progDetach(prog_t * __restrict prg, int pfd)
475: #endif
1.1.2.9 misho 476: {
477: register int i;
478:
479: if (!prg || !pfd)
480: return;
481:
482: pthread_mutex_lock(&prg->prog_mtx);
483: for (i = 0; i < array_Size(prg->prog_fds); i++)
1.1.2.11 misho 484: #ifdef POPEN_STREAM
1.1.2.9 misho 485: if (array(prg->prog_fds, i, FILE*) == pfd) {
1.1.2.11 misho 486: #else
487: if (array(prg->prog_fds, i, intptr_t) == pfd) {
488: #endif
1.1.2.9 misho 489: clrbit(prg->prog_used, i);
490: break;
491: }
492: pthread_mutex_unlock(&prg->prog_mtx);
493: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>