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>
27 #include <libdaemon/dlog.h>
28 #include <libdaemon/dpid.h>
32 #include <sys/types.h>
38 #include "inoclam.hxx"
39 #include "inotify.hxx"
40 #include "monitor.hxx"
45 * Determines if gatewayavd should run in the background (daemonized) or
46 * not. If daemonize is 1, then gatewayavd should run in the background.
51 * Determines if our process killed a running inoclam process successfully.
56 * Displays some version and copyright information upon request (-v or --version).
58 void display_version()
60 daemon_log(LOG_INFO
, "inoclam v%s (%s)", VERSION
, CODENAME
);
61 daemon_log(LOG_INFO
, "Copyright (C) 2007 Vermont Department of Taxes");
62 daemon_log(LOG_INFO
, "This is free software; see the source for copying conditions. There is NO");
63 daemon_log(LOG_INFO
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
67 * Displays some usage information, command line parameters and whatnot.
68 * @param program the name of the program.
70 void display_help(char *program
)
72 daemon_log(LOG_INFO
, "Usage: %s [options]", program
);
73 daemon_log(LOG_INFO
, "Options:");
74 daemon_log(LOG_INFO
, " -h --help Show this help message");
75 daemon_log(LOG_INFO
, " -v --version Show version information");
76 daemon_log(LOG_INFO
, " -k --kill Kill the running instance");
77 daemon_log(LOG_INFO
, " -f --foreground Run in the foreground");
81 * A command line parser using getopts.
82 * @param argc The number of command line arguments coming in argv.
83 * @param argv The command line arguments.
84 * @return Returns 0 on success and non-zero when we want the program to terminate.
86 int parse_args(int argc
, char **argv
)
91 static struct option long_options
[] = {
92 {"help", no_argument
, 0, 'h'},
93 {"version", no_argument
, 0, 'v'},
94 {"kill", no_argument
, 0, 'k'},
95 {"foreground", no_argument
, 0, 'f'},
106 c
= getopt_long(argc
, argv
, "hvkf", long_options
, &option_index
);
113 display_help(argv
[0]);
121 ret
= daemon_pid_file_kill_wait(SIGQUIT
, 30);
123 daemon_log(LOG_ERR
, "Daemon not killed: (%s)", strerror(errno
));
133 daemon_log(LOG_ERR
, "Unsupported option");
142 int main(int argc
, char *argv
[], char *envp
[])
148 /* Default Values for Global Variables */
154 if (argc
< 1 || !argv
|| !argv
[0]) {
155 daemon_log(LOG_ERR
, "(%u:%s) Cannot determine program name from argv[0]\n");
159 daemon_pid_file_ident
= daemon_log_ident
= daemon_ident_from_argv0(argv
[0]);
161 if (geteuid() != 0) {
162 daemon_log(LOG_ERR
, "You need root privileges to run this application.");
166 /* Command Line Arguements */
167 ret
= parse_args(argc
, argv
);
169 return (killed
? 0 : ret
);
172 pid
= daemon_pid_file_is_running();
174 daemon_log(LOG_ERR
, "%s is already running (PID => %u)", argv
[0], daemon_log_ident
, pid
);
175 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
;
189 } else if (pid
> 0) {
198 } else if (pid
> 0) {
204 daemon_log(LOG_ERR
, "Could not chdir() to '/': %s", strerror(errno
));
208 /* close open file descriptors */
209 for (fd
= 0; fd
< getdtablesize(); fd
++) {
211 if (ret
== -1 && errno
!= EBADF
) {
212 daemon_log(LOG_ERR
, "Could not close fd #%d: %s", fd
, strerror(errno
));
217 /* re-open stdin, stdout, stderr */
218 fd
= open("/dev/null", O_RDONLY
);
219 fd
= open("/dev/null", O_WRONLY
);
220 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 */
238 /* Install Signal Handlers */
239 install_signal_handlers();
241 /* Initialize Virus Detection Engine and Load Virus Definitions */
245 /* Begin Monitoring watch_dir for Viruses */
246 inotify_main(conf
, clamav
);
248 /* Free resources used by libclamav */
251 monitor_wait(); /* thread cleanup */
254 daemon_pid_file_remove();
256 daemon_log(LOG_INFO
, "Exiting...");