b151ef55 |
/* |
5c4d94a9 |
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net> |
b151ef55 |
*
* 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 |
30738099 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
b151ef55 |
*/
|
8b242bb9 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
b151ef55 |
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <errno.h>
|
e303cf29 |
#include "libclamav/clamav.h"
#include "shared/options.h"
#include "shared/cfgparser.h"
|
b151ef55 |
#include "others.h"
#include "server.h" |
36f2038b |
#include "output.h" |
b151ef55 |
|
b3483842 |
#ifdef C_WINDOWS
int localserver(const struct cfgstruct *copt, struct cl_node *root)
{
logg("!Localserver is not supported on this platform");
return -1;
}
#else
|
e303cf29 |
int localserver(const struct cfgstruct *copt) |
b151ef55 |
{
struct sockaddr_un server;
int sockfd, backlog;
struct stat foo;
char *estr;
memset((char *) &server, 0, sizeof(server));
server.sun_family = AF_UNIX; |
7cc3891c |
strncpy(server.sun_path, cfgopt(copt, "LocalSocket")->strarg, sizeof(server.sun_path)); |
b151ef55 |
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
estr = strerror(errno); |
c6259ac5 |
logg("!Socket allocation error: %s\n", estr); |
e303cf29 |
return -1; |
b151ef55 |
}
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) { |
c6259ac5 |
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); |
e303cf29 |
close(sockfd);
return -1; |
c6259ac5 |
} |
5c4d94a9 |
if(cfgopt(copt, "FixStaleSocket")->enabled) { |
c6259ac5 |
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); |
e303cf29 |
close(sockfd);
return -1; |
c6259ac5 |
}
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); |
e303cf29 |
close(sockfd);
return -1; |
c6259ac5 |
}
} 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); |
e303cf29 |
close(sockfd);
return -1; |
c6259ac5 |
}
} else {
estr = strerror(errno);
logg("!Socket file %s could not be bound: %s\n", server.sun_path, estr); |
e303cf29 |
close(sockfd);
return -1; |
b151ef55 |
} |
c6259ac5 |
} |
b151ef55 |
|
c6259ac5 |
logg("Unix socket file %s\n", server.sun_path); |
b151ef55 |
|
5c4d94a9 |
backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg; |
b151ef55 |
logg("Setting connection queue length to %d\n", backlog);
if(listen(sockfd, backlog) == -1) {
estr = strerror(errno);
logg("!listen() error: %s\n", estr); |
e303cf29 |
close(sockfd);
return -1; |
b151ef55 |
}
|
596b1e64 |
return sockfd; |
b151ef55 |
} |
b3483842 |
#endif /* C_WINDOWS */ |