diff options
author | Christian Pointner <equinox@helsinki.at> | 2009-11-19 21:04:54 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2009-11-19 21:04:54 (GMT) |
commit | d171586226b79bd0977f6f9c6584621e6c6e46bb (patch) | |
tree | 8df1d3399716dccbe54ae865c2b33c333464e046 | |
parent | a5b0a21c1a906ef871b32014c3f6357251141099 (diff) |
added basic inotify
-rw-r--r-- | rhdropbox.c | 40 | ||||
-rw-r--r-- | utils.c | 34 | ||||
-rw-r--r-- | utils.h | 1 |
3 files changed, 70 insertions, 5 deletions
diff --git a/rhdropbox.c b/rhdropbox.c index a9d5ad8..a7b3681 100644 --- a/rhdropbox.c +++ b/rhdropbox.c @@ -25,6 +25,7 @@ #include <stdio.h> #include <string.h> #include <errno.h> +#include <sys/inotify.h> #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"); @@ -22,9 +22,10 @@ #include "datatypes.h" #include <sys/un.h> -#include <termios.h> #include <unistd.h> #include <errno.h> +#include <fcntl.h> +#include <sys/inotify.h> #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; +} @@ -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 |