/[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.72 - (show annotations) (download) (as text)
Tue Jul 31 16:40:40 2001 UTC (17 years, 11 months ago) by guppy
Branch: MAIN
Changes since 1.71: +2 -1 lines
File MIME type: text/x-chdr
sync'd 1.7 to the latest 1.6

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23