--- /dev/null
+/*
+ * door_daemon
+ *
+ * Copyright (C) 2009 Christian Pointner <equinox@spreadspace.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+
+#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);
+ }
+}
--- /dev/null
+/*
+ * door_daemon
+ *
+ * Copyright (C) 2009 Christian Pointner <equinox@spreadspace.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#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
#include "options.h"
#include "command_queue.h"
+#include "client_list.h"
#include "daemon.h"
#include "sysexec.h"
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;
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;
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
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
} 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;
}
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)
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)) {
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)
}
cmd_clear(&cmd_q);
+ client_clear(&client_lst);
signal_stop();
return return_value;
}