/[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.47 - (show annotations) (download) (as text)
Thu Nov 16 03:27:01 2000 UTC (18 years, 8 months ago) by guppy
Branch: MAIN
Changes since 1.46: +3 -3 lines
File MIME type: text/x-chdr
guppy: 1.6.1 here we go

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23