/[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.37 - (show annotations) (download) (as text)
Sun May 7 01:10:35 2000 UTC (19 years, 3 months ago) by fabian
Branch: MAIN
Changes since 1.36: +3 -3 lines
File MIME type: text/x-chdr
start 1.5.4

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23