2 * inoclam - Inotify+ClamAV virus scanner
3 * Copyright (C) 2007, 2008 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
25 #include <libdaemon/dlog.h>
26 #include <libdaemon/dpid.h>
30 #include <sys/types.h>
36 #include "inoclam.hxx"
37 #include "inotify.hxx"
38 #include "monitor.hxx"
43 * Determines if gatewayavd should run in the background (daemonized) or
44 * not. If daemonize is 1, then gatewayavd should run in the background.
49 * Determines if our process killed a running inoclam process successfully.
54 * Displays some version and copyright information upon request (-v or --version).
56 void display_version()
58 daemon_log(LOG_INFO
, "inoclam v%s (%s)", VERSION
, CODENAME
);
59 daemon_log(LOG_INFO
, "Copyright (C) 2007 Vermont Department of Taxes");
60 daemon_log(LOG_INFO
, "This is free software; see the source for copying conditions. There is NO");
61 daemon_log(LOG_INFO
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
65 * Displays some usage information, command line parameters and whatnot.
66 * @param program the name of the program.
68 void display_help(char *program
)
70 daemon_log(LOG_INFO
, "Usage: %s [options]", program
);
71 daemon_log(LOG_INFO
, "Options:");
72 daemon_log(LOG_INFO
, " -c --config=[FILE] Specifiy an alternate config location");
73 daemon_log(LOG_INFO
, " -f --foreground Run in the foreground");
74 daemon_log(LOG_INFO
, " -h --help Show this help message");
75 daemon_log(LOG_INFO
, " -k --kill Kill the running instance");
76 daemon_log(LOG_INFO
, " -v --version Show version information");
80 * A command line parser using getopts.
81 * @param argc The number of command line arguments coming in argv.
82 * @param argv The command line arguments.
83 * @return Returns 0 on success and non-zero when we want the program to terminate.
85 int parse_args(int argc
, char **argv
)
90 static struct option long_options
[] = {
91 {"help", no_argument
, 0, 'h'},
92 {"version", no_argument
, 0, 'v'},
93 {"kill", no_argument
, 0, 'k'},
94 {"foreground", no_argument
, 0, 'f'},
95 {"config", required_argument
, 0, 'c'},
106 c
= getopt_long(argc
, argv
, "hvkfc:", long_options
, &option_index
);
117 configfile
= strdup(optarg
);
120 display_help(argv
[0]);
128 ret
= daemon_pid_file_kill_wait(SIGQUIT
, 30);
130 daemon_log(LOG_ERR
, "Daemon not killed: (%s)", strerror(errno
));
140 daemon_log(LOG_ERR
, "Unsupported option");
149 int main(int argc
, char *argv
[], char *envp
[])
154 pthread_t watch_thread
;
155 pthread_attr_t watch_thread_attr
;
157 /* Default Values for Global Variables */
161 configfile
= strdup(DEFAULT_CONFIGFILE
);
164 if (argc
< 1 || !argv
|| !argv
[0]) {
165 daemon_log(LOG_ERR
, "(%u:%s) Cannot determine program name from argv[0]\n");
169 daemon_pid_file_ident
= daemon_log_ident
= daemon_ident_from_argv0(argv
[0]);
171 if (geteuid() != 0) {
172 daemon_log(LOG_ERR
, "You need root privileges to run this application.");
176 /* Command Line Arguements */
177 ret
= parse_args(argc
, argv
);
179 return (killed
? 0 : ret
);
182 pid
= daemon_pid_file_is_running();
184 daemon_log(LOG_ERR
, "%s is already running (PID => %u)", argv
[0], daemon_log_ident
, pid
);
185 daemon_log(LOG_INFO
, "Use `%s -k` to kill the running instance", daemon_log_ident
);
191 /* Configure Logging */
192 daemon_log_use
= DAEMON_LOG_SYSLOG
;
199 } else if (pid
> 0) {
208 } else if (pid
> 0) {
214 daemon_log(LOG_ERR
, "Could not chdir() to '/': %s", strerror(errno
));
218 /* close open file descriptors */
219 for (fd
= 0; fd
< getdtablesize(); fd
++) {
221 if (ret
== -1 && errno
!= EBADF
) {
222 daemon_log(LOG_ERR
, "Could not close fd #%d: %s", fd
, strerror(errno
));
227 /* re-open stdin, stdout, stderr */
228 fd
= open("/dev/null", O_RDONLY
);
229 fd
= open("/dev/null", O_WRONLY
);
230 fd
= open("/dev/null", O_WRONLY
);
233 ret
= daemon_pid_file_create();
235 daemon_log(LOG_ERR
, "Could not create PID file: %s", strerror(errno
));
239 /* this must run before any threads are created */
243 std::list
< config::config
* >*conf
;
244 conf
= config_parse();
250 /* Install Signal Handlers */
251 install_signal_handlers();
253 /* Initialize Virus Detection Engine and Load Virus Definitions */
257 pthread_attr_init(&watch_thread_attr
);
258 pthread_attr_setdetachstate(&watch_thread_attr
, PTHREAD_CREATE_DETACHED
);
260 for (std::list
< config::config
* >::iterator itr
= conf
->begin(); itr
!= conf
->end(); ++itr
) {
262 inotify_main_args_t
*args
;
263 args
= (inotify_main_args_t
*) malloc(sizeof(inotify_main_args_t
));
265 daemon_log(LOG_ERR
, "MALLOC FAILED!");
269 args
->clamav
= clamav
;
272 ret
= pthread_create(&watch_thread
, &watch_thread_attr
, inotify_main
, (void *) args
);
275 daemon_log(LOG_ERR
, "Can't create watch thread: %s", strerror(errno
));
286 monitor_wait(); /* thread cleanup */
288 /* Free resources used by libclamav */
291 for (std::list
< config::config
* >::iterator itr
= conf
->begin(); itr
!= conf
->end(); ++itr
) {
296 daemon_pid_file_remove();
298 daemon_log(LOG_INFO
, "Exiting...");