cachesyncd/unixsock.c

78 lines
2.6 KiB
C
Raw Normal View History

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 <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include "configfile.h"
#include "logging.h"
#define DEFAULT_SOCKET "/tmp/cachesyncd"
int sock_init()
{
struct sockaddr_un addr;
2006-07-27 17:26:11 +02:00
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
2006-06-23 22:02:31 +02:00
if (sockfd == -1) {
log_print(LOG_ERROR, "unixsock: socket()");
return -1;
}
2006-07-27 17:26:11 +02:00
char *filename = config_get_string("global", "socket", DEFAULT_SOCKET);
2006-06-23 22:02:31 +02:00
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
2006-07-27 17:26:11 +02:00
int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
2006-06-23 22:02:31 +02:00
2006-07-27 17:26:11 +02:00
mode_t old_umask = umask(0077);
int ret = bind(sockfd, (struct sockaddr *) &addr, len);
2006-06-23 22:02:31 +02:00
umask(old_umask);
2006-07-27 17:26:11 +02:00
if (ret == -1) {
2006-06-23 22:02:31 +02:00
log_print(LOG_ERROR, "unixsock: bind()");
2006-07-27 17:26:11 +02:00
close(sockfd);
2006-06-23 22:02:31 +02:00
return -1;
}
2006-07-27 18:53:57 +02:00
ret = listen(sockfd, 5);
if (ret == -1) {
log_print(LOG_ERROR, "unixsock: listen()");
close(sockfd);
return -1;
}
2006-06-23 22:02:31 +02:00
return sockfd;
}
void sock_close(int sockfd)
{
char *filename;
close(sockfd);
filename = config_get_string("global", "socket", DEFAULT_SOCKET);
2006-07-27 17:26:11 +02:00
if (unlink(filename) == -1)
2006-06-23 22:02:31 +02:00
log_print(LOG_WARN, "unixsock: unlink()");
}