/[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.80 - (show annotations) (download) (as text)
Fri Oct 12 02:27:45 2001 UTC (17 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.79: +6 -3 lines
File MIME type: text/x-chdr
Moved some stuff from main.c to botnet.c (for modularizing botnet)
Changed irctoupper to _irctoupper in match.c (temporary fix, cuz something is making it crash (I think irctoupper isn't initialized or exported or something))
Added code to expand the socket table when it's full.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23