/[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.35 - (show annotations) (download) (as text)
Thu Mar 23 23:17:55 2000 UTC (19 years, 3 months ago) by fabian
Branch: MAIN
CVS Tags: eggdrop10403
Changes since 1.34: +3 -3 lines
File MIME type: text/x-chdr
conf_make_fix6 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.34 2000/03/23 23:10:26 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 unsigned long otraffic_irc = 0;
124 unsigned long otraffic_irc_today = 0;
125 unsigned long otraffic_bn = 0;
126 unsigned long otraffic_bn_today = 0;
127 unsigned long otraffic_dcc = 0;
128 unsigned long otraffic_dcc_today = 0;
129 unsigned long otraffic_filesys = 0;
130 unsigned long otraffic_filesys_today = 0;
131 unsigned long otraffic_trans = 0;
132 unsigned long otraffic_trans_today = 0;
133 unsigned long otraffic_unknown = 0;
134 unsigned long otraffic_unknown_today = 0;
135 unsigned long itraffic_irc = 0;
136 unsigned long itraffic_irc_today = 0;
137 unsigned long itraffic_bn = 0;
138 unsigned long itraffic_bn_today = 0;
139 unsigned long itraffic_dcc = 0;
140 unsigned long itraffic_dcc_today = 0;
141 unsigned long itraffic_trans = 0;
142 unsigned long itraffic_trans_today = 0;
143 unsigned long itraffic_unknown = 0;
144 unsigned long 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 * Note: Try to not put any Context lines in here (guppy 21Mar2000).
477 */
478 static void core_secondly()
479 {
480 static int cnt = 0;
481 int miltime;
482
483 do_check_timers(&utimer); /* Secondly timers */
484 cnt++;
485 if (cnt >= 10) { /* Every 10 seconds */
486 cnt = 0;
487 check_expired_dcc();
488 if (con_chan && !backgrd) {
489 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
490 tell_verbose_status(DP_STDOUT);
491 do_module_report(DP_STDOUT, 0, "server");
492 do_module_report(DP_STDOUT, 0, "channels");
493 tell_mem_status_dcc(DP_STDOUT);
494 }
495 }
496 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
497 if (nowtm.tm_min != lastmin) {
498 int i = 0;
499
500 /* Once a minute */
501 lastmin = (lastmin + 1) % 60;
502 call_hook(HOOK_MINUTELY);
503 check_expired_ignores();
504 autolink_cycle(NULL); /* Attempt autolinks */
505 /* In case for some reason more than 1 min has passed: */
506 while (nowtm.tm_min != lastmin) {
507 /* Timer drift, dammit */
508 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
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 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
517 call_hook(HOOK_5MINUTELY);
518 check_botnet_pings();
519 if (quick_logs == 0) {
520 flushlogs();
521 check_logsize();
522 }
523 if (miltime == 0) { /* At midnight */
524 char s[128];
525 int j;
526
527 s[my_strcpy(s, ctime(&now)) - 1] = 0;
528 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
529 backup_userfile();
530 for (j = 0; j < max_logs; j++) {
531 if (logs[j].filename != NULL && logs[j].f != NULL) {
532 fclose(logs[j].f);
533 logs[j].f = NULL;
534 }
535 }
536 }
537 }
538 if (nowtm.tm_min == notify_users_at)
539 call_hook(HOOK_HOURLY);
540 /* These no longer need checking since they are all check vs minutely
541 * settings and we only get this far on the minute.
542 */
543 if (miltime == switch_logfiles_at) {
544 call_hook(HOOK_DAILY);
545 if (!keep_all_logs) {
546 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
547 for (i = 0; i < max_logs; i++)
548 if (logs[i].filename) {
549 char s[1024];
550
551 if (logs[i].f) {
552 fclose(logs[i].f);
553 logs[i].f = NULL;
554 }
555 simple_sprintf(s, "%s.yesterday", logs[i].filename);
556 unlink(s);
557 movefile(logs[i].filename, s);
558 }
559 }
560 }
561 }
562 }
563
564 static void core_minutely()
565 {
566 check_tcl_time(&nowtm);
567 do_check_timers(&timer);
568 if (quick_logs != 0) {
569 flushlogs();
570 check_logsize();
571 }
572 }
573
574 static void core_hourly()
575 {
576 Context;
577 write_userfile(-1);
578 }
579
580 static void event_rehash()
581 {
582 Context;
583 check_tcl_event("rehash");
584 }
585
586 static void event_prerehash()
587 {
588 Context;
589 check_tcl_event("prerehash");
590 }
591
592 static void event_save()
593 {
594 Context;
595 check_tcl_event("save");
596 }
597
598 static void event_logfile()
599 {
600 Context;
601 check_tcl_event("logfile");
602 }
603
604 static void event_resettraffic()
605 {
606 Context;
607 otraffic_irc += otraffic_irc_today;
608 itraffic_irc += itraffic_irc_today;
609 otraffic_bn += otraffic_bn_today;
610 itraffic_bn += itraffic_bn_today;
611 otraffic_dcc += otraffic_dcc_today;
612 itraffic_dcc += itraffic_dcc_today;
613 otraffic_unknown += otraffic_unknown_today;
614 itraffic_unknown += itraffic_unknown_today;
615 otraffic_trans += otraffic_trans_today;
616 itraffic_trans += itraffic_trans_today;
617 otraffic_irc_today = otraffic_bn_today = 0;
618 otraffic_dcc_today = otraffic_unknown_today = 0;
619 itraffic_irc_today = itraffic_bn_today = 0;
620 itraffic_dcc_today = itraffic_unknown_today = 0;
621 itraffic_trans_today = otraffic_trans_today = 0;
622 }
623
624 void kill_tcl();
625 extern module_entry *module_list;
626 void restart_chons();
627
628 #ifdef STATIC
629 void check_static(char *, char *(*)());
630
631 #include "mod/static.h"
632 #endif
633 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
634 init_net(), init_modules(), init_tcl(int, char **),
635 init_language(int);
636
637 void patch(char *str)
638 {
639 char *p = strchr(egg_version, '+');
640
641 if (!p)
642 p = &egg_version[strlen(egg_version)];
643 sprintf(p, "+%s", str);
644 egg_numver++;
645 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
646 }
647
648 int main(int argc, char **argv)
649 {
650 int xx, i;
651 char buf[520], s[520];
652 FILE *f;
653 struct sigaction sv;
654 struct chanset_t *chan;
655
656 #ifdef DEBUG_MEM
657 /* Make sure it can write core, if you make debug. Else it's pretty
658 * useless (dw)
659 */
660 {
661 #include <sys/resource.h>
662 struct rlimit cdlim;
663
664 cdlim.rlim_cur = RLIM_INFINITY;
665 cdlim.rlim_max = RLIM_INFINITY;
666 setrlimit(RLIMIT_CORE, &cdlim);
667 }
668 #endif
669
670 /* Initialise context list */
671 for (i = 0; i < 16; i++)
672 Context;
673
674 #include "patch.h"
675 /* Version info! */
676 sprintf(ver, "eggdrop v%s", egg_version);
677 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999, 2000 Eggheads",
678 egg_version);
679 /* Now add on the patchlevel (for Tcl) */
680 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
681 strcat(egg_version, egg_xtra);
682 Context;
683 #ifdef STOP_UAC
684 {
685 int nvpair[2];
686
687 nvpair[0] = SSIN_UACPROC;
688 nvpair[1] = UAC_NOPRINT;
689 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
690 }
691 #endif
692
693 /* Set up error traps: */
694 sv.sa_handler = got_bus;
695 sigemptyset(&sv.sa_mask);
696 #ifdef SA_RESETHAND
697 sv.sa_flags = SA_RESETHAND;
698 #else
699 sv.sa_flags = 0;
700 #endif
701 sigaction(SIGBUS, &sv, NULL);
702 sv.sa_handler = got_segv;
703 sigaction(SIGSEGV, &sv, NULL);
704 #ifdef SA_RESETHAND
705 sv.sa_flags = 0;
706 #endif
707 sv.sa_handler = got_fpe;
708 sigaction(SIGFPE, &sv, NULL);
709 sv.sa_handler = got_term;
710 sigaction(SIGTERM, &sv, NULL);
711 sv.sa_handler = got_hup;
712 sigaction(SIGHUP, &sv, NULL);
713 sv.sa_handler = got_quit;
714 sigaction(SIGQUIT, &sv, NULL);
715 sv.sa_handler = SIG_IGN;
716 sigaction(SIGPIPE, &sv, NULL);
717 sv.sa_handler = got_ill;
718 sigaction(SIGILL, &sv, NULL);
719 sv.sa_handler = got_alarm;
720 sigaction(SIGALRM, &sv, NULL);
721
722 /* Initialize variables and stuff */
723 now = time(NULL);
724 chanset = NULL;
725 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
726 lastmin = nowtm.tm_min;
727 srandom(now);
728 init_mem();
729 init_language(1);
730 if (argc > 1)
731 for (i = 1; i < argc; i++)
732 do_arg(argv[i]);
733 printf("\n%s\n", version);
734 init_dcc_max();
735 init_userent();
736 init_misc();
737 init_bots();
738 init_net();
739 init_modules();
740 init_tcl(argc, argv);
741 init_language(0);
742 #ifdef STATIC
743 link_statics();
744 #endif
745 Context;
746 strcpy(s, ctime(&now));
747 s[strlen(s) - 1] = 0;
748 strcpy(&s[11], &s[20]);
749 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
750 chanprog();
751 Context;
752 if (encrypt_pass == 0) {
753 printf(MOD_NOCRYPT);
754 exit(1);
755 }
756 i = 0;
757 for (chan = chanset; chan; chan = chan->next)
758 i++;
759 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
760 botnetnick, i, count_users(userlist));
761 cache_miss = 0;
762 cache_hit = 0;
763 Context;
764 sprintf(pid_file, "pid.%s", botnetnick);
765 Context;
766
767 /* Check for pre-existing eggdrop! */
768 f = fopen(pid_file, "r");
769 if (f != NULL) {
770 fgets(s, 10, f);
771 xx = atoi(s);
772 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
773 is used */
774 if (errno != ESRCH) {
775 printf(EGG_RUNNING1, origbotname);
776 printf(EGG_RUNNING2, pid_file);
777 exit(1);
778 }
779 }
780 Context;
781
782 #ifndef CYGWIN_HACKS
783 /* Move into background? */
784 if (backgrd) {
785 xx = fork();
786 if (xx == -1)
787 fatal("CANNOT FORK PROCESS.", 0);
788 if (xx != 0) {
789 FILE *fp;
790
791 /* Need to attempt to write pid now, not later */
792 unlink(pid_file);
793 fp = fopen(pid_file, "w");
794 if (fp != NULL) {
795 fprintf(fp, "%u\n", xx);
796 if (fflush(fp)) {
797 /* Kill bot incase a botchk is run from crond */
798 printf(EGG_NOWRITE, pid_file);
799 printf(" Try freeing some disk space\n");
800 fclose(fp);
801 unlink(pid_file);
802 exit(1);
803 }
804 fclose(fp);
805 } else
806 printf(EGG_NOWRITE, pid_file);
807 printf("Launched into the background (pid: %d)\n\n", xx);
808 #if HAVE_SETPGID
809 setpgid(xx, xx);
810 #endif
811 exit(0);
812 }
813 }
814 #endif
815 use_stderr = 0; /* Stop writing to stderr now */
816 xx = getpid();
817 if ((xx != 0) && (!backgrd)) {
818 FILE *fp;
819
820 /* Write pid to file */
821 unlink(pid_file);
822 fp = fopen(pid_file, "w");
823 if (fp != NULL) {
824 fprintf(fp, "%u\n", xx);
825 if (fflush(fp)) {
826 /* Let the bot live since this doesn't appear to be a botchk */
827 printf(EGG_NOWRITE, pid_file);
828 fclose(fp);
829 unlink(pid_file);
830 } else
831 fclose(fp);
832 } else
833 printf(EGG_NOWRITE, pid_file);
834 }
835 if (backgrd) {
836 /* Ok, try to disassociate from controlling terminal (finger cross) */
837 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
838 setpgid(0, 0);
839 #endif
840 /* Close out stdin/out/err */
841 freopen("/dev/null", "r", stdin);
842 freopen("/dev/null", "w", stdout);
843 freopen("/dev/null", "w", stderr);
844 #ifdef CYGWIN_HACKS
845 FreeConsole();
846 #endif
847 /* Note: Tcl wants those file handles kept open!
848 * close(0); close(1); close(2);
849 */
850 }
851
852 /* Terminal emulating dcc chat */
853 if ((!backgrd) && (term_z)) {
854 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
855
856 dcc[n].addr = iptolong(getmyip());
857 dcc[n].sock = STDOUT;
858 dcc[n].timeval = now;
859 dcc[n].u.chat->con_flags = conmask;
860 dcc[n].u.chat->strip_flags = STRIP_ALL;
861 dcc[n].status = STAT_ECHO;
862 strcpy(dcc[n].nick, "HQ");
863 strcpy(dcc[n].host, "llama@console");
864 dcc[n].user = get_user_by_handle(userlist, "HQ");
865 /* Make sure there's an innocuous HQ user if needed */
866 if (!dcc[n].user) {
867 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
868 dcc[n].user = get_user_by_handle(userlist, "HQ");
869 }
870 setsock(STDOUT, 0); /* Entry in net table */
871 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
872 dcc_chatter(n);
873 }
874
875 then = now;
876 online_since = now;
877 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
878 add_help_reference("cmds1.help");
879 add_help_reference("cmds2.help");
880 add_help_reference("core.help");
881 add_hook(HOOK_SECONDLY, (Function) core_secondly);
882 add_hook(HOOK_MINUTELY, (Function) core_minutely);
883 add_hook(HOOK_HOURLY, (Function) core_hourly);
884 add_hook(HOOK_REHASH, (Function) event_rehash);
885 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
886 add_hook(HOOK_USERFILE, (Function) event_save);
887 add_hook(HOOK_DAILY, (Function) event_logfile);
888 add_hook(HOOK_DAILY, (Function) event_resettraffic);
889
890 debug0("main: entering loop");
891 while (1) {
892 int socket_cleanup = 0;
893
894 Context;
895 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
896 /* Process a single tcl event */
897 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
898 #endif
899 /* Lets move some of this here, reducing the numer of actual
900 * calls to periodic_timers
901 */
902 now = time(NULL);
903 random(); /* Woop, lets really jumble things */
904 if (now != then) { /* Once a second */
905 call_hook(HOOK_SECONDLY);
906 then = now;
907 }
908 Context;
909 /* Only do this every so often */
910 if (!socket_cleanup) {
911 dcc_remove_lost();
912 /* Check for server or dcc activity */
913 dequeue_sockets();
914 socket_cleanup = 5;
915 } else
916 socket_cleanup--;
917 xx = sockgets(buf, &i);
918 if (xx >= 0) { /* Non-error */
919 int idx;
920
921 Context;
922 for (idx = 0; idx < dcc_total; idx++)
923 if (dcc[idx].sock == xx) {
924 if (dcc[idx].type && dcc[idx].type->activity) {
925 /* Traffic stats */
926 if (dcc[idx].type->name) {
927 if (!strncmp(dcc[idx].type->name, "BOT", 3))
928 itraffic_bn_today += strlen(buf) + 1;
929 else if (!strcmp(dcc[idx].type->name, "SERVER"))
930 itraffic_irc_today += strlen(buf) + 1;
931 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
932 itraffic_dcc_today += strlen(buf) + 1;
933 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
934 itraffic_dcc_today += strlen(buf) + 1;
935 else if (!strcmp(dcc[idx].type->name, "SEND"))
936 itraffic_trans_today += strlen(buf) + 1;
937 else if (!strncmp(dcc[idx].type->name, "GET", 3))
938 itraffic_trans_today += strlen(buf) + 1;
939 else
940 itraffic_unknown_today += strlen(buf) + 1;
941 }
942 dcc[idx].type->activity(idx, buf, i);
943 } else
944 putlog(LOG_MISC, "*",
945 "!!! untrapped dcc activity: type %s, sock %d",
946 dcc[idx].type->name, dcc[idx].sock);
947 break;
948 }
949 } else if (xx == -1) { /* EOF from someone */
950 int idx;
951
952 if ((i == STDOUT) && !backgrd)
953 fatal("END OF FILE ON TERMINAL", 0);
954 Context;
955 for (idx = 0; idx < dcc_total; idx++)
956 if (dcc[idx].sock == i) {
957 if (dcc[idx].type && dcc[idx].type->eof)
958 dcc[idx].type->eof(idx);
959 else {
960 putlog(LOG_MISC, "*",
961 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
962 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
963 killsock(i);
964 lostdcc(idx);
965 }
966 idx = dcc_total + 1;
967 }
968 if (idx == dcc_total) {
969 putlog(LOG_MISC, "*",
970 "(@) EOF socket %d, not a dcc socket, not anything.", i);
971 close(i);
972 killsock(i);
973 }
974 } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
975 Context;
976 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
977 for (i = 0; i < dcc_total; i++) {
978 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
979 putlog(LOG_MISC, "*",
980 "DCC socket %d (type %d, name '%s') expired -- pfft",
981 dcc[i].sock, dcc[i].type, dcc[i].nick);
982 killsock(dcc[i].sock);
983 lostdcc(i);
984 i--;
985 }
986 }
987 } else if (xx == (-3)) {
988 call_hook(HOOK_IDLE);
989 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
990 }
991
992 if (do_restart) {
993 if (do_restart == -2)
994 rehash();
995 else {
996 /* Unload as many modules as possible */
997 int f = 1;
998 module_entry *p;
999 Function x;
1000 char xx[256];
1001
1002 while (f) {
1003 f = 0;
1004 for (p = module_list; p != NULL; p = p->next) {
1005 dependancy *d = dependancy_list;
1006 int ok = 1;
1007
1008 while (ok && d) {
1009 if (d->needed == p)
1010 ok = 0;
1011 d = d->next;
1012 }
1013 if (ok) {
1014 strcpy(xx, p->name);
1015 if (module_unload(xx, origbotname) == NULL) {
1016 f = 1;
1017 break;
1018 }
1019 }
1020 }
1021 }
1022 p = module_list;
1023 if (p && p->next && p->next->next)
1024 /* Should be only 2 modules now -
1025 * blowfish & eggdrop
1026 */
1027 putlog(LOG_MISC, "*", MOD_STAGNANT);
1028 Context;
1029 flushlogs();
1030 Context;
1031 kill_tcl();
1032 init_tcl(argc, argv);
1033 init_language(0);
1034 /* We expect the encryption module as the current module pointed
1035 * to by `module_list'.
1036 */
1037 x = p->funcs[MODCALL_START];
1038 /* `NULL' indicates that we just recently restarted. The module
1039 * is expected to re-initialise as needed.
1040 */
1041 x(NULL);
1042 rehash();
1043 restart_chons();
1044 }
1045 do_restart = 0;
1046 }
1047 }
1048 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23