From d171586226b79bd0977f6f9c6584621e6c6e46bb Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 19 Nov 2009 21:04:54 +0000 Subject: added basic inotify diff --git a/rhdropbox.c b/rhdropbox.c index a9d5ad8..a7b3681 100644 --- a/rhdropbox.c +++ b/rhdropbox.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "log.h" #include "sig_handler.h" @@ -57,6 +58,20 @@ int send_response(int fd, const char* response) return ret; } +int process_watch(int inotify_fd) +{ + log_printf(INFO, "something changed on watch descriptor '%d'", inotify_fd); + u_int8_t buf[1000]; + int ret = read(inotify_fd, buf, 1000); + if(ret < 0) { + log_printf(ERROR, "read inotify_fd error: %s", strerror(errno)); + return ret; + } + + log_printf(DEBUG, "received %d bytes from watch", ret); + return 0; +} + /* client_t* client; */ /* int listener_cnt = 0; */ /* for(client = client_lst; client; client = client->next) */ @@ -161,14 +176,15 @@ int process_cmd(const char* cmd, int fd, client_t* client_lst, options_t* opt) return 0; } -int main_loop(int cmd_listen_fd, options_t* opt) +int main_loop(int cmd_listen_fd, int inotify_fd, options_t* opt) { log_printf(NOTICE, "entering main loop"); fd_set readfds, tmpfds; FD_ZERO(&readfds); FD_SET(cmd_listen_fd, &readfds); - int max_fd = cmd_listen_fd; + FD_SET(inotify_fd, &readfds); + int max_fd = (cmd_listen_fd < inotify_fd) ? inotify_fd : cmd_listen_fd; client_t* client_lst = NULL; read_buffer_t switch_buffer; @@ -200,6 +216,12 @@ int main_loop(int cmd_listen_fd, options_t* opt) } } + if(FD_ISSET(inotify_fd, &tmpfds)) { + return_value = process_watch(inotify_fd); + if(return_value) + break; + } + if(FD_ISSET(cmd_listen_fd, &tmpfds)) { int new_fd = accept(cmd_listen_fd, NULL, NULL); if(new_fd < 0) { @@ -337,9 +359,19 @@ int main(int argc, char* argv[]) log_close(); exit(-1); } - - ret = main_loop(cmd_listen_fd, &opt); + + int inotify_fd = create_inotify(); + if(inotify_fd < 0) { + close(cmd_listen_fd); + options_clear(&opt); + log_close(); + exit(-1); + } + + ret = main_loop(cmd_listen_fd, inotify_fd, &opt); + close(cmd_listen_fd); + close(inotify_fd); if(!ret) log_printf(NOTICE, "normal shutdown"); diff --git a/utils.c b/utils.c index 2620e6f..a057ca2 100644 --- a/utils.c +++ b/utils.c @@ -22,9 +22,10 @@ #include "datatypes.h" #include -#include #include #include +#include +#include #include "log.h" @@ -139,3 +140,34 @@ int nonblock_recvline(read_buffer_t* buffer, int fd, client_t* client_lst, optio return ret; } + +int create_inotify() +{ + int fd = inotify_init(); + if(fd < 0) { + log_printf(ERROR, "error at inotify_init: %s", strerror(errno)); + return -1; + } + + int fs_flags = fcntl(fd, F_GETFL); + if(fs_flags == -1) { + log_printf(ERROR, "inotify init failed (fcntl read flags error: %s)", strerror(errno)); + return -1; + } + if(fcntl(fd, F_SETFL, fs_flags | O_NONBLOCK) == -1) { + log_printf(ERROR, "inotify init failed (fcntl write flags error: %s)", strerror(errno)); + return -1; + } + + int fd_flags = fcntl(fd, F_GETFD); + if(fd_flags == -1) { + log_printf(ERROR, "inotify init failed (fcntl read flags error: %s)", strerror(errno)); + return -1; + } + if(fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC) == -1) { + log_printf(ERROR, "inotify init failed (fcntl write flags error: %s)", strerror(errno)); + return -1; + } + + return fd; +} diff --git a/utils.h b/utils.h index 65afba2..500062d 100644 --- a/utils.h +++ b/utils.h @@ -30,5 +30,6 @@ int init_command_socket(const char* path); int connect_command_socket(const char* path); int send_string(int fd, const char* string); int nonblock_recvline(read_buffer_t* buffer, int fd, client_t* client_lst, options_t* opt); +int create_inotify(); #endif -- cgit v0.10.2