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 version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Tom Cort <tom.cort@state.vt.us>
20 * Matt Gagne <matt.gagne@state.vt.us>
28 #include <libdaemon/dlog.h>
29 #include <libdaemon/dpid.h>
33 #include <sys/types.h>
39 #include "inoclam.hxx"
40 #include "inotify.hxx"
41 #include "monitor.hxx"
46 * Determines if gatewayavd should run in the background (daemonized) or
47 * not. If daemonize is 1, then gatewayavd should run in the background.
52 * Determines if our process killed a running inoclam process successfully.
57 * Displays some version and copyright information upon request (-v or --version).
59 void display_version()
61 daemon_log(LOG_INFO
, "inoclam v%s (%s)", VERSION
, CODENAME
);
62 daemon_log(LOG_INFO
, "Copyright (C) 2007 Vermont Department of Taxes");
63 daemon_log(LOG_INFO
, "This is free software; see the source for copying conditions. There is NO");
64 daemon_log(LOG_INFO
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
68 * Displays some usage information, command line parameters and whatnot.
69 * @param program the name of the program.
71 void display_help(char *program
)
73 daemon_log(LOG_INFO
, "Usage: %s [options]", program
);
74 daemon_log(LOG_INFO
, "Options:");
75 daemon_log(LOG_INFO
, " -h --help Show this help message");
76 daemon_log(LOG_INFO
, " -v --version Show version information");
77 daemon_log(LOG_INFO
, " -k --kill Kill the running instance");
78 daemon_log(LOG_INFO
, " -f --foreground Run in the foreground");
82 * A command line parser using getopts.
83 * @param argc The number of command line arguments coming in argv.
84 * @param argv The command line arguments.
85 * @return Returns 0 on success and non-zero when we want the program to terminate.
87 int parse_args(int argc
, char **argv
)
92 static struct option long_options
[] = {
93 {"help", no_argument
, 0, 'h'},
94 {"version", no_argument
, 0, 'v'},
95 {"kill", no_argument
, 0, 'k'},
96 {"foreground", no_argument
, 0, 'f'},
107 c
= getopt_long(argc
, argv
, "hvkf", long_options
, &option_index
);
114 display_help(argv
[0]);
122 ret
= daemon_pid_file_kill_wait(SIGQUIT
, 30);
124 daemon_log(LOG_ERR
, "Daemon not killed: (%s)", strerror(errno
));
134 daemon_log(LOG_ERR
, "Unsupported option");
143 int main(int argc
, char *argv
[], char *envp
[])
148 pthread_t watch_thread
;
149 pthread_attr_t watch_thread_attr
;
151 /* Default Values for Global Variables */
157 if (argc
< 1 || !argv
|| !argv
[0]) {
158 daemon_log(LOG_ERR
, "(%u:%s) Cannot determine program name from argv[0]\n");
162 daemon_pid_file_ident
= daemon_log_ident
= daemon_ident_from_argv0(argv
[0]);
164 if (geteuid() != 0) {
165 daemon_log(LOG_ERR
, "You need root privileges to run this application.");
169 /* Command Line Arguements */
170 ret
= parse_args(argc
, argv
);
172 return (killed
? 0 : ret
);
175 pid
= daemon_pid_file_is_running();
177 daemon_log(LOG_ERR
, "%s is already running (PID => %u)", argv
[0], daemon_log_ident
, pid
);
178 daemon_log(LOG_INFO
, "Use `%s -k` to kill the running instance", daemon_log_ident
);
184 /* Configure Logging */
185 daemon_log_use
= DAEMON_LOG_SYSLOG
;
192 } else if (pid
> 0) {
201 } else if (pid
> 0) {
207 daemon_log(LOG_ERR
, "Could not chdir() to '/': %s", strerror(errno
));
211 /* close open file descriptors */
212 for (fd
= 0; fd
< getdtablesize(); fd
++) {
214 if (ret
== -1 && errno
!= EBADF
) {
215 daemon_log(LOG_ERR
, "Could not close fd #%d: %s", fd
, strerror(errno
));
220 /* re-open stdin, stdout, stderr */
221 fd
= open("/dev/null", O_RDONLY
);
222 fd
= open("/dev/null", O_WRONLY
);
223 fd
= open("/dev/null", O_WRONLY
);
226 ret
= daemon_pid_file_create();
228 daemon_log(LOG_ERR
, "Could not create PID file: %s", strerror(errno
));
232 /* this must run before any threads are created */
236 std::list
< config::config
* >*conf
;
237 conf
= config_parse();
239 /* Install Signal Handlers */
240 install_signal_handlers();
242 /* Initialize Virus Detection Engine and Load Virus Definitions */
246 pthread_attr_init(&watch_thread_attr
);
247 pthread_attr_setdetachstate(&watch_thread_attr
, PTHREAD_CREATE_DETACHED
);
249 for (std::list
< config::config
* >::iterator itr
= conf
->begin(); itr
!= conf
->end(); ++itr
) {
251 inotify_main_args_t
*args
;
252 args
= (inotify_main_args_t
*) malloc(sizeof(inotify_main_args_t
));
254 daemon_log(LOG_ERR
, "MALLOC FAILED!");
258 args
->clamav
= clamav
;
261 ret
= pthread_create(&watch_thread
, &watch_thread_attr
, inotify_main
, (void *) args
);
264 daemon_log(LOG_ERR
, "Can't create watch thread: %s", strerror(errno
));
275 monitor_wait(); /* thread cleanup */
277 /* Free resources used by libclamav */
280 for (std::list
< config::config
* >::iterator itr
= conf
->begin(); itr
!= conf
->end(); ++itr
) {
285 daemon_pid_file_remove();
287 daemon_log(LOG_INFO
, "Exiting...");