From 5cdaab19f6a0c16cbce882a16c987c5c33f4190d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 4 Jun 2009 22:42:22 +0000 Subject: [PATCH] added timeout for commands --- door_daemon/command_queue.c | 17 +++++++++++++++++ door_daemon/command_queue.h | 4 ++++ door_daemon/door_daemon.c | 15 +++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/door_daemon/command_queue.c b/door_daemon/command_queue.c index 638c08b..7ba22c7 100644 --- a/door_daemon/command_queue.c +++ b/door_daemon/command_queue.c @@ -57,6 +57,9 @@ int cmd_push(cmd_t** first, int fd, cmd_id_t cmd, const char* param) else new_cmd->param = NULL; new_cmd->sent = 0; + new_cmd->tv_start.tv_sec = 0; + new_cmd->tv_start.tv_usec = 0; + gettimeofday(&new_cmd->tv_start, NULL); new_cmd->next = NULL; if(!(*first)) { @@ -69,6 +72,20 @@ int cmd_push(cmd_t** first, int fd, cmd_id_t cmd, const char* param) return 0; } +// timeout between 1 and 2 seconds +int cmd_has_expired(const cmd_t cmd) +{ + struct timeval now; + now.tv_sec = 2; + now.tv_usec = 0; + gettimeofday(&now, NULL); + + if(cmd.tv_start.tv_sec + 2 >= now.tv_sec) + return 1; + + return 0; +} + void cmd_pop(cmd_t** first) { if(!first || !(*first)) diff --git a/door_daemon/command_queue.h b/door_daemon/command_queue.h index 61af577..d03602f 100644 --- a/door_daemon/command_queue.h +++ b/door_daemon/command_queue.h @@ -21,6 +21,8 @@ #ifndef _COMMAND_QUEUE_H_ #define _COMMAND_QUEUE_H_ +#include + enum cmd_id_enum { OPEN, CLOSE, TOGGLE, RESET, STATUS, LOG }; typedef enum cmd_id_enum cmd_id_t; @@ -29,11 +31,13 @@ struct cmd_struct { cmd_id_t cmd; char* param; int sent; + struct timeval tv_start; struct cmd_struct* next; }; typedef struct cmd_struct cmd_t; int cmd_push(cmd_t** first, int fd, cmd_id_t cmd, const char* param); +int cmd_has_expired(const cmd_t cmd); void cmd_pop(cmd_t** first); void cmd_clear(cmd_t** first); diff --git a/door_daemon/door_daemon.c b/door_daemon/door_daemon.c index 01bd6f3..0443910 100644 --- a/door_daemon/door_daemon.c +++ b/door_daemon/door_daemon.c @@ -305,18 +305,29 @@ int main_loop(int door_fd, int cmd_listen_fd) FD_SET(sig_fd, &readfds); max_fd = (max_fd < sig_fd) ? sig_fd : max_fd; + struct timeval timeout; int return_value = 0; while(!return_value) { memcpy(&tmpfds, &readfds, sizeof(tmpfds)); - int ret = select(max_fd+1, &tmpfds, NULL, NULL, NULL); + timeout.tv_sec = 0; + timeout.tv_usec = 200000; + int ret = select(max_fd+1, &tmpfds, NULL, NULL, &timeout); if(ret == -1 && errno != EINTR) { log_printf(ERROR, "select returned with error: %s", strerror(errno)); return_value = -1; break; } - if(!ret || ret == -1) + if(ret == -1) continue; + if(!ret) { + if(cmd_q && cmd_has_expired(*cmd_q)) { + log_printf(ERROR, "last command expired"); + cmd_pop(&cmd_q); + } + else + continue; + } if(FD_ISSET(sig_fd, &tmpfds)) { if(signal_handle()) { -- 1.7.10.4