/[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.61 - (show annotations) (download) (as text)
Fri Jun 1 21:56:41 2001 UTC (18 years, 1 month ago) by guppy
Branch: MAIN
Changes since 1.60: +10 -1 lines
File MIME type: text/x-chdr
new evnt: loaded

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23