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