Clarify License => GNU General Public License version 2.
[inoclam.git] / src / inoclam.cxx
bloba5e2c94d6d340ed31ec12b9e4dacec830e427c43
1 /*
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
18 * Contributor(s):
19 * Tom Cort <tom.cort@state.vt.us>
20 * Matt Gagne <matt.gagne@state.vt.us>
23 #include <list>
24 #include <iterator>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <iostream>
28 #include <libdaemon/dlog.h>
29 #include <libdaemon/dpid.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
37 #include "config.hxx"
38 #include "clam.hxx"
39 #include "inoclam.hxx"
40 #include "inotify.hxx"
41 #include "monitor.hxx"
42 #include "signal.hxx"
43 #include "smtp.hxx"
45 /**
46 * Determines if gatewayavd should run in the background (daemonized) or
47 * not. If daemonize is 1, then gatewayavd should run in the background.
49 int daemonize;
51 /**
52 * Determines if our process killed a running inoclam process successfully.
54 int killed;
56 /**
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.");
67 /**
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");
81 /**
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)
89 int option_index;
90 int done;
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'},
97 {0, 0, 0, 0}
100 option_index = 0;
101 done = 0;
103 while (!done) {
104 int c;
105 int ret;
107 c = getopt_long(argc, argv, "hvkf", long_options, &option_index);
108 if (c < 0) {
109 break;
112 switch (c) {
113 case 'h':
114 display_help(argv[0]);
115 done = 1;
116 break;
117 case 'v':
118 display_version();
119 done = 1;
120 break;
121 case 'k':
122 ret = daemon_pid_file_kill_wait(SIGQUIT, 30);
123 if (ret < 0) {
124 daemon_log(LOG_ERR, "Daemon not killed: (%s)", strerror(errno));
125 } else {
126 killed = 1;
128 done = 1;
129 break;
130 case 'f':
131 daemonize = 0;
132 break;
133 default:
134 daemon_log(LOG_ERR, "Unsupported option");
135 done = 1;
136 break;
140 return done;
143 int main(int argc, char *argv[], char *envp[])
145 int fd;
146 int ret;
147 pid_t pid;
148 pthread_t watch_thread;
149 pthread_attr_t watch_thread_attr;
151 /* Default Values for Global Variables */
152 daemonize = 1;
153 killed = 0;
154 exit_now = 0;
156 /* Sanity Checks */
157 if (argc < 1 || !argv || !argv[0]) {
158 daemon_log(LOG_ERR, "(%u:%s) Cannot determine program name from argv[0]\n");
159 return 1;
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.");
166 return 1;
169 /* Command Line Arguements */
170 ret = parse_args(argc, argv);
171 if (ret) {
172 return (killed ? 0 : ret);
175 pid = daemon_pid_file_is_running();
176 if (pid > 0) {
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);
179 return 1;
182 /* Daemonize */
183 if (daemonize) {
184 /* Configure Logging */
185 daemon_log_use = DAEMON_LOG_SYSLOG;
187 umask(0);
189 pid = fork();
190 if (pid < 0) {
191 return 1;
192 } else if (pid > 0) {
193 return 0;
196 setsid();
198 pid = fork();
199 if (pid < 0) {
200 return 1;
201 } else if (pid > 0) {
202 return 0;
205 ret = chdir("/");
206 if (ret < 0) {
207 daemon_log(LOG_ERR, "Could not chdir() to '/': %s", strerror(errno));
208 return 1;
211 /* close open file descriptors */
212 for (fd = 0; fd < getdtablesize(); fd++) {
213 ret = close(fd);
214 if (ret == -1 && errno != EBADF) {
215 daemon_log(LOG_ERR, "Could not close fd #%d: %s", fd, strerror(errno));
216 return 1;
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();
227 if (ret < 0) {
228 daemon_log(LOG_ERR, "Could not create PID file: %s", strerror(errno));
229 return 1;
232 /* this must run before any threads are created */
233 monitor_init();
235 /* Configure */
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 */
243 clam *clamav;
244 clamav = new clam();
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));
253 if (!args) {
254 daemon_log(LOG_ERR, "MALLOC FAILED!");
255 exit_now = 1;
256 } else {
257 args->conf = *itr;
258 args->clamav = clamav;
260 monitor_inc();
261 ret = pthread_create(&watch_thread, &watch_thread_attr, inotify_main, (void *) args);
262 if (ret) {
263 monitor_dec();
264 daemon_log(LOG_ERR, "Can't create watch thread: %s", strerror(errno));
265 exit_now = 1;
270 while (!exit_now) {
271 sleep(3);
272 sched_yield();
275 monitor_wait(); /* thread cleanup */
277 /* Free resources used by libclamav */
278 delete clamav;
280 for (std::list < config::config * >::iterator itr = conf->begin(); itr != conf->end(); ++itr) {
281 delete *itr;
284 delete conf;
285 daemon_pid_file_remove();
287 daemon_log(LOG_INFO, "Exiting...");
288 return 0;