e3aaff8e |
/* |
81837459 |
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
*
* This program is free software; you can redistribute it and/or modify |
bb34cb31 |
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. |
e3aaff8e |
*
* 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
e3aaff8e |
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <string.h>
#include <sys/types.h> |
477cfb97 |
#ifndef C_WINDOWS |
e3aaff8e |
#include <sys/socket.h> |
477cfb97 |
#endif |
e3aaff8e |
#include <sys/stat.h> |
477cfb97 |
#ifndef C_WINDOWS |
e3aaff8e |
#include <sys/un.h> |
477cfb97 |
#endif |
e3aaff8e |
#include <errno.h> |
ad3c01bf |
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif |
e3aaff8e |
|
bd8603aa |
#include "libclamav/clamav.h"
#include "shared/options.h"
#include "shared/cfgparser.h"
|
e3aaff8e |
#include "others.h"
#include "server.h" |
afb48b28 |
#include "output.h" |
fc83da82 |
#include "localserver.h" |
e3aaff8e |
|
477cfb97 |
#ifdef C_WINDOWS |
a57e3d41 |
int localserver(const struct cfgstruct *copt) |
67118e92 |
{
logg("!Localserver is not supported on this platform");
return -1;
}
#else
|
bd8603aa |
int localserver(const struct cfgstruct *copt) |
e3aaff8e |
{
struct sockaddr_un server;
int sockfd, backlog;
struct stat foo;
char *estr;
memset((char *) &server, 0, sizeof(server));
server.sun_family = AF_UNIX; |
658f19f8 |
strncpy(server.sun_path, cfgopt(copt, "LocalSocket")->strarg, sizeof(server.sun_path)); |
e3aaff8e |
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
estr = strerror(errno); |
049a18b9 |
logg("!Socket allocation error: %s\n", estr); |
bd8603aa |
return -1; |
e3aaff8e |
}
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) { |
049a18b9 |
if(errno == EADDRINUSE) {
if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) {
logg("!Socket file %s is in use by another process.\n", server.sun_path); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
} |
81837459 |
if(cfgopt(copt, "FixStaleSocket")->enabled) { |
049a18b9 |
logg("^Socket file %s exists. Unclean shutdown? Removing...\n", server.sun_path);
if(unlink(server.sun_path) == -1) {
estr = strerror(errno);
logg("!Socket file %s could not be removed: %s\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
estr = strerror(errno);
logg("!Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
} else if(stat(server.sun_path, &foo) != -1) {
logg("!Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
} else {
estr = strerror(errno);
logg("!Socket file %s could not be bound: %s\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
e3aaff8e |
} |
049a18b9 |
} |
e3aaff8e |
|
049a18b9 |
logg("Unix socket file %s\n", server.sun_path); |
e3aaff8e |
|
81837459 |
backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg; |
e3aaff8e |
logg("Setting connection queue length to %d\n", backlog);
if(listen(sockfd, backlog) == -1) {
estr = strerror(errno);
logg("!listen() error: %s\n", estr); |
bd8603aa |
close(sockfd);
return -1; |
e3aaff8e |
}
|
57358cc8 |
return sockfd; |
e3aaff8e |
} |
67118e92 |
#endif /* C_WINDOWS */ |