--- libaitio/src/sock.c 2013/11/22 10:05:17 1.4.4.11 +++ libaitio/src/sock.c 2013/11/22 10:19:50 1.4.4.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: sock.c,v 1.4.4.11 2013/11/22 10:05:17 misho Exp $ +* $Id: sock.c,v 1.4.4.12 2013/11/22 10:19:50 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -51,7 +51,7 @@ io_closeClient(sched_task_t *task) { sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task); sock_t *s = (sock_t*) cli->cli_parent; - int sock = (int) TASK_DATLEN(task); + int stat, sock = (int) TASK_DATLEN(task); pthread_mutex_lock(&s->sock_mtx); TAILQ_REMOVE(&s->sock_cli, cli, cli_node); @@ -68,6 +68,15 @@ io_closeClient(sched_task_t *task) AIT_FREE_VAL(&cli->cli_buf[1]); AIT_FREE_VAL(&cli->cli_buf[0]); + /* if optional argument have pid */ + if (TASK_DATA(task)) { + kill((pid_t) TASK_DATA(task), SIGTERM); + while (waitpid((pid_t) TASK_DATA(task), &stat, WNOHANG) > 0) { + usleep(1000); + kill((pid_t) TASK_DATA(task), SIGTERM); + } + } + e_free(cli); taskExit(task, NULL); } @@ -117,7 +126,7 @@ io_acceptClient(sched_task_t *task) AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf)); schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0); - ioUpdTimerSocket(cli); + ioUpdTimerSocket(cli, NULL); end: schedReadSelf(task); taskExit(task, NULL); @@ -126,7 +135,7 @@ end: static void * io_txNet(sched_task_t *task) { - int wlen, stat; + int wlen; sock_cli_t *cli = TASK_ARG(task); sock_t *s = (sock_t*) cli->cli_parent; @@ -135,14 +144,9 @@ io_txNet(sched_task_t *task) else wlen = sendto(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task), 0, &cli->cli_addr.sa, cli->cli_addr.sa.sa_len); - if (wlen < 1) { - kill((pid_t) cli->cli_func, SIGTERM); - while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) { - usleep(1000); - kill((pid_t) cli->cli_func, SIGTERM); - } - schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd); - } + if (wlen < 1) + schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, + (void*) cli->cli_func, cli->cli_fd); taskExit(task, NULL); } @@ -150,18 +154,13 @@ io_txNet(sched_task_t *task) static void * io_txPty(sched_task_t *task) { - int wlen, stat; + int wlen; sock_cli_t *cli = TASK_ARG(task); wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task)); - if (wlen < 1) { - kill((pid_t) cli->cli_func, SIGTERM); - while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) { - usleep(1000); - kill((pid_t) cli->cli_func, SIGTERM); - } - schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd); - } + if (wlen < 1) + schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, + (void*) cli->cli_func, cli->cli_fd); taskExit(task, NULL); } @@ -169,7 +168,7 @@ io_txPty(sched_task_t *task) static void * io_rxNet(sched_task_t *task) { - int rlen, stat; + int rlen; sock_cli_t *cli = TASK_ARG(task); sock_t *s = (sock_t*) cli->cli_parent; sockaddr_t sa; @@ -184,14 +183,10 @@ io_rxNet(sched_task_t *task) if (e_addrcmp(&cli->cli_addr, &sa, 42)) goto end; } - if (rlen < 1) { - kill((pid_t) cli->cli_func, SIGTERM); - while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) { - usleep(1000); - kill((pid_t) cli->cli_func, SIGTERM); - } - schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd); - } else + if (rlen < 1) + schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, + (void*) cli->cli_func, cli->cli_fd); + else schedWrite(TASK_ROOT(task), io_txPty, cli, cli->cli_pty, AIT_GET_BUF(&cli->cli_buf[0]), rlen); end: @@ -202,18 +197,14 @@ end: static void * io_rxPty(sched_task_t *task) { - int rlen, stat; + int rlen; sock_cli_t *cli = TASK_ARG(task); rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1])); - if (rlen < 1) { - kill((pid_t) cli->cli_func, SIGTERM); - while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) { - usleep(1000); - kill((pid_t) cli->cli_func, SIGTERM); - } - schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd); - } else + if (rlen < 1) + schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, + (void*) cli->cli_func, cli->cli_fd); + else schedWrite(TASK_ROOT(task), io_txNet, cli, cli->cli_fd, AIT_GET_BUF(&cli->cli_buf[1]), rlen); @@ -287,7 +278,7 @@ io_bridgeClient(sched_task_t *task) TASK_ARG(task), 0); schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, TASK_ARG(task), 0); - ioUpdTimerSocket(cli); + ioUpdTimerSocket(cli, (void*) pid); break; } end: @@ -481,10 +472,11 @@ ioUpSocket(sock_t * __restrict s, void *arg, int timeo * ioUpdTimerSocket() - Update timeout of socket * * @c = Client socket + * @arg = Optional data argument * return: none */ void -ioUpdTimerSocket(sock_cli_t * __restrict c) +ioUpdTimerSocket(sock_cli_t * __restrict c, void *arg) { sock_t *s; @@ -494,7 +486,7 @@ ioUpdTimerSocket(sock_cli_t * __restrict c) s = c->cli_parent; schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN, (void*) c->cli_fd, NULL); - schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, c->cli_fd); + schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, arg, c->cli_fd); } /*