diff --git a/Makefile b/Makefile index c88643e..86ce6a8 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,6 @@ testclient: testclient.o $(CC) $(CFLAGS) -o $@ -c $< clean: - rm -rf *.d *.o cachesyncd cachesyncd.log cachesync.sock + rm -rf *.d *.o cachesyncd cachesyncd.log cachesync.sock testclient -include $(SRC:.c=.d) diff --git a/cachesyncd.c b/cachesyncd.c index 1baec61..ab13389 100644 --- a/cachesyncd.c +++ b/cachesyncd.c @@ -28,6 +28,8 @@ #include "configfile.h" #include "logging.h" #include "selector.h" +#include "multicast.h" +#include "unixsock.h" #define DEFAULT_CONFIG "cachesyncd.conf" #define DEFAULT_LOGFILE "cachesyncd.log" @@ -90,9 +92,25 @@ int main(int argc, char *argv[]) daemon(-1, 0); } + // logfile (absolute filename, dropped privs???) + // (daemon) + // chroot (needs root) + // msockinit (needs root) + // setgid/pid (drop privs) + // usockinit (dropped privs, relative path) + // selector/unlink (dropped privs, relative path) + log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid()); - selector(); + int usock = sock_init(); + if (usock < 0) + return -1; + + int msock = mcast_init(); + if (msock < 0) + return -1; + + selector(usock, msock); return 0; } diff --git a/cachesyncd.conf b/cachesyncd.conf index 86eee40..5dcac01 100644 --- a/cachesyncd.conf +++ b/cachesyncd.conf @@ -2,9 +2,6 @@ # unpriviliged user user httpd -# pidfile location -pidfile ./cachesync.pid - # unix domain socket location socket ./cachesync.sock @@ -13,5 +10,5 @@ mcastdev eth0 mcastgroup 224.0.0.1 mcastport 2000 -# smarty cache directory +# smarty cache directory (home-dir) basedir /var/www/smarty/cache/ diff --git a/multicast.c b/multicast.c index 227d094..c748b8e 100644 --- a/multicast.c +++ b/multicast.c @@ -96,7 +96,7 @@ int mcast_init() return -1; } - char loop = 0; + char loop = 1; if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop))) { log_print(LOG_WARN, "mcast_init: setsockopt(IP_MULTICAST_LOOP)"); close(sockfd); diff --git a/selector.c b/selector.c index 1afd5be..d9d45dd 100644 --- a/selector.c +++ b/selector.c @@ -6,13 +6,21 @@ #include #include +#include "list.h" #include "logging.h" #include "multicast.h" #include "unixsock.h" #define BUF_SIZE 256 -void selector() +struct fd_entry { + struct list_head list; + int fd; +}; + +static LIST_HEAD(fd_list); + +void selector(int usock, int msock) { fd_set fdsel; @@ -22,80 +30,80 @@ void selector() return; } - int usock = sock_init(); - if (usock < 0) - return; - - int msock = mcast_init(); - if (msock < 0) - return; - - int usock_con = -1; - while (1) { struct timeval tv; + struct fd_entry *entry, *entry_safe; FD_ZERO(&fdsel); FD_SET(usock, &fdsel); FD_SET(msock, &fdsel); - if (usock_con != -1) - FD_SET(usock_con, &fdsel); + list_for_each_entry(entry, &fd_list, list) { + FD_SET(entry->fd, &fdsel); + } tv.tv_sec = 60; tv.tv_usec = 0; - int ret = select(64, &fdsel, NULL, NULL, &tv); + int ret = select(FD_SETSIZE, &fdsel, NULL, NULL, &tv); if (ret < 0) { log_print(LOG_ERROR, "selector: select()"); continue; } else if (ret == 0) { mcast_send(msock, "KEEPALIVE", 10); + } - } else if (FD_ISSET(usock, &fdsel)) { - if (usock_con != -1) { - log_print(LOG_INFO, "selector: unix socket overflow"); - close(usock_con); - } - - usock_con = accept(usock, NULL, NULL); - if (usock_con == -1) - log_print(LOG_ERROR, "selector: accept()"); - - } else if (usock_con != -1 && FD_ISSET(usock_con, &fdsel)) { - int len = read(usock_con, buf, BUF_SIZE); - if (len > 0) - mcast_send(msock, buf, len); - - if (!strncmp(buf, "DELETE ", 7)) { - log_print(LOG_DEBUG, "delete '%s'", buf +7); - // exec unlink - - } else { - log_print(LOG_DEBUG, "recv unknown cmd via unix socket: '%s'", buf); - } - - close(usock_con); - usock_con = -1; - - } else if (FD_ISSET(msock, &fdsel)) { + if (FD_ISSET(msock, &fdsel)) { int len = read(msock, buf, BUF_SIZE); if (len <= 0) { log_print(LOG_ERROR, "selector: multicast sock closed?"); } else if (!strncmp(buf, "KEEPALIVE", 10)) { + // nothing } else if (!strncmp(buf, "DELETE ", 7)) { log_print(LOG_DEBUG, "delete '%s'", buf +7); - // exec unlink +// delete_file(buf +7); } else { log_print(LOG_DEBUG, "recv unknown cmd via multicast: '%s'", buf); } } - } + if (FD_ISSET(usock, &fdsel)) { + int usock_con; + + usock_con = accept(usock, NULL, NULL); + if (usock_con != -1) { + entry = malloc(sizeof(struct fd_entry)); + if (entry) { + entry->fd = usock_con; + list_add_tail(&entry->list, &fd_list); + } else { + log_print(LOG_ERROR, "selector: out of memory"); + close(usock_con); + } + + } else { + log_print(LOG_ERROR, "selector: accept()"); + } + } + + list_for_each_entry_safe(entry, entry_safe, &fd_list, list) { + if (FD_ISSET(entry->fd, &fdsel)) { + int len = read(entry->fd, buf, BUF_SIZE); + if (len <= 0) { + list_del(&entry->list); + close(entry->fd); + free(entry); + + } else { + mcast_send(msock, buf, len); + } + } + } + } msock_close(msock); sock_close(usock); } diff --git a/selector.h b/selector.h index b9540d8..7cd693e 100644 --- a/selector.h +++ b/selector.h @@ -1,6 +1,6 @@ #ifndef _SELECTOR_H_ #define _SELECTOR_H_ -void selector(); +void selector(int usock, int msock); #endif // _SELECTOR_H_ diff --git a/testclient.c b/testclient.c index d50057c..ebd6e96 100644 --- a/testclient.c +++ b/testclient.c @@ -7,6 +7,8 @@ #include #include +#include + #define FILENAME "cachesync.sock" int main(int argc, char *argv[]) @@ -32,7 +34,13 @@ int main(int argc, char *argv[]) exit(-1); } + srandom(time(NULL)); + usleep((random() % 100) * 10000); + write(sockfd, argv[1], strlen(argv[1]) +1); + + usleep((random() % 100) * 10000); + close(sockfd); return 0;