ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/main.c
Revision: 5072
Committed: Mon Dec 22 15:33:29 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 6631 byte(s)
Log Message:
- Fixed a bunch of compile warnings

File Contents

# Content
1 /* vim: set shiftwidth=3 softtabstop=3 expandtab: */
2
3 /*
4 Copyright (C) 2002-2003 Erik Fears
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18
19 Foundation, Inc.
20 59 Temple Place - Suite 330
21 Boston, MA 02111-1307, USA.
22
23 */
24
25 #include "setup.h"
26
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <signal.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/resource.h> /* getrlimit */
37 #include <fcntl.h>
38
39 #ifdef STDC_HEADERS
40 #include <stdlib.h>
41 #include <string.h>
42 #endif
43
44 #include "compat.h"
45 #include "config.h"
46 #include "extern.h"
47 #include "irc.h"
48 #include "log.h"
49 #include "opercmd.h"
50 #include "scan.h"
51 #include "stats.h"
52 #include "negcache.h"
53 #include "options.h"
54 #include "malloc.h"
55 #include "firedns.h"
56 #include "main.h"
57
58 RCSID("$Id: main.c,v 1.18 2003/06/22 13:19:39 andy Exp $");
59
60 static RETSIGTYPE do_signal(int signum);
61
62 int RESTART = 0; /* Flagged to restart on next cycle */
63 int ALARMED = 0; /* Flagged to call timer functions on next cycle */
64 int REOPEN = 0; /* Flagged to reopen log files on next cycle */
65 unsigned int OPT_DEBUG = 0; /* Debug level */
66
67 char *CONFNAME = DEFAULTNAME;
68 static const char *CONFDIR = HOPM_ETCDIR;
69 static const char *LOGDIR = HOPM_LOGDIR;
70 char *CONFFILE, *LOGFILE;
71
72 struct sigaction ALARMACTION;
73 struct sigaction INTACTION;
74 struct sigaction USR1ACTION;
75
76 int main(int argc, char **argv)
77 {
78 char spid[16];
79 pid_t pid;
80 int c;
81 size_t lenc, lenl;
82 unsigned int i;
83 FILE *pidout;
84 struct rlimit rlim;
85
86 while (1)
87 {
88 c = getopt(argc, argv, "dc:");
89
90 if (c == -1)
91 break;
92
93 switch (c)
94 {
95 case 'c':
96 CONFNAME = strdup(optarg);
97 break;
98 case 'd':
99 OPT_DEBUG++;
100 break;
101 case '?':
102 default:
103 /* Unknown arg, guess we'll just do nothing for now. */
104 break;
105 }
106 }
107
108 lenc = strlen(CONFDIR) + strlen(CONFNAME) + strlen(CONFEXT) + 3;
109 lenl = strlen(LOGDIR) + strlen(CONFNAME) + strlen(LOGEXT) + 3;
110
111 CONFFILE = MyMalloc(lenc * sizeof *CONFFILE);
112 LOGFILE = MyMalloc(lenl * sizeof *LOGFILE);
113
114 snprintf(CONFFILE, lenc, "%s/%s.%s", CONFDIR, CONFNAME, CONFEXT);
115 snprintf(LOGFILE, lenl, "%s/%s.%s", LOGDIR, CONFNAME, LOGEXT);
116
117 /* Fork off. */
118
119 if (OPT_DEBUG <= 0)
120 {
121 if ((pid = fork()) < 0)
122 {
123 perror("fork()");
124 exit(EXIT_FAILURE);
125 }
126 else if (pid != 0)
127 {
128 _exit(EXIT_SUCCESS);
129 }
130
131 /* Get us in our own process group. */
132 if (setpgid(0, 0) < 0)
133 {
134 perror("setpgid()");
135 exit(EXIT_FAILURE);
136 }
137
138 /* Reset file mode. */
139 /* shasta: o+w is BAD, mmkay? */
140 umask(002);
141
142 /* Close file descriptors. */
143 close(STDIN_FILENO);
144 close(STDOUT_FILENO);
145 close(STDERR_FILENO);
146
147 log_open(LOGFILE);
148 }
149 else
150 log_printf("MAIN -> Debug level %d", OPT_DEBUG);
151
152
153 log_printf("MAIN -> HOPM %s started.", VERSION);
154 log_printf("MAIN -> Reading configuration file...");
155
156 config_load(CONFFILE);
157
158 if (OptionsItem->scanlog)
159 scanlog_open(OptionsItem->scanlog);
160
161 pid = getpid();
162
163 pidout = fopen(OptionsItem->pidfile, "w");
164 snprintf(spid, 16, "%u", pid);
165
166 if (pidout)
167 {
168 fwrite(spid, sizeof(char), strlen(spid), pidout);
169 fclose(pidout);
170 }
171 else
172 {
173 log_printf("MAIN -> Error opening %s: %s", OptionsItem->pidfile,
174 strerror(errno));
175 exit(EXIT_FAILURE);
176 }
177
178 /* Setup alarm & int handlers. */
179
180 ALARMACTION.sa_handler = &(do_signal);
181 ALARMACTION.sa_flags = SA_RESTART;
182 INTACTION.sa_handler = &(do_signal);
183 USR1ACTION.sa_handler = &(do_signal);
184
185 sigaction(SIGALRM, &ALARMACTION, 0);
186 sigaction(SIGINT, &INTACTION, 0);
187 sigaction(SIGUSR1, &USR1ACTION, 0);
188
189 /* Ignore SIGPIPE. */
190 signal(SIGPIPE, SIG_IGN);
191
192 alarm(1);
193
194 while (1)
195 {
196
197
198 /* Main cycles */
199 irc_cycle();
200 scan_cycle();
201
202
203 /* Restart bopm if main_restart() was called (usually happens by m_kill in irc.c) */
204 if(RESTART)
205 {
206 /* If restarted in debug mode, die */
207 if(OPT_DEBUG)
208 return(1);
209
210 log_printf("MAIN -> Restarting process");
211
212 /* Get upper file descriptor limit */
213 if(getrlimit(RLIMIT_NOFILE, &rlim) == -1)
214 {
215 log_printf("MAIN RESTART -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno));
216 return(1);
217 }
218
219 /* Set file descriptors 0-rlim_cur close on exec */
220 for(i = 0; i < rlim.rlim_cur; i++)
221 fcntl(i, F_SETFD, FD_CLOEXEC);
222
223 /* execute new process */
224 if(execve(argv[0], argv, NULL) == -1)
225 log_printf("MAIN RESTART -> Execution of \"%s\" failed. ERROR: %s", argv[0], strerror(errno));
226
227 /* Should only get here if execve failed */
228 RESTART = 0;
229 }
230
231 /* Check for log reopen */
232 if(REOPEN)
233 {
234 log_printf("MAIN -> Caught SIGUSR1, reopening logfiles");
235 log_close();
236 log_open(LOGFILE);
237
238 if(OptionsItem->scanlog)
239 {
240 scanlog_close();
241 scanlog_open(OptionsItem->scanlog);
242 }
243
244 log_printf("MAIN -> reopened logfiles");
245
246 REOPEN = 0;
247 }
248
249 /* Call 1 second timers */
250 if(ALARMED)
251 {
252 irc_timer();
253 scan_timer();
254 command_timer();
255
256 ALARMED = 0;
257 }
258
259
260 }
261
262 if (!OPT_DEBUG)
263 log_close();
264
265 /* If there's no scanlog open then this will do nothing anyway */
266 scanlog_close();
267
268 return(0);
269 }
270
271 static void do_signal(int signum)
272 {
273 switch (signum)
274 {
275 case SIGALRM:
276 ALARMED = 1;
277 alarm(1);
278 break;
279 case SIGINT:
280 log_printf("MAIN -> Caught SIGINT, bye!");
281 exit(0);
282 break;
283 case SIGUSR1:
284 REOPEN = 1;
285 break;
286 }
287 }
288
289
290 void main_restart(void)
291 {
292 RESTART = 1;
293 }