2006-06-23 22:02:31 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 06/2006 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2006-10-04 21:45:18 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2006-06-23 22:02:31 +02:00
|
|
|
#include "configfile.h"
|
|
|
|
#include "logging.h"
|
2006-10-04 21:45:18 +02:00
|
|
|
#include "event.h"
|
2006-09-26 20:59:08 +02:00
|
|
|
#include "multicast.h"
|
|
|
|
#include "unixsock.h"
|
2006-06-23 22:02:31 +02:00
|
|
|
|
|
|
|
#define DEFAULT_CONFIG "cachesyncd.conf"
|
|
|
|
#define DEFAULT_LOGFILE "cachesyncd.log"
|
|
|
|
|
2006-10-04 21:45:18 +02:00
|
|
|
#define BUF_SIZE 256
|
|
|
|
|
2006-06-23 22:02:31 +02:00
|
|
|
static struct option opts[] = {
|
|
|
|
{"config", 1, 0, 'c'},
|
|
|
|
{"debug", 0, 0, 'd'},
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2006-10-04 21:45:18 +02:00
|
|
|
static int usock, msock;
|
|
|
|
|
|
|
|
int usock_read_callback(int fd, void *privdata)
|
|
|
|
{
|
2006-10-04 21:57:19 +02:00
|
|
|
int len = read(fd, privdata, BUF_SIZE);
|
2006-10-04 21:45:18 +02:00
|
|
|
if (len <= 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
mcast_send(msock, privdata, len);
|
2006-10-04 21:45:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usock_accept_callback(int fd, void *privdata)
|
|
|
|
{
|
|
|
|
int con = accept(usock, NULL, NULL);
|
|
|
|
if (con < 0 ) {
|
|
|
|
log_print(LOG_ERROR, "selector: accept()");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
event_add_readfd(con, usock_read_callback, privdata);
|
2006-10-04 21:45:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msock_read_callback(int fd, void *privdata)
|
|
|
|
{
|
2006-10-04 21:57:19 +02:00
|
|
|
int len = read(fd, privdata, BUF_SIZE);
|
2006-10-04 21:45:18 +02:00
|
|
|
if (len <= 0) {
|
|
|
|
log_print(LOG_ERROR, "selector: multicast sock closed?");
|
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
} else if (!strncmp(privdata, "KEEPALIVE", 10)) {
|
2006-10-04 21:45:18 +02:00
|
|
|
// nothing
|
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
} else if (!strncmp(privdata, "DELETE ", 7)) {
|
|
|
|
log_print(LOG_DEBUG, "delete '%s'", privdata +7);
|
2006-10-04 21:45:18 +02:00
|
|
|
//delete_file(buf +7);
|
|
|
|
|
|
|
|
} else {
|
2006-10-04 21:57:19 +02:00
|
|
|
log_print(LOG_DEBUG, "recv unknown cmd via multicast: '%s'", privdata);
|
2006-10-04 21:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msock_keepalive_timeout(void *privdata)
|
|
|
|
{
|
|
|
|
mcast_send(msock, "KEEPALIVE", 10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-23 22:02:31 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *config = DEFAULT_CONFIG;
|
|
|
|
int code, arg = 0, debug = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
code = getopt_long(argc, argv, "c:dh", opts, &arg);
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 'c': /* config */
|
|
|
|
config = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* debug */
|
|
|
|
debug = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h': /* help */
|
|
|
|
printf("Usage: cachesyncd [options]\n"
|
|
|
|
"Options: \n"
|
|
|
|
" --config -c configfile use this configfile\n"
|
|
|
|
" --debug -d do not fork and log to stderr\n"
|
|
|
|
" --help -h this help\n"
|
|
|
|
"\n");
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?': /* error */
|
|
|
|
exit(-1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* unknown / all options parsed */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (code != -1);
|
|
|
|
|
|
|
|
/* parse config file */
|
|
|
|
if (!config_parse(config))
|
|
|
|
exit(-1);
|
|
|
|
|
|
|
|
/* check logfile */
|
|
|
|
char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
|
|
|
|
if (logfile != NULL && debug == 0) {
|
|
|
|
/* start logging */
|
|
|
|
if (!log_init(logfile))
|
|
|
|
exit(-1);
|
|
|
|
|
|
|
|
/* zum daemon mutieren */
|
|
|
|
daemon(-1, 0);
|
|
|
|
}
|
|
|
|
|
2006-07-27 17:26:11 +02:00
|
|
|
log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid());
|
2006-06-23 22:02:31 +02:00
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
char *buf = malloc(BUF_SIZE);
|
2006-10-04 21:45:18 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
log_print(LOG_ERROR, "selector: out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
usock = sock_init();
|
2006-09-26 20:59:08 +02:00
|
|
|
if (usock < 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-10-04 21:45:18 +02:00
|
|
|
msock = mcast_init();
|
2006-09-26 20:59:08 +02:00
|
|
|
if (msock < 0)
|
|
|
|
return -1;
|
|
|
|
|
2006-10-04 21:57:19 +02:00
|
|
|
event_add_readfd(usock, usock_accept_callback, buf);
|
|
|
|
event_add_readfd(msock, msock_read_callback, buf);
|
2006-10-04 21:45:18 +02:00
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 60;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
event_add_timeout(&tv, msock_keepalive_timeout, NULL);
|
|
|
|
|
|
|
|
event_loop();
|
2006-06-23 22:02:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|