From 45239ca054a5e5a18c159f5cd19806061104dc5b Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 12 May 2009 19:50:34 +0000 Subject: [PATCH] added client list and status listener support to door_daemon --- door_daemon/Makefile | 1 + door_daemon/client_list.c | 113 +++++++++++++++++++++++++++++++++++++++++++++ door_daemon/client_list.h | 36 +++++++++++++++ door_daemon/door_daemon.c | 56 ++++++++++++++-------- 4 files changed, 186 insertions(+), 20 deletions(-) create mode 100644 door_daemon/client_list.c create mode 100644 door_daemon/client_list.h diff --git a/door_daemon/Makefile b/door_daemon/Makefile index ab595c3..7381adf 100644 --- a/door_daemon/Makefile +++ b/door_daemon/Makefile @@ -29,6 +29,7 @@ OBJ := log.o \ options.o \ string_list.o \ command_queue.o \ + client_list.o \ door_daemon.o diff --git a/door_daemon/client_list.c b/door_daemon/client_list.c new file mode 100644 index 0000000..01652c6 --- /dev/null +++ b/door_daemon/client_list.c @@ -0,0 +1,113 @@ +/* + * door_daemon + * + * Copyright (C) 2009 Christian Pointner + * + * This file is part of door_daemon. + * + * door_daemon is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * door_daemon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with door_daemon. If not, see . + */ + +#include + +#include "client_list.h" +#include "datatypes.h" + +client_t* client_get_last(client_t* first) +{ + if(!first) + return NULL; + + while(first->next) { + first = first->next; + } + + return first; +} + +int client_add(client_t** first, int fd) +{ + if(!first) + return -1; + + client_t* new_client = malloc(sizeof(client_t)); + if(!new_client) + return -2; + + new_client->fd = fd; + new_client->status_listener = 0; + new_client->next = NULL; + + if(!(*first)) { + *first = new_client; + return 0; + } + + client_get_last(*first)->next = new_client; + + return 0; +} + +void client_remove(client_t** first, int fd) +{ + if(!first || !(*first)) + return; + + client_t* deletee = *first; + if((*first)->fd == fd) { + *first = (*first)->next; + close(deletee->fd); + free(deletee); + return; + } + + client_t* prev = deletee; + deletee = deletee->next; + while(deletee) { + if(deletee->fd == fd) { + prev->next = deletee->next; + close(deletee->fd); + free(deletee); + return; + } + prev = deletee; + deletee = deletee->next; + } +} + +client_t* client_find(client_t* first, int fd) +{ + if(!first) + return NULL; + + while(first) { + if(first->fd == fd) + return first; + + first = first->next; + } + return NULL; +} + +void client_clear(client_t** first) +{ + if(!first || !(*first)) + return; + + while(*first) { + client_t* deletee = *first; + *first = (*first)->next; + close(deletee->fd); + free(deletee); + } +} diff --git a/door_daemon/client_list.h b/door_daemon/client_list.h new file mode 100644 index 0000000..4dd3c21 --- /dev/null +++ b/door_daemon/client_list.h @@ -0,0 +1,36 @@ +/* + * door_daemon + * + * Copyright (C) 2009 Christian Pointner + * + * This file is part of door_daemon. + * + * door_daemon is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * door_daemon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with door_daemon. If not, see . + */ + +#ifndef _CLIENT_LIST_H_ +#define _CLIENT_LIST_H_ + +struct client_struct { + int fd; + int status_listener; + struct client_struct* next; +}; +typedef struct client_struct client_t; + +int client_add(client_t** first, int fd); +void client_remove(client_t** first, int fd); +client_t* client_find(client_t* first, int fd); +void client_clear(client_t** first); + +#endif diff --git a/door_daemon/door_daemon.c b/door_daemon/door_daemon.c index cf12668..c917f3b 100644 --- a/door_daemon/door_daemon.c +++ b/door_daemon/door_daemon.c @@ -32,6 +32,7 @@ #include "options.h" #include "command_queue.h" +#include "client_list.h" #include "daemon.h" #include "sysexec.h" @@ -125,7 +126,7 @@ int send_response(int fd, const char* response) return ret; } -int handle_command(const char* cmd, int fd, cmd_t** cmd_q) +int handle_command(const char* cmd, int fd, cmd_t** cmd_q, client_t* client_lst) { if(!cmd_q || !cmd) return -1; @@ -143,6 +144,17 @@ int handle_command(const char* cmd, int fd, cmd_t** cmd_q) cmd_id = STATUS; else if(!strncmp(cmd, "log", 3)) cmd_id = LOG; + else if(!strncmp(cmd, "listen", 6)) { + client_t* listener = client_find(client_lst, fd); + if(listener) { + log_printf(DEBUG, "adding status listener %d", fd); + listener->status_listener = 1; + } + else + log_printf(ERROR, "unable to add status listener %d", fd); + + return 0; + } else { log_printf(WARNING, "unknown command '%s'", cmd); return 1; @@ -176,8 +188,10 @@ int handle_command(const char* cmd, int fd, cmd_t** cmd_q) return 0; } -int process_cmd(int fd, cmd_t **cmd_q) +int process_cmd(int fd, cmd_t **cmd_q, client_t* client_lst) { + log_printf(DEBUG, "processing command from %d", fd); + static char buffer[100]; int ret = 0; do { // TODO: replace this whith a actually working readline @@ -188,19 +202,19 @@ int process_cmd(int fd, cmd_t **cmd_q) char* saveptr; char* tok = strtok_r(buffer, "\n\r", &saveptr); do { - ret = handle_command(tok, fd, cmd_q); + ret = handle_command(tok, fd, cmd_q, client_lst); if(ret < 0) return ret; } while(tok = strtok_r(NULL, "\n\r", &saveptr)); } while (ret == -1 && errno == EINTR); - log_printf(DEBUG, "processing command from %d", fd); - return 0; } -int process_door(int door_fd, cmd_t **cmd_q) +int process_door(int door_fd, cmd_t **cmd_q, client_t* client_lst) { + log_printf(DEBUG, "processing data from door (fd=%d)", door_fd); + static char buffer[100]; int ret = 0; do { // TODO: replace this whith a actually working readline @@ -221,8 +235,6 @@ int process_door(int door_fd, cmd_t **cmd_q) } while(tok = strtok_r(NULL, "\n\r", &saveptr)); } while (ret == -1 && errno == EINTR); - log_printf(DEBUG, "processing data from door (fd=%d)", door_fd); - return 0; } @@ -237,6 +249,7 @@ int main_loop(int door_fd, int cmd_listen_fd) FD_SET(cmd_listen_fd, &readfds); int max_fd = door_fd > cmd_listen_fd ? door_fd : cmd_listen_fd; cmd_t* cmd_q = NULL; + client_t* client_lst = NULL; int sig_fd = signal_init(); if(sig_fd < 0) @@ -261,14 +274,11 @@ int main_loop(int door_fd, int cmd_listen_fd) return_value = 1; break; } - FD_CLR(sig_fd, &tmpfds); if(FD_ISSET(door_fd, &tmpfds)) { - return_value = process_door(door_fd, &cmd_q); + return_value = process_door(door_fd, &cmd_q, client_lst); if(return_value) break; - - FD_CLR(door_fd, &tmpfds); } if(FD_ISSET(cmd_listen_fd, &tmpfds)) { @@ -281,22 +291,27 @@ int main_loop(int door_fd, int cmd_listen_fd) log_printf(DEBUG, "new command connection (fd=%d)", new_fd); FD_SET(new_fd, &readfds); max_fd = (max_fd < new_fd) ? new_fd : max_fd; - FD_CLR(cmd_listen_fd, &tmpfds); + client_add(&client_lst, new_fd); } - int fd; - for(fd = 0; fd <= max_fd; fd++) { - if(FD_ISSET(fd, &tmpfds)) { - return_value = process_cmd(fd, &cmd_q); + client_t* lst = client_lst; + while(lst) { + if(FD_ISSET(lst->fd, &tmpfds)) { + return_value = process_cmd(lst->fd, &cmd_q, client_lst); if(return_value == 1) { - log_printf(DEBUG, "removing closed command connection (fd=%d)", fd); - close(fd); - FD_CLR(fd, &readfds); + log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd); + client_t* deletee = lst; + lst = lst->next; + FD_CLR(deletee->fd, &readfds); + client_remove(&client_lst, deletee->fd); return_value = 0; + continue; } if(return_value) break; + } + lst = lst->next; } if(cmd_q && !cmd_q->sent) @@ -304,6 +319,7 @@ int main_loop(int door_fd, int cmd_listen_fd) } cmd_clear(&cmd_q); + client_clear(&client_lst); signal_stop(); return return_value; } -- 1.7.10.4