--- libaitio/src/sock.c 2013/11/22 10:37:44 1.4.4.16 +++ libaitio/src/sock.c 2013/11/22 12:47:32 1.4.4.17 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: sock.c,v 1.4.4.16 2013/11/22 10:37:44 misho Exp $ +* $Id: sock.c,v 1.4.4.17 2013/11/22 12:47:32 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -57,8 +57,6 @@ io_closeClient(sched_task_t *task) TAILQ_REMOVE(&s->sock_cli, cli, cli_node); pthread_mutex_unlock(&s->sock_mtx); - schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN, - (void*) cli->cli_fd, NULL); schedCancelby(s->sock_root, taskMAX, CRITERIA_ARG, cli, NULL); if (s->sock_type == SOCK_STREAM) { @@ -68,12 +66,11 @@ 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) { + if (cli->cli_pid > 0) { + kill(cli->cli_pid, SIGTERM); + while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) { usleep(1000); - kill((pid_t) TASK_DATA(task), SIGTERM); + kill(cli->cli_pid, SIGTERM); } } @@ -90,8 +87,6 @@ io_acceptClient(sched_task_t *task) sock_cli_t *cli = NULL; sock_t *s = (sock_t*) TASK_ARG(task); - ioUpdTimerSocket(cli, (void*) cli->cli_func); - if (s->sock_type == SOCK_STREAM) { if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) { LOGERR; @@ -141,7 +136,7 @@ io_txNet(sched_task_t *task) sock_cli_t *cli = TASK_ARG(task); sock_t *s = (sock_t*) cli->cli_parent; - ioUpdTimerSocket(cli, (void*) cli->cli_func); + ioUpdTimerSocket(cli, NULL); if (s->sock_type == SOCK_STREAM) wlen = send(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task), 0); @@ -161,6 +156,8 @@ io_txPty(sched_task_t *task) int wlen; sock_cli_t *cli = TASK_ARG(task); + ioUpdTimerSocket(cli, NULL); + wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task)); if (wlen < 1) schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, @@ -178,9 +175,8 @@ io_rxNet(sched_task_t *task) sockaddr_t sa; socklen_t salen = sizeof sa.ss; - ioUpdTimerSocket(cli, (void*) cli->cli_func); + ioUpdTimerSocket(cli, NULL); - memset(AIT_GET_BUF(&cli->cli_buf[0]), 0, AIT_LEN(&cli->cli_buf[0])); if (s->sock_type == SOCK_STREAM) rlen = recv(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]), AIT_LEN(&cli->cli_buf[0]), 0); @@ -207,9 +203,8 @@ io_rxPty(sched_task_t *task) int rlen; sock_cli_t *cli = TASK_ARG(task); - ioUpdTimerSocket(cli, (void*) cli->cli_func); + ioUpdTimerSocket(cli, NULL); - memset(AIT_GET_BUF(&cli->cli_buf[1]), 0, AIT_LEN(&cli->cli_buf[1])); rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1])); if (rlen < 1) schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, @@ -283,13 +278,13 @@ io_bridgeClient(sched_task_t *task) execv(*argv, argv); break; default: - cli->cli_func = (sched_task_func_t) pid; + cli->cli_pid = pid; schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty, TASK_ARG(task), 0); schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd, TASK_ARG(task), 0); - ioUpdTimerSocket(cli, (void*) pid); + ioUpdTimerSocket(cli, NULL); break; } end: @@ -394,14 +389,13 @@ void ioCloseSocket(sock_t ** __restrict s) { sock_cli_t *cli; + int stat; if (s && *s) { pthread_mutex_lock(&(*s)->sock_mtx); while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) { TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node); - schedCancelby((*s)->sock_root, taskTIMER, CRITERIA_DATLEN, - (void*) cli->cli_fd, NULL); schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL); if ((*s)->sock_type == SOCK_STREAM) { @@ -410,6 +404,15 @@ ioCloseSocket(sock_t ** __restrict s) } AIT_FREE_VAL(&cli->cli_buf[1]); AIT_FREE_VAL(&cli->cli_buf[0]); + + if (cli->cli_pid > 0) { + kill(cli->cli_pid, SIGTERM); + while (waitpid(cli->cli_pid, &stat, WNOHANG) > 0) { + usleep(1000); + kill(cli->cli_pid, SIGTERM); + } + } + e_free(cli); } pthread_mutex_unlock(&(*s)->sock_mtx);