/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Contents of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.149 - (show annotations) (download) (as text)
Mon May 12 11:54:53 2003 UTC (16 years, 2 months ago) by wingman
Branch: MAIN
Changes since 1.148: +1 -5 lines
File MIME type: text/x-chdr
 * removed some compile warnings here and there
 * removed some old notes code which was still in our core

1 /*
2 * main.c --
3 *
4 * core event handling
5 * signal handling
6 * command line arguments
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Eggheads Development Team
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26 /*
27 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 * NOTE: Robey is no longer working on this code, there is a discussion
29 * list available at eggheads@eggheads.org.
30 */
31
32 #ifndef lint
33 static const char rcsid[] = "$Id: main.c,v 1.148 2003/05/11 02:33:32 stdarg Exp $";
34 #endif
35
36 #include <unistd.h>
37 #include <eggdrop/eggdrop.h>
38 #include "main.h"
39 #include "core_config.h"
40 #include <fcntl.h>
41 #ifdef TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 # else
48 # include <time.h>
49 # endif
50 #endif
51 #include <sys/stat.h>
52 #include <errno.h>
53 #include <signal.h>
54 #include <netdb.h>
55 #include <setjmp.h>
56 #include <locale.h>
57 #include <ctype.h>
58 #include <ltdl.h>
59
60 #ifdef STOP_UAC /* osf/1 complains a lot */
61 #include <sys/sysinfo.h>
62 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
63 #endif
64 /* Some systems have a working sys/wait.h even though configure will
65 * decide it's not bsd compatable. Oh well.
66 */
67
68 #include "bg.h"
69 #include "core_binds.h"
70 #include "logfile.h"
71
72 #ifdef CYGWIN_HACKS
73 #include <windows.h>
74 #endif
75
76 #ifndef _POSIX_SOURCE
77 /* Solaris needs this */
78 #define _POSIX_SOURCE 1
79 #endif
80
81 char egg_version[1024] = VERSION;
82 int egg_numver = VERSION_NUM;
83
84 eggdrop_t *egg = NULL; /* Eggdrop's context */
85
86 int default_flags = 0; /* Default user flags and */
87 int default_uflags = 0; /* Default userdefinied flags for people
88 who say 'hello' or for .adduser */
89
90 int backgrd = 1; /* Run in the background? */
91 int con_chan = 0; /* Foreground: constantly display channel
92 stats? */
93 int term_z = 0; /* Foreground: use the terminal as a party
94 line? */
95 char configfile[121] = "config.xml"; /* Name of the config file */
96 char helpdir[121] = "help/"; /* Directory of help files (if used) */
97 char textdir[121] = "text/"; /* Directory for text files that get dumped */
98 time_t online_since; /* Unix-time that the bot loaded up */
99 int make_userfile = 0; /* Using bot in make-userfile mode? (first
100 user to 'hello' becomes master) */
101 char pid_file[120]; /* Name of the file for the pid to be
102 stored in */
103 int save_users_at = 0; /* How many minutes past the hour to
104 save the userfile? */
105 char version[81]; /* Version info (long form) */
106 char ver[41]; /* Version info (short form) */
107 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
108 int do_restart = 0; /* .restart has been called, restart asap */
109 int die_on_sighup = 0; /* die if bot receives SIGHUP */
110 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
111 int resolve_timeout = 15; /* hostname/address lookup timeout */
112 char quit_msg[1024]; /* quit message */
113 time_t now; /* duh, now :) */
114 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
115
116 void fatal(const char *s, int recoverable)
117 {
118 putlog(LOG_MISC, "*", "* %s", s);
119 flushlogs();
120 unlink(pid_file);
121 if (!recoverable) {
122 bg_send_quit(BG_ABORT);
123 exit(1);
124 }
125 }
126
127 static void got_bus(int z)
128 {
129 fatal("BUS ERROR -- CRASHING!", 1);
130 #ifdef SA_RESETHAND
131 kill(getpid(), SIGBUS);
132 #else
133 bg_send_quit(BG_ABORT);
134 exit(1);
135 #endif
136 }
137
138 static void got_segv(int z)
139 {
140 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
141 #ifdef SA_RESETHAND
142 kill(getpid(), SIGSEGV);
143 #else
144 bg_send_quit(BG_ABORT);
145 exit(1);
146 #endif
147 }
148
149 static void got_fpe(int z)
150 {
151 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
152 }
153
154 static void got_term(int z)
155 {
156 check_bind_event("sigterm");
157 if (die_on_sigterm) {
158 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
159 } else {
160 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
161 }
162 }
163
164 static void got_quit(int z)
165 {
166 check_bind_event("sigquit");
167 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
168 return;
169 }
170
171 static void got_hup(int z)
172 {
173 check_bind_event("sighup");
174 if (die_on_sighup) {
175 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
176 } else
177 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
178 do_restart = -2;
179 return;
180 }
181
182 /* Got ILL signal
183 */
184 static void got_ill(int z)
185 {
186 check_bind_event("sigill");
187 }
188
189
190 static void print_version(void)
191 {
192 char x[1024];
193
194 strlcpy(x, egg_version, sizeof x);
195 printf("%s\n", version);
196 }
197
198 static void print_help(void)
199 {
200 print_version ();
201 printf("\n\
202 Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
203 printf(_("\n\
204 -h, --help Print help and exit\n\
205 -v, --version Print version and exit\n\
206 -n, --foreground Don't go into the background (default=off)\n\
207 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
208 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
209 -m --make-userfile Userfile creation mode\n\
210 FILE optional config filename (default 'config.xml')\n"));
211 printf("\n");
212 }
213
214 static void do_args(int argc, char * const *argv)
215 {
216 int c;
217
218 optarg = 0;
219 optind = 1;
220 opterr = 1;
221 optopt = '?';
222
223 while (1) {
224 int option_index = 0;
225 static struct option long_options[] = {
226 { "help", 0, NULL, 'h' },
227 { "version", 0, NULL, 'v' },
228 { "load-module", 1, NULL, 'p' },
229 { "foreground", 0, NULL, 'n' },
230 { "channel-stats", 0, NULL, 'c' },
231 { "terminal", 0, NULL, 't' },
232 { "make-userfile", 0, NULL, 'm' },
233 { NULL, 0, NULL, 0 }
234 };
235
236 c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
237
238 if (c == -1) break;
239
240 switch (c) {
241 case 'n':
242 backgrd = 0;
243 break;
244
245 case 'c':
246 con_chan = 1;
247 term_z = 0;
248 break;
249
250 case 't':
251 con_chan = 0;
252 term_z = 1;
253 break;
254
255 case 'm':
256 make_userfile = 1;
257 break;
258
259 case 'v':
260 print_version();
261 bg_send_quit(BG_ABORT);
262 exit(0);
263 break; /* this should never be reached */
264
265 case 'h':
266 print_help();
267 bg_send_quit(BG_ABORT);
268 exit(0);
269 break; /* this should never be reached */
270
271 case 0: /* Long option with no short option */
272 case '?': /* Invalid option. */
273 /* getopt_long() already printed an error message. */
274 bg_send_quit(BG_ABORT);
275 exit(1);
276
277 default: /* bug: option not considered. */
278 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
279 bg_send_quit(BG_ABORT);
280 exit(1);
281 break; /* this should never be reached */
282 }
283 }
284 if (optind < argc)
285 strlcpy(configfile, argv[optind], sizeof configfile);
286 }
287
288 /* Timer info */
289 static int lastmin = 99;
290 static struct tm nowtm;
291
292 /* Called once a second.
293 */
294 static int core_secondly()
295 {
296 static int cnt = 0;
297 int miltime;
298
299 check_bind_secondly();
300 cnt++;
301 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
302 if (nowtm.tm_min != lastmin) {
303 int i = 0;
304
305 /* Once a minute */
306 lastmin = (lastmin + 1) % 60;
307 check_bind_event("minutely");
308 check_bind_time(&nowtm);
309 /* In case for some reason more than 1 min has passed: */
310 while (nowtm.tm_min != lastmin) {
311 /* Timer drift, dammit */
312 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin,
313 nowtm.tm_min);
314 i++;
315 lastmin = (lastmin + 1) % 60;
316 check_bind_event("minutely");
317 }
318 if (i > 1)
319 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
320 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
321 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
322 check_bind_event("5minutely");
323 if (!miltime) { /* At midnight */
324 char s[25];
325
326 strlcpy(s, ctime(&now), sizeof s);
327 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
328 check_bind_event("backup");
329 check_bind_event("daily");
330 }
331 }
332 }
333 return(0);
334 }
335
336 void core_party_init();
337 void core_config_init();
338 void telnet_init();
339
340 int file_check(const char *filename)
341 {
342 struct stat sbuf = {0};
343 int r;
344
345 r = stat(filename, &sbuf);
346 if (r) return(-1);
347 errno = 0;
348 if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
349 return(0);
350 }
351
352 int main(int argc, char **argv)
353 {
354 int xx, i;
355 char s[25];
356 FILE *f;
357 struct sigaction sv;
358 egg_timeval_t howlong;
359 int timeout;
360 void *config_root, *entry;
361 char *modname, *scriptname;
362
363 #ifdef DEBUG
364 /* Make sure it can write core, if you make debug. Else it's pretty
365 * useless (dw)
366 */
367 {
368 #include <sys/resource.h>
369 struct rlimit cdlim;
370
371 cdlim.rlim_cur = RLIM_INFINITY;
372 cdlim.rlim_max = RLIM_INFINITY;
373 setrlimit(RLIMIT_CORE, &cdlim);
374 }
375 #endif
376
377 #ifdef ENABLE_NLS
378 setlocale(LC_MESSAGES, "");
379 bindtextdomain(PACKAGE, LOCALEDIR);
380 textdomain(PACKAGE);
381 #endif
382
383 /* Initialize ltdl. */
384 LTDL_SET_PRELOADED_SYMBOLS();
385 if (lt_dlinit()) {
386 printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
387 return(-1);
388 }
389
390 /* Version info! */
391 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
392 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
393 egg_version);
394 /* Now add on the patchlevel (for Tcl) */
395 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
396 #ifdef STOP_UAC
397 {
398 int nvpair[2];
399
400 nvpair[0] = SSIN_UACPROC;
401 nvpair[1] = UAC_NOPRINT;
402 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
403 }
404 #endif
405
406 /* Set up error traps: */
407 sv.sa_handler = got_bus;
408 sigemptyset(&sv.sa_mask);
409 #ifdef SA_RESETHAND
410 sv.sa_flags = SA_RESETHAND;
411 #else
412 sv.sa_flags = 0;
413 #endif
414 sigaction(SIGBUS, &sv, NULL);
415 sv.sa_handler = got_segv;
416 sigaction(SIGSEGV, &sv, NULL);
417 #ifdef SA_RESETHAND
418 sv.sa_flags = 0;
419 #endif
420 sv.sa_handler = got_fpe;
421 sigaction(SIGFPE, &sv, NULL);
422 sv.sa_handler = got_term;
423 sigaction(SIGTERM, &sv, NULL);
424 sv.sa_handler = got_hup;
425 sigaction(SIGHUP, &sv, NULL);
426 sv.sa_handler = got_quit;
427 sigaction(SIGQUIT, &sv, NULL);
428 sv.sa_handler = SIG_IGN;
429 sigaction(SIGPIPE, &sv, NULL);
430 sv.sa_handler = got_ill;
431 sigaction(SIGILL, &sv, NULL);
432
433 /* Initialize variables and stuff */
434 timer_update_now(&egg_timeval_now);
435 now = egg_timeval_now.sec;
436 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
437 lastmin = nowtm.tm_min;
438 srandom(now % (getpid() + getppid()));
439
440 do_args(argc, argv);
441
442 printf("\n%s\n", version);
443 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
444
445 /* Don't allow eggdrop to run as root */
446 if (((int) getuid() == 0) || ((int) geteuid() == 0))
447 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
448
449 if (file_check(configfile)) {
450 if (errno) perror(configfile);
451 else fprintf(stderr, "ERROR\n\nCheck file permissions on your config file!\nMake sure other groups and users cannot read/write it.\n");
452 exit(-1);
453 }
454
455 egg = eggdrop_new();
456 config_init();
457 core_config_init(configfile);
458 egglog_init();
459 logfile_init();
460 script_init();
461 user_init();
462 if (core_config.userfile) user_load(core_config.userfile);
463 partyline_init();
464 core_party_init();
465 telnet_init();
466 module_init();
467 egg_net_init();
468 core_binds_init();
469
470 if (backgrd)
471 bg_prepare_split();
472
473 strlcpy(s, ctime(&now), sizeof s);
474 strcpy(&s[11], &s[20]);
475 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
476
477 /* Put the module directory in the ltdl search path. */
478 if (core_config.module_path) module_add_dir(core_config.module_path);
479
480 /* Scan the autoload section of config. */
481 config_root = config_get_root("eggdrop");
482 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
483 modname = NULL;
484 config_get_str(&modname, entry, NULL);
485 module_load(modname);
486 }
487 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
488 scriptname = NULL;
489 config_get_str(&scriptname, entry, NULL);
490 script_load(scriptname);
491 }
492
493
494
495 if (!pid_file[0])
496 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
497
498 /* Check for pre-existing eggdrop! */
499 f = fopen(pid_file, "r");
500 if (f != NULL) {
501 fgets(s, 10, f);
502 xx = atoi(s);
503 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
504 is used */
505 if (errno != ESRCH) {
506 printf(_("I detect %s already running from this directory.\n"), core_config.botname);
507 printf(_("If this is incorrect, erase the %s\n"), pid_file);
508 bg_send_quit(BG_ABORT);
509 exit(1);
510 }
511 }
512
513 /* Move into background? */
514 if (backgrd) {
515 #ifndef CYGWIN_HACKS
516 bg_do_split();
517 } else { /* !backgrd */
518 #endif
519 xx = getpid();
520 if (xx != 0) {
521 FILE *fp;
522
523 /* Write pid to file */
524 unlink(pid_file);
525 fp = fopen(pid_file, "w");
526 if (fp != NULL) {
527 fprintf(fp, "%u\n", xx);
528 if (fflush(fp)) {
529 /* Let the bot live since this doesn't appear to be a botchk */
530 printf(_("* Warning! Could not write %s file!\n"), pid_file);
531 fclose(fp);
532 unlink(pid_file);
533 } else
534 fclose(fp);
535 } else
536 printf(_("* Warning! Could not write %s file!\n"), pid_file);
537 #ifdef CYGWIN_HACKS
538 printf("Launched into the background (pid: %d)\n\n", xx);
539 #endif
540 }
541 }
542
543 use_stderr = 0; /* Stop writing to stderr now */
544 if (backgrd) {
545 /* Ok, try to disassociate from controlling terminal (finger cross) */
546 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
547 setpgid(0, 0);
548 #endif
549 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
550 freopen("/dev/null", "r", stdin);
551 freopen("/dev/null", "w", stdout);
552 freopen("/dev/null", "w", stderr);
553 #ifdef CYGWIN_HACKS
554 FreeConsole();
555 #endif
556 }
557
558 online_since = now;
559 howlong.sec = 1;
560 howlong.usec = 0;
561 timer_create_repeater(&howlong, "main loop", core_secondly);
562
563 putlog(LOG_DEBUG, "*", "main: entering loop");
564 while (1) {
565 timer_update_now(&egg_timeval_now);
566 now = egg_timeval_now.sec;
567 random(); /* Woop, lets really jumble things */
568 timer_run();
569
570 if (timer_get_shortest(&howlong)) {
571 timeout = 1000;
572 }
573 else {
574 timeout = howlong.sec * 1000 + howlong.usec / 1000;
575 }
576 sockbuf_update_all(timeout);
577 }
578 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23