Annotation of libaitio/src/sock.c, revision 1.4.4.15

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>