2 * inoclam - Inotify+ClamAV virus scanner
3 * Copyright (C) 2007 Vermont Department of Taxes
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Tom Cort <tom.cort@state.vt.us>
21 * Matt Gagne <matt.gagne@state.vt.us>
29 #include <libdaemon/dlog.h>
30 #include <libdaemon/dpid.h>
34 #include <sys/types.h>
40 #include "inoclam.hxx"
41 #include "inotify.hxx"
42 #include "monitor.hxx"
47 * Determines if gatewayavd should run in the background (daemonized) or
48 * not. If daemonize is 1, then gatewayavd should run in the background.
53 * Determines if our process killed a running inoclam process successfully.
58 * Displays some version and copyright information upon request (-v or --version).
60 void display_version()
62 daemon_log(LOG_INFO
, "inoclam v%s (%s)", VERSION
, CODENAME
);
63 daemon_log(LOG_INFO
, "Copyright (C) 2007 Vermont Department of Taxes");
64 daemon_log(LOG_INFO
, "This is free software; see the source for copying conditions. There is NO");
65 daemon_log(LOG_INFO
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
69 * Displays some usage information, command line parameters and whatnot.
70 * @param program the name of the program.
72 void display_help(char *program
)
74 daemon_log(LOG_INFO
, "Usage: %s [options]", program
);
75 daemon_log(LOG_INFO
, "Options:");
76 daemon_log(LOG_INFO
, " -h --help Show this help message");
77 daemon_log(LOG_INFO
, " -v --version Show version information");
78 daemon_log(LOG_INFO
, " -k --kill Kill the running instance");
79 daemon_log(LOG_INFO
, " -f --foreground Run in the foreground");
83 * A command line parser using getopts.
84 * @param argc The number of command line arguments coming in argv.
85 * @param argv The command line arguments.
86 * @return Returns 0 on success and non-zero when we want the program to terminate.
88 int parse_args(int argc
, char **argv
)
93 static struct option long_options
[] = {
94 {"help", no_argument
, 0, 'h'},
95 {"version", no_argument
, 0, 'v'},
96 {"kill", no_argument
, 0, 'k'},
97 {"foreground", no_argument
, 0, 'f'},
108 c
= getopt_long(argc
, argv
, "hvkf", long_options
, &option_index
);
115 display_help(argv
[0]);
123 ret
= daemon_pid_file_kill_wait(SIGQUIT
, 30);
125 daemon_log(LOG_ERR
, "Daemon not killed: (%s)", strerror(errno
));
135 daemon_log(LOG_ERR
, "Unsupported option");
144 int main(int argc
, char *argv
[], char *envp
[])
150 /* Default Values for Global Variables */
156 if (argc
< 1 || !argv
|| !argv
[0]) {
157 daemon_log(LOG_ERR
, "(%u:%s) Cannot determine program name from argv[0]\n");
161 daemon_pid_file_ident
= daemon_log_ident
= daemon_ident_from_argv0(argv
[0]);
163 if (geteuid() != 0) {
164 daemon_log(LOG_ERR
, "You need root privileges to run this application.");
168 /* Command Line Arguements */
169 ret
= parse_args(argc
, argv
);
171 return (killed
? 0 : ret
);
174 pid
= daemon_pid_file_is_running();
176 daemon_log(LOG_ERR
, "%s is already running (PID => %u)", argv
[0], daemon_log_ident
, pid
);
177 daemon_log(LOG_INFO
, "Use `%s -k` to kill the running instance", daemon_log_ident
);
183 /* Configure Logging */
184 daemon_log_use
= DAEMON_LOG_SYSLOG
;
191 } else if (pid
> 0) {
200 } else if (pid
> 0) {
206 daemon_log(LOG_ERR
, "Could not chdir() to '/': %s", strerror(errno
));
210 /* close open file descriptors */
211 for (fd
= 0; fd
< getdtablesize(); fd
++) {
213 if (ret
== -1 && errno
!= EBADF
) {
214 daemon_log(LOG_ERR
, "Could not close fd #%d: %s", fd
, strerror(errno
));
219 /* re-open stdin, stdout, stderr */
220 fd
= open("/dev/null", O_RDONLY
);
221 fd
= open("/dev/null", O_WRONLY
);
222 fd
= open("/dev/null", O_WRONLY
);
225 ret
= daemon_pid_file_create();
227 daemon_log(LOG_ERR
, "Could not create PID file: %s", strerror(errno
));
231 /* this must run before any threads are created */
235 std::list
<config::config
*> *conf
;
236 conf
= config_parse();
238 /* Install Signal Handlers */
239 install_signal_handlers();
241 /* Initialize Virus Detection Engine and Load Virus Definitions */
245 std::list
<config::config
*>::iterator itr
= conf
->begin();
247 /* Begin Monitoring watch_dir for Viruses */
248 inotify_main(*itr
, clamav
);
250 /* Free resources used by libclamav */
253 monitor_wait(); /* thread cleanup */
257 daemon_pid_file_remove();
259 daemon_log(LOG_INFO
, "Exiting...");