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

1.2       misho       1: /*************************************************************************
                      2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.14    ! misho       6: * $Id: sock.c,v 1.13.2.1 2014/02/05 02:24:28 misho Exp $
1.2       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.14    ! misho      15: Copyright 2004 - 2014
1.2       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: 
                     48: 
1.5       misho      49: static void *
                     50: io_closeClient(sched_task_t *task)
                     51: {
                     52:        sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task);
                     53:        sock_t *s = (sock_t*) cli->cli_parent;
1.7       misho      54:        int stat;
1.5       misho      55: 
                     56:        pthread_mutex_lock(&s->sock_mtx);
                     57:        TAILQ_REMOVE(&s->sock_cli, cli, cli_node);
                     58:        pthread_mutex_unlock(&s->sock_mtx);
                     59: 
                     60:        schedCancelby(s->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
                     61: 
1.9       misho      62:        if (*cli->cli_name)
                     63:                ioFreePTY(cli->cli_pty, cli->cli_name);
1.12      misho      64:        if (s->sock_prog) {
                     65:                io_progDetach(s->sock_prog, cli->cli_pty);
                     66: #if 0
                     67:                io_progCloseAt(s->sock_prog, cli->cli_pty);
                     68: #endif
                     69:                cli->cli_pty ^= cli->cli_pty;
                     70:                io_progCheck(s->sock_prog, 42);
                     71:        }
1.8       misho      72: 
1.5       misho      73:        if (s->sock_type == SOCK_STREAM) {
1.7       misho      74:                shutdown(cli->cli_fd, SHUT_RDWR);
                     75:                close(cli->cli_fd);
1.5       misho      76:        }
                     77:        AIT_FREE_VAL(&cli->cli_buf[1]);
                     78:        AIT_FREE_VAL(&cli->cli_buf[0]);
                     79: 
                     80:        if (cli->cli_pid > 0) {
1.7       misho      81:                kill(cli->cli_pid, SIGKILL);
1.5       misho      82:                while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
                     83:                        usleep(1000);
1.7       misho      84:                        kill(cli->cli_pid, SIGKILL);
1.5       misho      85:                }
                     86:        }
                     87: 
                     88:        e_free(cli);
                     89:        taskExit(task, NULL);
                     90: }
                     91: 
                     92: static void *
                     93: io_acceptClient(sched_task_t *task)
                     94: {
                     95:        int c, rlen;
                     96:        sockaddr_t sa;
                     97:        socklen_t salen = sizeof sa.ss;
                     98:        sock_cli_t *cli = NULL;
                     99:        sock_t *s = (sock_t*) TASK_ARG(task);
                    100: 
                    101:        if (s->sock_type == SOCK_STREAM) {
                    102:                if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
                    103:                        LOGERR;
                    104:                        goto end;
                    105:                }
                    106:        } else {
                    107:                if ((rlen = recvfrom(TASK_FD(task), 
                    108:                                                AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf), 
                    109:                                                MSG_PEEK, &sa.sa, &salen)) == -1) {
                    110:                        LOGERR;
                    111:                        goto end;
                    112:                } else
                    113:                        c = TASK_FD(task);
                    114:        }
                    115: 
                    116:        cli = e_malloc(sizeof(sock_cli_t));
                    117:        if (!cli) {
                    118:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    119:                if (s->sock_type == SOCK_STREAM)
                    120:                        close(c);
                    121:                goto end;
                    122:        } else {
                    123:                memset(cli, 0, sizeof(sock_cli_t));
                    124:                pthread_mutex_lock(&s->sock_mtx);
                    125:                TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
                    126:                pthread_mutex_unlock(&s->sock_mtx);
                    127:        }
                    128: 
                    129:        cli->cli_parent = TASK_ARG(task);
                    130:        cli->cli_fd = c;
                    131:        cli->cli_func = TASK_DATA(task);
                    132:        memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
                    133:        AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
                    134:        AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
                    135: 
                    136:        schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0);
1.7       misho     137:        ioUpdTimerSocket(cli);
1.5       misho     138: end:
                    139:        schedReadSelf(task);
                    140:        taskExit(task, NULL);
                    141: }
                    142: 
                    143: static void *
                    144: io_txNet(sched_task_t *task)
                    145: {
1.10      misho     146:        int wlen, ret, len = TASK_DATLEN(task);
1.5       misho     147:        sock_cli_t *cli = TASK_ARG(task);
                    148:        sock_t *s = (sock_t*) cli->cli_parent;
1.10      misho     149:        u_char *buf = TASK_DATA(task);
                    150:        struct pollfd pfd[1];
1.5       misho     151: 
1.10      misho     152:        pfd->fd = TASK_FD(task);
                    153:        pfd->events = POLLOUT;
                    154:        pfd->revents = 0;
                    155:        for(; len > 0; len -= wlen, buf += wlen) {
                    156:                ioUpdTimerSocket(cli);
                    157: 
                    158:                if ((ret = poll(pfd, 1, s->sock_timeout.tv_sec * 1000)) < 1 || 
                    159:                                pfd->revents & (POLLNVAL | POLLERR | POLLHUP)) {
1.12      misho     160: #if 0
1.10      misho     161:                        if (!ret)
                    162:                                continue;
1.12      misho     163: #endif
1.10      misho     164:                        schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
                    165:                        break;
                    166:                }
1.5       misho     167: 
1.10      misho     168:                if (s->sock_type == SOCK_STREAM)
                    169:                        wlen = send(TASK_FD(task), buf, len, 0);
                    170:                else
                    171:                        wlen = sendto(TASK_FD(task), buf, len, 0, 
                    172:                                        &cli->cli_addr.sa, cli->cli_addr.sa.sa_len);
                    173:                if (wlen < 1) {
                    174:                        schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
                    175:                        break;
                    176:                }
                    177:        }
1.5       misho     178: 
                    179:        taskExit(task, NULL);
                    180: }
                    181: 
                    182: static void *
                    183: io_txPty(sched_task_t *task)
                    184: {
                    185:        int wlen;
                    186:        sock_cli_t *cli = TASK_ARG(task);
                    187: 
1.7       misho     188:        ioUpdTimerSocket(cli);
1.5       misho     189: 
                    190:        wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task));
                    191:        if (wlen < 1)
1.7       misho     192:                schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5       misho     193: 
                    194:        taskExit(task, NULL);
                    195: }
                    196: 
                    197: static void *
                    198: io_rxNet(sched_task_t *task)
                    199: {
                    200:        int rlen;
                    201:        sock_cli_t *cli = TASK_ARG(task);
                    202:        sock_t *s = (sock_t*) cli->cli_parent;
                    203:        sockaddr_t sa;
                    204:        socklen_t salen = sizeof sa.ss;
                    205: 
1.7       misho     206:        ioUpdTimerSocket(cli);
1.5       misho     207: 
                    208:        if (s->sock_type == SOCK_STREAM)
                    209:                rlen = recv(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]), 
                    210:                                AIT_LEN(&cli->cli_buf[0]), 0);
                    211:        else {
                    212:                rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]), 
                    213:                                AIT_LEN(&cli->cli_buf[0]), 0, &sa.sa, &salen);
                    214:                if (e_addrcmp(&cli->cli_addr, &sa, 42))
                    215:                        goto end;
                    216:        }
                    217:        if (rlen < 1)
1.7       misho     218:                schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5       misho     219:        else
                    220:                schedEvent(TASK_ROOT(task), io_txPty, cli, cli->cli_pty, 
                    221:                                AIT_GET_BUF(&cli->cli_buf[0]), rlen);
                    222: end:
                    223:        schedReadSelf(task);
                    224:        taskExit(task, NULL);
                    225: }
                    226: 
                    227: static void *
                    228: io_rxPty(sched_task_t *task)
                    229: {
                    230:        int rlen;
                    231:        sock_cli_t *cli = TASK_ARG(task);
                    232: 
1.7       misho     233:        ioUpdTimerSocket(cli);
1.5       misho     234: 
                    235:        rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1]));
                    236:        if (rlen < 1)
1.7       misho     237:                schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, 0);
1.5       misho     238:        else
                    239:                schedEvent(TASK_ROOT(task), io_txNet, cli, cli->cli_fd, 
                    240:                                AIT_GET_BUF(&cli->cli_buf[1]), rlen);
                    241: 
                    242:        schedReadSelf(task);
                    243:        taskExit(task, NULL);
                    244: }
                    245: 
                    246: static void *
                    247: io_bridgeClient(sched_task_t *task)
                    248: {
1.13      misho     249:        int c, rlen, pty;
1.5       misho     250:        pid_t pid;
                    251:        sockaddr_t sa;
                    252:        socklen_t salen = sizeof sa.ss;
                    253:        sock_cli_t *cli = NULL;
                    254:        sock_t *s = (sock_t*) TASK_ARG(task);
                    255:        array_t *args = NULL;
                    256:        char **argv = NULL;
                    257: 
                    258:        if (s->sock_type == SOCK_STREAM) {
                    259:                if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
                    260:                        LOGERR;
                    261:                        goto end;
                    262:                }
                    263:        } else {
                    264:                if ((rlen = recvfrom(TASK_FD(task), 
                    265:                                                AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf), 
                    266:                                                MSG_PEEK, &sa.sa, &salen)) == -1) {
                    267:                        LOGERR;
                    268:                        goto end;
                    269:                } else
                    270:                        c = TASK_FD(task);
                    271:        }
                    272: 
                    273:        cli = e_malloc(sizeof(sock_cli_t));
                    274:        if (!cli) {
                    275:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    276:                if (s->sock_type == SOCK_STREAM)
                    277:                        close(c);
                    278:                goto end;
                    279:        } else {
                    280:                memset(cli, 0, sizeof(sock_cli_t));
                    281:                pthread_mutex_lock(&s->sock_mtx);
                    282:                TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
                    283:                pthread_mutex_unlock(&s->sock_mtx);
                    284:        }
                    285: 
                    286:        cli->cli_parent = TASK_ARG(task);
                    287:        cli->cli_fd = c;
                    288:        strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
                    289:        memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
                    290:        AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
                    291:        AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
                    292: 
1.13      misho     293:        switch ((pid = ioForkPTY(&pty, cli->cli_name, sizeof cli->cli_name, 
1.5       misho     294:                                NULL, NULL, NULL))) {
                    295:                case -1:
                    296:                        ELIBERR(io);
                    297:                        break;
                    298:                case 0:
1.13      misho     299:                        cli->cli_pty = pty;
                    300: 
1.5       misho     301:                        array_Args(cli->cli_cmdline, 0, " \t", &args);
                    302:                        argv = array_To(args);
                    303:                        array_Destroy(&args);
                    304: 
1.11      misho     305: #if 0
1.5       misho     306:                        printf("Console %s\n", cli->cli_name);
1.11      misho     307: #endif
1.6       misho     308:                        rlen = execv(*argv, argv);
                    309:                        _exit(rlen);
1.5       misho     310:                        break;
                    311:                default:
1.13      misho     312:                        cli->cli_pty = pty;
1.5       misho     313:                        cli->cli_pid = pid;
                    314: 
                    315:                        schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty, 
                    316:                                        TASK_ARG(task), 0);
                    317:                        schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, 
                    318:                                        TASK_ARG(task), 0);
1.7       misho     319:                        ioUpdTimerSocket(cli);
1.5       misho     320:                        break;
                    321:        }
                    322: end:
                    323:        schedReadSelf(task);
                    324:        taskExit(task, NULL);
                    325: }
                    326: 
1.12      misho     327: static void *
                    328: io_bridgeClient2Pool(sched_task_t *task)
                    329: {
                    330:        int c, rlen;
                    331:        sockaddr_t sa;
                    332:        socklen_t salen = sizeof sa.ss;
                    333:        sock_cli_t *cli = NULL;
                    334:        sock_t *s = (sock_t*) TASK_ARG(task);
                    335: 
                    336:        if (s->sock_type == SOCK_STREAM) {
                    337:                if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
                    338:                        LOGERR;
                    339:                        goto end;
                    340:                }
                    341:        } else {
                    342:                if ((rlen = recvfrom(TASK_FD(task), 
                    343:                                                AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf), 
                    344:                                                MSG_PEEK, &sa.sa, &salen)) == -1) {
                    345:                        LOGERR;
                    346:                        goto end;
                    347:                } else
                    348:                        c = TASK_FD(task);
                    349:        }
                    350: 
                    351:        cli = e_malloc(sizeof(sock_cli_t));
                    352:        if (!cli) {
                    353:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    354:                if (s->sock_type == SOCK_STREAM)
                    355:                        close(c);
                    356:                goto end;
                    357:        } else {
                    358:                memset(cli, 0, sizeof(sock_cli_t));
                    359:                pthread_mutex_lock(&s->sock_mtx);
                    360:                TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
                    361:                pthread_mutex_unlock(&s->sock_mtx);
                    362:        }
                    363: 
                    364:        io_progCheck(s->sock_prog, 42);
                    365: 
                    366:        cli->cli_parent = TASK_ARG(task);
                    367:        cli->cli_fd = c;
                    368:        cli->cli_pty = io_progAttach(s->sock_prog, 42);
                    369:        strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
                    370:        memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
                    371:        AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
                    372:        AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
                    373: 
                    374:        schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty, TASK_ARG(task), 0);
                    375:        schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, TASK_ARG(task), 0);
                    376:        ioUpdTimerSocket(cli);
                    377: end:
                    378:        schedReadSelf(task);
                    379:        taskExit(task, NULL);
                    380: }
                    381: 
1.5       misho     382: 
1.2       misho     383: /*
                    384:  * ioInitSocket() - Init socket and allocate resources
                    385:  *
                    386:  * @role = Socket role
                    387:  * @type = Socket type
                    388:  * @proto = Socket protocol
                    389:  * @addr = Bind to address
                    390:  * @port = Bind to port
                    391:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
                    392:  * return: NULL error or !=NULL created socket
                    393:  */
                    394: sock_t *
                    395: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
                    396: {
                    397:        sock_t *s = NULL;
                    398:        int n = 1;
                    399: 
                    400:        if (!addr)
                    401:                return NULL;
                    402: 
                    403:        s = e_malloc(sizeof(sock_t));
                    404:        if (!s) {
                    405:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    406:                return NULL;
                    407:        } else
                    408:                memset(s, 0, sizeof(sock_t));
                    409: 
1.3       misho     410:        TAILQ_INIT(&s->sock_cli);
                    411: 
1.2       misho     412:        s->sock_role = role;
                    413:        s->sock_type = type;
                    414:        s->sock_proto = proto;
                    415:        if (!e_gethostbyname(addr, port, &s->sock_addr)) {
                    416:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    417:                e_free(s);
                    418:                return NULL;
                    419:        } else {
                    420:                buflen = buflen ? buflen : BUFSIZ;
1.5       misho     421:                buflen = E_ALIGN(buflen, 2);    /* align buflen length */
1.2       misho     422:                AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
                    423:        }
                    424: 
                    425:        s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
                    426:        if (s->sock_fd == -1) {
                    427:                LOGERR;
                    428:                AIT_FREE_VAL(&s->sock_buf);
                    429:                e_free(s);
                    430:                return NULL;
                    431:        }
                    432:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
                    433:                LOGERR;
                    434:                AIT_FREE_VAL(&s->sock_buf);
                    435:                e_free(s);
                    436:                return NULL;
                    437:        }
                    438:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
                    439:                LOGERR;
                    440:                AIT_FREE_VAL(&s->sock_buf);
                    441:                e_free(s);
                    442:                return NULL;
                    443:        }
                    444:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
                    445:                LOGERR;
                    446:                AIT_FREE_VAL(&s->sock_buf);
                    447:                e_free(s);
                    448:                return NULL;
                    449:        }
                    450:        if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
                    451:                LOGERR;
                    452:                AIT_FREE_VAL(&s->sock_buf);
                    453:                e_free(s);
                    454:                return NULL;
                    455:        }
                    456: 
