/[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.140 - (show annotations) (download) (as text)
Tue Feb 25 10:28:22 2003 UTC (16 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.139: +23 -49 lines
File MIME type: text/x-chdr
* Cleanups

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.139 2003/02/25 06:52:19 stdarg Exp $";
34 #endif
35
36 #include <eggdrop/eggdrop.h>
37 #include "main.h"
38 #include "core_config.h"
39 #include <fcntl.h>
40 #ifdef TIME_WITH_SYS_TIME
41 # include <sys/time.h>
42 # include <time.h>
43 #else
44 # ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
46 # else
47 # include <time.h>
48 # endif
49 #endif
50 #include <errno.h>
51 #include <signal.h>
52 #include <netdb.h>
53 #include <setjmp.h>
54 #include "chanprog.h" /* tell_verbose_status,
55 chanprog, rehash */
56 #include <locale.h>
57
58 #ifdef STOP_UAC /* osf/1 complains a lot */
59 #include <sys/sysinfo.h>
60 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
61 #endif
62 /* Some systems have a working sys/wait.h even though configure will
63 * decide it's not bsd compatable. Oh well.
64 */
65
66 #include "chan.h"
67 #include "modules.h"
68 #include "bg.h"
69 #include "core_binds.h"
70 #include "logfile.h"
71 #include "misc.h"
72 #include "traffic.h"
73
74 #ifdef CYGWIN_HACKS
75 #include <windows.h>
76 #endif
77
78 #ifndef _POSIX_SOURCE
79 /* Solaris needs this */
80 #define _POSIX_SOURCE 1
81 #endif
82
83 extern int conmask;
84 extern struct dcc_t *dcc;
85 extern struct userrec *userlist;
86
87 #ifndef MAKING_MODS
88 extern struct dcc_table DCC_CHAT;
89 #endif /* MAKING_MODS */
90
91 /*
92 * Please use the PATCH macro instead of directly altering the version
93 * string from now on (it makes it much easier to maintain patches).
94 * Also please read the README file regarding your rights to distribute
95 * modified versions of this bot.
96 */
97
98 char egg_version[1024] = VERSION;
99 int egg_numver = VERSION_NUM;
100
101 eggdrop_t *egg = NULL; /* Eggdrop's context */
102
103 char notify_new[121] = ""; /* Person to send a note to for new users */
104 int default_flags = 0; /* Default user flags and */
105 int default_uflags = 0; /* Default userdefinied flags for people
106 who say 'hello' or for .adduser */
107
108 int backgrd = 1; /* Run in the background? */
109 int con_chan = 0; /* Foreground: constantly display channel
110 stats? */
111 int term_z = 0; /* Foreground: use the terminal as a party
112 line? */
113 char configfile[121] = "config.xml"; /* Name of the config file */
114 char helpdir[121] = "help/"; /* Directory of help files (if used) */
115 char textdir[121] = "text/"; /* Directory for text files that get dumped */
116 time_t online_since; /* Unix-time that the bot loaded up */
117 int make_userfile = 0; /* Using bot in make-userfile mode? (first
118 user to 'hello' becomes master) */
119 char owner[121] = ""; /* Permanent owner(s) of the bot */
120 char pid_file[120]; /* Name of the file for the pid to be
121 stored in */
122 int save_users_at = 0; /* How many minutes past the hour to
123 save the userfile? */
124 int notify_users_at = 0; /* How many minutes past the hour to
125 notify users of notes? */
126 char version[81]; /* Version info (long form) */
127 char ver[41]; /* Version info (short form) */
128 char egg_xtra[2048]; /* Patch info */
129 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
130 int do_restart = 0; /* .restart has been called, restart asap */
131 int die_on_sighup = 0; /* die if bot receives SIGHUP */
132 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
133 int resolve_timeout = 15; /* hostname/address lookup timeout */
134 char quit_msg[1024]; /* quit message */
135 time_t now; /* duh, now :) */
136 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
137
138 void fatal(const char *s, int recoverable)
139 {
140 putlog(LOG_MISC, "*", "* %s", s);
141 flushlogs();
142 unlink(pid_file);
143 if (!recoverable) {
144 bg_send_quit(BG_ABORT);
145 exit(1);
146 }
147 }
148
149 static void got_bus(int z)
150 {
151 fatal("BUS ERROR -- CRASHING!", 1);
152 #ifdef SA_RESETHAND
153 kill(getpid(), SIGBUS);
154 #else
155 bg_send_quit(BG_ABORT);
156 exit(1);
157 #endif
158 }
159
160 static void got_segv(int z)
161 {
162 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
163 #ifdef SA_RESETHAND
164 kill(getpid(), SIGSEGV);
165 #else
166 bg_send_quit(BG_ABORT);
167 exit(1);
168 #endif
169 }
170
171 static void got_fpe(int z)
172 {
173 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
174 }
175
176 static void got_term(int z)
177 {
178 check_bind_event("sigterm");
179 if (die_on_sigterm) {
180 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
181 } else {
182 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
183 }
184 }
185
186 static void got_quit(int z)
187 {
188 check_bind_event("sigquit");
189 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
190 return;
191 }
192
193 static void got_hup(int z)
194 {
195 check_bind_event("sighup");
196 if (die_on_sighup) {
197 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
198 } else
199 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
200 do_restart = -2;
201 return;
202 }
203
204 /* Got ILL signal
205 */
206 static void got_ill(int z)
207 {
208 check_bind_event("sigill");
209 }
210
211
212 static void print_version(void)
213 {
214 char x[1024], *z = x;
215
216 strlcpy(x, egg_version, sizeof x);
217 newsplit(&z);
218 newsplit(&z);
219 printf("%s\n", version);
220 if (z[0])
221 printf(" (patches: %s)\n", z);
222 }
223
224 static void print_help(void)
225 {
226 print_version ();
227 printf("\n\
228 Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
229 printf(_("\n\
230 -h, --help Print help and exit\n\
231 -v, --version Print version and exit\n\
232 -n, --foreground Don't go into the background (default=off)\n\
233 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
234 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
235 -m --make-userfile Userfile creation mode\n\
236 FILE optional config filename (default 'config.xml')\n"));
237 printf("\n");
238 }
239
240 static void do_args(int argc, char * const *argv)
241 {
242 int c;
243
244 optarg = 0;
245 optind = 1;
246 opterr = 1;
247 optopt = '?';
248
249 while (1) {
250 int option_index = 0;
251 static struct option long_options[] = {
252 { "help", 0, NULL, 'h' },
253 { "version", 0, NULL, 'v' },
254 { "load-module", 1, NULL, 'p' },
255 { "foreground", 0, NULL, 'n' },
256 { "channel-stats", 0, NULL, 'c' },
257 { "terminal", 0, NULL, 't' },
258 { "make-userfile", 0, NULL, 'm' },
259 { NULL, 0, NULL, 0 }
260 };
261
262 c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
263
264 if (c == -1) break;
265
266 switch (c) {
267 case 'n':
268 backgrd = 0;
269 break;
270
271 case 'c':
272 con_chan = 1;
273 term_z = 0;
274 break;
275
276 case 't':
277 con_chan = 0;
278 term_z = 1;
279 break;
280
281 case 'm':
282 make_userfile = 1;
283 break;
284
285 case 'v':
286 print_version();
287 bg_send_quit(BG_ABORT);
288 exit(0);
289 break; /* this should never be reached */
290
291 case 'h':
292 print_help();
293 bg_send_quit(BG_ABORT);
294 exit(0);
295 break; /* this should never be reached */
296
297 case 0: /* Long option with no short option */
298 case '?': /* Invalid option. */
299 /* getopt_long() already printed an error message. */
300 bg_send_quit(BG_ABORT);
301 exit(1);
302
303 default: /* bug: option not considered. */
304 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
305 bg_send_quit(BG_ABORT);
306 exit(1);
307 break; /* this should never be reached */
308 }
309 }
310 if (optind < argc)
311 strlcpy(configfile, argv[optind], sizeof configfile);
312 }
313
314 /* Timer info */
315 static int lastmin = 99;
316 static struct tm nowtm;
317
318 /* Called once a second.
319 */
320 static int core_secondly()
321 {
322 static int cnt = 0;
323 int miltime;
324
325 call_hook(HOOK_SECONDLY); /* Will be removed later */
326 cnt++;
327 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
328 if (nowtm.tm_min != lastmin) {
329 int i = 0;
330
331 /* Once a minute */
332 lastmin = (lastmin + 1) % 60;
333 call_hook(HOOK_MINUTELY);
334 /* In case for some reason more than 1 min has passed: */
335 while (nowtm.tm_min != lastmin) {
336 /* Timer drift, dammit */
337 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
338 i++;
339 lastmin = (lastmin + 1) % 60;
340 call_hook(HOOK_MINUTELY);
341 }
342 if (i > 1)
343 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
344 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
345 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
346 call_hook(HOOK_5MINUTELY);
347 if (!miltime) { /* At midnight */
348 char s[25];
349
350 strlcpy(s, ctime(&now), sizeof s);
351 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
352 call_hook(HOOK_BACKUP);
353 call_hook(HOOK_DAILY);
354 }
355 }
356 if (nowtm.tm_min == notify_users_at)
357 call_hook(HOOK_HOURLY);
358 }
359 return(0);
360 }
361
362 static void core_minutely()
363 {
364 check_bind_time(&nowtm);
365 }
366
367 static void core_hourly()
368 {
369 }
370
371 static void event_rehash()
372 {
373 check_bind_event("rehash");
374 }
375
376 static void event_prerehash()
377 {
378 check_bind_event("prerehash");
379 }
380
381 static void event_save()
382 {
383 check_bind_event("save");
384 }
385
386 static void event_logfile()
387 {
388 check_bind_event("logfile");
389 }
390
391 static void event_loaded()
392 {
393 check_bind_event("loaded");
394 }
395
396 extern module_entry *module_list;
397
398 void core_party_init();
399 void core_config_init();
400 void telnet_init();
401 void binds_init();
402
403 void patch(const char *str)
404 {
405 char *p = strchr(egg_version, '+');
406
407 if (!p)
408 p = &egg_version[strlen(egg_version)];
409 sprintf(p, "+%s", str);
410 egg_numver++;
411 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
412 }
413
414 /*
415 static inline void garbage_collect(void)
416 {
417 static u_8bit_t run_cnt = 0;
418
419 if (run_cnt == 3)
420 garbage_collect_tclhash();
421 else
422 run_cnt++;
423 }
424 */
425
426 int owner_check(const char *handle)
427 {
428 int len;
429 char *powner;
430
431 len = strlen(handle);
432 if (!len) return(-1);
433
434 powner = core_config.owner;
435 while (powner) {
436 while (*powner && !isalnum(*powner)) powner++;
437 if (!*powner) break;
438 if (!strncasecmp(powner, handle, len)) {
439 powner += len;
440 if (!*powner || isalnum(*powner)) return(0);
441 }
442 while (*powner && isalnum(*powner)) powner++;
443 }
444 return(-1);
445 }
446
447 int main(int argc, char **argv)
448 {
449 int xx, i;
450 char s[25];
451 FILE *f;
452 struct sigaction sv;
453 egg_timeval_t howlong;
454 int timeout;
455 void *config_root, *entry;
456 char *modname, *scriptname;
457
458 #ifdef DEBUG
459 /* Make sure it can write core, if you make debug. Else it's pretty
460 * useless (dw)
461 */
462 {
463 #include <sys/resource.h>
464 struct rlimit cdlim;
465
466 cdlim.rlim_cur = RLIM_INFINITY;
467 cdlim.rlim_max = RLIM_INFINITY;
468 setrlimit(RLIMIT_CORE, &cdlim);
469 }
470 #endif
471
472 #ifdef ENABLE_NLS
473 setlocale(LC_MESSAGES, "");
474 bindtextdomain(PACKAGE, LOCALEDIR);
475 textdomain(PACKAGE);
476 #endif
477
478 #include "patch.h"
479 /* Version info! */
480 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
481 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
482 egg_version);
483 /* Now add on the patchlevel (for Tcl) */
484 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
485 strcat(egg_version, egg_xtra);
486 #ifdef STOP_UAC
487 {
488 int nvpair[2];
489
490 nvpair[0] = SSIN_UACPROC;
491 nvpair[1] = UAC_NOPRINT;
492 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
493 }
494 #endif
495
496 /* Set up error traps: */
497 sv.sa_handler = got_bus;
498 sigemptyset(&sv.sa_mask);
499 #ifdef SA_RESETHAND
500 sv.sa_flags = SA_RESETHAND;
501 #else
502 sv.sa_flags = 0;
503 #endif
504 sigaction(SIGBUS, &sv, NULL);
505 sv.sa_handler = got_segv;
506 sigaction(SIGSEGV, &sv, NULL);
507 #ifdef SA_RESETHAND
508 sv.sa_flags = 0;
509 #endif
510 sv.sa_handler = got_fpe;
511 sigaction(SIGFPE, &sv, NULL);
512 sv.sa_handler = got_term;
513 sigaction(SIGTERM, &sv, NULL);
514 sv.sa_handler = got_hup;
515 sigaction(SIGHUP, &sv, NULL);
516 sv.sa_handler = got_quit;
517 sigaction(SIGQUIT, &sv, NULL);
518 sv.sa_handler = SIG_IGN;
519 sigaction(SIGPIPE, &sv, NULL);
520 sv.sa_handler = got_ill;
521 sigaction(SIGILL, &sv, NULL);
522
523 /* Initialize variables and stuff */
524 timer_update_now(&egg_timeval_now);
525 now = egg_timeval_now.sec;
526 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
527 lastmin = nowtm.tm_min;
528 srandom(now % (getpid() + getppid()));
529
530 do_args(argc, argv);
531
532 printf("\n%s\n", version);
533 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
534
535 /* Don't allow eggdrop to run as root */
536 if (((int) getuid() == 0) || ((int) geteuid() == 0))
537 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
538
539 egg = eggdrop_new();
540 config_init();
541 core_config_init(configfile);
542 egglog_init();
543 logfile_init();
544 script_init();
545 user_init();
546 if (core_config.userfile) user_load(core_config.userfile);
547 partyline_init();
548 core_party_init();
549 telnet_init();
550 modules_init();
551 egg_net_init();
552 core_binds_init();
553
554 if (backgrd)
555 bg_prepare_split();
556
557 strlcpy(s, ctime(&now), sizeof s);
558 strcpy(&s[11], &s[20]);
559 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
560
561 /* Scan the autoload section of config. */
562 config_root = config_get_root("eggdrop");
563 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
564 modname = NULL;
565 config_get_str(&modname, entry, NULL);
566 module_load(modname);
567 }
568 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
569 scriptname = NULL;
570 config_get_str(&scriptname, entry, NULL);
571 script_load(scriptname);
572 }
573
574
575
576 if (!pid_file[0])
577 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
578
579 /* Check for pre-existing eggdrop! */
580 f = fopen(pid_file, "r");
581 if (f != NULL) {
582 fgets(s, 10, f);
583 xx = atoi(s);
584 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
585 is used */
586 if (errno != ESRCH) {
587 printf(_("I detect %s already running from this directory.\n"), core_config.botname);
588 printf(_("If this is incorrect, erase the %s\n"), pid_file);
589 bg_send_quit(BG_ABORT);
590 exit(1);
591 }
592 }
593
594 /* Move into background? */
595 if (backgrd) {
596 #ifndef CYGWIN_HACKS
597 bg_do_split();
598 } else { /* !backgrd */
599 #endif
600 xx = getpid();
601 if (xx != 0) {
602 FILE *fp;
603
604 /* Write pid to file */
605 unlink(pid_file);
606 fp = fopen(pid_file, "w");
607 if (fp != NULL) {
608 fprintf(fp, "%u\n", xx);
609 if (fflush(fp)) {
610 /* Let the bot live since this doesn't appear to be a botchk */
611 printf(_("* Warning! Could not write %s file!\n"), pid_file);
612 fclose(fp);
613 unlink(pid_file);
614 } else
615 fclose(fp);
616 } else
617 printf(_("* Warning! Could not write %s file!\n"), pid_file);
618 #ifdef CYGWIN_HACKS
619 printf("Launched into the background (pid: %d)\n\n", xx);
620 #endif
621 }
622 }
623
624 use_stderr = 0; /* Stop writing to stderr now */
625 if (backgrd) {
626 /* Ok, try to disassociate from controlling terminal (finger cross) */
627 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
628 setpgid(0, 0);
629 #endif
630 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
631 freopen("/dev/null", "r", stdin);
632 freopen("/dev/null", "w", stdout);
633 freopen("/dev/null", "w", stderr);
634 #ifdef CYGWIN_HACKS
635 FreeConsole();
636 #endif
637 }
638
639 online_since = now;
640 howlong.sec = 1;
641 howlong.usec = 0;
642 timer_create_repeater(&howlong, core_secondly);
643
644 /* init time'd hooks */
645 add_hook(HOOK_MINUTELY, (Function) core_minutely);
646 add_hook(HOOK_HOURLY, (Function) core_hourly);
647 add_hook(HOOK_REHASH, (Function) event_rehash);
648 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
649 add_hook(HOOK_USERFILE, (Function) event_save);
650 add_hook(HOOK_DAILY, (Function) event_logfile);
651 add_hook(HOOK_LOADED, (Function) event_loaded);
652
653 call_hook(HOOK_LOADED);
654
655 debug0("main: entering loop");
656 while (1) {
657 timer_update_now(&egg_timeval_now);
658 now = egg_timeval_now.sec;
659 random(); /* Woop, lets really jumble things */
660 timer_run();
661
662 if (timer_get_shortest(&howlong)) {
663 timeout = 1000;
664 }
665 else {
666 timeout = howlong.sec * 1000 + howlong.usec / 1000;
667 }
668 sockbuf_update_all(timeout);
669 }
670 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23