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);
}
log_print(LOG_EVERYTIME, "cachesyncd started");
log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid());
run();

View File

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

View File

@ -32,28 +32,26 @@
int sock_init()
{
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) {
log_print(LOG_ERROR, "unixsock: socket()");
return -1;
}
filename = config_get_string("global", "socket", DEFAULT_SOCKET);
char *filename = config_get_string("global", "socket", DEFAULT_SOCKET);
addr.sun_family = AF_UNIX;
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);
status = bind(sockfd, (struct sockaddr *) &addr, len);
mode_t old_umask = umask(0077);
int ret = bind(sockfd, (struct sockaddr *) &addr, len);
umask(old_umask);
if (status == -1) {
if (ret == -1) {
log_print(LOG_ERROR, "unixsock: bind()");
close(sockfd);
return -1;
}
@ -63,12 +61,10 @@ int sock_init()
void sock_close(int sockfd)
{
char *filename;
int status;
close(sockfd);
filename = config_get_string("global", "socket", DEFAULT_SOCKET);
status = unlink(filename);
if (status == -1)
if (unlink(filename) == -1)
log_print(LOG_WARN, "unixsock: unlink()");
}