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>
26 #include <libdaemon/dlog.h>
27 #include <libdaemon/dpid.h>
31 #include <sys/types.h>
37 #include "inotify.hxx"
38 #include "monitor.hxx"
42 #define VERSION "1.0.0"
44 #define CODENAME "Bender"
47 * Determines if our process killed a running inoclam process successfully.
52 * Displays some version and copyright information upon request (-v or --version).
54 void display_version()
56 daemon_log(LOG_INFO
, "inoclam v%s (%s)", VERSION
, CODENAME
);
57 daemon_log(LOG_INFO
, "Copyright (C) 2007 Vermont Department of Taxes");
58 daemon_log(LOG_INFO
, "This is free software; see the source for copying conditions. There is NO");
59 daemon_log(LOG_INFO
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
63 * Displays some usage information, command line parameters and whatnot.
64 * @param program the name of the program.
66 void display_help(char *program
)
68 daemon_log(LOG_INFO
, "Usage: %s [options]", program
);
69 daemon_log(LOG_INFO
, "Options:");
70 daemon_log(LOG_INFO
, " -h --help Show this help message");
71 daemon_log(LOG_INFO
, " -v --version Show version information");
72 daemon_log(LOG_INFO
, " -k --kill Kill the running instance");
76 * A command line parser using getopts.
77 * @param argc The number of command line arguments coming in argv.
78 * @param argv The command line arguments.
79 * @return Returns 0 on success and non-zero when we want the program to terminate.
81 int parse_args(int argc
, char **argv
)
86 static struct option long_options
[] = {
87 {"help", no_argument
, 0, 'h'},
88 {"version", no_argument
, 0, 'v'},
89 {"kill", no_argument
, 0, 'k'},
100 c
= getopt_long(argc
, argv
, "hvk", long_options
, &option_index
);
107 display_help(argv
[0]);
115 ret
= daemon_pid_file_kill_wait(SIGQUIT
, 30);
117 daemon_log(LOG_ERR
, "Daemon not killed: (%s)", strerror(errno
));
124 daemon_log(LOG_ERR
, "Unsupported option");
133 int main(int argc
, char *argv
[], char *envp
[])
139 /* Default Values for Global Variables */
146 virus_removal_enabled
= cfg_true
;
148 smtp_enabled
= cfg_true
;
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
);
181 /* Configure Logging */
182 daemon_log_use
= DAEMON_LOG_SYSLOG
;
190 } else if (pid
> 0) {
199 } else if (pid
> 0) {
205 daemon_log(LOG_ERR
, "Could not chdir() to '/': %s", strerror(errno
));
209 /* close open file descriptors */
210 for (fd
= 0; fd
< getdtablesize(); fd
++) {
212 if (ret
== -1 && errno
!= EBADF
) {
213 daemon_log(LOG_ERR
, "Could not close fd #%d: %s", fd
, strerror(errno
));
218 /* re-open stdin, stdout, stderr */
219 fd
= open("/dev/null", O_RDONLY
);
220 fd
= open("/dev/null", O_WRONLY
);
221 fd
= open("/dev/null", O_WRONLY
);
223 ret
= daemon_pid_file_create();
225 daemon_log(LOG_ERR
, "Could not create PID file: %s", strerror(errno
));
229 /* this must run before any threads are created */
235 /* Install Signal Handlers */
236 install_signal_handlers();
238 /* Initialize Virus Detection Engine and Load Virus Definitions */
241 /* Begin Monitoring watch_dir for Viruses */
242 inotify_main(watch_dir
);
244 /* Free resources used by libclamav */
247 monitor_wait(); /* thread cleanup */
248 pthread_mutex_destroy(&engine_lock
);
249 pthread_attr_destroy(&ta
);
252 daemon_pid_file_remove();
254 daemon_log(LOG_INFO
, "Exiting...");