1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
6: * $Id: sock.c,v 1.16 2016/08/18 09:06:31 misho Exp $
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 - 2016
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:
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;
54: int stat;
55:
56: schedCancelby(s->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
57:
58: pthread_mutex_lock(&s->sock_mtx);
59: TAILQ_REMOVE(&s->sock_cli, cli, cli_node);
60: pthread_mutex_unlock(&s->sock_mtx);
61:
62: if (*cli->cli_name)
63: ioFreePTY(cli->cli_pty, cli->cli_name);
64: if (s->sock_prog) {
65: io_progDetach(s->sock_prog, cli->cli_pty);
66: #if 0
67: io_progCloseAt(s->sock_prog, cli->cli_pty);
68: #endif
69: cli->cli_pty ^= cli->cli_pty;
70: io_progCheck(s->sock_prog, 42);
71: }
72:
73: if (s->sock_type == SOCK_STREAM) {
74: shutdown(cli->cli_fd, SHUT_RDWR);
75: close(cli->cli_fd);
76: }
77: AIT_FREE_VAL(&cli->cli_buf[1]);
78: AIT_FREE_VAL(&cli->cli_buf[0]);
79:
80: if (cli->cli_pid > 0) {
81: kill(cli->cli_pid, SIGKILL);
82: while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
83: usleep(1000);
84: kill(cli->cli_pid, SIGKILL);
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);
137: ioUpdTimerSocket(cli);
138: end:
139: schedReadSelf(task);
140: taskExit(task, NULL);
141: }
142:
143: static void *
144: io_txNet(sched_task_t *task)
145: {
146: int wlen, ret, len = TASK_DATLEN(task);
147: sock_cli_t *cli = TASK_ARG(task);
148: sock_t *s = (sock_t*) cli->cli_parent;
149: u_char *buf = TASK_DATA(task);
150: struct pollfd pfd[1];
151:
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)) {
160: #if 0
161: if (!ret)
162: continue;
163: #endif
164: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
165: break;
166: }
167:
168: if (s->sock_type == SOCK_STREAM)
169: wlen = send(TASK_FD(task), buf, len, 0);
170: else
171: #ifndef __linux__
172: wlen = sendto(TASK_FD(task), buf, len, 0, &cli->cli_addr.sa, cli->cli_addr.sa.sa_len);
173: #else
174: wlen = sendto(TASK_FD(task), buf, len, 0, &cli->cli_addr.sa,
175: cli->cli_addr.sa.sa_family == AF_INET ?
176: sizeof cli->cli_addr.sin : sizeof cli->cli_addr.sin6);
177: #endif
178: if (wlen < 1) {
179: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
180: break;
181: }
182: }
183:
184: taskExit(task, NULL);
185: }
186:
187: static void *
188: io_txPty(sched_task_t *task)
189: {
190: int wlen;
191: sock_cli_t *cli = TASK_ARG(task);
192:
193: ioUpdTimerSocket(cli);
194:
195: wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task));
196: if (wlen < 1)
197: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
198:
199: taskExit(task, NULL);
200: }
201:
202: static void *
203: io_rxNet(sched_task_t *task)
204: {
205: int rlen;
206: sock_cli_t *cli = TASK_ARG(task);
207: sock_t *s = (sock_t*) cli->cli_parent;
208: sockaddr_t sa;
209: socklen_t salen = sizeof sa.ss;
210:
211: ioUpdTimerSocket(cli);
212:
213: if (s->sock_type == SOCK_STREAM)
214: rlen = recv(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
215: AIT_LEN(&cli->cli_buf[0]), 0);
216: else {
217: rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
218: AIT_LEN(&cli->cli_buf[0]), 0, &sa.sa, &salen);
219: if (e_addrcmp(&cli->cli_addr, &sa, 42)) {
220: schedReadSelf(task);
221: taskExit(task, NULL);
222: }
223: }
224: if (rlen < 1)
225: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
226: else {
227: schedEvent(TASK_ROOT(task), io_txPty, cli, cli->cli_pty,
228: AIT_GET_BUF(&cli->cli_buf[0]), rlen);
229: schedReadSelf(task);
230: }
231:
232: taskExit(task, NULL);
233: }
234:
235: static void *
236: io_rxPty(sched_task_t *task)
237: {
238: int rlen;
239: sock_cli_t *cli = TASK_ARG(task);
240:
241: ioUpdTimerSocket(cli);
242:
243: rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1]));
244: if (rlen < 1)
245: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
246: else {
247: schedEvent(TASK_ROOT(task), io_txNet, cli, cli->cli_fd,
248: AIT_GET_BUF(&cli->cli_buf[1]), rlen);
249: schedReadSelf(task);
250: }
251:
252: taskExit(task, NULL);
253: }
254:
255: static void *
256: io_bridgeClient(sched_task_t *task)
257: {
258: int c, rlen, pty;
259: pid_t pid;
260: sockaddr_t sa;
261: socklen_t salen = sizeof sa.ss;
262: sock_cli_t *cli = NULL;
263: sock_t *s = (sock_t*) TASK_ARG(task);
264: array_t *args = NULL;
265: char **argv = NULL;
266:
267: if (s->sock_type == SOCK_STREAM) {
268: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
269: LOGERR;
270: goto end;
271: }
272: } else {
273: if ((rlen = recvfrom(TASK_FD(task),
274: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
275: MSG_PEEK, &sa.sa, &salen)) == -1) {
276: LOGERR;
277: goto end;
278: } else
279: c = TASK_FD(task);
280: }
281:
282: cli = e_malloc(sizeof(sock_cli_t));
283: if (!cli) {
284: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
285: if (s->sock_type == SOCK_STREAM)
286: close(c);
287: goto end;
288: } else {
289: memset(cli, 0, sizeof(sock_cli_t));
290: pthread_mutex_lock(&s->sock_mtx);
291: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
292: pthread_mutex_unlock(&s->sock_mtx);
293: }
294:
295: cli->cli_parent = TASK_ARG(task);
296: cli->cli_fd = c;
297: strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
298: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
299: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
300: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
301:
302: switch ((pid = ioForkPTY(&pty, cli->cli_name, sizeof cli->cli_name,
303: NULL, NULL, NULL))) {
304: case -1:
305: ELIBERR(io);
306: break;
307: case 0:
308: cli->cli_pty = pty;
309: ioSetRAWMode(STDIN_FILENO, NULL);
310:
311: array_Args(cli->cli_cmdline, 0, " \t", &args);
312: argv = array_To(args);
313: array_Destroy(&args);
314:
315: #if 0
316: printf("Console %s\n", cli->cli_name);
317: #endif
318: rlen = execv(*argv, argv);
319: _exit(rlen);
320: break;
321: default:
322: cli->cli_pty = pty;
323: cli->cli_pid = pid;
324:
325: schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty,
326: TASK_ARG(task), 0);
327: schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd,
328: TASK_ARG(task), 0);
329: ioUpdTimerSocket(cli);
330: break;
331: }
332: end:
333: schedReadSelf(task);
334: taskExit(task, NULL);
335: }
336:
337: static void *
338: io_bridgeClient2Pool(sched_task_t *task)
339: {
340: int c, rlen;
341: sockaddr_t sa;
342: socklen_t salen = sizeof sa.ss;
343: sock_cli_t *cli = NULL;
344: sock_t *s = (sock_t*) TASK_ARG(task);
345:
346: if (s->sock_type == SOCK_STREAM) {
347: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
348: LOGERR;
349: goto end;
350: }
351: } else {
352: if ((rlen = recvfrom(TASK_FD(task),
353: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
354: MSG_PEEK, &sa.sa, &salen)) == -1) {
355: LOGERR;
356: goto end;
357: } else
358: c = TASK_FD(task);
359: }
360:
361: cli = e_malloc(sizeof(sock_cli_t));
362: if (!cli) {
363: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
364: if (s->sock_type == SOCK_STREAM)
365: close(c);
366: goto end;
367: } else {
368: memset(cli, 0, sizeof(sock_cli_t));
369: pthread_mutex_lock(&s->sock_mtx);
370: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
371: pthread_mutex_unlock(&s->sock_mtx);
372: }
373:
374: io_progCheck(s->sock_prog, 42);
375:
376: cli->cli_parent = TASK_ARG(task);
377: cli->cli_fd = c;
378: cli->cli_pty = io_progAttach(s->sock_prog, 42);
379: strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
380: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
381: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
382: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
383:
384: schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty, TASK_ARG(task), 0);
385: schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, TASK_ARG(task), 0);
386: ioUpdTimerSocket(cli);
387: end:
388: schedReadSelf(task);
389: taskExit(task, NULL);
390: }
391:
392:
393: /*
394: * ioInitSocket() - Init socket and allocate resources
395: *
396: * @role = Socket role
397: * @type = Socket type
398: * @proto = Socket protocol
399: * @addr = Bind to address
400: * @port = Bind to port
401: * @buflen = Socket buffer, optional if =0 == BUFSIZ
402: * return: NULL error or !=NULL created socket
403: */
404: sock_t *
405: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
406: {
407: sock_t *s = NULL;
408: int n = 1;
409:
410: if (!addr)
411: return NULL;
412:
413: s = e_malloc(sizeof(sock_t));
414: if (!s) {
415: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
416: return NULL;
417: } else
418: memset(s, 0, sizeof(sock_t));
419:
420: TAILQ_INIT(&s->sock_cli);
421:
422: s->sock_role = role;
423: s->sock_type = type;
424: s->sock_proto = proto;
425: if (!e_gethostbyname(addr, port, &s->sock_addr)) {
426: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
427: e_free(s);
428: return NULL;
429: } else {
430: buflen = buflen ? buflen : BUFSIZ;
431: buflen = E_ALIGN(buflen, 2); /* align buflen length */
432: AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
433: }
434:
435: s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
436: if (s->sock_fd == -1) {
437: LOGERR;
438: AIT_FREE_VAL(&s->sock_buf);
439: e_free(s);
440: return NULL;
441: }
442: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
443: LOGERR;
444: AIT_FREE_VAL(&s->sock_buf);
445: e_free(s);
446: return NULL;
447: }
448: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
449: LOGERR;
450: AIT_FREE_VAL(&s->sock_buf);
451: e_free(s);
452: return NULL;
453: }
454: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
455: LOGERR;
456: AIT_FREE_VAL(&s->sock_buf);
457: e_free(s);
458: return NULL;
459: }
460: #ifndef __linux__
461: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
462: #else
463: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_family == AF_INET ?
464: sizeof s->sock_addr.sin : sizeof s->sock_addr.sin6) == -1) {
465: #endif
466: LOGERR;
467: AIT_FREE_VAL(&s->sock_buf);
468: e_free(s);
469: return NULL;
470: }
471:
472: s->sock_root = schedBegin();
473: if (!s->sock_root) {
474: io_SetErr(sched_GetErrno(), "%s", sched_GetError());
475: AIT_FREE_VAL(&s->sock_buf);
476: e_free(s);
477: return NULL;
478: }
479:
480: pthread_mutex_init(&s->sock_mtx, NULL);
481: return s;
482: }
483:
484: /*
485: * ioCloseSocket() - Close socket and free resources
486: *
487: * @s = Socket
488: * return: none
489: */
490: void
491: ioCloseSocket(sock_t ** __restrict s)
492: {
493: sock_cli_t *cli;
494: int stat;
495:
496: if (s && *s) {
497: pthread_mutex_lock(&(*s)->sock_mtx);
498: while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
499: TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
500:
501: schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
502:
503: if ((*s)->sock_type == SOCK_STREAM) {
504: shutdown(cli->cli_fd, SHUT_RDWR);
505: close(cli->cli_fd);
506: }
507: AIT_FREE_VAL(&cli->cli_buf[1]);
508: AIT_FREE_VAL(&cli->cli_buf[0]);
509:
510: if (cli->cli_pid > 0) {
511: kill(cli->cli_pid, SIGKILL);
512: while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
513: usleep(1000);
514: kill(cli->cli_pid, SIGKILL);
515: }
516: }
517:
518: e_free(cli);
519: }
520: pthread_mutex_unlock(&(*s)->sock_mtx);
521:
522: shutdown((*s)->sock_fd, SHUT_RDWR);
523: close((*s)->sock_fd);
524:
525: AIT_FREE_VAL(&(*s)->sock_buf);
526:
527: schedEnd(&(*s)->sock_root);
528:
529: pthread_mutex_destroy(&(*s)->sock_mtx);
530: e_free(*s);
531: *s = NULL;
532: }
533: }
534:
535: /*
536: * ioUpSocket() - Setup socket for use
537: *
538: * @s = Socket
539: * @arg = Server role = listen backlog queue and Client role = peer address
540: * @timeout = Socket timeout in sec (default -1 infinit)
541: * return: -1 error or 0 ok
542: */
543: int
544: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
545: {
546: int ret = 0;
547: sockaddr_t *peer = (sockaddr_t*) arg;
548: uintptr_t backlog = (uintptr_t) arg;
549:
550: if (!s || !arg)
551: return -1;
552: else {
553: s->sock_timeout.tv_sec = timeout;
554: s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
555: schedPolling(s->sock_root, &s->sock_timeout, NULL);
556: }
557:
558: switch (s->sock_role) {
559: case IO_SOCK_ROLE_CLIENT:
560: memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
561:
562: #ifndef __linux__
563: if (connect(s->sock_fd, &s->sock_peer.sa, s->sock_peer.sa.sa_len) == -1) {
564: #else
565: if (connect(s->sock_fd, &s->sock_peer.sa, s->sock_addr.sa.sa_family == AF_INET ?
566: sizeof s->sock_peer.sin : sizeof s->sock_peer.sin6) == -1) {
567: #endif
568: LOGERR;
569: return -1;
570: }
571: break;
572: case IO_SOCK_ROLE_SERVER:
573: if (s->sock_type == SOCK_STREAM) {
574: s->sock_backq = backlog;
575:
576: if (listen(s->sock_fd, s->sock_backq) == -1) {
577: LOGERR;
578: return -1;
579: }
580: }
581: break;
582: default:
583: io_SetErr(EINVAL, "Unsupported socket type");
584: return -1;
585: }
586:
587: fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
588: return ret;
589: }
590:
591: /*
592: * ioUpdTimerSocket() - Update timeout of socket
593: *
594: * @c = Client socket
595: * return: none
596: */
597: void
598: ioUpdTimerSocket(sock_cli_t * __restrict c)
599: {
600: sock_t *s;
601:
602: if (!c)
603: return;
604: else
605: s = c->cli_parent;
606:
607: if (s->sock_prog)
608: io_progCheck(s->sock_prog, 42);
609:
610: schedCancelby(s->sock_root, taskTIMER, CRITERIA_ARG, c, NULL);
611: schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, 0);
612: }
613:
614: /*
615: * ioCloseClient() - Close client socket
616: *
617: * @c = Client socket
618: * return: 0 ok or !=0 error
619: */
620: int
621: ioCloseClient(sock_cli_t * __restrict c)
622: {
623: sock_t *s;
624:
625: if (!c)
626: return -1;
627: else
628: s = c->cli_parent;
629:
630: return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, 0);
631: }
632:
633: /*
634: * ioLoopSocket() - Start socket scheduler
635: *
636: * @s = Socket
637: * @rcb = Read callback
638: * return: -1 error or return result from scheduler
639: */
640: int
641: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
642: {
643: if (!s || !rcb || s->sock_kill)
644: return -1;
645:
646: schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
647: return schedRun(s->sock_root, &s->sock_kill);
648: }
649:
650: static void *
651: io_progPurge(sched_task_t *task)
652: {
653: sock_t *s = (sock_t*) TASK_ARG(task);
654:
655: io_progVacuum(s->sock_prog, 0);
656:
657: schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task),
658: s->sock_timeout, TASK_DATA(task), TASK_DATLEN(task));
659: taskExit(task, NULL);
660: }
661:
662: /*
663: * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
664: *
665: * @s = Socket
666: * @prgname = Program name
667: * return: 0 ok or !=0 error
668: */
669: int
670: ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname)
671: {
672: if (!s || !prgname || s->sock_kill)
673: return -1;
674:
675: if (s->sock_prog) {
676: schedRead(s->sock_root, io_bridgeClient2Pool,
677: s, s->sock_fd, (void*) prgname, 0);
678: schedTimer(s->sock_root, io_progPurge, s, s->sock_timeout, NULL, 0);
679: } else
680: schedRead(s->sock_root, io_bridgeClient,
681: s, s->sock_fd, (void*) prgname, 0);
682: return schedRun(s->sock_root, &s->sock_kill);
683: }
684:
685: /*
686: * ioSetupProg2Socket() - Setup program pool to socket server
687: *
688: * @s = Socket
689: * @p = Program pool
690: * return: -1 error or 0 ok
691: */
692: int
693: ioSetupProg2Socket(sock_t * __restrict s, prog_t * __restrict p)
694: {
695: if (!s)
696: return -1;
697:
698: s->sock_prog = p;
699: return 0;
700: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>