/[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.56 - (show annotations) (download) (as text)
Thu Apr 12 02:39:43 2001 UTC (18 years, 6 months ago) by guppy
Branch: MAIN
Changes since 1.55: +15 -16 lines
File MIME type: text/x-chdr
lets update the copyright info for 2001 k? :P~

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23