/[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.139 - (show annotations) (download) (as text)
Tue Feb 25 06:52:19 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.138: +21 -99 lines
File MIME type: text/x-chdr
* Well, I think Tcl is finally removed from the core (except for detection/configuration).

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23