/[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.144 - (show annotations) (download) (as text)
Fri Apr 4 04:04:46 2003 UTC (16 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.143: +2 -2 lines
File MIME type: text/x-chdr
* Added a name field to timers

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23