2
* Copyright (C) 2007-2009 Sourcefire, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include "clamav-config.h"
27
#include <sys/types.h>
29
#include <sys/socket.h>
38
#include "libclamav/clamav.h"
40
#include "shared/optparser.h"
41
#include "shared/output.h"
45
#include "localserver.h"
48
int localserver(const struct optstruct *opts)
50
logg("!Localserver is not supported on this platform");
56
int localserver(const struct optstruct *opts)
58
struct sockaddr_un server;
63
memset((char *) &server, 0, sizeof(server));
64
server.sun_family = AF_UNIX;
65
strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
66
server.sun_path[sizeof(server.sun_path)-1]='\0';
68
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
69
estr = strerror(errno);
70
logg("!LOCAL: Socket allocation error: %s\n", estr);
74
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
75
if(errno == EADDRINUSE) {
76
if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) {
77
logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path);
81
if(optget(opts, "FixStaleSocket")->enabled) {
82
logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
83
if(unlink(server.sun_path) == -1) {
84
estr = strerror(errno);
85
logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr);
89
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
90
estr = strerror(errno);
91
logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
95
} else if(CLAMSTAT(server.sun_path, &foo) != -1) {
96
logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
101
estr = strerror(errno);
102
logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr);
108
logg("#LOCAL: Unix socket file %s\n", server.sun_path);
110
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
111
logg("#LOCAL: Setting connection queue length to %d\n", backlog);
113
if(listen(sockfd, backlog) == -1) {
114
estr = strerror(errno);
115
logg("!LOCAL: listen() error: %s\n", estr);