1.5       misho     457:        s->sock_root = schedBegin();
                    458:        if (!s->sock_root) {
                    459:                io_SetErr(sched_GetErrno(), "%s", sched_GetError());
                    460:                AIT_FREE_VAL(&s->sock_buf);
                    461:                e_free(s);
                    462:                return NULL;
                    463:        }
                    464: 
1.3       misho     465:        pthread_mutex_init(&s->sock_mtx, NULL);
1.2       misho     466:        return s;
                    467: }
                    468: 
                    469: /*
                    470:  * ioCloseSocket() - Close socket and free resources
                    471:  *
                    472:  * @s = Socket
                    473:  * return: none
                    474:  */
                    475: void
                    476: ioCloseSocket(sock_t ** __restrict s)
                    477: {
1.5       misho     478:        sock_cli_t *cli;
                    479:        int stat;
1.3       misho     480: 
1.2       misho     481:        if (s && *s) {
1.3       misho     482:                pthread_mutex_lock(&(*s)->sock_mtx);
                    483:                while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
                    484:                        TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
1.5       misho     485: 
                    486:                        schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
                    487: 
                    488:                        if ((*s)->sock_type == SOCK_STREAM) {
                    489:                                shutdown(cli->cli_fd, SHUT_RDWR);
                    490:                                close(cli->cli_fd);
                    491:                        }
                    492:                        AIT_FREE_VAL(&cli->cli_buf[1]);
                    493:                        AIT_FREE_VAL(&cli->cli_buf[0]);
                    494: 
                    495:                        if (cli->cli_pid > 0) {
1.7       misho     496:                                kill(cli->cli_pid, SIGKILL);
1.5       misho     497:                                while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) {
                    498:                                        usleep(1000);
1.7       misho     499:                                        kill(cli->cli_pid, SIGKILL);
1.5       misho     500:                                }
                    501:                        }
                    502: 
1.3       misho     503:                        e_free(cli);
                    504:                }
                    505:                pthread_mutex_unlock(&(*s)->sock_mtx);
                    506: 
