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