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