1.2       misho     507:                shutdown((*s)->sock_fd, SHUT_RDWR);
                    508:                close((*s)->sock_fd);
                    509: 
                    510:                AIT_FREE_VAL(&(*s)->sock_buf);
1.3       misho     511: 
1.5       misho     512:                schedEnd(&(*s)->sock_root);
                    513: 
1.3       misho     514:                pthread_mutex_destroy(&(*s)->sock_mtx);
1.2       misho     515:                e_free(*s);
                    516:                *s = NULL;
                    517:        }
                    518: }
                    519: 
                    520: /*
                    521:  * ioUpSocket() - Setup socket for use
                    522:  *
                    523:  * @s = Socket
                    524:  * @arg = Server role = listen backlog queue and Client role = peer address
1.5       misho     525:  * @timeout = Socket timeout in sec (default -1 infinit)
1.2       misho     526:  * return: -1 error or 0 ok
                    527:  */
                    528: int
1.5       misho     529: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
1.2       misho     530: {
                    531:        int ret = 0;
                    532:        sockaddr_t *peer = (sockaddr_t*) arg;
                    533:        uintptr_t backlog = (uintptr_t) arg;
                    534: 
                    535:        if (!s || !arg)
                    536:                return -1;
1.5       misho     537:        else {
                    538:                s->sock_timeout.tv_sec = timeout;
                    539:                s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
                    540:                schedPolling(s->sock_root, &s->sock_timeout, NULL);
                    541:        }
1.2       misho     542: 
                    543:        switch (s->sock_role) {
                    544:                case IO_SOCK_ROLE_CLIENT:
                    545:                        memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
                    546: 
                    547:                        if (connect(s->sock_fd, &s->sock_peer.sa, 
                    548:                                                s->sock_peer.sa.sa_len) == -1) {
                    549:                                LOGERR;
                    550:                                return -1;
                    551:                        }
                    552:                        break;
                    553:                case IO_SOCK_ROLE_SERVER:
                    554:                        if (s->sock_type == SOCK_STREAM) {
                    555:                                s->sock_backq = backlog;
                    556: 
                    557:                                if (listen(s->sock_fd, s->sock_backq) == -1) {
                    558:                                        LOGERR;
                    559:                                        return -1;
                    560:                                }
                    561:                        }
                    562:                        break;
                    563:                default:
                    564:                        io_SetErr(EINVAL, "Unsupported socket type");
                    565:                        return -1;
                    566:        }
                    567: 
                    568:        fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
                    569:        return ret;
                    570: }
