/[svn]/hopm/trunk/src/main.c
ViewVC logotype

Contents of /hopm/trunk/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5069 - (show annotations)
Mon Dec 22 14:57:12 2014 UTC (7 years, 9 months ago) by michael
File MIME type: text/x-chdr
File size: 6709 byte(s)
- Replaced more references of bopm with hopm

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 char *CONFDIR = HOPM_ETCDIR;
69 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, lenp;
82 unsigned int nc_counter, i;
83 FILE *pidout;
84 struct rlimit rlim;
85
86 nc_counter = 0;
87
88 while (1)
89 {
90 c = getopt(argc, argv, "dc:");
91
92 if (c == -1)
93 break;
94
95 switch (c)
96 {
97 case 'c':
98 CONFNAME = strdup(optarg);
99 break;
100 case 'd':
101 OPT_DEBUG++;
102 break;
103 case '?':
104 default:
105 /* Unknown arg, guess we'll just do nothing for now. */
106 break;
107 }
108 }
109
110 lenc = strlen(CONFDIR) + strlen(CONFNAME) + strlen(CONFEXT) + 3;
111 lenl = strlen(LOGDIR) + strlen(CONFNAME) + strlen(LOGEXT) + 3;
112 lenp = strlen(LOGDIR) + strlen(CONFNAME) + strlen(PIDEXT) + 3;
113
114 CONFFILE = MyMalloc(lenc * sizeof *CONFFILE);
115 LOGFILE = MyMalloc(lenl * sizeof *LOGFILE);
116
117 snprintf(CONFFILE, lenc, "%s/%s.%s", CONFDIR, CONFNAME, CONFEXT);
118 snprintf(LOGFILE, lenl, "%s/%s.%s", LOGDIR, CONFNAME, LOGEXT);
119
120 /* Fork off. */
121
122 if (OPT_DEBUG <= 0)
123 {
124 if ((pid = fork()) < 0)
125 {
126 perror("fork()");
127 exit(EXIT_FAILURE);
128 }
129 else if (pid != 0)
130 {
131 _exit(EXIT_SUCCESS);
132 }
133
134 /* Get us in our own process group. */
135 if (setpgid(0, 0) < 0)
136 {
137 perror("setpgid()");
138 exit(EXIT_FAILURE);
139 }
140
141 /* Reset file mode. */
142 /* shasta: o+w is BAD, mmkay? */
143 umask(002);
144
145 /* Close file descriptors. */
146 close(STDIN_FILENO);
147 close(STDOUT_FILENO);
148 close(STDERR_FILENO);
149
150 log_open(LOGFILE);
151 }
152 else
153 log_printf("MAIN -> Debug level %d", OPT_DEBUG);
154
155
156 log_printf("MAIN -> HOPM %s started.", VERSION);
157 log_printf("MAIN -> Reading configuration file...");
158
159 config_load(CONFFILE);
160
161 if (OptionsItem->scanlog)
162 scanlog_open(OptionsItem->scanlog);
163
164 pid = getpid();
165
166 pidout = fopen(OptionsItem->pidfile, "w");
167 snprintf(spid, 16, "%u", pid);
168
169 if (pidout)
170 {
171 fwrite(spid, sizeof(char), strlen(spid), pidout);
172 fclose(pidout);
173 }
174 else
175 {
176 log_printf("MAIN -> Error opening %s: %s", OptionsItem->pidfile,
177 strerror(errno));
178 exit(EXIT_FAILURE);
179 }
180
181 /* Setup alarm & int handlers. */
182
183 ALARMACTION.sa_handler = &(do_signal);
184 ALARMACTION.sa_flags = SA_RESTART;
185 INTACTION.sa_handler = &(do_signal);
186 USR1ACTION.sa_handler = &(do_signal);
187
188 sigaction(SIGALRM, &ALARMACTION, 0);
189 sigaction(SIGINT, &INTACTION, 0);
190 sigaction(SIGUSR1, &USR1ACTION, 0);
191
192 /* Ignore SIGPIPE. */
193 signal(SIGPIPE, SIG_IGN);
194
195 alarm(1);
196
197 while (1)
198 {
199
200
201 /* Main cycles */
202 irc_cycle();
203 scan_cycle();
204
205
206 /* Restart bopm if main_restart() was called (usually happens by m_kill in irc.c) */
207 if(RESTART)
208 {
209 /* If restarted in debug mode, die */
210 if(OPT_DEBUG)
211 return(1);
212
213 log_printf("MAIN -> Restarting process");
214
215 /* Get upper file descriptor limit */
216 if(getrlimit(RLIMIT_NOFILE, &rlim) == -1)
217 {
218 log_printf("MAIN RESTART -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno));
219 return(1);
220 }
221
222 /* Set file descriptors 0-rlim_cur close on exec */
223 for(i = 0; i < rlim.rlim_cur; i++)
224 fcntl(i, F_SETFD, FD_CLOEXEC);
225
226 /* execute new process */
227 if(execve(argv[0], argv, NULL) == -1)
228 log_printf("MAIN RESTART -> Execution of \"%s\" failed. ERROR: %s", argv[0], strerror(errno));
229
230 /* Should only get here if execve failed */
231 RESTART = 0;
232 }
233
234 /* Check for log reopen */
235 if(REOPEN)
236 {
237 log_printf("MAIN -> Caught SIGUSR1, reopening logfiles");
238 log_close();
239 log_open(LOGFILE);
240
241 if(OptionsItem->scanlog)
242 {
243 scanlog_close();
244 scanlog_open(OptionsItem->scanlog);
245 }
246
247 log_printf("MAIN -> reopened logfiles");
248
249 REOPEN = 0;
250 }
251
252 /* Call 1 second timers */
253 if(ALARMED)
254 {
255 irc_timer();
256 scan_timer();
257 command_timer();
258
259 ALARMED = 0;
260 }
261
262
263 }
264
265 if (!OPT_DEBUG)
266 log_close();
267
268 /* If there's no scanlog open then this will do nothing anyway */
269 scanlog_close();
270
271 return(0);
272 }
273
274 static void do_signal(int signum)
275 {
276 switch (signum)
277 {
278 case SIGALRM:
279 ALARMED = 1;
280 alarm(1);
281 break;
282 case SIGINT:
283 log_printf("MAIN -> Caught SIGINT, bye!");
284 exit(0);
285 break;
286 case SIGUSR1:
287 REOPEN = 1;
288 break;
289 }
290 }
291
292
293 void main_restart(void)
294 {
295 RESTART = 1;
296 }

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28