/[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.44 - (show annotations) (download) (as text)
Fri Oct 27 19:28:21 2000 UTC (18 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.43: +15 -17 lines
File MIME type: text/x-chdr
fabian: applied the assert_fix patch

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.43 2000/09/27 19:48:54 fabian 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.5.5";
84 int egg_numver = 1050500;
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 int main(int argc, char **argv)
658 {
659 int xx, i;
660 char buf[520], s[520];
661 FILE *f;
662 struct sigaction sv;
663 struct chanset_t *chan;
664
665 #ifdef DEBUG_MEM
666 /* Make sure it can write core, if you make debug. Else it's pretty
667 * useless (dw)
668 */
669 {
670 #include <sys/resource.h>
671 struct rlimit cdlim;
672
673 cdlim.rlim_cur = RLIM_INFINITY;
674 cdlim.rlim_max = RLIM_INFINITY;
675 setrlimit(RLIMIT_CORE, &cdlim);
676 }
677 #endif
678
679 /* Initialise context list */
680 for (i = 0; i < 16; i++)
681 Context;
682
683 #include "patch.h"
684 /* Version info! */
685 sprintf(ver, "eggdrop v%s", egg_version);
686 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999, 2000 Eggheads",
687 egg_version);
688 /* Now add on the patchlevel (for Tcl) */
689 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
690 strcat(egg_version, egg_xtra);
691 Context;
692 #ifdef STOP_UAC
693 {
694 int nvpair[2];
695
696 nvpair[0] = SSIN_UACPROC;
697 nvpair[1] = UAC_NOPRINT;
698 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
699 }
700 #endif
701
702 /* Set up error traps: */
703 sv.sa_handler = got_bus;
704 sigemptyset(&sv.sa_mask);
705 #ifdef SA_RESETHAND
706 sv.sa_flags = SA_RESETHAND;
707 #else
708 sv.sa_flags = 0;
709 #endif
710 sigaction(SIGBUS, &sv, NULL);
711 sv.sa_handler = got_segv;
712 sigaction(SIGSEGV, &sv, NULL);
713 #ifdef SA_RESETHAND
714 sv.sa_flags = 0;
715 #endif
716 sv.sa_handler = got_fpe;
717 sigaction(SIGFPE, &sv, NULL);
718 sv.sa_handler = got_term;
719 sigaction(SIGTERM, &sv, NULL);
720 sv.sa_handler = got_hup;
721 sigaction(SIGHUP, &sv, NULL);
722 sv.sa_handler = got_quit;
723 sigaction(SIGQUIT, &sv, NULL);
724 sv.sa_handler = SIG_IGN;
725 sigaction(SIGPIPE, &sv, NULL);
726 sv.sa_handler = got_ill;
727 sigaction(SIGILL, &sv, NULL);
728 sv.sa_handler = got_alarm;
729 sigaction(SIGALRM, &sv, NULL);
730
731 /* Initialize variables and stuff */
732 now = time(NULL);
733 chanset = NULL;
734 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
735 lastmin = nowtm.tm_min;
736 srandom(now);
737 init_mem();
738 init_language(1);
739 if (argc > 1)
740 for (i = 1; i < argc; i++)
741 do_arg(argv[i]);
742 printf("\n%s\n", version);
743 init_dcc_max();
744 init_userent();
745 init_misc();
746 init_bots();
747 init_net();
748 init_modules();
749 if (backgrd)
750 bg_prepare_split();
751 init_tcl(argc, argv);
752 init_language(0);
753 #ifdef STATIC
754 link_statics();
755 #endif
756 Context;
757 strcpy(s, ctime(&now));
758 s[strlen(s) - 1] = 0;
759 strcpy(&s[11], &s[20]);
760 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
761 chanprog();
762 Context;
763 if (encrypt_pass == 0) {
764 printf(MOD_NOCRYPT);
765 bg_send_quit(BG_ABORT);
766 exit(1);
767 }
768 i = 0;
769 for (chan = chanset; chan; chan = chan->next)
770 i++;
771 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
772 botnetnick, i, count_users(userlist));
773 cache_miss = 0;
774 cache_hit = 0;
775 sprintf(pid_file, "pid.%s", botnetnick);
776 Context;
777
778 /* Check for pre-existing eggdrop! */
779 f = fopen(pid_file, "r");
780 if (f != NULL) {
781 fgets(s, 10, f);
782 xx = atoi(s);
783 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
784 is used */
785 if (errno != ESRCH) {
786 printf(EGG_RUNNING1, origbotname);
787 printf(EGG_RUNNING2, pid_file);
788 bg_send_quit(BG_ABORT);
789 exit(1);
790 }
791 }
792 Context;
793
794 /* Move into background? */
795 if (backgrd) {
796 #ifndef CYGWIN_HACKS
797 bg_do_split();
798 } else { /* !backgrd */
799 #endif
800 xx = getpid();
801 if (xx != 0) {
802 FILE *fp;
803
804 /* Write pid to file */
805 unlink(pid_file);
806 fp = fopen(pid_file, "w");
807 if (fp != NULL) {
808 fprintf(fp, "%u\n", xx);
809 if (fflush(fp)) {
810 /* Let the bot live since this doesn't appear to be a botchk */
811 printf(EGG_NOWRITE, pid_file);
812 fclose(fp);
813 unlink(pid_file);
814 } else
815 fclose(fp);
816 } else
817 printf(EGG_NOWRITE, pid_file);
818 #ifdef CYGWIN_HACKS
819 printf("Launched (pid: %d)\n\n", xx);
820 #endif
821 }
822 }
823
824 use_stderr = 0; /* Stop writing to stderr now */
825 if (backgrd) {
826 /* Ok, try to disassociate from controlling terminal (finger cross) */
827 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
828 setpgid(0, 0);
829 #endif
830 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
831 freopen("/dev/null", "r", stdin);
832 freopen("/dev/null", "w", stdout);
833 freopen("/dev/null", "w", stderr);
834 #ifdef CYGWIN_HACKS
835 FreeConsole();
836 #endif
837 }
838
839 /* Terminal emulating dcc chat */
840 if (!backgrd && term_z) {
841 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
842
843 dcc[n].addr = iptolong(getmyip());
844 dcc[n].sock = STDOUT;
845 dcc[n].timeval = now;
846 dcc[n].u.chat->con_flags = conmask;
847 dcc[n].u.chat->strip_flags = STRIP_ALL;
848 dcc[n].status = STAT_ECHO;
849 strcpy(dcc[n].nick, "HQ");
850 strcpy(dcc[n].host, "llama@console");
851 dcc[n].user = get_user_by_handle(userlist, "HQ");
852 /* Make sure there's an innocuous HQ user if needed */
853 if (!dcc[n].user) {
854 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
855 dcc[n].user = get_user_by_handle(userlist, "HQ");
856 }
857 setsock(STDOUT, 0); /* Entry in net table */
858 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
859 dcc_chatter(n);
860 }
861
862 then = now;
863 online_since = now;
864 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
865 add_help_reference("cmds1.help");
866 add_help_reference("cmds2.help");
867 add_help_reference("core.help");
868 add_hook(HOOK_SECONDLY, (Function) core_secondly);
869 add_hook(HOOK_MINUTELY, (Function) core_minutely);
870 add_hook(HOOK_HOURLY, (Function) core_hourly);
871 add_hook(HOOK_REHASH, (Function) event_rehash);
872 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
873 add_hook(HOOK_USERFILE, (Function) event_save);
874 add_hook(HOOK_DAILY, (Function) event_logfile);
875 add_hook(HOOK_DAILY, (Function) event_resettraffic);
876
877 debug0("main: entering loop");
878 while (1) {
879 int socket_cleanup = 0;
880
881 Context;
882 #if !defined(HAVE_PRE7_5_TCL)
883 /* Process a single tcl event */
884 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
885 #endif
886 /* Lets move some of this here, reducing the numer of actual
887 * calls to periodic_timers
888 */
889 now = time(NULL);
890 random(); /* Woop, lets really jumble things */
891 if (now != then) { /* Once a second */
892 call_hook(HOOK_SECONDLY);
893 then = now;
894 }
895 Context;
896 /* Only do this every so often */
897 if (!socket_cleanup) {
898 dcc_remove_lost();
899 /* Check for server or dcc activity */
900 dequeue_sockets();
901 socket_cleanup = 5;
902 } else
903 socket_cleanup--;
904 xx = sockgets(buf, &i);
905 if (xx >= 0) { /* Non-error */
906 int idx;
907
908 Context;
909 for (idx = 0; idx < dcc_total; idx++)
910 if (dcc[idx].sock == xx) {
911 if (dcc[idx].type && dcc[idx].type->activity) {
912 /* Traffic stats */
913 if (dcc[idx].type->name) {
914 if (!strncmp(dcc[idx].type->name, "BOT", 3))
915 itraffic_bn_today += strlen(buf) + 1;
916 else if (!strcmp(dcc[idx].type->name, "SERVER"))
917 itraffic_irc_today += strlen(buf) + 1;
918 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
919 itraffic_dcc_today += strlen(buf) + 1;
920 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
921 itraffic_dcc_today += strlen(buf) + 1;
922 else if (!strcmp(dcc[idx].type->name, "SEND"))
923 itraffic_trans_today += strlen(buf) + 1;
924 else if (!strncmp(dcc[idx].type->name, "GET", 3))
925 itraffic_trans_today += strlen(buf) + 1;
926 else
927 itraffic_unknown_today += strlen(buf) + 1;
928 }
929 dcc[idx].type->activity(idx, buf, i);
930 } else
931 putlog(LOG_MISC, "*",
932 "!!! untrapped dcc activity: type %s, sock %d",
933 dcc[idx].type->name, dcc[idx].sock);
934 break;
935 }
936 } else if (xx == -1) { /* EOF from someone */
937 int idx;
938
939 if ((i == STDOUT) && !backgrd)
940 fatal("END OF FILE ON TERMINAL", 0);
941 Context;
942 for (idx = 0; idx < dcc_total; idx++)
943 if (dcc[idx].sock == i) {
944 if (dcc[idx].type && dcc[idx].type->eof)
945 dcc[idx].type->eof(idx);
946 else {
947 putlog(LOG_MISC, "*",
948 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
949 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
950 killsock(i);
951 lostdcc(idx);
952 }
953 idx = dcc_total + 1;
954 }
955 if (idx == dcc_total) {
956 putlog(LOG_MISC, "*",
957 "(@) EOF socket %d, not a dcc socket, not anything.", i);
958 close(i);
959 killsock(i);
960 }
961 } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
962 Context;
963 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
964 for (i = 0; i < dcc_total; i++) {
965 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
966 putlog(LOG_MISC, "*",
967 "DCC socket %d (type %d, name '%s') expired -- pfft",
968 dcc[i].sock, dcc[i].type, dcc[i].nick);
969 killsock(dcc[i].sock);
970 lostdcc(i);
971 i--;
972 }
973 }
974 } else if (xx == (-3)) {
975 call_hook(HOOK_IDLE);
976 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
977 }
978
979 if (do_restart) {
980 if (do_restart == -2)
981 rehash();
982 else {
983 /* Unload as many modules as possible */
984 int f = 1;
985 module_entry *p;
986 Function x;
987 char xx[256];
988
989 while (f) {
990 f = 0;
991 for (p = module_list; p != NULL; p = p->next) {
992 dependancy *d = dependancy_list;
993 int ok = 1;
994
995 while (ok && d) {
996 if (d->needed == p)
997 ok = 0;
998 d = d->next;
999 }
1000 if (ok) {
1001 strcpy(xx, p->name);
1002 if (module_unload(xx, origbotname) == NULL) {
1003 f = 1;
1004 break;
1005 }
1006 }
1007 }
1008 }
1009 p = module_list;
1010 if (p && p->next && p->next->next)
1011 /* Should be only 2 modules now -
1012 * blowfish & eggdrop
1013 */
1014 putlog(LOG_MISC, "*", MOD_STAGNANT);
1015 Context;
1016 flushlogs();
1017 Context;
1018 kill_tcl();
1019 init_tcl(argc, argv);
1020 init_language(0);
1021 /* We expect the encryption module as the current module pointed
1022 * to by `module_list'.
1023 */
1024 x = p->funcs[MODCALL_START];
1025 /* `NULL' indicates that we just recently restarted. The module
1026 * is expected to re-initialise as needed.
1027 */
1028 x(NULL);
1029 rehash();
1030 restart_chons();
1031 }
1032 do_restart = 0;
1033 }
1034 }
1035 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23