1.3       misho     571: 
1.5       misho     572: /*
                    573:  * ioUpdTimerSocket() - Update timeout of socket
                    574:  *
                    575:  * @c = Client socket
                    576:  * return:  none
                    577:  */
                    578: void
1.7       misho     579: ioUpdTimerSocket(sock_cli_t * __restrict c)
1.3       misho     580: {
1.5       misho     581:        sock_t *s;
1.3       misho     582: 
1.5       misho     583:        if (!c)
                    584:                return;
                    585:        else
                    586:                s = c->cli_parent;
1.3       misho     587: 
1.12      misho     588:        if (s->sock_prog)
                    589:                io_progCheck(s->sock_prog, 42);
                    590: 
1.7       misho     591:        schedCancelby(s->sock_root, taskTIMER, CRITERIA_ARG, c, NULL);
                    592:        schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, 0);
1.3       misho     593: }
                    594: 
1.5       misho     595: /*
                    596:  * ioCloseClient() - Close client socket
                    597:  *
                    598:  * @c = Client socket
                    599:  * return: 0 ok or !=0 error
                    600:  */
                    601: int
                    602: ioCloseClient(sock_cli_t * __restrict c)
1.3       misho     603: {
1.5       misho     604:        sock_t *s;
1.3       misho     605: 
1.5       misho     606:        if (!c)
                    607:                return -1;
                    608:        else
                    609:                s = c->cli_parent;
1.3       misho     610: 
1.7       misho     611:        return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, 0);
1.3       misho     612: }
                    613: 
                    614: /*
1.5       misho     615:  * ioLoopSocket() - Start socket scheduler
1.3       misho     616:  *
                    617:  * @s = Socket
1.5       misho     618:  * @rcb = Read callback
                    619:  * return: -1 error or return result from scheduler
1.3       misho     620:  */
                    621: int
