/[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.29 - (show annotations) (download) (as text)
Fri Feb 4 23:31:51 2000 UTC (19 years, 6 months ago) by fabian
Branch: MAIN
Changes since 1.28: +3 -3 lines
File MIME type: text/x-chdr
release of 1.5.2

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23