/[cvs]/eggdrop1.8/src/main.c
ViewVC logotype

Contents of /eggdrop1.8/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Tue Oct 19 14:20:56 2010 UTC (8 years, 7 months ago) by pseudo
Branch: MAIN
Changes since 1.4: +10 -1 lines
File MIME type: text/x-chdr
Added few autoconf checks for ssl.
Added tls to tcl_status()

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.4 2010/10/19 12:13:33 pseudo Exp $
9 */
10 /*
11 * Copyright (C) 1997 Robey Pointer
12 * Copyright (C) 1999 - 2010 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 available at eggheads@eggheads.org.
32 */
33
34 #include "main.h"
35
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <netdb.h>
40 #include <setjmp.h>
41
42 #ifdef TIME_WITH_SYS_TIME
43 # include <sys/time.h>
44 # include <time.h>
45 #else
46 # ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 # else
49 # include <time.h>
50 # endif
51 #endif
52
53 #ifdef STOP_UAC /* OSF/1 complains a lot */
54 # include <sys/sysinfo.h>
55 # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
56 #endif
57
58 #include "chan.h"
59 #include "modules.h"
60 #include "tandem.h"
61 #include "bg.h"
62
63 #ifdef DEBUG /* For debug compile */
64 # include <sys/resource.h> /* setrlimit() */
65 #endif
66
67 #ifdef CYGWIN_HACKS
68 # include <windows.h>
69 #endif
70
71 #ifndef _POSIX_SOURCE
72 # define _POSIX_SOURCE 1 /* Solaris needs this */
73 #endif
74
75 extern char origbotname[], userfile[], botnetnick[];
76 extern int dcc_total, conmask, cache_hit, cache_miss, max_logs, quick_logs,
77 quiet_save;
78 extern struct dcc_t *dcc;
79 extern struct userrec *userlist;
80 extern struct chanset_t *chanset;
81 extern log_t *logs;
82 extern Tcl_Interp *interp;
83 extern tcl_timer_t *timer, *utimer;
84 extern sigjmp_buf alarmret;
85 time_t now;
86 static int argc;
87 static char **argv;
88
89 /*
90 * Please use the PATCH macro instead of directly altering the version
91 * string from now on (it makes it much easier to maintain patches).
92 * Also please read the README file regarding your rights to distribute
93 * modified versions of this bot.
94 */
95
96 char egg_version[1024] = "1.8.0";
97 int egg_numver = 1080000;
98
99 char notify_new[121] = ""; /* Person to send a note to for new users */
100 int default_flags = 0; /* Default user flags */
101 int default_uflags = 0; /* Default user-definied flags */
102
103 int backgrd = 1; /* Run in the background? */
104 int con_chan = 0; /* Foreground: constantly display channel stats? */
105 int term_z = 0; /* Foreground: use the terminal as a partyline? */
106 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
107
108 char configfile[121] = "eggdrop.conf"; /* Default config file name */
109 char pid_file[120]; /* Name of the pid file */
110 char helpdir[121] = "help/"; /* Directory of help files */
111 char textdir[121] = "text/"; /* Directory for text files */
112
113 int keep_all_logs = 0; /* Never erase logfiles? */
114 char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
115 int switch_logfiles_at = 300; /* When to switch logfiles */
116
117 time_t online_since; /* time that the bot was started */
118
119 int make_userfile = 0; /* Using bot in userfile-creation mode? */
120 char owner[121] = ""; /* Permanent owner(s) of the bot */
121
122 int save_users_at = 0; /* Minutes past the hour to save the userfile? */
123 int notify_users_at = 0; /* Minutes past the hour to notify users of notes? */
124
125 char version[81]; /* Version info (long form) */
126 char ver[41]; /* Version info (short form) */
127 char egg_xtra[2048]; /* Patch info */
128
129 int do_restart = 0; /* .restart has been called, restart ASAP */
130 int die_on_sighup = 0; /* Die if bot receives SIGHUP */
131 int die_on_sigterm = 1; /* Die if bot receives SIGTERM */
132 int resolve_timeout = 15; /* Hostname/address lookup timeout */
133 char quit_msg[1024]; /* Quit message */
134
135 /* Traffic stats */
136 unsigned long otraffic_irc = 0;
137 unsigned long otraffic_irc_today = 0;
138 unsigned long otraffic_bn = 0;
139 unsigned long otraffic_bn_today = 0;
140 unsigned long otraffic_dcc = 0;
141 unsigned long otraffic_dcc_today = 0;
142 unsigned long otraffic_filesys = 0;
143 unsigned long otraffic_filesys_today = 0;
144 unsigned long otraffic_trans = 0;
145 unsigned long otraffic_trans_today = 0;
146 unsigned long otraffic_unknown = 0;
147 unsigned long otraffic_unknown_today = 0;
148 unsigned long itraffic_irc = 0;
149 unsigned long itraffic_irc_today = 0;
150 unsigned long itraffic_bn = 0;
151 unsigned long itraffic_bn_today = 0;
152 unsigned long itraffic_dcc = 0;
153 unsigned long itraffic_dcc_today = 0;
154 unsigned long itraffic_trans = 0;
155 unsigned long itraffic_trans_today = 0;
156 unsigned long itraffic_unknown = 0;
157 unsigned long itraffic_unknown_today = 0;
158
159 #ifdef DEBUG_CONTEXT
160 /* Context storage for fatal crashes */
161 char cx_file[16][30];
162 char cx_note[16][256];
163 int cx_line[16];
164 int cx_ptr = 0;
165 #endif
166
167 #ifdef TLS
168 int ssl_cleanup();
169 #endif
170
171 void fatal(const char *s, int recoverable)
172 {
173 int i;
174
175 putlog(LOG_MISC, "*", "* %s", s);
176 flushlogs();
177 for (i = 0; i < dcc_total; i++)
178 if (dcc[i].sock >= 0)
179 killsock(dcc[i].sock);
180 #ifdef TLS
181 ssl_cleanup();
182 #endif
183 unlink(pid_file);
184 if (!recoverable) {
185 bg_send_quit(BG_ABORT);
186 exit(1);
187 }
188 }
189
190 int expmem_chanprog();
191 int expmem_users();
192 int expmem_misc();
193 int expmem_dccutil();
194 int expmem_botnet();
195 int expmem_tcl();
196 int expmem_tclhash();
197 int expmem_net();
198 int expmem_modules(int);
199 int expmem_language();
200 int expmem_tcldcc();
201 int expmem_tclmisc();
202 int expmem_dns();
203 #ifdef TLS
204 int expmem_tls();
205 #endif
206
207 /* For mem.c : calculate memory we SHOULD be using
208 */
209 int expected_memory(void)
210 {
211 int tot;
212
213 tot = expmem_chanprog() + expmem_users() + expmem_misc() + expmem_dccutil() +
214 expmem_botnet() + expmem_tcl() + expmem_tclhash() + expmem_net() +
215 expmem_modules(0) + expmem_language() + expmem_tcldcc() +
216 expmem_tclmisc() + expmem_dns();
217 #ifdef TLS
218 tot += expmem_tls();
219 #endif
220 return tot;
221 }
222
223 static void check_expired_dcc()
224 {
225 int i;
226
227 for (i = 0; i < dcc_total; i++)
228 if (dcc[i].type && dcc[i].type->timeout_val &&
229 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
230 if (dcc[i].type->timeout)
231 dcc[i].type->timeout(i);
232 else if (dcc[i].type->eof)
233 dcc[i].type->eof(i);
234 else
235 continue;
236 /* Only timeout 1 socket per cycle, too risky for more */
237 return;
238 }
239 }
240
241 #ifndef DEBUG_CONTEXT
242 #define write_debug() do {} while (0)
243 #else
244 static int nested_debug = 0;
245
246 static void write_debug()
247 {
248 int x;
249 char s[25];
250 int y;
251
252 if (nested_debug) {
253 /* Yoicks, if we have this there's serious trouble!
254 * All of these are pretty reliable, so we'll try these.
255 *
256 * NOTE: dont try and display context-notes in here, it's
257 * _not_ safe <cybah>
258 */
259 x = creat("DEBUG.DEBUG", 0644);
260 setsock(x, SOCK_NONSOCK);
261 if (x >= 0) {
262 strncpyz(s, ctime(&now), sizeof s);
263 dprintf(-x, "Debug (%s) written %s\n", ver, s);
264 dprintf(-x, "Please report problem to bugs@eggheads.org\n");
265 dprintf(-x, "after a visit to http://www.eggheads.org/bugzilla/\n");
266 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
267 dprintf(-x, "Context: ");
268 cx_ptr = cx_ptr & 15;
269 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
270 dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
271 dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
272 killsock(x);
273 close(x);
274 }
275 bg_send_quit(BG_ABORT);
276 exit(1); /* Dont even try & tell people about, that may
277 * have caused the fault last time. */
278 } else
279 nested_debug = 1;
280 putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
281 cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
282 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
283 putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
284 x = creat("DEBUG", 0644);
285 setsock(x, SOCK_NONSOCK);
286 if (x < 0) {
287 putlog(LOG_MISC, "*", "* Failed to write DEBUG");
288 } else {
289 strncpyz(s, ctime(&now), sizeof s);
290 dprintf(-x, "Debug (%s) written %s\n", ver, s);
291 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
292 #ifdef STATIC
293 dprintf(-x, "STATICALLY LINKED\n");
294 #endif
295
296 /* info library */
297 dprintf(-x, "Tcl library: %s\n",
298 ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
299 tcl_resultstring() : "*unknown*");
300
301 /* info tclversion/patchlevel */
302 dprintf(-x, "Tcl version: %s (header version %s)\n",
303 ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
304 tcl_resultstring() : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
305 tcl_resultstring() : "*unknown*", TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL :
306 "*unknown*");
307
308 if (tcl_threaded())
309 dprintf(-x, "Tcl is threaded\n");
310 #ifdef IPV6
311 dprintf(-x, "Compiled with IPv6 support\n");
312 #else
313 dprintf(-x, "Compiled without IPv6 support\n");
314 #endif
315
316 #ifdef TLS
317 dprintf(-x, "Compiled with TLS support\n");
318 #else
319 dprintf(-x, "Compiled without TLS support\n");
320 #endif
321
322 dprintf(-x, "Configure flags: %s\n", EGG_AC_ARGS);
323 #ifdef CCFLAGS
324 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
325 #endif
326
327 #ifdef LDFLAGS
328 dprintf(-x, "Link flags: %s\n", LDFLAGS);
329 #endif
330
331 #ifdef STRIPFLAGS
332 dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
333 #endif
334
335 dprintf(-x, "Context: ");
336 cx_ptr = cx_ptr & 15;
337 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
338 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
339 (cx_note[y][0]) ? cx_note[y] : "");
340 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
341 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
342 tell_dcc(-x);
343 dprintf(-x, "\n");
344 debug_mem_to_dcc(-x);
345 killsock(x);
346 close(x);
347 putlog(LOG_MISC, "*", "* Wrote DEBUG");
348 }
349 }
350 #endif /* DEBUG_CONTEXT */
351
352 static void got_bus(int z)
353 {
354 write_debug();
355 fatal("BUS ERROR -- CRASHING!", 1);
356 #ifdef SA_RESETHAND
357 kill(getpid(), SIGBUS);
358 #else
359 bg_send_quit(BG_ABORT);
360 exit(1);
361 #endif
362 }
363
364 static void got_segv(int z)
365 {
366 write_debug();
367 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
368 #ifdef SA_RESETHAND
369 kill(getpid(), SIGSEGV);
370 #else
371 bg_send_quit(BG_ABORT);
372 exit(1);
373 #endif
374 }
375
376 static void got_fpe(int z)
377 {
378 write_debug();
379 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
380 }
381
382 static void got_term(int z)
383 {
384 write_userfile(-1);
385 check_tcl_event("sigterm");
386 if (die_on_sigterm) {
387 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
388 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
389 } else
390 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
391 }
392
393 static void got_quit(int z)
394 {
395 check_tcl_event("sigquit");
396 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
397 return;
398 }
399
400 static void got_hup(int z)
401 {
402 write_userfile(-1);
403 check_tcl_event("sighup");
404 if (die_on_sighup) {
405 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
406 } else
407 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
408 do_restart = -2;
409 return;
410 }
411
412 /* A call to resolver (gethostbyname, etc) timed out
413 */
414 static void got_alarm(int z)
415 {
416 siglongjmp(alarmret, 1);
417
418 /* -Never reached- */
419 }
420
421 /* Got ILL signal -- log context and continue
422 */
423 static void got_ill(int z)
424 {
425 check_tcl_event("sigill");
426 #ifdef DEBUG_CONTEXT
427 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
428 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
429 #endif
430 }
431
432 #ifdef DEBUG_CONTEXT
433 /* Called from the Context macro.
434 */
435 void eggContext(const char *file, int line, const char *module)
436 {
437 char x[31], *p;
438
439 p = strrchr(file, '/');
440 if (!module) {
441 strncpyz(x, p ? p + 1 : file, sizeof x);
442 } else
443 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
444 cx_ptr = ((cx_ptr + 1) & 15);
445 strcpy(cx_file[cx_ptr], x);
446 cx_line[cx_ptr] = line;
447 cx_note[cx_ptr][0] = 0;
448 }
449
450 /* Called from the ContextNote macro.
451 */
452 void eggContextNote(const char *file, int line, const char *module,
453 const char *note)
454 {
455 char x[31], *p;
456
457 p = strrchr(file, '/');
458 if (!module)
459 strncpyz(x, p ? p + 1 : file, sizeof x);
460 else
461 egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
462 cx_ptr = ((cx_ptr + 1) & 15);
463 strcpy(cx_file[cx_ptr], x);
464 cx_line[cx_ptr] = line;
465 strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
466 }
467 #endif /* DEBUG_CONTEXT */
468
469 #ifdef DEBUG_ASSERT
470 /* Called from the Assert macro.
471 */
472 void eggAssert(const char *file, int line, const char *module)
473 {
474 write_debug();
475 if (!module)
476 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
477 else
478 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
479 fatal("ASSERT FAILED -- CRASHING!", 1);
480 }
481 #endif
482
483 static void do_arg(char *s)
484 {
485 char x[512], *z = x;
486 int i;
487
488 if (s[0] == '-')
489 for (i = 1; i < strlen(s); i++) {
490 switch (s[i]) {
491 case 'n':
492 backgrd = 0;
493 break;
494 case 'c':
495 con_chan = 1;
496 term_z = 0;
497 break;
498 case 't':
499 con_chan = 0;
500 term_z = 1;
501 break;
502 case 'm':
503 make_userfile = 1;
504 break;
505 case 'v':
506 strncpyz(x, egg_version, sizeof x);
507 newsplit(&z);
508 newsplit(&z);
509 printf("%s\n", version);
510 if (z[0])
511 printf(" (patches: %s)\n", z);
512 printf("Configured with: " EGG_AC_ARGS "\n");
513 printf("Compiled with: ");
514 #ifdef IPV6
515 printf("IPv6, ");
516 #endif
517 #ifdef TLS
518 printf("TLS, ");
519 #endif
520 printf("handlen=%d\n", HANDLEN);
521 bg_send_quit(BG_ABORT);
522 exit(0);
523 break; /* this should never be reached */
524 case 'h':
525 printf("\n%s\n\n", version);
526 printf(EGG_USAGE);
527 printf("\n");
528 bg_send_quit(BG_ABORT);
529 exit(0);
530 break; /* this should never be reached */
531 }
532 } else
533 strncpyz(configfile, s, sizeof configfile);
534 }
535
536 void backup_userfile(void)
537 {
538 char s[125];
539
540 if (quiet_save < 2)
541 putlog(LOG_MISC, "*", USERF_BACKUP);
542 egg_snprintf(s, sizeof s, "%s~bak", userfile);
543 copyfile(userfile, s);
544 }
545
546 /* Timer info */
547 static int lastmin = 99;
548 static time_t then;
549 static struct tm nowtm;
550
551 /* Called once a second.
552 *
553 * Note: Try to not put any Context lines in here (guppy 21Mar2000).
554 */
555 static void core_secondly()
556 {
557 static int cnt = 0;
558 int miltime;
559
560 do_check_timers(&utimer); /* Secondly timers */
561 cnt++;
562 if (cnt >= 10) { /* Every 10 seconds */
563 cnt = 0;
564 check_expired_dcc();
565 if (con_chan && !backgrd) {
566 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
567 tell_verbose_status(DP_STDOUT);
568 do_module_report(DP_STDOUT, 0, "server");
569 do_module_report(DP_STDOUT, 0, "channels");
570 tell_mem_status_dcc(DP_STDOUT);
571 }
572 }
573 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
574 if (nowtm.tm_min != lastmin) {
575 int i = 0;
576
577 /* Once a minute */
578 lastmin = (lastmin + 1) % 60;
579 call_hook(HOOK_MINUTELY);
580 check_expired_ignores();
581 autolink_cycle(NULL); /* Attempt autolinks */
582 /* In case for some reason more than 1 min has passed: */
583 while (nowtm.tm_min != lastmin) {
584 /* Timer drift, dammit */
585 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
586 i++;
587 lastmin = (lastmin + 1) % 60;
588 call_hook(HOOK_MINUTELY);
589 }
590 if (i > 1)
591 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
592 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
593 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
594 call_hook(HOOK_5MINUTELY);
595 check_botnet_pings();
596 if (!quick_logs) {
597 flushlogs();
598 check_logsize();
599 }
600 if (!miltime) { /* At midnight */
601 char s[25];
602 int j;
603
604 strncpyz(s, ctime(&now), sizeof s);
605 if (quiet_save < 3)
606 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
607 call_hook(HOOK_BACKUP);
608 for (j = 0; j < max_logs; j++) {
609 if (logs[j].filename != NULL && logs[j].f != NULL) {
610 fclose(logs[j].f);
611 logs[j].f = NULL;
612 }
613 }
614 }
615 }
616 if (nowtm.tm_min == notify_users_at)
617 call_hook(HOOK_HOURLY);
618 /* These no longer need checking since they are all check vs minutely
619 * settings and we only get this far on the minute.
620 */
621 if (miltime == switch_logfiles_at) {
622 call_hook(HOOK_DAILY);
623 if (!keep_all_logs) {
624 if (quiet_save < 3)
625 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
626 for (i = 0; i < max_logs; i++)
627 if (logs[i].filename) {
628 char s[1024];
629
630 if (logs[i].f) {
631 fclose(logs[i].f);
632 logs[i].f = NULL;
633 }
634 egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
635 unlink(s);
636 movefile(logs[i].filename, s);
637 }
638 }
639 }
640 }
641 }
642
643 static void core_minutely()
644 {
645 check_tcl_time(&nowtm);
646 check_tcl_cron(&nowtm);
647 do_check_timers(&timer);
648 if (quick_logs != 0) {
649 flushlogs();
650 check_logsize();
651 }
652 }
653
654 static void core_hourly()
655 {
656 write_userfile(-1);
657 }
658
659 static void event_rehash()
660 {
661 check_tcl_event("rehash");
662 }
663
664 static void event_prerehash()
665 {
666 check_tcl_event("prerehash");
667 }
668
669 static void event_save()
670 {
671 check_tcl_event("save");
672 }
673
674 static void event_logfile()
675 {
676 check_tcl_event("logfile");
677 }
678
679 static void event_resettraffic()
680 {
681 otraffic_irc += otraffic_irc_today;
682 itraffic_irc += itraffic_irc_today;
683 otraffic_bn += otraffic_bn_today;
684 itraffic_bn += itraffic_bn_today;
685 otraffic_dcc += otraffic_dcc_today;
686 itraffic_dcc += itraffic_dcc_today;
687 otraffic_unknown += otraffic_unknown_today;
688 itraffic_unknown += itraffic_unknown_today;
689 otraffic_trans += otraffic_trans_today;
690 itraffic_trans += itraffic_trans_today;
691 otraffic_irc_today = otraffic_bn_today = 0;
692 otraffic_dcc_today = otraffic_unknown_today = 0;
693 itraffic_irc_today = itraffic_bn_today = 0;
694 itraffic_dcc_today = itraffic_unknown_today = 0;
695 itraffic_trans_today = otraffic_trans_today = 0;
696 }
697
698 static void event_loaded()
699 {
700 check_tcl_event("loaded");
701 }
702
703 void kill_tcl();
704 extern module_entry *module_list;
705 void restart_chons();
706
707 #ifdef STATIC
708 void check_static(char *, char *(*)());
709
710 #include "mod/static.h"
711 #endif
712 int init_threaddata(int);
713 int init_mem();
714 int init_userent();
715 int init_misc();
716 int init_bots();
717 int init_modules();
718 int init_tcl(int, char **);
719 int init_language(int);
720 #ifdef TLS
721 int ssl_init();
722 #endif
723
724 static void patch(const char *str)
725 {
726 char *p = strchr(egg_version, '+');
727
728 if (!p)
729 p = &egg_version[strlen(egg_version)];
730 sprintf(p, "+%s", str);
731 egg_numver++;
732 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
733 }
734
735 static inline void garbage_collect(void)
736 {
737 static u_8bit_t run_cnt = 0;
738
739 if (run_cnt == 3)
740 garbage_collect_tclhash();
741 else
742 run_cnt++;
743 }
744
745 int mainloop(int toplevel)
746 {
747 static int socket_cleanup = 0;
748 int xx, i, eggbusy = 1, tclbusy = 0;
749 char buf[520];
750
751 /* Lets move some of this here, reducing the numer of actual
752 * calls to periodic_timers
753 */
754 now = time(NULL);
755 /*
756 * FIXME: Get rid of this, it's ugly and wastes lots of cpu.
757 *
758 * pre-1.3.0 Eggdrop had random() in the once a second block below.
759 *
760 * This attempts to keep random() more random by constantly
761 * calling random() and updating the state information.
762 */
763 random(); /* Woop, lets really jumble things */
764
765 /* If we want to restart, we have to unwind to the toplevel.
766 * Tcl will Panic if we kill the interp with Tcl_Eval in progress.
767 * This is done by returning -1 in tickle_WaitForEvent.
768 */
769 if (do_restart && do_restart != -2 && !toplevel)
770 return -1;
771
772 /* Once a second */
773 if (now != then) {
774 call_hook(HOOK_SECONDLY);
775 then = now;
776 }
777
778 /* Only do this every so often. */
779 if (!socket_cleanup) {
780 socket_cleanup = 5;
781
782 /* Remove dead dcc entries. */
783 dcc_remove_lost();
784
785 /* Check for server or dcc activity. */
786 dequeue_sockets();
787 } else
788 socket_cleanup--;
789
790 /* Free unused structures. */
791 garbage_collect();
792
793 xx = sockgets(buf, &i);
794 if (xx >= 0) { /* Non-error */
795 int idx;
796
797 for (idx = 0; idx < dcc_total; idx++)
798 if (dcc[idx].sock == xx) {
799 if (dcc[idx].type && dcc[idx].type->activity) {
800 /* Traffic stats */
801 if (dcc[idx].type->name) {
802 if (!strncmp(dcc[idx].type->name, "BOT", 3))
803 itraffic_bn_today += strlen(buf) + 1;
804 else if (!strcmp(dcc[idx].type->name, "SERVER"))
805 itraffic_irc_today += strlen(buf) + 1;
806 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
807 itraffic_dcc_today += strlen(buf) + 1;
808 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
809 itraffic_dcc_today += strlen(buf) + 1;
810 else if (!strcmp(dcc[idx].type->name, "SEND"))
811 itraffic_trans_today += strlen(buf) + 1;
812 else if (!strncmp(dcc[idx].type->name, "GET", 3))
813 itraffic_trans_today += strlen(buf) + 1;
814 else
815 itraffic_unknown_today += strlen(buf) + 1;
816 }
817 dcc[idx].type->activity(idx, buf, i);
818 } else
819 putlog(LOG_MISC, "*",
820 "!!! untrapped dcc activity: type %s, sock %d",
821 dcc[idx].type->name, dcc[idx].sock);
822 break;
823 }
824 } else if (xx == -1) { /* EOF from someone */
825 int idx;
826
827 if (i == STDOUT && !backgrd)
828 fatal("END OF FILE ON TERMINAL", 0);
829 for (idx = 0; idx < dcc_total; idx++)
830 if (dcc[idx].sock == i) {
831 if (dcc[idx].type && dcc[idx].type->eof)
832 dcc[idx].type->eof(idx);
833 else {
834 putlog(LOG_MISC, "*",
835 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
836 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
837 killsock(i);
838 lostdcc(idx);
839 }
840 idx = dcc_total + 1;
841 }
842 if (idx == dcc_total) {
843 putlog(LOG_MISC, "*",
844 "(@) EOF socket %d, not a dcc socket, not anything.", i);
845 close(i);
846 killsock(i);
847 }
848 } else if (xx == -2 && errno != EINTR) { /* select() error */
849 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
850 for (i = 0; i < dcc_total; i++) {
851 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) {
852 putlog(LOG_MISC, "*",
853 "DCC socket %d (type %d, name '%s') expired -- pfft",
854 dcc[i].sock, dcc[i].type, dcc[i].nick);
855 killsock(dcc[i].sock);
856 lostdcc(i);
857 i--;
858 }
859 }
860 } else if (xx == -3) {
861 call_hook(HOOK_IDLE);
862 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
863 eggbusy = 0;
864 } else if (xx == -5) {
865 eggbusy = 0;
866 tclbusy = 1;
867 }
868
869 if (do_restart) {
870 if (do_restart == -2)
871 rehash();
872 else if (!toplevel)
873 return -1; /* Unwind to toplevel before restarting */
874 else {
875 /* Unload as many modules as possible */
876 int f = 1;
877 module_entry *p;
878 Function startfunc;
879 char name[256];
880
881 /* oops, I guess we should call this event before tcl is restarted */
882 check_tcl_event("prerestart");
883
884 while (f) {
885 f = 0;
886 for (p = module_list; p != NULL; p = p->next) {
887 dependancy *d = dependancy_list;
888 int ok = 1;
889
890 while (ok && d) {
891 if (d->needed == p)
892 ok = 0;
893 d = d->next;
894 }
895 if (ok) {
896 strcpy(name, p->name);
897 if (module_unload(name, botnetnick) == NULL) {
898 f = 1;
899 break;
900 }
901 }
902 }
903 }
904
905 /* Make sure we don't have any modules left hanging around other than
906 * "eggdrop" and the two that are supposed to be.
907 */
908 for (f = 0, p = module_list; p; p = p->next) {
909 if (strcmp(p->name, "eggdrop") && strcmp(p->name, "encryption") &&
910 strcmp(p->name, "uptime")) {
911 f++;
912 }
913 }
914 if (f != 0) {
915 putlog(LOG_MISC, "*", MOD_STAGNANT);
916 }
917
918 flushlogs();
919 kill_tcl();
920 init_tcl(argc, argv);
921 init_language(0);
922
923 /* this resets our modules which we didn't unload (encryption and uptime) */
924 for (p = module_list; p; p = p->next) {
925 if (p->funcs) {
926 startfunc = p->funcs[MODCALL_START];
927 startfunc(NULL);
928 }
929 }
930
931 rehash();
932 #ifdef TLS
933 ssl_cleanup();
934 ssl_init();
935 #endif
936 restart_chons();
937 call_hook(HOOK_LOADED);
938 }
939 eggbusy = 1;
940 do_restart = 0;
941 }
942
943 #ifdef USE_TCL_EVENTS
944 if (!eggbusy) {
945 /* Process all pending tcl events */
946 # ifdef REPLACE_NOTIFIER
947 if (Tcl_ServiceAll())
948 tclbusy = 1;
949 # else
950 while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS))
951 tclbusy = 1;
952 # endif /* REPLACE_NOTIFIER */
953 #endif /* USE_TCL_EVENTS */
954 }
955
956 return (eggbusy || tclbusy);
957 }
958
959 int main(int arg_c, char **arg_v)
960 {
961 int i, xx;
962 char s[25];
963 FILE *f;
964 struct sigaction sv;
965 struct chanset_t *chan;
966 #ifdef DEBUG
967 struct rlimit cdlim;
968 #endif
969 #ifdef STOP_UAC
970 int nvpair[2];
971 #endif
972
973 /* Make sure it can write core, if you make debug. Else it's pretty
974 * useless (dw)
975 *
976 * Only allow unlimited size core files when compiled with DEBUG defined.
977 * This is not a good idea for normal builds -- in these cases, use the
978 * default system resource limits instead.
979 */
980 #ifdef DEBUG
981 cdlim.rlim_cur = RLIM_INFINITY;
982 cdlim.rlim_max = RLIM_INFINITY;
983 setrlimit(RLIMIT_CORE, &cdlim);
984 #endif
985
986 #ifdef DEBUG_CONTEXT
987 /* Initialise context list */
988 for (i = 0; i < 16; i++)
989 Context;
990 #endif
991
992 /* Include patch.h header for patch("...") */
993 #include "patch.h"
994
995 argc = arg_c;
996 argv = arg_v;
997
998 /* Version info! */
999 egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
1000 egg_snprintf(version, sizeof version,
1001 "Eggdrop v%s (C) 1997 Robey Pointer (C) 2010 Eggheads",
1002 egg_version);
1003 /* Now add on the patchlevel (for Tcl) */
1004 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
1005 strcat(egg_version, egg_xtra);
1006
1007 /* For OSF/1 */
1008 #ifdef STOP_UAC
1009 /* Don't print "unaligned access fixup" warning to the user */
1010 nvpair[0] = SSIN_UACPROC;
1011 nvpair[1] = UAC_NOPRINT;
1012 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
1013 #endif
1014
1015 /* Set up error traps: */
1016 sv.sa_handler = got_bus;
1017 sigemptyset(&sv.sa_mask);
1018 #ifdef SA_RESETHAND
1019 sv.sa_flags = SA_RESETHAND;
1020 #else
1021 sv.sa_flags = 0;
1022 #endif
1023 sigaction(SIGBUS, &sv, NULL);
1024 sv.sa_handler = got_segv;
1025 sigaction(SIGSEGV, &sv, NULL);
1026 #ifdef SA_RESETHAND
1027 sv.sa_flags = 0;
1028 #endif
1029 sv.sa_handler = got_fpe;
1030 sigaction(SIGFPE, &sv, NULL);
1031 sv.sa_handler = got_term;
1032 sigaction(SIGTERM, &sv, NULL);
1033 sv.sa_handler = got_hup;
1034 sigaction(SIGHUP, &sv, NULL);
1035 sv.sa_handler = got_quit;
1036 sigaction(SIGQUIT, &sv, NULL);
1037 sv.sa_handler = SIG_IGN;
1038 sigaction(SIGPIPE, &sv, NULL);
1039 sv.sa_handler = got_ill;
1040 sigaction(SIGILL, &sv, NULL);
1041 sv.sa_handler = got_alarm;
1042 sigaction(SIGALRM, &sv, NULL);
1043
1044 /* Initialize variables and stuff */
1045 now = time(NULL);
1046 chanset = NULL;
1047 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
1048 lastmin = nowtm.tm_min;
1049 srandom((unsigned int) (now % (getpid() + getppid())));
1050 init_mem();
1051 init_language(1);
1052 if (argc > 1)
1053 for (i = 1; i < argc; i++)
1054 do_arg(argv[i]);
1055
1056 printf("\n%s\n", version);
1057
1058 #ifndef CYGWIN_HACKS
1059 /* Don't allow eggdrop to run as root
1060 * This check isn't useful under cygwin and has been
1061 * reported to cause trouble in some situations.
1062 */
1063 if (((int) getuid() == 0) || ((int) geteuid() == 0))
1064 fatal("ERROR: Eggdrop will not run as root!", 0);
1065 #endif
1066
1067 #ifndef REPLACE_NOTIFIER
1068 init_threaddata(1);
1069 #endif
1070 init_userent();
1071 init_misc();
1072 init_bots();
1073 init_modules();
1074 if (backgrd)
1075 bg_prepare_split();
1076 init_tcl(argc, argv);
1077 init_language(0);
1078 #ifdef STATIC
1079 link_statics();
1080 #endif
1081 strncpyz(s, ctime(&now), sizeof s);
1082 strcpy(&s[11], &s[20]);
1083 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
1084 chanprog();
1085 if (!encrypt_pass) {
1086 printf(MOD_NOCRYPT);
1087 bg_send_quit(BG_ABORT);
1088 exit(1);
1089 }
1090 i = 0;
1091 for (chan = chanset; chan; chan = chan->next)
1092 i++;
1093 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
1094 botnetnick, i, count_users(userlist));
1095 #ifdef TLS
1096 ssl_init();
1097 #endif
1098 cache_miss = 0;
1099 cache_hit = 0;
1100 if (!pid_file[0])
1101 egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
1102
1103 /* Check for pre-existing eggdrop! */
1104 f = fopen(pid_file, "r");
1105 if (f != NULL) {
1106 fgets(s, 10, f);
1107 xx = atoi(s);
1108 i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
1109 * is used */
1110 if (i == 0 || errno != ESRCH) {
1111 printf(EGG_RUNNING1, botnetnick);
1112 printf(EGG_RUNNING2, pid_file);
1113 bg_send_quit(BG_ABORT);
1114 exit(1);
1115 }
1116 }
1117
1118 /* Move into background? */
1119 if (backgrd) {
1120 bg_do_split();
1121 } else { /* !backgrd */
1122 xx = getpid();
1123 if (xx != 0) {
1124 FILE *fp;
1125
1126 /* Write pid to file */
1127 unlink(pid_file);
1128 fp = fopen(pid_file, "w");
1129 if (fp != NULL) {
1130 fprintf(fp, "%u\n", xx);
1131 if (fflush(fp)) {
1132 /* Let the bot live since this doesn't appear to be a botchk */
1133 printf(EGG_NOWRITE, pid_file);
1134 fclose(fp);
1135 unlink(pid_file);
1136 } else
1137 fclose(fp);
1138 } else
1139 printf(EGG_NOWRITE, pid_file);
1140 }
1141 }
1142
1143 use_stderr = 0; /* Stop writing to stderr now */
1144 if (backgrd) {
1145 /* Ok, try to disassociate from controlling terminal (finger cross) */
1146 #ifdef HAVE_SETPGID
1147 setpgid(0, 0);
1148 #endif
1149 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
1150 freopen("/dev/null", "r", stdin);
1151 freopen("/dev/null", "w", stdout);
1152 freopen("/dev/null", "w", stderr);
1153 #ifdef CYGWIN_HACKS
1154 FreeConsole();
1155 #endif
1156 }
1157
1158 /* Terminal emulating dcc chat */
1159 if (!backgrd && term_z) {
1160 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
1161
1162 getvhost(&dcc[n].sockname, AF_INET);
1163 dcc[n].sock = STDOUT;
1164 dcc[n].timeval = now;
1165 dcc[n].u.chat->con_flags = conmask;
1166 dcc[n].u.chat->strip_flags = STRIP_ALL;
1167 dcc[n].status = STAT_ECHO;
1168 strcpy(dcc[n].nick, "HQ");
1169 strcpy(dcc[n].host, "llama@console");
1170 /* HACK: Workaround not to pass literal "HQ" as a non-const arg */
1171 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1172 /* Make sure there's an innocuous HQ user if needed */
1173 if (!dcc[n].user) {
1174 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
1175 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1176 }
1177 setsock(STDOUT, 0); /* Entry in net table */
1178 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
1179 dcc_chatter(n);
1180 }
1181
1182 then = now;
1183 online_since = now;
1184 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
1185 add_help_reference("cmds1.help");
1186 add_help_reference("cmds2.help");
1187 add_help_reference("core.help");
1188 add_hook(HOOK_SECONDLY, (Function) core_secondly);
1189 add_hook(HOOK_MINUTELY, (Function) core_minutely);
1190 add_hook(HOOK_HOURLY, (Function) core_hourly);
1191 add_hook(HOOK_REHASH, (Function) event_rehash);
1192 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
1193 add_hook(HOOK_USERFILE, (Function) event_save);
1194 add_hook(HOOK_BACKUP, (Function) backup_userfile);
1195 add_hook(HOOK_DAILY, (Function) event_logfile);
1196 add_hook(HOOK_DAILY, (Function) event_resettraffic);
1197 add_hook(HOOK_LOADED, (Function) event_loaded);
1198
1199 call_hook(HOOK_LOADED);
1200
1201 debug0("main: entering loop");
1202 while (1) {
1203 mainloop(1);
1204 }
1205 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23