1.5       misho     622: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
1.3       misho     623: {
1.5       misho     624:        if (!s || !rcb || s->sock_kill)
1.3       misho     625:                return -1;
                    626: 
1.5       misho     627:        schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
                    628:        return schedRun(s->sock_root, &s->sock_kill);
                    629: }
1.3       misho     630: 
1.12      misho     631: static void *
                    632: io_progPurge(sched_task_t *task)
                    633: {
                    634:        sock_t *s = (sock_t*) TASK_ARG(task);
                    635: 
                    636:        io_progVacuum(s->sock_prog, 0);
                    637: 
                    638:        schedTimer(TASK_ROOT(task), TASK_FUNC(task), TASK_ARG(task), 
                    639:                        s->sock_timeout, TASK_DATA(task), TASK_DATLEN(task));
                    640:        taskExit(task, NULL);
                    641: }
                    642: 
1.5       misho     643: /*
                    644:  * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
                    645:  *
                    646:  * @s = Socket
                    647:  * @prgname = Program name
                    648:  * return: 0 ok or !=0 error
                    649:  */
                    650: int
                    651: ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname)
                    652: {
                    653:        if (!s || !prgname || s->sock_kill)
                    654:                return -1;
1.3       misho     655: 
1.12      misho     656:        if (s->sock_prog) {
                    657:                schedRead(s->sock_root, io_bridgeClient2Pool, 
                    658:                                s, s->sock_fd, (void*) prgname, 0);
                    659:                schedTimer(s->sock_root, io_progPurge, s, s->sock_timeout, NULL, 0);
                    660:        } else
                    661:                schedRead(s->sock_root, io_bridgeClient, 
                    662:                                s, s->sock_fd, (void*) prgname, 0);
1.5       misho     663:        return schedRun(s->sock_root, &s->sock_kill);
1.3       misho     664: }
1.12      misho     665: 
                    666: /*
                    667:  * ioSetupProg2Socket() - Setup program pool to socket server
                    668:  *
                    669:  * @s = Socket
                    670:  * @p = Program pool
                    671:  * return: -1 error or 0 ok
                    672:  */
                    673: int
                    674: ioSetupProg2Socket(sock_t * __restrict s, prog_t * __restrict p)
                    675: {
                    676:        if (!s)
                    677:                return -1;
                    678: 
                    679:        s->sock_prog = p;
                    680:        return 0;
                    681: }

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