This commit is contained in:
Olaf Rempel 2006-07-27 18:53:57 +02:00
parent 9918ee0b56
commit fd80e53214
8 changed files with 171 additions and 19 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
*.d
cachesyncd
cachesyncd.log
cachesync.sock
testclient

View File

@ -1,14 +1,17 @@
# Toplevel Makefile
SRC := cachesyncd.c configfile.c logging.c multicast.c unixsock.c
SRC := cachesyncd.c configfile.c logging.c multicast.c selector.c unixsock.c
CFLAGS := -O2 -Wall
# ############################
all: cachesyncd
all: cachesyncd testclient
cachesyncd: $(SRC:%.c=%.o)
$(CC) $(LDFLAGS) $^ -o $@
$(CC) $(CFLAGS) $^ -o $@
testclient: testclient.o
$(CC) $(CFLAGS) $^ -o $@
%.d: %.c
$(CC) $(CFLAGS) -MM -c $< -o $@
@ -17,6 +20,6 @@ cachesyncd: $(SRC:%.c=%.o)
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -rf *.d *.o cachesyncd
rm -rf *.d *.o cachesyncd cachesyncd.log cachesync.sock
-include $(SRC:.c=.d)

View File

@ -27,8 +27,7 @@
#include "configfile.h"
#include "logging.h"
#include "multicast.h"
#include "unixsock.h"
#include "selector.h"
#define DEFAULT_CONFIG "cachesyncd.conf"
#define DEFAULT_LOGFILE "cachesyncd.log"
@ -40,18 +39,6 @@ static struct option opts[] = {
{0, 0, 0, 0}
};
static void run()
{
int usock, msock;
usock = sock_init();
msock = mcast_init();
mcast_send(msock, "HALLO", 6);
msock_close(msock);
sock_close(usock);
}
int main(int argc, char *argv[])
{
char *config = DEFAULT_CONFIG;
@ -105,7 +92,7 @@ int main(int argc, char *argv[])
log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid());
run();
selector();
return 0;
}

View File

@ -96,6 +96,13 @@ int mcast_init()
return -1;
}
char loop = 0;
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop))) {
log_print(LOG_WARN, "mcast_init: setsockopt(IP_MULTICAST_LOOP)");
close(sockfd);
return -1;
}
inet_aton(mcastgroup, &multiaddr.imr_multiaddr);
multiaddr.imr_interface.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;

101
selector.c Normal file
View File

@ -0,0 +1,101 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "logging.h"
#include "multicast.h"
#include "unixsock.h"
#define BUF_SIZE 256
void selector()
{
fd_set fdsel;
char *buf = malloc(BUF_SIZE);
if (buf == NULL) {
log_print(LOG_ERROR, "selector: out of memory");
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;
FD_ZERO(&fdsel);
FD_SET(usock, &fdsel);
FD_SET(msock, &fdsel);
if (usock_con != -1)
FD_SET(usock_con, &fdsel);
tv.tv_sec = 60;
tv.tv_usec = 0;
int ret = select(64, &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)) {
int len = read(msock, buf, BUF_SIZE);
if (len <= 0) {
log_print(LOG_ERROR, "selector: multicast sock closed?");
} else if (!strncmp(buf, "KEEPALIVE", 10)) {
} else if (!strncmp(buf, "DELETE ", 7)) {
log_print(LOG_DEBUG, "delete '%s'", buf +7);
// exec unlink
} else {
log_print(LOG_DEBUG, "recv unknown cmd via multicast: '%s'", buf);
}
}
}
msock_close(msock);
sock_close(usock);
}

6
selector.h Normal file
View File

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

39
testclient.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define FILENAME "cachesync.sock"
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("$ testclient <cmd>\n");
exit(0);
}
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket()");
exit(-1);
}
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, FILENAME, sizeof(addr.sun_path));
int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
if (connect(sockfd, (struct sockaddr *)&addr, len) < 0) {
perror("connect()");
exit(-1);
}
write(sockfd, argv[1], strlen(argv[1]) +1);
close(sockfd);
return 0;
}

View File

@ -55,6 +55,13 @@ int sock_init()
return -1;
}
ret = listen(sockfd, 5);
if (ret == -1) {
log_print(LOG_ERROR, "unixsock: listen()");
close(sockfd);
return -1;
}
return sockfd;
}