/[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.65 - (show annotations) (download) (as text)
Sat Jul 14 12:37:08 2001 UTC (18 years, 1 month ago) by poptix
Branch: MAIN
Changes since 1.64: +10 -4 lines
File MIME type: text/x-chdr

Cosmetic TCL fixes, plus TCL threaded info, primarily ITE's patch.

1 /*
2 * main.c -- handles:
3 * core event handling
4 * signal handling
5 * command line arguments
6 * context and assert debugging
7 *
8 * $Id: main.c,v 1.64 2001/07/05 22:19:51 guppy Exp $
9 */
10 /*
11 * Copyright (C) 1997 Robey Pointer
12 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28 /*
29 * The author (Robey Pointer) can be reached at: robey@netcom.com
30 * NOTE: Robey is no long working on this code, there is a discussion
31 * list available at eggheads@eggheads.org.
32 */
33
34 #include "main.h"
35 #include <fcntl.h>
36 #include <time.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <netdb.h>
40 #include <setjmp.h>
41
42 #ifdef STOP_UAC /* osf/1 complains a lot */
43 #include <sys/sysinfo.h>
44 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
45 #endif
46 /* Some systems have a working sys/wait.h even though configure will
47 * decide it's not bsd compatable. Oh well.
48 */
49
50 #include "chan.h"
51 #include "modules.h"
52 #include "tandem.h"
53 #include "bg.h"
54
55 #ifdef CYGWIN_HACKS
56 #include <windows.h>
57 #endif
58
59 #ifndef _POSIX_SOURCE
60 /* Solaris needs this */
61 #define _POSIX_SOURCE 1
62 #endif
63
64 extern char origbotname[], userfile[], botnetnick[];
65 extern int dcc_total, conmask, cache_hit, cache_miss,
66 max_logs, quick_logs;
67 extern struct dcc_t *dcc;
68 extern struct userrec *userlist;
69 extern struct chanset_t *chanset;
70 extern log_t *logs;
71 extern Tcl_Interp *interp;
72 extern tcl_timer_t *timer,
73 *utimer;
74 extern jmp_buf alarmret;
75
76
77 /*
78 * Please use the PATCH macro instead of directly altering the version
79 * string from now on (it makes it much easier to maintain patches).
80 * Also please read the README file regarding your rights to distribute
81 * modified versions of this bot.
82 */
83
84 char egg_version[1024] = "1.6.5";
85 int egg_numver = 1060500;
86
87 char notify_new[121] = ""; /* Person to send a note to for new users */
88 int default_flags = 0; /* Default user flags and */
89 int default_uflags = 0; /* Default userdefinied flags for people
90 who say 'hello' or for .adduser */
91
92 int backgrd = 1; /* Run in the background? */
93 int con_chan = 0; /* Foreground: constantly display channel
94 stats? */
95 int term_z = 0; /* Foreground: use the terminal as a party
96 line? */
97 char configfile[121] = "eggdrop.conf"; /* Name of the config file */
98 char helpdir[121]; /* Directory of help files (if used) */
99 char textdir[121] = ""; /* Directory for text files that get dumped */
100 int keep_all_logs = 0; /* Never erase logfiles, no matter how old
101 they are? */
102 char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix. */
103 time_t online_since; /* Unix-time that the bot loaded up */
104 int make_userfile = 0; /* Using bot in make-userfile mode? (first
105 user to 'hello' becomes master) */
106 char owner[121] = ""; /* Permanent owner(s) of the bot */
107 char pid_file[HANDLEN + 5]; /* Name of the file for the pid to be
108 stored in */
109 int save_users_at = 0; /* How many minutes past the hour to
110 save the userfile? */
111 int notify_users_at = 0; /* How many minutes past the hour to
112 notify users of notes? */
113 int switch_logfiles_at = 300; /* When (military time) to switch logfiles */
114 char version[81]; /* Version info (long form) */
115 char ver[41]; /* Version info (short form) */
116 char egg_xtra[2048]; /* Patch info */
117 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
118 int do_restart = 0; /* .restart has been called, restart asap */
119 int die_on_sighup = 0; /* die if bot receives SIGHUP */
120 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
121 int resolve_timeout = 15; /* hostname/address lookup timeout */
122 time_t now; /* duh, now :) */
123
124 /* Traffic stats
125 */
126 unsigned long otraffic_irc = 0;
127 unsigned long otraffic_irc_today = 0;
128 unsigned long otraffic_bn = 0;
129 unsigned long otraffic_bn_today = 0;
130 unsigned long otraffic_dcc = 0;
131 unsigned long otraffic_dcc_today = 0;
132 unsigned long otraffic_filesys = 0;
133 unsigned long otraffic_filesys_today = 0;
134 unsigned long otraffic_trans = 0;
135 unsigned long otraffic_trans_today = 0;
136 unsigned long otraffic_unknown = 0;
137 unsigned long otraffic_unknown_today = 0;
138 unsigned long itraffic_irc = 0;
139 unsigned long itraffic_irc_today = 0;
140 unsigned long itraffic_bn = 0;
141 unsigned long itraffic_bn_today = 0;
142 unsigned long itraffic_dcc = 0;
143 unsigned long itraffic_dcc_today = 0;
144 unsigned long itraffic_trans = 0;
145 unsigned long itraffic_trans_today = 0;
146 unsigned long itraffic_unknown = 0;
147 unsigned long itraffic_unknown_today = 0;
148
149 #ifdef DEBUG_CONTEXT
150 /* Context storage for fatal crashes */
151 char cx_file[16][30];
152 char cx_note[16][256];
153 int cx_line[16];
154 int cx_ptr = 0;
155 #endif
156
157
158 void fatal(const char *s, int recoverable)
159 {
160 int i;
161
162 putlog(LOG_MISC, "*", "* %s", s);
163 flushlogs();
164 for (i = 0; i < dcc_total; i++)
165 if (dcc[i].sock >= 0)
166 killsock(dcc[i].sock);
167 unlink(pid_file);
168 if (!recoverable) {
169 bg_send_quit(BG_ABORT);
170 exit(1);
171 }
172 }
173
174
175 int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
176 expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
177 expmem_modules(int), expmem_language(), expmem_tcldcc();
178
179 /* For mem.c : calculate memory we SHOULD be using
180 */
181 int expected_memory(void)
182 {
183 int tot;
184
185 tot = expmem_chanprog() + expmem_users() + expmem_misc() +
186 expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
187 expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
188 return tot;
189 }
190
191 static void check_expired_dcc()
192 {
193 int i;
194
195 for (i = 0; i < dcc_total; i++)
196 if (dcc[i].type && dcc[i].type->timeout_val &&
197 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
198 if (dcc[i].type->timeout)
199 dcc[i].type->timeout(i);
200 else if (dcc[i].type->eof)
201 dcc[i].type->eof(i);
202 else
203 continue;
204 /* Only timeout 1 socket per cycle, too risky for more */
205 return;
206 }
207 }
208
209 #ifdef DEBUG_CONTEXT
210 static int nested_debug = 0;
211
212 void write_debug()
213 {
214 int x;
215 char s[25];
216 int y;
217
218 if (nested_debug) {
219 /* Yoicks, if we have this there's serious trouble!
220 * All of these are pretty reliable, so we'll try these.
221 *
222 * NOTE: dont try and display context-notes in here, it's
223 * _not_ safe <cybah>
224 */
225 x = creat("DEBUG.DEBUG", 0644);
226 setsock(x, SOCK_NONSOCK);
227 if (x >= 0) {
228 strncpyz(s, ctime(&now), sizeof s);
229 dprintf(-x, "Debug (%s) written %s\n", ver, s);
230 dprintf(-x, "Please report problem to bugs@eggheads.org\n");
231 dprintf(-x, "after a visit to http://www.eggheads.org/bugzilla/\n");
232 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
233 dprintf(-x, "Context: ");
234 cx_ptr = cx_ptr & 15;
235 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
236 dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
237 dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
238 killsock(x);
239 close(x);
240 }
241 bg_send_quit(BG_ABORT);
242 exit(1); /* Dont even try & tell people about, that may
243 have caused the fault last time. */
244 } else
245 nested_debug = 1;
246 putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
247 cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
248 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
249 putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
250 x = creat("DEBUG", 0644);
251 setsock(x, SOCK_NONSOCK);
252 if (x < 0) {
253 putlog(LOG_MISC, "*", "* Failed to write DEBUG");
254 } else {
255 strncpyz(s, ctime(&now), sizeof s);
256 dprintf(-x, "Debug (%s) written %s\n", ver, s);
257 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
258 #ifdef STATIC
259 dprintf(-x, "STATICALLY LINKED\n");
260 #endif
261
262 /* info library */
263 dprintf(-x, "TCL library: %s\n",
264 ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
265 interp->result : "*unknown*");
266
267 /* info tclversion */
268 dprintf(-x, "TCL version: %s (header version %s)\n",
269 ((interp) && (Tcl_Eval(interp, "info tclversion") == TCL_OK)) ?
270 interp->result : "*unknown*", TCL_VERSION);
271
272 /* info patchlevel */
273 dprintf(-x, "TCL patchlevel: %s (header patchlevel %s)\n",
274 ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
275 interp->result : "*unknown*",
276 TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL : "*unknown*");
277
278 #if HAVE_TCL_THREADS
279 dprintf(-x, "TCL is threaded\n");
280 #else
281 dprintf(-x, "TCL isn't threaded\n");
282 #endif
283
284 #ifdef CCFLAGS
285 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
286 #endif
287 #ifdef LDFLAGS
288 dprintf(-x, "Link flags: %s\n", LDFLAGS);
289 #endif
290 #ifdef STRIPFLAGS
291 dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
292 #endif
293
294 dprintf(-x, "Context: ");
295 cx_ptr = cx_ptr & 15;
296 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
297 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
298 (cx_note[y][0]) ? cx_note[y] : "");
299 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
300 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
301 tell_dcc(-x);
302 dprintf(-x, "\n");
303 debug_mem_to_dcc(-x);
304 killsock(x);
305 close(x);
306 putlog(LOG_MISC, "*", "* Wrote DEBUG");
307 }
308 }
309 #endif
310
311 static void got_bus(int z)
312 {
313 #ifdef DEBUG_CONTEXT
314 write_debug();
315 #endif
316 fatal("BUS ERROR -- CRASHING!", 1);
317 #ifdef SA_RESETHAND
318 kill(getpid(), SIGBUS);
319 #else
320 bg_send_quit(BG_ABORT);
321 exit(1);
322 #endif
323 }
324
325 static void got_segv(int z)
326 {
327 #ifdef DEBUG_CONTEXT
328 write_debug();
329 #endif
330 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
331 #ifdef SA_RESETHAND
332 kill(getpid(), SIGSEGV);
333 #else
334 bg_send_quit(BG_ABORT);
335 exit(1);
336 #endif
337 }
338
339 static void got_fpe(int z)
340 {
341 #ifdef DEBUG_CONTEXT
342 write_debug();
343 #endif
344 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
345 }
346
347 static void got_term(int z)
348 {
349 write_userfile(-1);
350 check_tcl_event("sigterm");
351 if (die_on_sigterm) {
352 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
353 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
354 } else {
355 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
356 }
357 }
358
359 static void got_quit(int z)
360 {
361 check_tcl_event("sigquit");
362 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
363 return;
364 }
365
366 static void got_hup(int z)
367 {
368 write_userfile(-1);
369 check_tcl_event("sighup");
370 if (die_on_sighup) {
371 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
372 } else
373 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
374 do_restart = -2;
375 return;
376 }
377
378 /* A call to resolver (gethostbyname, etc) timed out
379 */
380 static void got_alarm(int z)
381 {
382 longjmp(alarmret, 1);
383
384 /* -Never reached- */
385 }
386
387 /* Got ILL signal -- log context and continue
388 */
389 static void got_ill(int z)
390 {
391 check_tcl_event("sigill");
392 #ifdef DEBUG_CONTEXT
393 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
394 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
395 #endif
396 }
397
398 #ifdef DEBUG_CONTEXT
399 /* Context */
400 void eggContext(const char *file, int line, const char *module)
401 {
402 char x[31], *p;
403
404 p = strrchr(file, '/');
405 if (!module) {
406 strncpyz(x, p ? p + 1 : file, sizeof x);
407 } else
408 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
409 cx_ptr = ((cx_ptr + 1) & 15);
410 strcpy(cx_file[cx_ptr], x);
411 cx_line[cx_ptr] = line;
412 cx_note[cx_ptr][0] = 0;
413 }
414
415 /* Called from the ContextNote macro.
416 */
417 void eggContextNote(const char *file, int line, const char *module,
418 const char *note)
419 {
420 char x[31], *p;
421
422 p = strrchr(file, '/');
423 if (!module) {
424 strncpyz(x, p ? p + 1 : file, sizeof x);
425 } else
426 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
427 cx_ptr = ((cx_ptr + 1) & 15);
428 strcpy(cx_file[cx_ptr], x);
429 cx_line[cx_ptr] = line;
430 strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
431 }
432 #endif
433
434 #ifdef DEBUG_ASSERT
435 /* Called from the Assert macro.
436 */
437 void eggAssert(const char *file, int line, const char *module)
438 {
439 #ifdef DEBUG_CONTEXT
440 write_debug();
441 #endif
442 if (!module)
443 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
444 else
445 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
446 fatal("ASSERT FAILED -- CRASHING!", 1);
447 }
448 #endif
449
450 static void do_arg(char *s)
451 {
452 char x[1024], *z = x;
453 int i;
454
455 if (s[0] == '-')
456 for (i = 1; i < strlen(s); i++) {
457 switch (s[i]) {
458 case 'n':
459 backgrd = 0;
460 break;
461 case 'c':
462 con_chan = 1;
463 term_z = 0;
464 break;
465 case 't':
466 con_chan = 0;
467 term_z = 1;
468 break;
469 case 'm':
470 make_userfile = 1;
471 break;
472 case 'v':
473 strncpyz(x, egg_version, sizeof x);
474 newsplit(&z);
475 newsplit(&z);
476 printf("%s\n", version);
477 if (z[0])
478 printf(" (patches: %s)\n", z);
479 bg_send_quit(BG_ABORT);
480 exit(0);
481 break; /* this should never be reached */
482 case 'h':
483 printf("\n%s\n\n", version);
484 printf(EGG_USAGE);
485 printf("\n");
486 bg_send_quit(BG_ABORT);
487 exit(0);
488 break; /* this should never be reached */
489 }
490 } else
491 strncpyz(configfile, s, sizeof configfile);
492 }
493
494 void backup_userfile(void)
495 {
496 char s[125];
497
498 putlog(LOG_MISC, "*", USERF_BACKUP);
499 egg_snprintf(s, sizeof s, "%s~bak", userfile);
500 copyfile(userfile, s);
501 }
502
503 /* Timer info */
504 static int lastmin = 99;
505 static time_t then;
506 static struct tm nowtm;
507
508 /* Called once a second.
509 *
510 * Note: Try to not put any Context lines in here (guppy 21Mar2000).
511 */
512 static void core_secondly()
513 {
514 static int cnt = 0;
515 int miltime;
516
517 do_check_timers(&utimer); /* Secondly timers */
518 cnt++;
519 if (cnt >= 10) { /* Every 10 seconds */
520 cnt = 0;
521 check_expired_dcc();
522 if (con_chan && !backgrd) {
523 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
524 tell_verbose_status(DP_STDOUT);
525 do_module_report(DP_STDOUT, 0, "server");
526 do_module_report(DP_STDOUT, 0, "channels");
527 tell_mem_status_dcc(DP_STDOUT);
528 }
529 }
530 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
531 if (nowtm.tm_min != lastmin) {
532 int i = 0;
533
534 /* Once a minute */
535 lastmin = (lastmin + 1) % 60;
536 call_hook(HOOK_MINUTELY);
537 check_expired_ignores();
538 autolink_cycle(NULL); /* Attempt autolinks */
539 /* In case for some reason more than 1 min has passed: */
540 while (nowtm.tm_min != lastmin) {
541 /* Timer drift, dammit */
542 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
543 i++;
544 lastmin = (lastmin + 1) % 60;
545 call_hook(HOOK_MINUTELY);
546 }
547 if (i > 1)
548 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
549 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
550 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
551 call_hook(HOOK_5MINUTELY);
552 check_botnet_pings();
553 if (!quick_logs) {
554 flushlogs();
555 check_logsize();
556 }
557 if (!miltime) { /* At midnight */
558 char s[25];
559 int j;
560
561 strncpyz(s, ctime(&now), sizeof s);
562 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
563 backup_userfile();
564 for (j = 0; j < max_logs; j++) {
565 if (logs[j].filename != NULL && logs[j].f != NULL) {
566 fclose(logs[j].f);
567 logs[j].f = NULL;
568 }
569 }
570 }
571 }
572 if (nowtm.tm_min == notify_users_at)
573 call_hook(HOOK_HOURLY);
574 /* These no longer need checking since they are all check vs minutely
575 * settings and we only get this far on the minute.
576 */
577 if (miltime == switch_logfiles_at) {
578 call_hook(HOOK_DAILY);
579 if (!keep_all_logs) {
580 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
581 for (i = 0; i < max_logs; i++)
582 if (logs[i].filename) {
583 char s[1024];
584
585 if (logs[i].f) {
586 fclose(logs[i].f);
587 logs[i].f = NULL;
588 }
589 egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
590 unlink(s);
591 movefile(logs[i].filename, s);
592 }
593 }
594 }
595 }
596 }
597
598 static void core_minutely()
599 {
600 check_tcl_time(&nowtm);
601 do_check_timers(&timer);
602 if (quick_logs != 0) {
603 flushlogs();
604 check_logsize();
605 }
606 }
607
608 static void core_hourly()
609 {
610 write_userfile(-1);
611 }
612
613 static void event_rehash()
614 {
615 check_tcl_event("rehash");
616 }
617
618 static void event_prerehash()
619 {
620 check_tcl_event("prerehash");
621 }
622
623 static void event_save()
624 {
625 check_tcl_event("save");
626 }
627
628 static void event_logfile()
629 {
630 check_tcl_event("logfile");
631 }
632
633 static void event_resettraffic()
634 {
635 otraffic_irc += otraffic_irc_today;
636 itraffic_irc += itraffic_irc_today;
637 otraffic_bn += otraffic_bn_today;
638 itraffic_bn += itraffic_bn_today;
639 otraffic_dcc += otraffic_dcc_today;
640 itraffic_dcc += itraffic_dcc_today;
641 otraffic_unknown += otraffic_unknown_today;
642 itraffic_unknown += itraffic_unknown_today;
643 otraffic_trans += otraffic_trans_today;
644 itraffic_trans += itraffic_trans_today;
645 otraffic_irc_today = otraffic_bn_today = 0;
646 otraffic_dcc_today = otraffic_unknown_today = 0;
647 itraffic_irc_today = itraffic_bn_today = 0;
648 itraffic_dcc_today = itraffic_unknown_today = 0;
649 itraffic_trans_today = otraffic_trans_today = 0;
650 }
651
652 static void event_loaded()
653 {
654 check_tcl_event("loaded");
655 }
656
657 void kill_tcl();
658 extern module_entry *module_list;
659 void restart_chons();
660
661 #ifdef STATIC
662 void check_static(char *, char *(*)());
663
664 #include "mod/static.h"
665 #endif
666 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
667 init_net(), init_modules(), init_tcl(int, char **),
668 init_language(int);
669
670 void patch(const char *str)
671 {
672 char *p = strchr(egg_version, '+');
673
674 if (!p)
675 p = &egg_version[strlen(egg_version)];
676 sprintf(p, "+%s", str);
677 egg_numver++;
678 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
679 }
680
681 static inline void garbage_collect(void)
682 {
683 static u_8bit_t run_cnt = 0;
684
685 if (run_cnt == 3)
686 garbage_collect_tclhash();
687 else
688 run_cnt++;
689 }
690
691 int main(int argc, char **argv)
692 {
693 int xx, i;
694 char buf[520], s[25];
695 FILE *f;
696 struct sigaction sv;
697 struct chanset_t *chan;
698
699 #ifdef DEBUG_MEM
700 /* Make sure it can write core, if you make debug. Else it's pretty
701 * useless (dw)
702 */
703 {
704 #include <sys/resource.h>
705 struct rlimit cdlim;
706
707 cdlim.rlim_cur = RLIM_INFINITY;
708 cdlim.rlim_max = RLIM_INFINITY;
709 setrlimit(RLIMIT_CORE, &cdlim);
710 }
711 #endif
712
713 /* Initialise context list */
714 for (i = 0; i < 16; i++)
715 Context;
716
717 #include "patch.h"
718 /* Version info! */
719 egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
720 egg_snprintf(version, sizeof version, "Eggdrop v%s (C)1997 Robey Pointer (C)2001 Eggheads",
721 egg_version);
722 /* Now add on the patchlevel (for Tcl) */
723 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
724 strcat(egg_version, egg_xtra);
725 #ifdef STOP_UAC
726 {
727 int nvpair[2];
728
729 nvpair[0] = SSIN_UACPROC;
730 nvpair[1] = UAC_NOPRINT;
731 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
732 }
733 #endif
734
735 /* Set up error traps: */
736 sv.sa_handler = got_bus;
737 sigemptyset(&sv.sa_mask);
738 #ifdef SA_RESETHAND
739 sv.sa_flags = SA_RESETHAND;
740 #else
741 sv.sa_flags = 0;
742 #endif
743 sigaction(SIGBUS, &sv, NULL);
744 sv.sa_handler = got_segv;
745 sigaction(SIGSEGV, &sv, NULL);
746 #ifdef SA_RESETHAND
747 sv.sa_flags = 0;
748 #endif
749 sv.sa_handler = got_fpe;
750 sigaction(SIGFPE, &sv, NULL);
751 sv.sa_handler = got_term;
752 sigaction(SIGTERM, &sv, NULL);
753 sv.sa_handler = got_hup;
754 sigaction(SIGHUP, &sv, NULL);
755 sv.sa_handler = got_quit;
756 sigaction(SIGQUIT, &sv, NULL);
757 sv.sa_handler = SIG_IGN;
758 sigaction(SIGPIPE, &sv, NULL);
759 sv.sa_handler = got_ill;
760 sigaction(SIGILL, &sv, NULL);
761 sv.sa_handler = got_alarm;
762 sigaction(SIGALRM, &sv, NULL);
763
764 /* Initialize variables and stuff */
765 now = time(NULL);
766 chanset = NULL;
767 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
768 lastmin = nowtm.tm_min;
769 srandom(now % (getpid() + getppid()));
770 init_mem();
771 init_language(1);
772 if (argc > 1)
773 for (i = 1; i < argc; i++)
774 do_arg(argv[i]);
775 printf("\n%s\n", version);
776
777 /* Don't allow eggdrop to run as root */
778 if (((int) getuid() == 0) || ((int) geteuid() == 0))
779 fatal("ERROR: Eggdrop will not run as root!", 0);
780
781 init_dcc_max();
782 init_userent();
783 init_misc();
784 init_bots();
785 init_net();
786 init_modules();
787 if (backgrd)
788 bg_prepare_split();
789 init_tcl(argc, argv);
790 init_language(0);
791 #ifdef STATIC
792 link_statics();
793 #endif
794 strncpyz(s, ctime(&now), sizeof s);
795 strcpy(&s[11], &s[20]);
796 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
797 chanprog();
798 if (!encrypt_pass) {
799 printf(MOD_NOCRYPT);
800 bg_send_quit(BG_ABORT);
801 exit(1);
802 }
803 i = 0;
804 for (chan = chanset; chan; chan = chan->next)
805 i++;
806 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
807 botnetnick, i, count_users(userlist));
808 cache_miss = 0;
809 cache_hit = 0;
810 egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
811
812 /* Check for pre-existing eggdrop! */
813 f = fopen(pid_file, "r");
814 if (f != NULL) {
815 fgets(s, 10, f);
816 xx = atoi(s);
817 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
818 is used */
819 if (errno != ESRCH) {
820 printf(EGG_RUNNING1, origbotname);
821 printf(EGG_RUNNING2, pid_file);
822 bg_send_quit(BG_ABORT);
823 exit(1);
824 }
825 }
826
827 /* Move into background? */
828 if (backgrd) {
829 #ifndef CYGWIN_HACKS
830 bg_do_split();
831 } else { /* !backgrd */
832 #endif
833 xx = getpid();
834 if (xx != 0) {
835 FILE *fp;
836
837 /* Write pid to file */
838 unlink(pid_file);
839 fp = fopen(pid_file, "w");
840 if (fp != NULL) {
841 fprintf(fp, "%u\n", xx);
842 if (fflush(fp)) {
843 /* Let the bot live since this doesn't appear to be a botchk */
844 printf(EGG_NOWRITE, pid_file);
845 fclose(fp);
846 unlink(pid_file);
847 } else
848 fclose(fp);
849 } else
850 printf(EGG_NOWRITE, pid_file);
851 #ifdef CYGWIN_HACKS
852 printf("Launched (pid: %d)\n\n", xx);
853 #endif
854 }
855 }
856
857 use_stderr = 0; /* Stop writing to stderr now */
858 if (backgrd) {
859 /* Ok, try to disassociate from controlling terminal (finger cross) */
860 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
861 setpgid(0, 0);
862 #endif
863 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
864 freopen("/dev/null", "r", stdin);
865 freopen("/dev/null", "w", stdout);
866 freopen("/dev/null", "w", stderr);
867 #ifdef CYGWIN_HACKS
868 FreeConsole();
869 #endif
870 }
871
872 /* Terminal emulating dcc chat */
873 if (!backgrd && term_z) {
874 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
875
876 dcc[n].addr = iptolong(getmyip());
877 dcc[n].sock = STDOUT;
878 dcc[n].timeval = now;
879 dcc[n].u.chat->con_flags = conmask;
880 dcc[n].u.chat->strip_flags = STRIP_ALL;
881 dcc[n].status = STAT_ECHO;
882 strcpy(dcc[n].nick, "HQ");
883 strcpy(dcc[n].host, "llama@console");
884 dcc[n].user = get_user_by_handle(userlist, "HQ");
885 /* Make sure there's an innocuous HQ user if needed */
886 if (!dcc[n].user) {
887 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
888 dcc[n].user = get_user_by_handle(userlist, "HQ");
889 }
890 setsock(STDOUT, 0); /* Entry in net table */
891 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
892 dcc_chatter(n);
893 }
894
895 then = now;
896 online_since = now;
897 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
898 add_help_reference("cmds1.help");
899 add_help_reference("cmds2.help");
900 add_help_reference("core.help");
901 add_hook(HOOK_SECONDLY, (Function) core_secondly);
902 add_hook(HOOK_MINUTELY, (Function) core_minutely);
903 add_hook(HOOK_HOURLY, (Function) core_hourly);
904 add_hook(HOOK_REHASH, (Function) event_rehash);
905 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
906 add_hook(HOOK_USERFILE, (Function) event_save);
907 add_hook(HOOK_DAILY, (Function) event_logfile);
908 add_hook(HOOK_DAILY, (Function) event_resettraffic);
909 add_hook(HOOK_LOADED, (Function) event_loaded);
910
911 call_hook(HOOK_LOADED);
912
913 debug0("main: entering loop");
914 while (1) {
915 int socket_cleanup = 0;
916
917 #if !defined(HAVE_PRE7_5_TCL)
918 /* Process a single tcl event */
919 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
920 #endif
921
922 /* Lets move some of this here, reducing the numer of actual
923 * calls to periodic_timers
924 */
925 now = time(NULL);
926 random(); /* Woop, lets really jumble things */
927 if (now != then) { /* Once a second */
928 call_hook(HOOK_SECONDLY);
929 then = now;
930 }
931
932 /* Only do this every so often. */
933 if (!socket_cleanup) {
934 socket_cleanup = 5;
935
936 /* Remove dead dcc entries. */
937 dcc_remove_lost();
938
939 /* Check for server or dcc activity. */
940 dequeue_sockets();
941 } else
942 socket_cleanup--;
943
944 /* Free unused structures. */
945 garbage_collect();
946
947 xx = sockgets(buf, &i);
948 if (xx >= 0) { /* Non-error */
949 int idx;
950
951 for (idx = 0; idx < dcc_total; idx++)
952 if (dcc[idx].sock == xx) {
953 if (dcc[idx].type && dcc[idx].type->activity) {
954 /* Traffic stats */
955 if (dcc[idx].type->name) {
956 if (!strncmp(dcc[idx].type->name, "BOT", 3))
957 itraffic_bn_today += strlen(buf) + 1;
958 else if (!strcmp(dcc[idx].type->name, "SERVER"))
959 itraffic_irc_today += strlen(buf) + 1;
960 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
961 itraffic_dcc_today += strlen(buf) + 1;
962 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
963 itraffic_dcc_today += strlen(buf) + 1;
964 else if (!strcmp(dcc[idx].type->name, "SEND"))
965 itraffic_trans_today += strlen(buf) + 1;
966 else if (!strncmp(dcc[idx].type->name, "GET", 3))
967 itraffic_trans_today += strlen(buf) + 1;
968 else
969 itraffic_unknown_today += strlen(buf) + 1;
970 }
971 dcc[idx].type->activity(idx, buf, i);
972 } else
973 putlog(LOG_MISC, "*",
974 "!!! untrapped dcc activity: type %s, sock %d",
975 dcc[idx].type->name, dcc[idx].sock);
976 break;
977 }
978 } else if (xx == -1) { /* EOF from someone */
979 int idx;
980
981 if (i == STDOUT && !backgrd)
982 fatal("END OF FILE ON TERMINAL", 0);
983 for (idx = 0; idx < dcc_total; idx++)
984 if (dcc[idx].sock == i) {
985 if (dcc[idx].type && dcc[idx].type->eof)
986 dcc[idx].type->eof(idx);
987 else {
988 putlog(LOG_MISC, "*",
989 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
990 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
991 killsock(i);
992 lostdcc(idx);
993 }
994 idx = dcc_total + 1;
995 }
996 if (idx == dcc_total) {
997 putlog(LOG_MISC, "*",
998 "(@) EOF socket %d, not a dcc socket, not anything.", i);
999 close(i);
1000 killsock(i);
1001 }
1002 } else if (xx == -2 && errno != EINTR) { /* select() error */
1003 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
1004 for (i = 0; i < dcc_total; i++) {
1005 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
1006 putlog(LOG_MISC, "*",
1007 "DCC socket %d (type %d, name '%s') expired -- pfft",
1008 dcc[i].sock, dcc[i].type, dcc[i].nick);
1009 killsock(dcc[i].sock);
1010 lostdcc(i);
1011 i--;
1012 }
1013 }
1014 } else if (xx == -3) {
1015 call_hook(HOOK_IDLE);
1016 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
1017 }
1018
1019 if (do_restart) {
1020 if (do_restart == -2)
1021 rehash();
1022 else {
1023 /* Unload as many modules as possible */
1024 int f = 1;
1025 module_entry *p;
1026 Function x;
1027 char xx[256];
1028
1029 /* oops, I guess we should call this event before tcl is restarted */
1030 check_tcl_event("prerestart");
1031
1032 while (f) {
1033 f = 0;
1034 for (p = module_list; p != NULL; p = p->next) {
1035 dependancy *d = dependancy_list;
1036 int ok = 1;
1037
1038 while (ok && d) {
1039 if (d->needed == p)
1040 ok = 0;
1041 d = d->next;
1042 }
1043 if (ok) {
1044 strcpy(xx, p->name);
1045 if (module_unload(xx, origbotname) == NULL) {
1046 f = 1;
1047 break;
1048 }
1049 }
1050 }
1051 }
1052 p = module_list;
1053 if (p && p->next && p->next->next)
1054 /* Should be only 2 modules now - blowfish (or some other
1055 encryption module) and eggdrop. */
1056 putlog(LOG_MISC, "*", MOD_STAGNANT);
1057 flushlogs();
1058 kill_tcl();
1059 init_tcl(argc, argv);
1060 init_language(0);
1061 /* We expect the encryption module as the current module pointed
1062 * to by `module_list'.
1063 */
1064 x = p->funcs[MODCALL_START];
1065 /* `NULL' indicates that we just recently restarted. The module
1066 * is expected to re-initialise as needed.
1067 */
1068 x(NULL);
1069 rehash();
1070 restart_chons();
1071 call_hook(HOOK_LOADED);
1072 }
1073 do_restart = 0;
1074 }
1075 }
1076 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23