code cleanup

This commit is contained in:
Olaf Rempel 2006-07-27 17:26:11 +02:00
parent eacec9b628
commit 9918ee0b56
3 changed files with 31 additions and 32 deletions

View File

@ -103,7 +103,7 @@ int main(int argc, char *argv[])
daemon(-1, 0); daemon(-1, 0);
} }
log_print(LOG_EVERYTIME, "cachesyncd started"); log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid());
run(); run();

View File

@ -29,81 +29,84 @@
#include "configfile.h" #include "configfile.h"
#include "logging.h" #include "logging.h"
#define DEFAULT_DEVICE "eth0"
#define DEFAULT_GROUP "224.0.0.1"
#define DEFAULT_PORT 2000
static struct sockaddr_in dest_addr; static struct sockaddr_in dest_addr;
int mcast_init() int mcast_init()
{ {
struct ip_mreq multiaddr; struct ip_mreq multiaddr;
struct ifreq ifr; struct ifreq ifr;
char *mcastdev, *mcastgroup;
int mcastport, sock;
// open socket // open socket
sock = socket(AF_INET, SOCK_DGRAM, 0); int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) { if (sockfd == -1) {
log_print(LOG_ERROR, "mcast_init: socket()"); log_print(LOG_ERROR, "mcast_init: socket()");
return -1; return -1;
} }
// get device flags // get device flags
mcastdev = config_get_string("global", "mcastdev", "eth0"); char *mcastdev = config_get_string("global", "mcastdev", DEFAULT_DEVICE);
strncpy(ifr.ifr_name, mcastdev, sizeof(ifr.ifr_name)); strncpy(ifr.ifr_name, mcastdev, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFFLAGS, &ifr) != 0) { if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) != 0) {
log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFFLAGS)"); log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFFLAGS)");
close(sock); close(sockfd);
return -1; return -1;
} }
// check device flags // check device flags
if ((ifr.ifr_flags & (IFF_UP | IFF_MULTICAST)) != (IFF_UP | IFF_MULTICAST)) { if ((ifr.ifr_flags & (IFF_UP | IFF_MULTICAST)) != (IFF_UP | IFF_MULTICAST)) {
log_print(LOG_WARN, "mcast_init: device %s not up"); log_print(LOG_WARN, "mcast_init: device %s not up");
close(sock); close(sockfd);
return -1; return -1;
} }
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, sizeof(ifr.ifr_name)) < 0) { if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, sizeof(ifr.ifr_name)) < 0) {
log_print(LOG_WARN, "mcast_init: setsockopt(SO_BINDTODEVICE)"); log_print(LOG_WARN, "mcast_init: setsockopt(SO_BINDTODEVICE)");
// close(sock); // close(sockfd);
// return -1; // return -1;
} }
mcastgroup = config_get_string("global", "mcastgroup", "224.0.0.1"); char *mcastgroup = config_get_string("global", "mcastgroup", DEFAULT_GROUP);
mcastport = config_get_int("global", "mcastport", 2000); int mcastport = config_get_int("global", "mcastport", DEFAULT_PORT);
dest_addr.sin_family = AF_INET; dest_addr.sin_family = AF_INET;
inet_aton(mcastgroup, &dest_addr.sin_addr); inet_aton(mcastgroup, &dest_addr.sin_addr);
dest_addr.sin_port = htons(mcastport); dest_addr.sin_port = htons(mcastport);
if (bind(sock, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0) { if (bind(sockfd, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0) {
log_print(LOG_WARN, "mcast_init: bind()"); log_print(LOG_WARN, "mcast_init: bind()");
close(sockfd);
return -1; return -1;
} }
// get interface address // get interface address
if (ioctl(sock, SIOCGIFADDR, &ifr) != 0) { if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0) {
log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFADDR)"); log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFADDR)");
close(sock); close(sockfd);
return -1; return -1;
} }
// check address family // check address family
if (ifr.ifr_addr.sa_family != AF_INET) { if (ifr.ifr_addr.sa_family != AF_INET) {
log_print(LOG_WARN, "mcast_init: unknown address family"); log_print(LOG_WARN, "mcast_init: unknown address family");
close(sock); close(sockfd);
return -1; return -1;
} }
inet_aton(mcastgroup, &multiaddr.imr_multiaddr); inet_aton(mcastgroup, &multiaddr.imr_multiaddr);
multiaddr.imr_interface.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; multiaddr.imr_interface.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&multiaddr, sizeof(multiaddr)) == -1) { &multiaddr, sizeof(multiaddr)) == -1) {
log_print(LOG_WARN, "mcast_init: setsockopt(IP_ADD_MEMBERSHIP)"); log_print(LOG_WARN, "mcast_init: setsockopt(IP_ADD_MEMBERSHIP)");
close(sock); close(sockfd);
return -1; return -1;
} }
return sock; return sockfd;
} }
int mcast_send(int sock, char *buf, int len) int mcast_send(int sock, char *buf, int len)

View File

@ -32,28 +32,26 @@
int sock_init() int sock_init()
{ {
struct sockaddr_un addr; struct sockaddr_un addr;
char *filename;
mode_t old_umask;
int sockfd, len, status;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) { if (sockfd == -1) {
log_print(LOG_ERROR, "unixsock: socket()"); log_print(LOG_ERROR, "unixsock: socket()");
return -1; return -1;
} }
filename = config_get_string("global", "socket", DEFAULT_SOCKET); char *filename = config_get_string("global", "socket", DEFAULT_SOCKET);
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path)); strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
len = sizeof(addr.sun_family) + strlen(addr.sun_path); int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
old_umask = umask(0077); mode_t old_umask = umask(0077);
status = bind(sockfd, (struct sockaddr *) &addr, len); int ret = bind(sockfd, (struct sockaddr *) &addr, len);
umask(old_umask); umask(old_umask);
if (status == -1) { if (ret == -1) {
log_print(LOG_ERROR, "unixsock: bind()"); log_print(LOG_ERROR, "unixsock: bind()");
close(sockfd);
return -1; return -1;
} }
@ -63,12 +61,10 @@ int sock_init()
void sock_close(int sockfd) void sock_close(int sockfd)
{ {
char *filename; char *filename;
int status;
close(sockfd); close(sockfd);
filename = config_get_string("global", "socket", DEFAULT_SOCKET); filename = config_get_string("global", "socket", DEFAULT_SOCKET);
status = unlink(filename); if (unlink(filename) == -1)
if (status == -1)
log_print(LOG_WARN, "unixsock: unlink()"); log_print(LOG_WARN, "unixsock: unlink()");
} }