/[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.49 - (show annotations) (download) (as text)
Sun Dec 10 15:10:27 2000 UTC (18 years, 8 months ago) by guppy
Branch: MAIN
Changes since 1.48: +40 -59 lines
File MIME type: text/x-chdr
guppy: core_cleanup.diff ...

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23