smarty template cache syncing daemon (PoC)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

77 lines
2.6 KiB

/***************************************************************************
* 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;
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
log_print(LOG_ERROR, "unixsock: socket()");
return -1;
}
char *filename = config_get_string("global", "socket", DEFAULT_SOCKET);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
mode_t old_umask = umask(0077);
int ret = bind(sockfd, (struct sockaddr *) &addr, len);
umask(old_umask);
if (ret == -1) {
log_print(LOG_ERROR, "unixsock: bind()");
close(sockfd);
return -1;
}
ret = listen(sockfd, 5);
if (ret == -1) {
log_print(LOG_ERROR, "unixsock: listen()");
close(sockfd);
return -1;
}
return sockfd;
}
void sock_close(int sockfd)
{
char *filename;
close(sockfd);
filename = config_get_string("global", "socket", DEFAULT_SOCKET);
if (unlink(filename) == -1)
log_print(LOG_WARN, "unixsock: unlink()");
}