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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23