This commit is contained in:
Olaf Rempel 2006-09-26 20:59:08 +02:00
parent fd80e53214
commit a0657aad83
7 changed files with 82 additions and 51 deletions

View File

@ -20,6 +20,6 @@ testclient: testclient.o
$(CC) $(CFLAGS) -o $@ -c $< $(CC) $(CFLAGS) -o $@ -c $<
clean: clean:
rm -rf *.d *.o cachesyncd cachesyncd.log cachesync.sock rm -rf *.d *.o cachesyncd cachesyncd.log cachesync.sock testclient
-include $(SRC:.c=.d) -include $(SRC:.c=.d)

View File

@ -28,6 +28,8 @@
#include "configfile.h" #include "configfile.h"
#include "logging.h" #include "logging.h"
#include "selector.h" #include "selector.h"
#include "multicast.h"
#include "unixsock.h"
#define DEFAULT_CONFIG "cachesyncd.conf" #define DEFAULT_CONFIG "cachesyncd.conf"
#define DEFAULT_LOGFILE "cachesyncd.log" #define DEFAULT_LOGFILE "cachesyncd.log"
@ -90,9 +92,25 @@ int main(int argc, char *argv[])
daemon(-1, 0); 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()); 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; return 0;
} }

View File

@ -2,9 +2,6 @@
# unpriviliged user # unpriviliged user
user httpd user httpd
# pidfile location
pidfile ./cachesync.pid
# unix domain socket location # unix domain socket location
socket ./cachesync.sock socket ./cachesync.sock
@ -13,5 +10,5 @@ mcastdev eth0
mcastgroup 224.0.0.1 mcastgroup 224.0.0.1
mcastport 2000 mcastport 2000
# smarty cache directory # smarty cache directory (home-dir)
basedir /var/www/smarty/cache/ basedir /var/www/smarty/cache/

View File

@ -96,7 +96,7 @@ int mcast_init()
return -1; return -1;
} }
char loop = 0; char loop = 1;
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop))) { if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop))) {
log_print(LOG_WARN, "mcast_init: setsockopt(IP_MULTICAST_LOOP)"); log_print(LOG_WARN, "mcast_init: setsockopt(IP_MULTICAST_LOOP)");
close(sockfd); close(sockfd);

View File

@ -6,13 +6,21 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "list.h"
#include "logging.h" #include "logging.h"
#include "multicast.h" #include "multicast.h"
#include "unixsock.h" #include "unixsock.h"
#define BUF_SIZE 256 #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; fd_set fdsel;
@ -22,80 +30,80 @@ void selector()
return; return;
} }
int usock = sock_init();
if (usock < 0)
return;
int msock = mcast_init();
if (msock < 0)
return;
int usock_con = -1;
while (1) { while (1) {
struct timeval tv; struct timeval tv;
struct fd_entry *entry, *entry_safe;
FD_ZERO(&fdsel); FD_ZERO(&fdsel);
FD_SET(usock, &fdsel); FD_SET(usock, &fdsel);
FD_SET(msock, &fdsel); FD_SET(msock, &fdsel);
if (usock_con != -1) list_for_each_entry(entry, &fd_list, list) {
FD_SET(usock_con, &fdsel); FD_SET(entry->fd, &fdsel);
}
tv.tv_sec = 60; tv.tv_sec = 60;
tv.tv_usec = 0; tv.tv_usec = 0;
int ret = select(64, &fdsel, NULL, NULL, &tv); int ret = select(FD_SETSIZE, &fdsel, NULL, NULL, &tv);
if (ret < 0) { if (ret < 0) {
log_print(LOG_ERROR, "selector: select()"); log_print(LOG_ERROR, "selector: select()");
continue; continue;
} else if (ret == 0) { } else if (ret == 0) {
mcast_send(msock, "KEEPALIVE", 10); mcast_send(msock, "KEEPALIVE", 10);
}
} else if (FD_ISSET(usock, &fdsel)) { if (FD_ISSET(msock, &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)) {
int len = read(msock, buf, BUF_SIZE); int len = read(msock, buf, BUF_SIZE);
if (len <= 0) { if (len <= 0) {
log_print(LOG_ERROR, "selector: multicast sock closed?"); log_print(LOG_ERROR, "selector: multicast sock closed?");
} else if (!strncmp(buf, "KEEPALIVE", 10)) { } else if (!strncmp(buf, "KEEPALIVE", 10)) {
// nothing
} else if (!strncmp(buf, "DELETE ", 7)) { } else if (!strncmp(buf, "DELETE ", 7)) {
log_print(LOG_DEBUG, "delete '%s'", buf +7); log_print(LOG_DEBUG, "delete '%s'", buf +7);
// exec unlink // delete_file(buf +7);
} else { } else {
log_print(LOG_DEBUG, "recv unknown cmd via multicast: '%s'", buf); 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); msock_close(msock);
sock_close(usock); sock_close(usock);
} }

View File

@ -1,6 +1,6 @@
#ifndef _SELECTOR_H_ #ifndef _SELECTOR_H_
#define _SELECTOR_H_ #define _SELECTOR_H_
void selector(); void selector(int usock, int msock);
#endif // _SELECTOR_H_ #endif // _SELECTOR_H_

View File

@ -7,6 +7,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <time.h>
#define FILENAME "cachesync.sock" #define FILENAME "cachesync.sock"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -32,7 +34,13 @@ int main(int argc, char *argv[])
exit(-1); exit(-1);
} }
srandom(time(NULL));
usleep((random() % 100) * 10000);
write(sockfd, argv[1], strlen(argv[1]) +1); write(sockfd, argv[1], strlen(argv[1]) +1);
usleep((random() % 100) * 10000);
close(sockfd); close(sockfd);
return 0; return 0;