Merge changes submitted by Matt Gagne <matt.gagne@state.vt.us>
[inoclam.git] / inoclam.cxx
blob5f8836af393a1e1fa840ca21527ad128f5e9d9ee
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 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
19 * Contributor(s):
20 * Tom Cort <tom.cort@state.vt.us>
21 * Matt Gagne <matt.gagne@state.vt.us>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <iostream>
27 #include <libdaemon/dlog.h>
28 #include <libdaemon/dpid.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
36 #include "config.hxx"
37 #include "clam.hxx"
38 #include "inoclam.hxx"
39 #include "inotify.hxx"
40 #include "monitor.hxx"
41 #include "signal.hxx"
42 #include "smtp.hxx"
44 /**
45 * Determines if gatewayavd should run in the background (daemonized) or
46 * not. If daemonize is 1, then gatewayavd should run in the background.
48 int daemonize;
50 /**
51 * Determines if our process killed a running inoclam process successfully.
53 int killed;
55 /**
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.");
66 /**
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");
80 /**
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)
88 int option_index;
89 int done;
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'},
96 {0, 0, 0, 0}
99 option_index = 0;
100 done = 0;
102 while (!done) {
103 int c;
104 int ret;
106 c = getopt_long(argc, argv, "hvkf", long_options, &option_index);
107 if (c < 0) {
108 break;
111 switch (c) {
112 case 'h':
113 display_help(argv[0]);
114 done = 1;
115 break;
116 case 'v':
117 display_version();
118 done = 1;
119 break;
120 case 'k':
121 ret = daemon_pid_file_kill_wait(SIGQUIT, 30);
122 if (ret < 0) {
123 daemon_log(LOG_ERR, "Daemon not killed: (%s)", strerror(errno));
124 } else {
125 killed = 1;
127 done = 1;
128 break;
129 case 'f':
130 daemonize = 0;
131 break;
132 default:
133 daemon_log(LOG_ERR, "Unsupported option");
134 done = 1;
135 break;
139 return done;
142 int main(int argc, char *argv[], char *envp[])
144 int fd;
145 int ret;
146 pid_t pid;
148 /* Default Values for Global Variables */
149 daemonize = 1;
150 killed = 0;
151 exit_now = 0;
153 /* Sanity Checks */
154 if (argc < 1 || !argv || !argv[0]) {
155 daemon_log(LOG_ERR, "(%u:%s) Cannot determine program name from argv[0]\n");
156 return 1;
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.");
163 return 1;
166 /* Command Line Arguements */
167 ret = parse_args(argc, argv);
168 if (ret) {
169 return (killed ? 0 : ret);
172 pid = daemon_pid_file_is_running();
173 if (pid > 0) {
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);
176 return 1;
179 /* Daemonize */
180 if (daemonize) {
181 /* Configure Logging */
182 daemon_log_use = DAEMON_LOG_SYSLOG;
184 umask(0);
186 pid = fork();
187 if (pid < 0) {
188 return 1;
189 } else if (pid > 0) {
190 return 0;
193 setsid();
195 pid = fork();
196 if (pid < 0) {
197 return 1;
198 } else if (pid > 0) {
199 return 0;
202 ret = chdir("/");
203 if (ret < 0) {
204 daemon_log(LOG_ERR, "Could not chdir() to '/': %s", strerror(errno));
205 return 1;
208 /* close open file descriptors */
209 for (fd = 0; fd < getdtablesize(); fd++) {
210 ret = close(fd);
211 if (ret == -1 && errno != EBADF) {
212 daemon_log(LOG_ERR, "Could not close fd #%d: %s", fd, strerror(errno));
213 return 1;
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();
224 if (ret < 0) {
225 daemon_log(LOG_ERR, "Could not create PID file: %s", strerror(errno));
226 return 1;
229 /* this must run before any threads are created */
230 monitor_init();
232 /* Configure */
233 config *conf;
234 conf = new config;
236 conf->parse();
238 /* Install Signal Handlers */
239 install_signal_handlers();
241 /* Initialize Virus Detection Engine and Load Virus Definitions */
242 clam_init();
244 /* Begin Monitoring watch_dir for Viruses */
245 inotify_main(conf);
247 /* Free resources used by libclamav */
248 clam_exit();
250 monitor_wait(); /* thread cleanup */
251 pthread_mutex_destroy(&engine_lock);
252 pthread_attr_destroy(&ta);
254 conf->config_free();
255 delete conf;
256 daemon_pid_file_remove();
258 daemon_log(LOG_INFO, "Exiting...");
259 return 0;