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