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