Implement daemonization and command line arguements.
[inoclam.git] / inoclam.cxx
blob4241b2f94cd238d698758ff6a6a3b7e51adc8202
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>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <iostream>
26 #include <libdaemon/dlog.h>
27 #include <libdaemon/dpid.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
35 #include "config.hxx"
36 #include "clam.hxx"
37 #include "inotify.hxx"
38 #include "monitor.hxx"
39 #include "signal.hxx"
40 #include "smtp.hxx"
42 #define VERSION "1.0.0"
44 #define CODENAME "Bender"
46 /**
47 * Determines if our process killed a running inoclam process successfully.
49 int killed;
51 /**
52 * Displays some version and copyright information upon request (-v or --version).
54 void display_version()
56 daemon_log(LOG_INFO, "inoclam v%s (%s)", VERSION, CODENAME);
57 daemon_log(LOG_INFO, "Copyright (C) 2007 Vermont Department of Taxes");
58 daemon_log(LOG_INFO, "This is free software; see the source for copying conditions. There is NO");
59 daemon_log(LOG_INFO, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
62 /**
63 * Displays some usage information, command line parameters and whatnot.
64 * @param program the name of the program.
66 void display_help(char *program)
68 daemon_log(LOG_INFO, "Usage: %s [options]", program);
69 daemon_log(LOG_INFO, "Options:");
70 daemon_log(LOG_INFO, " -h --help Show this help message");
71 daemon_log(LOG_INFO, " -v --version Show version information");
72 daemon_log(LOG_INFO, " -k --kill Kill the running instance");
75 /**
76 * A command line parser using getopts.
77 * @param argc The number of command line arguments coming in argv.
78 * @param argv The command line arguments.
79 * @return Returns 0 on success and non-zero when we want the program to terminate.
81 int parse_args(int argc, char **argv)
83 int option_index;
84 int done;
86 static struct option long_options[] = {
87 {"help", no_argument, 0, 'h'},
88 {"version", no_argument, 0, 'v'},
89 {"kill", no_argument, 0, 'k'},
90 {0, 0, 0, 0}
93 option_index = 0;
94 done = 0;
96 while (!done) {
97 int c;
98 int ret;
100 c = getopt_long(argc, argv, "hvk", long_options, &option_index);
101 if (c < 0) {
102 break;
105 switch (c) {
106 case 'h':
107 display_help(argv[0]);
108 done = 1;
109 break;
110 case 'v':
111 display_version();
112 done = 1;
113 break;
114 case 'k':
115 ret = daemon_pid_file_kill_wait(SIGQUIT, 30);
116 if (ret < 0) {
117 daemon_log(LOG_ERR, "Daemon not killed: (%s)", strerror(errno));
118 } else {
119 killed = 1;
121 done = 1;
122 break;
123 default:
124 daemon_log(LOG_ERR, "Unsupported option");
125 done = 1;
126 break;
130 return done;
133 int main(int argc, char *argv[], char *envp[])
135 int fd;
136 int ret;
137 pid_t pid;
139 /* Default Values for Global Variables */
140 killed = 0;
142 exit_now = 0;
144 watch_dir = NULL;
146 virus_removal_enabled = cfg_true;
148 smtp_enabled = cfg_true;
149 smtp_host = NULL;
150 smtp_port = 0;
151 smtp_to = NULL;
152 smtp_from = NULL;
153 smtp_subject = NULL;
155 /* Sanity Checks */
156 if (argc < 1 || !argv || !argv[0]) {
157 daemon_log(LOG_ERR, "(%u:%s) Cannot determine program name from argv[0]\n");
158 return 1;
161 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
163 if (geteuid() != 0) {
164 daemon_log(LOG_ERR, "You need root privileges to run this application.");
165 return 1;
168 /* Command Line Arguements */
169 ret = parse_args(argc, argv);
170 if (ret) {
171 return (killed ? 0 : ret);
174 pid = daemon_pid_file_is_running();
175 if (pid > 0) {
176 daemon_log(LOG_ERR, "%s is already running (PID => %u)", argv[0], daemon_log_ident, pid);
177 daemon_log(LOG_INFO, "Use `%s -k` to kill the running instance", daemon_log_ident);
178 return 1;
181 /* Configure Logging */
182 daemon_log_use = DAEMON_LOG_SYSLOG;
184 /* Daemonize */
185 umask(0);
187 pid = fork();
188 if (pid < 0) {
189 return 1;
190 } else if (pid > 0) {
191 return 0;
194 setsid();
196 pid = fork();
197 if (pid < 0) {
198 return 1;
199 } else if (pid > 0) {
200 return 0;
203 ret = chdir("/");
204 if (ret < 0) {
205 daemon_log(LOG_ERR, "Could not chdir() to '/': %s", strerror(errno));
206 return 1;
209 /* close open file descriptors */
210 for (fd = 0; fd < getdtablesize(); fd++) {
211 ret = close(fd);
212 if (ret == -1 && errno != EBADF) {
213 daemon_log(LOG_ERR, "Could not close fd #%d: %s", fd, strerror(errno));
214 return 1;
218 /* re-open stdin, stdout, stderr */
219 fd = open("/dev/null", O_RDONLY);
220 fd = open("/dev/null", O_WRONLY);
221 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_parse();
235 /* Install Signal Handlers */
236 install_signal_handlers();
238 /* Initialize Virus Detection Engine and Load Virus Definitions */
239 clam_init();
241 /* Begin Monitoring watch_dir for Viruses */
242 inotify_main(watch_dir);
244 /* Free resources used by libclamav */
245 clam_exit();
247 monitor_wait(); /* thread cleanup */
248 pthread_mutex_destroy(&engine_lock);
249 pthread_attr_destroy(&ta);
251 config_free();
252 daemon_pid_file_remove();
254 daemon_log(LOG_INFO, "Exiting...");
255 return 0;