/[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.74 - (show annotations) (download) (as text)
Mon Aug 13 16:52:13 2001 UTC (18 years ago) by guppy
Branch: MAIN
Changes since 1.73: +5 -11 lines
File MIME type: text/x-chdr
Cleaned up the output of .status inregards to TCL ...

Instead of showing:

Using Tcl library: /usr/local/lib/tcl8.3
Tcl version: 8.3 (header version 8.3)
Tcl patchlevel: 8.3.3 (header patchlevel 8.3.3)
TCL isn't threaded

We now show:

Tcl library: /usr/lib/tcl8.3
Tcl version: 8.3.2 (header version 8.3.2)

and is TCL is threaded:

Tcl library: /usr/lib/tcl8.3
Tcl version: 8.3.2 (header version 8.3.2)
Tcl is threaded

Do we even need to show the header version?

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23