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

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