Annotation of libaitio/src/sock.c, revision 1.11.2.7
1.2 misho 1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.11.2.7! misho 6: * $Id: sock.c,v 1.11.2.6 2013/12/15 22:31:44 misho Exp $
1.2 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
1.5 misho 49: static void *
50: io_closeClient(sched_task_t *task)
51: {
52: sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task);
53: sock_t *s = (sock_t*) cli->cli_parent;
1.7 misho 54: int stat;
1.5 misho 55:
56: pthread_mutex_lock(&s->sock_mtx);
57: TAILQ_REMOVE(&s->sock_cli, cli, cli_node);
58: pthread_mutex_unlock(&s->sock_mtx);
59:
60: schedCancelby(s->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
61:
1.9 misho 62: if (*cli->cli_name)
63: ioFreePTY(cli->cli_pty, cli->cli_name);
1.11.2.3 misho 64: if (s->sock_prog) {
65: io_progDetach(s->sock_prog, cli->cli_pty);
1.11.2.6 misho 66: #if 0
67: io_progCloseAt(s->sock_prog, cli->cli_pty);
68: #endif
1.11.2.3 misho 69: cli->cli_pty ^= cli->cli_pty;
1.11.2.4 misho 70: io_progCheck(s->sock_prog, 42);
1.11.2.3 misho 71: }
1.8 misho 72:
1.5 misho 73: if (s->sock_type == SOCK_STREAM) {
1.7 misho 74: shutdown(cli->cli_fd, SHUT_RDWR);
75: close(cli->cli_fd);
1.5 misho 76: }
77: AIT_FREE_VAL(&cli->cli_buf[1]);
78: AIT_FREE_VAL(&cli->cli_buf[0]);
79:
80: if (cli->cli_pid > 0) {
1.7 misho 81: kill(cli->cli_pid, SIGKILL);
1.5 misho 82: while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
83: usleep(1000);
1.7 misho 84: kill(cli->cli_pid, SIGKILL);
1.5 misho 85: }
86: }
87:
88: e_free(cli);
89: taskExit(task, NULL);
90: }
91:
92: static void *
93: io_acceptClient(sched_task_t *task)
94: {
95: int c, rlen;
96: sockaddr_t sa;
97: socklen_t salen = sizeof sa.ss;
98: sock_cli_t *cli = NULL;
99: sock_t *s = (sock_t*) TASK_ARG(task);
100:
101: if (s->sock_type == SOCK_STREAM) {
102: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
103: LOGERR;
104: goto end;
105: }
106: } else {
107: if ((rlen = recvfrom(TASK_FD(task),
108: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
109: MSG_PEEK, &sa.sa, &salen)) == -1) {
110: LOGERR;
111: goto end;
112: } else
113: c = TASK_FD(task);
114: }
115:
116: cli = e_malloc(sizeof(sock_cli_t));
117: if (!cli) {
118: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
119: if (s->sock_type == SOCK_STREAM)
120: close(c);
121: goto end;
122: } else {
123: memset(cli, 0, sizeof(sock_cli_t));
124: pthread_mutex_lock(&s->sock_mtx);
125: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
126: pthread_mutex_unlock(&s->sock_mtx);
127: }
128:
129: cli->cli_parent = TASK_ARG(task);
130: cli->cli_fd = c;
131: cli->cli_func = TASK_DATA(task);
132: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
133: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
134: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
135:
136: schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0);
1.7 misho 137: ioUpdTimerSocket(cli);
1.5 misho 138: end:
139: schedReadSelf(task);
140: taskExit(task, NULL);
141: }
142:
143: static void *
144: io_txNet(sched_task_t *task)
145: {
1.10 misho 146: int wlen, ret, len = TASK_DATLEN(task);
1.5 misho 147: sock_cli_t *cli = TASK_ARG(task);
148: sock_t *s = (sock_t*) cli->cli_parent;
1.10 misho 149: u_char *buf = TASK_DATA(task);
150: struct pollfd pfd[1];
1.5 misho 151:
1.10 misho 152: pfd->fd = TASK_FD(task);
153: pfd->events = POLLOUT;
154: pfd->revents = 0;
155: for(; len > 0; len -= wlen, buf += wlen) {
156: ioUpdTimerSocket(cli);
157:
158: if ((ret = poll(pfd, 1, s->sock_timeout.tv_sec * 1000)) < 1 ||
159: pfd->revents & (POLLNVAL | POLLERR | POLLHUP)) {
1.11.2.5 misho 160: #if 0
1.10 misho 161: if (!ret)
162: continue;
1.11.2.5 misho 163: #endif
1.10 misho 164: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
165: break;
166: }
1.5 misho 167:
1.10 misho 168: if (s->sock_type == SOCK_STREAM)
169: wlen = send(TASK_FD(task), buf, len, 0);
170: else
171: wlen = sendto(TASK_FD(task), buf, len, 0,
172: &cli->cli_addr.sa, cli->cli_addr.sa.sa_len);
173: if (wlen < 1) {
174: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
175: break;
176: }
177: }
1.5 misho 178:
179: taskExit(task, NULL);
180: }
181:
182: static void *
183: io_txPty(sched_task_t *task)
184: {
185: int wlen;
186: sock_cli_t *cli = TASK_ARG(task);
187:
1.7 misho 188: ioUpdTimerSocket(cli);
1.5 misho 189:
190: wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task));
191: if (wlen < 1)
1.7 misho 192: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5 misho 193:
194: taskExit(task, NULL);
195: }
196:
197: static void *
198: io_rxNet(sched_task_t *task)
199: {
200: int rlen;
201: sock_cli_t *cli = TASK_ARG(task);
202: sock_t *s = (sock_t*) cli->cli_parent;
203: sockaddr_t sa;
204: socklen_t salen = sizeof sa.ss;
205:
1.7 misho 206: ioUpdTimerSocket(cli);
1.5 misho 207:
208: if (s->sock_type == SOCK_STREAM)
209: rlen = recv(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
210: AIT_LEN(&cli->cli_buf[0]), 0);
211: else {
212: rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
213: AIT_LEN(&cli->cli_buf[0]), 0, &sa.sa, &salen);
214: if (e_addrcmp(&cli->cli_addr, &sa, 42))
215: goto end;
216: }
217: if (rlen < 1)
1.7 misho 218: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5 misho 219: else
220: schedEvent(TASK_ROOT(task), io_txPty, cli, cli->cli_pty,
221: AIT_GET_BUF(&cli->cli_buf[0]), rlen);
222: end:
223: schedReadSelf(task);
224: taskExit(task, NULL);
225: }
226:
227: static void *
228: io_rxPty(sched_task_t *task)
229: {
230: int rlen;
231: sock_cli_t *cli = TASK_ARG(task);
232:
1.7 misho 233: ioUpdTimerSocket(cli);
1.5 misho 234:
235: rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1]));
236: if (rlen < 1)
1.7 misho 237: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5 misho 238: else
239: schedEvent(TASK_ROOT(task), io_txNet, cli, cli->cli_fd,
240: AIT_GET_BUF(&cli->cli_buf[1]), rlen);
241:
242: schedReadSelf(task);
243: taskExit(task, NULL);
244: }
245:
246: static void *
247: io_bridgeClient(sched_task_t *task)
248: {
249: int c, rlen;
250: pid_t pid;
251: sockaddr_t sa;
252: socklen_t salen = sizeof sa.ss;
253: sock_cli_t *cli = NULL;
254: sock_t *s = (sock_t*) TASK_ARG(task);
255: array_t *args = NULL;
256: char **argv = NULL;
257:
258: if (s->sock_type == SOCK_STREAM) {
259: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
260: LOGERR;
261: goto end;
262: }
263: } else {
264: if ((rlen = recvfrom(TASK_FD(task),
265: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
266: MSG_PEEK, &sa.sa, &salen)) == -1) {
267: LOGERR;
268: goto end;
269: } else
270: c = TASK_FD(task);
271: }
272:
273: cli = e_malloc(sizeof(sock_cli_t));
274: if (!cli) {
275: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
276: if (s->sock_type == SOCK_STREAM)
277: close(c);
278: goto end;
279: } else {
280: memset(cli, 0, sizeof(sock_cli_t));
281: pthread_mutex_lock(&s->sock_mtx);
282: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
283: pthread_mutex_unlock(&s->sock_mtx);
284: }
285:
286: cli->cli_parent = TASK_ARG(task);
287: cli->cli_fd = c;
288: strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
289: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
290: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
291: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
292:
293: switch ((pid = ioForkPTY(&cli->cli_pty, cli->cli_name, sizeof cli->cli_name,
294: NULL, NULL, NULL))) {
295: case -1:
296: ELIBERR(io);
297: break;
298: case 0:
299: array_Args(cli->cli_cmdline, 0, " \t", &args);
300: argv = array_To(args);
301: array_Destroy(&args);
302:
1.11 misho 303: #if 0
1.5 misho 304: printf("Console %s\n", cli->cli_name);
1.11 misho 305: #endif
1.6 misho 306: rlen = execv(*argv, argv);
307: _exit(rlen);
1.5 misho 308: break;
309: default:
310: cli->cli_pid = pid;
311:
312: schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty,
313: TASK_ARG(task), 0);
314: schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd,
315: TASK_ARG(task), 0);
1.7 misho 316: ioUpdTimerSocket(cli);
1.5 misho 317: break;
318: }
319: end:
320: schedReadSelf(task);
321: taskExit(task, NULL);
322: }
323:
1.11.2.3 misho 324: static void *
325: io_bridgeClient2Pool(sched_task_t *task)
326: {
327: int c, rlen;
328: sockaddr_t sa;
329: socklen_t salen = sizeof sa.ss;
330: sock_cli_t *cli = NULL;
331: sock_t *s = (sock_t*) TASK_ARG(task);
332:
333: if (s->sock_type == SOCK_STREAM) {
334: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
335: LOGERR;
336: goto end;
337: }
338: } else {
339: if ((rlen = recvfrom(TASK_FD(task),
340: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
341: MSG_PEEK, &sa.sa, &salen)) == -1) {
342: LOGERR;
343: goto end;
344: } else
345: c = TASK_FD(task);
346: }
347:
348: cli = e_malloc(sizeof(sock_cli_t));
349: if (!cli) {
350: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
351: if (s->sock_type == SOCK_STREAM)
352: close(c);
353: goto end;
354: } else {
355: memset(cli, 0, sizeof(sock_cli_t));
356: pthread_mutex_lock(&s->sock_mtx);
357: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
358: pthread_mutex_unlock(&s->sock_mtx);
359: }
360:
1.11.2.6 misho 361: io_progCheck(s->sock_prog, 42);
362:
1.11.2.3 misho 363: cli->cli_parent = TASK_ARG(task);
364: cli->cli_fd = c;
365: cli->cli_pty = io_progAttach(s->sock_prog, 42);
366: strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
367: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
368: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
369: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
370:
371: schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty, TASK_ARG(task), 0);
372: schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, TASK_ARG(task), 0);
373: ioUpdTimerSocket(cli);
374: end:
375: schedReadSelf(task);
376: taskExit(task, NULL);
377: }
378:
1.5 misho 379:
1.2 misho 380: /*
381: * ioInitSocket() - Init socket and allocate resources
382: *
383: * @role = Socket role
384: * @type = Socket type
385: * @proto = Socket protocol
386: * @addr = Bind to address
387: * @port = Bind to port
388: * @buflen = Socket buffer, optional if =0 == BUFSIZ
389: * return: NULL error or !=NULL created socket
390: */
391: sock_t *
392: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
393: {
394: sock_t *s = NULL;
395: int n = 1;
396:
397: if (!addr)
398: return NULL;
399:
400: s = e_malloc(sizeof(sock_t));
401: if (!s) {
402: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
403: return NULL;
404: } else
405: memset(s, 0, sizeof(sock_t));
406:
1.3 misho 407: TAILQ_INIT(&s->sock_cli);
408:
1.2 misho 409: s->sock_role = role;
410: s->sock_type = type;
411: s->sock_proto = proto;
412: if (!e_gethostbyname(addr, port, &s->sock_addr)) {
413: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
414: e_free(s);
415: return NULL;
416: } else {
417: buflen = buflen ? buflen : BUFSIZ;
1.5 misho 418: buflen = E_ALIGN(buflen, 2); /* align buflen length */
1.2 misho 419: AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
420: }
421:
422: s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
423: if (s->sock_fd == -1) {
424: LOGERR;
425: AIT_FREE_VAL(&s->sock_buf);
426: e_free(s);
427: return NULL;
428: }
429: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
430: LOGERR;
431: AIT_FREE_VAL(&s->sock_buf);
432: e_free(s);
433: return NULL;
434: }
435: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
436: LOGERR;
437: AIT_FREE_VAL(&s->sock_buf);
438: e_free(s);
439: return NULL;
440: }
441: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
442: LOGERR;
443: AIT_FREE_VAL(&s->sock_buf);
444: e_free(s);
445: return NULL;
446: }
447: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
448: LOGERR;
449: AIT_FREE_VAL(&s->sock_buf);
450: e_free(s);
451: return NULL;
452: }
453:
1.5 misho 454: s->sock_root = schedBegin();
455: if (!s->sock_root) {
456: io_SetErr(sched_GetErrno(), "%s", sched_GetError());
457: AIT_FREE_VAL(&s->sock_buf);
458: e_free(s);
459: return NULL;
460: }
461:
1.3 misho 462: pthread_mutex_init(&s->sock_mtx, NULL);
1.2 misho 463: return s;
464: }
465:
466: /*
467: * ioCloseSocket() - Close socket and free resources
468: *
469: * @s = Socket
470: * return: none
471: */
472: void
473: ioCloseSocket(sock_t ** __restrict s)
474: {
1.5 misho 475: sock_cli_t *cli;
476: int stat;
1.3 misho 477:
1.2 misho 478: if (s && *s) {
1.3 misho 479: pthread_mutex_lock(&(*s)->sock_mtx);
480: while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
481: TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
1.5 misho 482:
483: schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
484:
485: if ((*s)->sock_type == SOCK_STREAM) {
486: shutdown(cli->cli_fd, SHUT_RDWR);
487: close(cli->cli_fd);
488: }
489: AIT_FREE_VAL(&cli->cli_buf[1]);
490: AIT_FREE_VAL(&cli->cli_buf[0]);
491:
492: if (cli->cli_pid > 0) {
1.7 misho 493: kill(cli->cli_pid, SIGKILL);
1.5 misho 494: while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
495: usleep(1000);
1.7 misho 496: kill(cli->cli_pid, SIGKILL);
1.5 misho 497: }
498: }
499:
1.3 misho 500: e_free(cli);
501: }
502: pthread_mutex_unlock(&(*s)->sock_mtx);
503:
1.2 misho 504: shutdown((*s)->sock_fd, SHUT_RDWR);
505: close((*s)->sock_fd);
506:
507: AIT_FREE_VAL(&(*s)->sock_buf);
1.3 misho 508:
1.5 misho 509: schedEnd(&(*s)->sock_root);
510:
1.3 misho 511: pthread_mutex_destroy(&(*s)->sock_mtx);
1.2 misho 512: e_free(*s);
513: *s = NULL;
514: }
515: }
516:
517: /*
518: * ioUpSocket() - Setup socket for use
519: *
520: * @s = Socket
521: * @arg = Server role = listen backlog queue and Client role = peer address
1.5 misho 522: * @timeout = Socket timeout in sec (default -1 infinit)
1.2 misho 523: * return: -1 error or 0 ok
524: */
525: int
1.5 misho 526: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
1.2 misho 527: {
528: int ret = 0;
529: sockaddr_t *peer = (sockaddr_t*) arg;
530: uintptr_t backlog = (uintptr_t) arg;
531:
532: if (!s || !arg)
533: return -1;
1.5 misho 534: else {
535: s->sock_timeout.tv_sec = timeout;
536: s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
537: schedPolling(s->sock_root, &s->sock_timeout, NULL);
538: }
1.2 misho 539:
540: switch (s->sock_role) {
541: case IO_SOCK_ROLE_CLIENT:
542: memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
543:
544: if (connect(s->sock_fd, &s->sock_peer.sa,
545: s->sock_peer.sa.sa_len) == -1) {
546: LOGERR;
547: return -1;
548: }
549: break;
550: case IO_SOCK_ROLE_SERVER:
551: if (s->sock_type == SOCK_STREAM) {
552: s->sock_backq = backlog;
553:
554: if (listen(s->sock_fd, s->sock_backq) == -1) {
555: LOGERR;
556: return -1;
557: }
558: }
559: break;
560: default:
561: io_SetErr(EINVAL, "Unsupported socket type");
562: return -1;
563: }
564:
565: fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
566: return ret;
567: }
1.3 misho 568:
1.5 misho 569: /*
570: * ioUpdTimerSocket() - Update timeout of socket
571: *
572: * @c = Client socket
573: * return: none
574: */
575: void
1.7 misho 576: ioUpdTimerSocket(sock_cli_t * __restrict c)
1.3 misho 577: {
1.5 misho 578: sock_t *s;
1.3 misho 579:
1.5 misho 580: if (!c)
581: return;
582: else
583: s = c->cli_parent;
1.3 misho 584:
1.11.2.3 misho 585: if (s->sock_prog)
586: io_progCheck(s->sock_prog, 42);
587:
1.7 misho 588: schedCancelby(s->sock_root, taskTIMER, CRITERIA_ARG, c, NULL);
589: schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, 0);
1.3 misho 590: }
591:
1.5 misho 592: /*
593: * ioCloseClient() - Close client socket
594: *
595: * @c = Client socket
596: * return: 0 ok or !=0 error
597: */
598: int
599: ioCloseClient(sock_cli_t * __restrict c)
1.3 misho 600: {
1.5 misho 601: sock_t *s;
1.3 misho 602:
1.5 misho 603: if (!c)
604: return -1;
605: else
606: s = c->cli_parent;
1.3 misho 607:
1.7 misho 608: return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, 0);
1.3 misho 609: }
610:
611: /*
1.5 misho 612: * ioLoopSocket() - Start socket scheduler
1.3 misho 613: *
614: * @s = Socket
1.5 misho 615: * @rcb = Read callback
616: * return: -1 error or return result from scheduler
1.3 misho 617: */
618: int
1.5 misho 619: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
1.3 misho 620: {
1.5 misho 621: if (!s || !rcb || s->sock_kill)
1.3 misho 622: return -1;
623:
1.5 misho 624: schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
625: return schedRun(s->sock_root, &s->sock_kill);
626: }
1.3 misho 627:
1.11.2.7! misho 628: static void *
! 629: io_progPurge(sched_task_t *task)
! 630: {
! 631: sock_t *s = (sock_t*) TASK_ARG(task);
! 632:
! 633: io_progVacuum(s->sock_prog, 0);
! 634:
! 635: schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task),
! 636: s->sock_timeout, TASK_DATA(task), TASK_DATLEN(task));
! 637: taskExit(task, NULL);
! 638: }
! 639:
1.5 misho 640: /*
641: * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
642: *
643: * @s = Socket
644: * @prgname = Program name
645: * return: 0 ok or !=0 error
646: */
647: int
648: ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname)
649: {
650: if (!s || !prgname || s->sock_kill)
651: return -1;
1.3 misho 652:
1.11.2.7! misho 653: if (s->sock_prog) {
1.11.2.6 misho 654: schedRead(s->sock_root, io_bridgeClient2Pool,
655: s, s->sock_fd, (void*) prgname, 0);
1.11.2.7! misho 656: schedTimer(s->sock_root, io_progPurge, s, s->sock_timeout, NULL, 0);
! 657: } else
1.11.2.6 misho 658: schedRead(s->sock_root, io_bridgeClient,
659: s, s->sock_fd, (void*) prgname, 0);
1.5 misho 660: return schedRun(s->sock_root, &s->sock_kill);
1.3 misho 661: }
1.11.2.1 misho 662:
663: /*
1.11.2.2 misho 664: * ioSetupProg2Socket() - Setup program pool to socket server
1.11.2.1 misho 665: *
666: * @s = Socket
667: * @p = Program pool
668: * return: -1 error or 0 ok
669: */
670: int
1.11.2.2 misho 671: ioSetupProg2Socket(sock_t * __restrict s, prog_t * __restrict p)
1.11.2.1 misho 672: {
673: if (!s)
674: return -1;
675:
676: s->sock_prog = p;
677: return 0;
678: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>