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