/[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.64 - (show annotations) (download) (as text)
Thu Jul 5 22:19:51 2001 UTC (18 years ago) by guppy
Branch: MAIN
CVS Tags: eggdrop1_6_5
Changes since 1.63: +3 -3 lines
File MIME type: text/x-chdr
two unalligned colons in the debug code

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.63 2001/06/20 14:53:36 poptix 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 #ifdef CCFLAGS
279 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
280 #endif
281 #ifdef LDFLAGS
282 dprintf(-x, "Link flags: %s\n", LDFLAGS);
283 #endif
284 #ifdef STRIPFLAGS
285 dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
286 #endif
287
288 dprintf(-x, "Context: ");
289 cx_ptr = cx_ptr & 15;
290 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
291 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
292 (cx_note[y][0]) ? cx_note[y] : "");
293 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
294 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
295 tell_dcc(-x);
296 dprintf(-x, "\n");
297 debug_mem_to_dcc(-x);
298 killsock(x);
299 close(x);
300 putlog(LOG_MISC, "*", "* Wrote DEBUG");
301 }
302 }
303 #endif
304
305 static void got_bus(int z)
306 {
307 #ifdef DEBUG_CONTEXT
308 write_debug();
309 #endif
310 fatal("BUS ERROR -- CRASHING!", 1);
311 #ifdef SA_RESETHAND
312 kill(getpid(), SIGBUS);
313 #else
314 bg_send_quit(BG_ABORT);
315 exit(1);
316 #endif
317 }
318
319 static void got_segv(int z)
320 {
321 #ifdef DEBUG_CONTEXT
322 write_debug();
323 #endif
324 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
325 #ifdef SA_RESETHAND
326 kill(getpid(), SIGSEGV);
327 #else
328 bg_send_quit(BG_ABORT);
329 exit(1);
330 #endif
331 }
332
333 static void got_fpe(int z)
334 {
335 #ifdef DEBUG_CONTEXT
336 write_debug();
337 #endif
338 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
339 }
340
341 static void got_term(int z)
342 {
343 write_userfile(-1);
344 check_tcl_event("sigterm");
345 if (die_on_sigterm) {
346 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
347 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
348 } else {
349 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
350 }
351 }
352
353 static void got_quit(int z)
354 {
355 check_tcl_event("sigquit");
356 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
357 return;
358 }
359
360 static void got_hup(int z)
361 {
362 write_userfile(-1);
363 check_tcl_event("sighup");
364 if (die_on_sighup) {
365 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
366 } else
367 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
368 do_restart = -2;
369 return;
370 }
371
372 /* A call to resolver (gethostbyname, etc) timed out
373 */
374 static void got_alarm(int z)
375 {
376 longjmp(alarmret, 1);
377
378 /* -Never reached- */
379 }
380
381 /* Got ILL signal -- log context and continue
382 */
383 static void got_ill(int z)
384 {
385 check_tcl_event("sigill");
386 #ifdef DEBUG_CONTEXT
387 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
388 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
389 #endif
390 }
391
392 #ifdef DEBUG_CONTEXT
393 /* Context */
394 void eggContext(const char *file, int line, const char *module)
395 {
396 char x[31], *p;
397
398 p = strrchr(file, '/');
399 if (!module) {
400 strncpyz(x, p ? p + 1 : file, sizeof x);
401 } else
402 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
403 cx_ptr = ((cx_ptr + 1) & 15);
404 strcpy(cx_file[cx_ptr], x);
405 cx_line[cx_ptr] = line;
406 cx_note[cx_ptr][0] = 0;
407 }
408
409 /* Called from the ContextNote macro.
410 */
411 void eggContextNote(const char *file, int line, const char *module,
412 const char *note)
413 {
414 char x[31], *p;
415
416 p = strrchr(file, '/');
417 if (!module) {
418 strncpyz(x, p ? p + 1 : file, sizeof x);
419 } else
420 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
421 cx_ptr = ((cx_ptr + 1) & 15);
422 strcpy(cx_file[cx_ptr], x);
423 cx_line[cx_ptr] = line;
424 strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
425 }
426 #endif
427
428 #ifdef DEBUG_ASSERT
429 /* Called from the Assert macro.
430 */
431 void eggAssert(const char *file, int line, const char *module)
432 {
433 #ifdef DEBUG_CONTEXT
434 write_debug();
435 #endif
436 if (!module)
437 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
438 else
439 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
440 fatal("ASSERT FAILED -- CRASHING!", 1);
441 }
442 #endif
443
444 static void do_arg(char *s)
445 {
446 char x[1024], *z = x;
447 int i;
448
449 if (s[0] == '-')
450 for (i = 1; i < strlen(s); i++) {
451 switch (s[i]) {
452 case 'n':
453 backgrd = 0;
454 break;
455 case 'c':
456 con_chan = 1;
457 term_z = 0;
458 break;
459 case 't':
460 con_chan = 0;
461 term_z = 1;
462 break;
463 case 'm':
464 make_userfile = 1;
465 break;
466 case 'v':
467 strncpyz(x, egg_version, sizeof x);
468 newsplit(&z);
469 newsplit(&z);
470 printf("%s\n", version);
471 if (z[0])
472 printf(" (patches: %s)\n", z);
473 bg_send_quit(BG_ABORT);
474 exit(0);
475 break; /* this should never be reached */
476 case 'h':
477 printf("\n%s\n\n", version);
478 printf(EGG_USAGE);
479 printf("\n");
480 bg_send_quit(BG_ABORT);
481 exit(0);
482 break; /* this should never be reached */
483 }
484 } else
485 strncpyz(configfile, s, sizeof configfile);
486 }
487
488 void backup_userfile(void)
489 {
490 char s[125];
491
492 putlog(LOG_MISC, "*", USERF_BACKUP);
493 egg_snprintf(s, sizeof s, "%s~bak", userfile);
494 copyfile(userfile, s);
495 }
496
497 /* Timer info */
498 static int lastmin = 99;
499 static time_t then;
500 static struct tm nowtm;
501
502 /* Called once a second.
503 *
504 * Note: Try to not put any Context lines in here (guppy 21Mar2000).
505 */
506 static void core_secondly()
507 {
508 static int cnt = 0;
509 int miltime;
510
511 do_check_timers(&utimer); /* Secondly timers */
512 cnt++;
513 if (cnt >= 10) { /* Every 10 seconds */
514 cnt = 0;
515 check_expired_dcc();
516 if (con_chan && !backgrd) {
517 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
518 tell_verbose_status(DP_STDOUT);
519 do_module_report(DP_STDOUT, 0, "server");
520 do_module_report(DP_STDOUT, 0, "channels");
521 tell_mem_status_dcc(DP_STDOUT);
522 }
523 }
524 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
525 if (nowtm.tm_min != lastmin) {
526 int i = 0;
527
528 /* Once a minute */
529 lastmin = (lastmin + 1) % 60;
530 call_hook(HOOK_MINUTELY);
531 check_expired_ignores();
532 autolink_cycle(NULL); /* Attempt autolinks */
533 /* In case for some reason more than 1 min has passed: */
534 while (nowtm.tm_min != lastmin) {
535 /* Timer drift, dammit */
536 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
537 i++;
538 lastmin = (lastmin + 1) % 60;
539 call_hook(HOOK_MINUTELY);
540 }
541 if (i > 1)
542 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
543 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
544 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
545 call_hook(HOOK_5MINUTELY);
546 check_botnet_pings();
547 if (!quick_logs) {
548 flushlogs();
549 check_logsize();
550 }
551 if (!miltime) { /* At midnight */
552 char s[25];
553 int j;
554
555 strncpyz(s, ctime(&now), sizeof s);
556 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
557 backup_userfile();
558 for (j = 0; j < max_logs; j++) {
559 if (logs[j].filename != NULL && logs[j].f != NULL) {
560 fclose(logs[j].f);
561 logs[j].f = NULL;
562 }
563 }
564 }
565 }
566 if (nowtm.tm_min == notify_users_at)
567 call_hook(HOOK_HOURLY);
568 /* These no longer need checking since they are all check vs minutely
569 * settings and we only get this far on the minute.
570 */
571 if (miltime == switch_logfiles_at) {
572 call_hook(HOOK_DAILY);
573 if (!keep_all_logs) {
574 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
575 for (i = 0; i < max_logs; i++)
576 if (logs[i].filename) {
577 char s[1024];
578
579 if (logs[i].f) {
580 fclose(logs[i].f);
581 logs[i].f = NULL;
582 }
583 egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
584 unlink(s);
585 movefile(logs[i].filename, s);
586 }
587 }
588 }
589 }
590 }
591
592 static void core_minutely()
593 {
594 check_tcl_time(&nowtm);
595 do_check_timers(&timer);
596 if (quick_logs != 0) {
597 flushlogs();
598 check_logsize();
599 }
600 }
601
602 static void core_hourly()
603 {
604 write_userfile(-1);
605 }
606
607 static void event_rehash()
608 {
609 check_tcl_event("rehash");
610 }
611
612 static void event_prerehash()
613 {
614 check_tcl_event("prerehash");
615 }
616
617 static void event_save()
618 {
619 check_tcl_event("save");
620 }
621
622 static void event_logfile()
623 {
624 check_tcl_event("logfile");
625 }
626
627 static void event_resettraffic()
628 {
629 otraffic_irc += otraffic_irc_today;
630 itraffic_irc += itraffic_irc_today;
631 otraffic_bn += otraffic_bn_today;
632 itraffic_bn += itraffic_bn_today;
633 otraffic_dcc += otraffic_dcc_today;
634 itraffic_dcc += itraffic_dcc_today;
635 otraffic_unknown += otraffic_unknown_today;
636 itraffic_unknown += itraffic_unknown_today;
637 otraffic_trans += otraffic_trans_today;
638 itraffic_trans += itraffic_trans_today;
639 otraffic_irc_today = otraffic_bn_today = 0;
640 otraffic_dcc_today = otraffic_unknown_today = 0;
641 itraffic_irc_today = itraffic_bn_today = 0;
642 itraffic_dcc_today = itraffic_unknown_today = 0;
643 itraffic_trans_today = otraffic_trans_today = 0;
644 }
645
646 static void event_loaded()
647 {
648 check_tcl_event("loaded");
649 }
650
651 void kill_tcl();
652 extern module_entry *module_list;
653 void restart_chons();
654
655 #ifdef STATIC
656 void check_static(char *, char *(*)());
657
658 #include "mod/static.h"
659 #endif
660 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
661 init_net(), init_modules(), init_tcl(int, char **),
662 init_language(int);
663
664 void patch(const char *str)
665 {
666 char *p = strchr(egg_version, '+');
667
668 if (!p)
669 p = &egg_version[strlen(egg_version)];
670 sprintf(p, "+%s", str);
671 egg_numver++;
672 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
673 }
674
675 static inline void garbage_collect(void)
676 {
677 static u_8bit_t run_cnt = 0;
678
679 if (run_cnt == 3)
680 garbage_collect_tclhash();
681 else
682 run_cnt++;
683 }
684
685 int main(int argc, char **argv)
686 {
687 int xx, i;
688 char buf[520], s[25];
689 FILE *f;
690 struct sigaction sv;
691 struct chanset_t *chan;
692
693 #ifdef DEBUG_MEM
694 /* Make sure it can write core, if you make debug. Else it's pretty
695 * useless (dw)
696 */
697 {
698 #include <sys/resource.h>
699 struct rlimit cdlim;
700
701 cdlim.rlim_cur = RLIM_INFINITY;
702 cdlim.rlim_max = RLIM_INFINITY;
703 setrlimit(RLIMIT_CORE, &cdlim);
704 }
705 #endif
706
707 /* Initialise context list */
708 for (i = 0; i < 16; i++)
709 Context;
710
711 #include "patch.h"
712 /* Version info! */
713 egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
714 egg_snprintf(version, sizeof version, "Eggdrop v%s (C)1997 Robey Pointer (C)2001 Eggheads",
715 egg_version);
716 /* Now add on the patchlevel (for Tcl) */
717 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
718 strcat(egg_version, egg_xtra);
719 #ifdef STOP_UAC
720 {
721 int nvpair[2];
722
723 nvpair[0] = SSIN_UACPROC;
724 nvpair[1] = UAC_NOPRINT;
725 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
726 }
727 #endif
728
729 /* Set up error traps: */
730 sv.sa_handler = got_bus;
731 sigemptyset(&sv.sa_mask);
732 #ifdef SA_RESETHAND
733 sv.sa_flags = SA_RESETHAND;
734 #else
735 sv.sa_flags = 0;
736 #endif
737 sigaction(SIGBUS, &sv, NULL);
738 sv.sa_handler = got_segv;
739 sigaction(SIGSEGV, &sv, NULL);
740 #ifdef SA_RESETHAND
741 sv.sa_flags = 0;
742 #endif
743 sv.sa_handler = got_fpe;
744 sigaction(SIGFPE, &sv, NULL);
745 sv.sa_handler = got_term;
746 sigaction(SIGTERM, &sv, NULL);
747 sv.sa_handler = got_hup;
748 sigaction(SIGHUP, &sv, NULL);
749 sv.sa_handler = got_quit;
750 sigaction(SIGQUIT, &sv, NULL);
751 sv.sa_handler = SIG_IGN;
752 sigaction(SIGPIPE, &sv, NULL);
753 sv.sa_handler = got_ill;
754 sigaction(SIGILL, &sv, NULL);
755 sv.sa_handler = got_alarm;
756 sigaction(SIGALRM, &sv, NULL);
757
758 /* Initialize variables and stuff */
759 now = time(NULL);
760 chanset = NULL;
761 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
762 lastmin = nowtm.tm_min;
763 srandom(now % (getpid() + getppid()));
764 init_mem();
765 init_language(1);
766 if (argc > 1)
767 for (i = 1; i < argc; i++)
768 do_arg(argv[i]);
769 printf("\n%s\n", version);
770
771 /* Don't allow eggdrop to run as root */
772 if (((int) getuid() == 0) || ((int) geteuid() == 0))
773 fatal("ERROR: Eggdrop will not run as root!", 0);
774
775 init_dcc_max();
776 init_userent();
777 init_misc();
778 init_bots();
779 init_net();
780 init_modules();
781 if (backgrd)
782 bg_prepare_split();
783 init_tcl(argc, argv);
784 init_language(0);
785 #ifdef STATIC
786 link_statics();
787 #endif
788 strncpyz(s, ctime(&now), sizeof s);
789 strcpy(&s[11], &s[20]);
790 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
791 chanprog();
792 if (!encrypt_pass) {
793 printf(MOD_NOCRYPT);
794 bg_send_quit(BG_ABORT);
795 exit(1);
796 }
797 i = 0;
798 for (chan = chanset; chan; chan = chan->next)
799 i++;
800 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
801 botnetnick, i, count_users(userlist));
802 cache_miss = 0;
803 cache_hit = 0;
804 egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
805
806 /* Check for pre-existing eggdrop! */
807 f = fopen(pid_file, "r");
808 if (f != NULL) {
809 fgets(s, 10, f);
810 xx = atoi(s);
811 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
812 is used */
813 if (errno != ESRCH) {
814 printf(EGG_RUNNING1, origbotname);
815 printf(EGG_RUNNING2, pid_file);
816 bg_send_quit(BG_ABORT);
817 exit(1);
818 }
819 }
820
821 /* Move into background? */
822 if (backgrd) {
823 #ifndef CYGWIN_HACKS
824 bg_do_split();
825 } else { /* !backgrd */
826 #endif
827 xx = getpid();
828 if (xx != 0) {
829 FILE *fp;
830
831 /* Write pid to file */
832 unlink(pid_file);
833 fp = fopen(pid_file, "w");
834 if (fp != NULL) {
835 fprintf(fp, "%u\n", xx);
836 if (fflush(fp)) {
837 /* Let the bot live since this doesn't appear to be a botchk */
838 printf(EGG_NOWRITE, pid_file);
839 fclose(fp);
840 unlink(pid_file);
841 } else
842 fclose(fp);
843 } else
844 printf(EGG_NOWRITE, pid_file);
845 #ifdef CYGWIN_HACKS
846 printf("Launched (pid: %d)\n\n", xx);
847 #endif
848 }
849 }
850
851 use_stderr = 0; /* Stop writing to stderr now */
852 if (backgrd) {
853 /* Ok, try to disassociate from controlling terminal (finger cross) */
854 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
855 setpgid(0, 0);
856 #endif
857 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
858 freopen("/dev/null", "r", stdin);
859 freopen("/dev/null", "w", stdout);
860 freopen("/dev/null", "w", stderr);
861 #ifdef CYGWIN_HACKS
862 FreeConsole();
863 #endif
864 }
865
866 /* Terminal emulating dcc chat */
867 if (!backgrd && term_z) {
868 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
869
870 dcc[n].addr = iptolong(getmyip());
871 dcc[n].sock = STDOUT;
872 dcc[n].timeval = now;
873 dcc[n].u.chat->con_flags = conmask;
874 dcc[n].u.chat->strip_flags = STRIP_ALL;
875 dcc[n].status = STAT_ECHO;
876 strcpy(dcc[n].nick, "HQ");
877 strcpy(dcc[n].host, "llama@console");
878 dcc[n].user = get_user_by_handle(userlist, "HQ");
879 /* Make sure there's an innocuous HQ user if needed */
880 if (!dcc[n].user) {
881 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
882 dcc[n].user = get_user_by_handle(userlist, "HQ");
883 }
884 setsock(STDOUT, 0); /* Entry in net table */
885 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
886 dcc_chatter(n);
887 }
888
889 then = now;
890 online_since = now;
891 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
892 add_help_reference("cmds1.help");
893 add_help_reference("cmds2.help");
894 add_help_reference("core.help");
895 add_hook(HOOK_SECONDLY, (Function) core_secondly);
896 add_hook(HOOK_MINUTELY, (Function) core_minutely);
897 add_hook(HOOK_HOURLY, (Function) core_hourly);
898 add_hook(HOOK_REHASH, (Function) event_rehash);
899 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
900 add_hook(HOOK_USERFILE, (Function) event_save);
901 add_hook(HOOK_DAILY, (Function) event_logfile);
902 add_hook(HOOK_DAILY, (Function) event_resettraffic);
903 add_hook(HOOK_LOADED, (Function) event_loaded);
904
905 call_hook(HOOK_LOADED);
906
907 debug0("main: entering loop");
908 while (1) {
909 int socket_cleanup = 0;
910
911 #if !defined(HAVE_PRE7_5_TCL)
912 /* Process a single tcl event */
913 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
914 #endif
915
916 /* Lets move some of this here, reducing the numer of actual
917 * calls to periodic_timers
918 */
919 now = time(NULL);
920 random(); /* Woop, lets really jumble things */
921 if (now != then) { /* Once a second */
922 call_hook(HOOK_SECONDLY);
923 then = now;
924 }
925
926 /* Only do this every so often. */
927 if (!socket_cleanup) {
928 socket_cleanup = 5;
929
930 /* Remove dead dcc entries. */
931 dcc_remove_lost();
932
933 /* Check for server or dcc activity. */
934 dequeue_sockets();
935 } else
936 socket_cleanup--;
937
938 /* Free unused structures. */
939 garbage_collect();
940
941 xx = sockgets(buf, &i);
942 if (xx >= 0) { /* Non-error */
943 int idx;
944
945 for (idx = 0; idx < dcc_total; idx++)
946 if (dcc[idx].sock == xx) {
947 if (dcc[idx].type && dcc[idx].type->activity) {
948 /* Traffic stats */
949 if (dcc[idx].type->name) {
950 if (!strncmp(dcc[idx].type->name, "BOT", 3))
951 itraffic_bn_today += strlen(buf) + 1;
952 else if (!strcmp(dcc[idx].type->name, "SERVER"))
953 itraffic_irc_today += strlen(buf) + 1;
954 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
955 itraffic_dcc_today += strlen(buf) + 1;
956 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
957 itraffic_dcc_today += strlen(buf) + 1;
958 else if (!strcmp(dcc[idx].type->name, "SEND"))
959 itraffic_trans_today += strlen(buf) + 1;
960 else if (!strncmp(dcc[idx].type->name, "GET", 3))
961 itraffic_trans_today += strlen(buf) + 1;
962 else
963 itraffic_unknown_today += strlen(buf) + 1;
964 }
965 dcc[idx].type->activity(idx, buf, i);
966 } else
967 putlog(LOG_MISC, "*",
968 "!!! untrapped dcc activity: type %s, sock %d",
969 dcc[idx].type->name, dcc[idx].sock);
970 break;
971 }
972 } else if (xx == -1) { /* EOF from someone */
973 int idx;
974
975 if (i == STDOUT && !backgrd)
976 fatal("END OF FILE ON TERMINAL", 0);
977 for (idx = 0; idx < dcc_total; idx++)
978 if (dcc[idx].sock == i) {
979 if (dcc[idx].type && dcc[idx].type->eof)
980 dcc[idx].type->eof(idx);
981 else {
982 putlog(LOG_MISC, "*",
983 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
984 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
985 killsock(i);
986 lostdcc(idx);
987 }
988 idx = dcc_total + 1;
989 }
990 if (idx == dcc_total) {
991 putlog(LOG_MISC, "*",
992 "(@) EOF socket %d, not a dcc socket, not anything.", i);
993 close(i);
994 killsock(i);
995 }
996 } else if (xx == -2 && errno != EINTR) { /* select() error */
997 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
998 for (i = 0; i < dcc_total; i++) {
999 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
1000 putlog(LOG_MISC, "*",
1001 "DCC socket %d (type %d, name '%s') expired -- pfft",
1002 dcc[i].sock, dcc[i].type, dcc[i].nick);
1003 killsock(dcc[i].sock);
1004 lostdcc(i);
1005 i--;
1006 }
1007 }
1008 } else if (xx == -3) {
1009 call_hook(HOOK_IDLE);
1010 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
1011 }
1012
1013 if (do_restart) {
1014 if (do_restart == -2)
1015 rehash();
1016 else {
1017 /* Unload as many modules as possible */
1018 int f = 1;
1019 module_entry *p;
1020 Function x;
1021 char xx[256];
1022
1023 /* oops, I guess we should call this event before tcl is restarted */
1024 check_tcl_event("prerestart");
1025
1026 while (f) {
1027 f = 0;
1028 for (p = module_list; p != NULL; p = p->next) {
1029 dependancy *d = dependancy_list;
1030 int ok = 1;
1031
1032 while (ok && d) {
1033 if (d->needed == p)
1034 ok = 0;
1035 d = d->next;
1036 }
1037 if (ok) {
1038 strcpy(xx, p->name);
1039 if (module_unload(xx, origbotname) == NULL) {
1040 f = 1;
1041 break;
1042 }
1043 }
1044 }
1045 }
1046 p = module_list;
1047 if (p && p->next && p->next->next)
1048 /* Should be only 2 modules now - blowfish (or some other
1049 encryption module) and eggdrop. */
1050 putlog(LOG_MISC, "*", MOD_STAGNANT);
1051 flushlogs();
1052 kill_tcl();
1053 init_tcl(argc, argv);
1054 init_language(0);
1055 /* We expect the encryption module as the current module pointed
1056 * to by `module_list'.
1057 */
1058 x = p->funcs[MODCALL_START];
1059 /* `NULL' indicates that we just recently restarted. The module
1060 * is expected to re-initialise as needed.
1061 */
1062 x(NULL);
1063 rehash();
1064 restart_chons();
1065 call_hook(HOOK_LOADED);
1066 }
1067 do_restart = 0;
1068 }
1069 }
1070 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23