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

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

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


Revision 1.41 - (show annotations) (download) (as text)
Sun Jan 30 20:24:15 2000 UTC (20 years, 9 months ago) by guppy
Branch: MAIN
CVS Tags: eggdrop10403RC1
Changes since 1.40: +3 -3 lines
File MIME type: text/x-chdr
eggdrop1.4.3 - the start of it anyways

1 /*
2 * main.c -- handles:
3 * core event handling
4 * signal handling
5 * command line arguments
6 * context and assert debugging
7 *
8 * dprintf'ized, 15nov1995
9 *
10 * $Id: main.c,v 1.40 2000/01/17 16:14:45 per Exp $
11 */
12 /*
13 * Copyright (C) 1997 Robey Pointer
14 * Copyright (C) 1999, 2000 Eggheads
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30 /*
31 * The author (Robey Pointer) can be reached at: robey@netcom.com
32 * NOTE: Robey is no long working on this code, there is a discussion
33 * list avaliable at eggheads@eggheads.org.
34 */
35
36 #include "main.h"
37 #include <time.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <netdb.h>
42 #include <setjmp.h>
43 #ifdef STOP_UAC /* osf/1 complains a lot */
44 #include <sys/sysinfo.h>
45 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
46 #endif
47 /* some systems have a working sys/wait.h even though configure will
48 * decide it's not bsd compatable. oh well. */
49 #include "chan.h"
50 #include "modules.h"
51 #include "tandem.h"
52 #ifdef CYGWIN_HACKS
53 #include <windows.h>
54 BOOL FreeConsole(VOID);
55 #endif
56
57 #ifndef _POSIX_SOURCE
58 /* solaris needs this */
59 #define _POSIX_SOURCE 1
60 #endif
61
62 extern char origbotname[], userfile[], botnetnick[];
63 extern int dcc_total, conmask, cache_hit, cache_miss, max_logs, quick_logs;
64 extern struct dcc_t *dcc;
65 extern struct userrec *userlist;
66 extern struct chanset_t *chanset;
67 extern log_t *logs;
68 extern Tcl_Interp *interp;
69 extern tcl_timer_t *timer, *utimer;
70 extern jmp_buf alarmret;
71
72 /*
73 * Please use the PATCH macro instead of directly altering the version
74 * string from now on (it makes it much easier to maintain patches).
75 * Also please read the README file regarding your rights to distribute
76 * modified versions of this bot.
77 */
78
79 char egg_version[1024] = "1.4.3";
80 int egg_numver = 1040300;
81
82 char notify_new[121] = ""; /* person to send a note to for new users */
83 int default_flags = 0; /* default user flags and */
84 int default_uflags = 0; /* default userdefinied flags for people */
85 /* who say 'hello' or for .adduser */
86
87 int backgrd = 1; /* run in the background? */
88 int con_chan = 0; /* foreground: constantly display channel
89 * stats? */
90 int term_z = 0; /* foreground: use the terminal as a party
91 * line? */
92 char configfile[121] = "egg.config"; /* name of the config file */
93 char helpdir[121]; /* directory of help files (if used) */
94 char textdir[121] = ""; /* directory for text files that get dumped */
95 int keep_all_logs = 0; /* never erase logfiles, no matter how old
96 * they are? */
97 time_t online_since; /* unix-time that the bot loaded up */
98 int make_userfile = 0; /* using bot in make-userfile mode? (first
99 * user to 'hello' becomes master) */
100 char owner[121] = ""; /* permanent owner(s) of the bot */
101 char pid_file[40]; /* name of the file for the pid to be
102 * stored in */
103 int save_users_at = 0; /* how many minutes past the hour to
104 * save the userfile? */
105 int notify_users_at = 0; /* how many minutes past the hour to
106 * notify users of notes? */
107 int switch_logfiles_at = 300; /* when (military time) to switch logfiles */
108 char version[81]; /* version info (long form) */
109 char ver[41]; /* version info (short form) */
110 char egg_xtra[2048]; /* patch info */
111 int use_stderr = 1; /* send stuff to stderr instead of logfiles? */
112 int do_restart = 0; /* .restart has been called, restart asap */
113 int die_on_sighup = 0; /* die if bot receives SIGHUP */
114 int die_on_sigterm = 0; /* die if bot receives SIGTERM */
115 int resolve_timeout = 15; /* hostname/address lookup timeout */
116 time_t now; /* duh, now :) */
117
118 #ifdef DEBUG_CONTEXT
119 /* context storage for fatal crashes */
120 char cx_file[16][30];
121 char cx_note[16][256];
122 int cx_line[16];
123 int cx_ptr = 0;
124 #endif
125
126 void fatal(char *s, int recoverable)
127 {
128 int i;
129
130 putlog(LOG_MISC, "*", "* %s", s);
131 flushlogs();
132 for (i = 0; i < dcc_total; i++)
133 if (dcc[i].sock >= 0)
134 killsock(dcc[i].sock);
135 unlink(pid_file);
136 if (!recoverable)
137 exit(1);
138 }
139
140 int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
141 expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
142 expmem_modules(int), expmem_language(), expmem_tcldcc();
143
144 /* for mem.c : calculate memory we SHOULD be using */
145 int expected_memory()
146 {
147 int tot;
148
149 Context;
150 tot = expmem_chanprog() + expmem_users() + expmem_misc() +
151 expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
152 expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
153 return tot;
154 }
155
156 static void check_expired_dcc()
157 {
158 int i;
159
160 for (i = 0; i < dcc_total; i++)
161 if (dcc[i].type && dcc[i].type->timeout_val &&
162 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
163 if (dcc[i].type->timeout)
164 dcc[i].type->timeout(i);
165 else if (dcc[i].type->eof)
166 dcc[i].type->eof(i);
167 else
168 continue;
169 /* only timeout 1 socket per cycle, too risky for more */
170 return;
171 }
172 }
173
174 #ifdef DEBUG_CONTEXT
175 static int nested_debug = 0;
176
177 void write_debug()
178 {
179 int x;
180 char s[80];
181 int y;
182
183 if (nested_debug) {
184 /* yoicks, if we have this there's serious trouble */
185 /* all of these are pretty reliable, so we'll try these */
186 /* dont try and display context-notes in here, it's _not_ safe <cybah> */
187 x = creat("DEBUG.DEBUG", 0644);
188 setsock(x, SOCK_NONSOCK);
189 if (x >= 0) {
190 strcpy(s, ctime(&now));
191 dprintf(-x, "Debug (%s) written %s", ver, s);
192 dprintf(-x, "Please report problem to eggheads@eggheads.org");
193 dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
194 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
195 dprintf(-x, "Context: ");
196 cx_ptr = cx_ptr & 15;
197 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
198 dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
199 dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
200 killsock(x);
201 close(x);
202 }
203 exit(1); /* dont even try & tell people about, that may
204 * have caused the fault last time */
205 } else
206 nested_debug = 1;
207 putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
208 cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
209 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
210 putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
211 x = creat("DEBUG", 0644);
212 setsock(x, SOCK_NONSOCK);
213 if (x < 0) {
214 putlog(LOG_MISC, "*", "* Failed to write DEBUG");
215 } else {
216 strcpy(s, ctime(&now));
217 dprintf(-x, "Debug (%s) written %s", ver, s);
218 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
219 #ifdef STATIC
220 dprintf(-x, "STATICALLY LINKED\n");
221 #endif
222 strcpy(s, "info library");
223 if (interp && (Tcl_Eval(interp, s) == TCL_OK))
224 dprintf(-x, "Using tcl library: %s (header version %s)\n",
225 interp->result, TCL_VERSION);
226 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
227 dprintf(-x, "Link flags : %s\n", LDFLAGS);
228 dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
229 dprintf(-x, "Context: ");
230 cx_ptr = cx_ptr & 15;
231 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
232 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
233 (cx_note[y][0]) ? cx_note[y] : "");
234 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
235 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
236 tell_dcc(-x);
237 dprintf(-x, "\n");
238 debug_mem_to_dcc(-x);
239 killsock(x);
240 close(x);
241 putlog(LOG_MISC, "*", "* Wrote DEBUG");
242 }
243 }
244 #endif
245
246 static void got_bus(int z)
247 {
248 #ifdef DEBUG_CONTEXT
249 write_debug();
250 #endif
251 fatal("BUS ERROR -- CRASHING!", 1);
252 #ifdef SA_RESETHAND
253 kill(getpid(), SIGBUS);
254 #else
255 exit(1);
256 #endif
257 }
258
259 static void got_segv(int z)
260 {
261 #ifdef DEBUG_CONTEXT
262 write_debug();
263 #endif
264 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
265 #ifdef SA_RESETHAND
266 kill(getpid(), SIGSEGV);
267 #else
268 exit(1);
269 #endif
270 }
271
272 static void got_fpe(int z)
273 {
274 #ifdef DEBUG_CONTEXT
275 write_debug();
276 #endif
277 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
278 }
279
280 static void got_term(int z)
281 {
282 write_userfile(-1);
283 check_tcl_event("sigterm");
284 if (die_on_sigterm) {
285 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
286 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
287 } else {
288 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
289 }
290 }
291
292 static void got_quit(int z)
293 {
294 check_tcl_event("sigquit");
295 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
296 return;
297 }
298
299 static void got_hup(int z)
300 {
301 write_userfile(-1);
302 check_tcl_event("sighup");
303 if (die_on_sighup) {
304 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
305 } else
306 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
307 do_restart = -2;
308 return;
309 }
310
311 static void got_alarm(int z)
312 {
313 /* a call to resolver (gethostbyname, etc) timed out */
314 longjmp(alarmret, 1);
315 /* STUPID STUPID STUPID */
316 /* return; */
317 }
318
319 /* got ILL signal -- log context and continue */
320 static void got_ill(int z)
321 {
322 check_tcl_event("sigill");
323 #ifdef DEBUG_CONTEXT
324 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
325 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
326 #endif
327 }
328
329 #ifdef DEBUG_CONTEXT
330 /* Context */
331 void eggContext(char *file, int line, char *module)
332 {
333 char x[100];
334
335 if (!module)
336 sprintf(x, "%s", file);
337 else
338 sprintf(x, "%s:%s", module, file);
339 x[30] = 0;
340 cx_ptr = ((cx_ptr + 1) & 15);
341 strcpy(cx_file[cx_ptr], x);
342 cx_line[cx_ptr] = line;
343 cx_note[cx_ptr][0] = 0;
344 }
345
346 /* ContextNote */
347 void eggContextNote(char *file, int line, char *module, char *note)
348 {
349 char x[100];
350
351 if (!module)
352 sprintf(x, "%s", file);
353 else
354 sprintf(x, "%s:%s", module, file);
355 x[30] = 0;
356 cx_ptr = ((cx_ptr + 1) & 15);
357 strcpy(cx_file[cx_ptr], x);
358 cx_line[cx_ptr] = line;
359 strncpy(cx_note[cx_ptr], note, 255);
360 cx_note[cx_ptr][255] = 0;
361 }
362 #endif
363
364 #ifdef DEBUG_ASSERT
365 /* Assert */
366 void eggAssert(char *file, int line, char *module, int expr)
367 {
368 if (!(expr)) {
369 #ifdef DEBUG_CONTEXT
370 write_debug();
371 #endif
372 if (!module) {
373 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
374 } else {
375 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
376 }
377 fatal("ASSERT FAILED -- CRASHING!", 1);
378 }
379 }
380 #endif
381
382 static void do_arg(char *s)
383 {
384 int i;
385
386 if (s[0] == '-')
387 for (i = 1; i < strlen(s); i++) {
388 if (s[i] == 'n')
389 backgrd = 0;
390 if (s[i] == 'c') {
391 con_chan = 1;
392 term_z = 0;
393 }
394 if (s[i] == 't') {
395 con_chan = 0;
396 term_z = 1;
397 }
398 if (s[i] == 'm')
399 make_userfile = 1;
400 if (s[i] == 'v') {
401 char x[256], *z = x;
402
403 strcpy(x, egg_version);
404 newsplit(&z);
405 newsplit(&z);
406 printf("%s\n", version);
407 if (z[0])
408 printf(" (patches: %s)\n", z);
409 exit(0);
410 }
411 if (s[i] == 'h') {
412 printf("\n%s\n\n", version);
413 printf(EGG_USAGE);
414 printf("\n");
415 exit(0);
416 }
417 } else
418 strcpy(configfile, s);
419 }
420
421 void backup_userfile()
422 {
423 char s[150];
424
425 putlog(LOG_MISC, "*", USERF_BACKUP);
426 strcpy(s, userfile);
427 strcat(s, "~bak");
428 copyfile(userfile, s);
429 }
430
431 /* timer info: */
432 static int lastmin = 99;
433 static time_t then;
434 static struct tm nowtm;
435
436 /* rally BB, this is not QUITE as bad as it seems <G> */
437 /* ONCE A SECOND */
438 static void core_secondly()
439 {
440 static int cnt = 0;
441 int miltime;
442
443 do_check_timers(&utimer); /* secondly timers */
444 cnt++;
445 if (cnt >= 10) { /* every 10 seconds */
446 cnt = 0;
447 check_expired_dcc();
448 if (con_chan && !backgrd) {
449 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
450 tell_verbose_status(DP_STDOUT);
451 do_module_report(DP_STDOUT, 0, "server");
452 do_module_report(DP_STDOUT, 0, "channels");
453 tell_mem_status_dcc(DP_STDOUT);
454 }
455 }
456 Context;
457 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
458 if (nowtm.tm_min != lastmin) {
459 int i = 0;
460
461 /* once a minute */
462 lastmin = (lastmin + 1) % 60;
463 call_hook(HOOK_MINUTELY);
464 check_expired_ignores();
465 autolink_cycle(NULL); /* attempt autolinks */
466 /* in case for some reason more than 1 min has passed: */
467 while (nowtm.tm_min != lastmin) {
468 /* timer drift, dammit */
469 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
470 Context;
471 i++;
472 lastmin = (lastmin + 1) % 60;
473 call_hook(HOOK_MINUTELY);
474 }
475 if (i > 1)
476 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
477 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
478 Context;
479 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
480 call_hook(HOOK_5MINUTELY);
481 check_botnet_pings();
482 Context;
483 if (quick_logs == 0) {
484 flushlogs();
485 check_logsize();
486 }
487 if (miltime == 0) { /* at midnight */
488 char s[128];
489 int j;
490
491 s[my_strcpy(s, ctime(&now)) - 1] = 0;
492 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
493 backup_userfile();
494 for (j = 0; j < max_logs; j++) {
495 if (logs[j].filename != NULL && logs[j].f != NULL) {
496 fclose(logs[j].f);
497 logs[j].f = NULL;
498 }
499 }
500 }
501 }
502 Context;
503 if (nowtm.tm_min == notify_users_at)
504 call_hook(HOOK_HOURLY);
505 Context; /* these no longer need checking since they are
506 * all check vs minutely settings and we only
507 * get this far on the minute */
508 if (miltime == switch_logfiles_at) {
509 call_hook(HOOK_DAILY);
510 if (!keep_all_logs) {
511 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
512 for (i = 0; i < max_logs; i++)
513 if (logs[i].filename) {
514 char s[1024];
515
516 if (logs[i].f) {
517 fclose(logs[i].f);
518 logs[i].f = NULL;
519 }
520 simple_sprintf(s, "%s.yesterday", logs[i].filename);
521 unlink(s);
522 movefile(logs[i].filename, s);
523 }
524 }
525 }
526 }
527 }
528
529 static void core_minutely()
530 {
531 Context;
532 check_tcl_time(&nowtm);
533 do_check_timers(&timer);
534 Context;
535 if (quick_logs != 0) {
536 flushlogs();
537 check_logsize();
538 }
539 }
540
541 static void core_hourly()
542 {
543 Context;
544 write_userfile(-1);
545 }
546
547 static void event_rehash()
548 {
549 Context;
550 check_tcl_event("rehash");
551 }
552
553 static void event_prerehash()
554 {
555 Context;
556 check_tcl_event("prerehash");
557 }
558
559 static void event_save()
560 {
561 Context;
562 check_tcl_event("save");
563 }
564
565 static void event_logfile()
566 {
567 Context;
568 check_tcl_event("logfile");
569 }
570
571 void kill_tcl();
572 extern module_entry *module_list;
573 void restart_chons();
574
575 #ifdef STATIC
576 void check_static(char *, char *(*)());
577
578 #include "mod/static.h"
579 #endif
580 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
581 init_net(), init_modules(), init_tcl(int, char **),
582 init_language(int);
583
584 void patch(char *str)
585 {
586 char *p = strchr(egg_version, '+');
587
588 if (!p)
589 p = &egg_version[strlen(egg_version)];
590 sprintf(p, "+%s", str);
591 egg_numver++;
592 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
593 }
594
595 int main(int argc, char **argv)
596 {
597 int xx, i;
598 char buf[520], s[520];
599 FILE *f;
600 struct sigaction sv;
601 struct chanset_t *chan;
602
603 #ifdef DEBUG_MEM
604 {
605 #include <sys/resource.h>
606 struct rlimit cdlim;
607 cdlim.rlim_cur = RLIM_INFINITY;
608 cdlim.rlim_max = RLIM_INFINITY;
609 setrlimit(RLIMIT_CORE, &cdlim);
610 }
611 #endif
612
613 /* initialise context list */
614 for (i = 0; i < 16; i++) {
615 Context;
616 }
617 #include "patch.h"
618 /* version info! */
619 sprintf(ver, "eggdrop v%s", egg_version);
620 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999, 2000 Eggheads",
621 egg_version);
622 /* now add on the patchlevel (for Tcl) */
623 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
624 strcat(egg_version, egg_xtra);
625 Context;
626 #ifdef STOP_UAC
627 {
628 int nvpair[2];
629
630 nvpair[0] = SSIN_UACPROC;
631 nvpair[1] = UAC_NOPRINT;
632 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
633 }
634 #endif
635 /* set up error traps: */
636 sv.sa_handler = got_bus;
637 sigemptyset(&sv.sa_mask);
638 #ifdef SA_RESETHAND
639 sv.sa_flags = SA_RESETHAND;
640 #else
641 sv.sa_flags = 0;
642 #endif
643 sigaction(SIGBUS, &sv, NULL);
644 sv.sa_handler = got_segv;
645 sigaction(SIGSEGV, &sv, NULL);
646 #ifdef SA_RESETHAND
647 sv.sa_flags = 0;
648 #endif
649 sv.sa_handler = got_fpe;
650 sigaction(SIGFPE, &sv, NULL);
651 sv.sa_handler = got_term;
652 sigaction(SIGTERM, &sv, NULL);
653 sv.sa_handler = got_hup;
654 sigaction(SIGHUP, &sv, NULL);
655 sv.sa_handler = got_quit;
656 sigaction(SIGQUIT, &sv, NULL);
657 sv.sa_handler = SIG_IGN;
658 sigaction(SIGPIPE, &sv, NULL);
659 sv.sa_handler = got_ill;
660 sigaction(SIGILL, &sv, NULL);
661 sv.sa_handler = got_alarm;
662 sigaction(SIGALRM, &sv, NULL);
663 /* initialize variables and stuff */
664 now = time(NULL);
665 chanset = NULL;
666 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
667 lastmin = nowtm.tm_min;
668 srandom(now);
669 init_mem();
670 init_language(1);
671 if (argc > 1)
672 for (i = 1; i < argc; i++)
673 do_arg(argv[i]);
674 printf("\n%s\n", version);
675 init_dcc_max();
676 init_userent();
677 init_misc();
678 init_bots();
679 init_net();
680 init_modules();
681 init_tcl(argc, argv);
682 init_language(0);
683 #ifdef STATIC
684 link_statics();
685 #endif
686 Context;
687 strcpy(s, ctime(&now));
688 s[strlen(s) - 1] = 0;
689 strcpy(&s[11], &s[20]);
690 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
691 chanprog();
692 Context;
693 if (encrypt_pass == 0) {
694 printf(MOD_NOCRYPT);
695 exit(1);
696 }
697 i = 0;
698 for (chan = chanset; chan; chan = chan->next)
699 i++;
700 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
701 botnetnick, i, count_users(userlist));
702 cache_miss = 0;
703 cache_hit = 0;
704 Context;
705 sprintf(pid_file, "pid.%s", botnetnick);
706 Context;
707 /* check for pre-existing eggdrop! */
708 f = fopen(pid_file, "r");
709 if (f != NULL) {
710 fgets(s, 10, f);
711 xx = atoi(s);
712 kill(xx, SIGCHLD); /* meaningless kill to determine if pid
713 * is used */
714 if (errno != ESRCH) {
715 printf(EGG_RUNNING1, origbotname);
716 printf(EGG_RUNNING2, pid_file);
717 exit(1);
718 }
719 }
720 Context;
721 #ifndef CYGWIN_HACKS
722 /* move into background? */
723 if (backgrd) {
724 xx = fork();
725 if (xx == -1)
726 fatal("CANNOT FORK PROCESS.", 0);
727 if (xx != 0) {
728 FILE *fp;
729
730 /* need to attempt to write pid now, not later */
731 unlink(pid_file);
732 fp = fopen(pid_file, "w");
733 if (fp != NULL) {
734 fprintf(fp, "%u\n", xx);
735 if (fflush(fp)) {
736 /* kill bot incase a botchk is run from crond */
737 printf(EGG_NOWRITE, pid_file);
738 printf(" Try freeing some disk space\n");
739 fclose(fp);
740 unlink(pid_file);
741 exit(1);
742 }
743 fclose(fp);
744 } else
745 printf(EGG_NOWRITE, pid_file);
746 printf("Launched into the background (pid: %d)\n\n", xx);
747 #if HAVE_SETPGID
748 setpgid(xx, xx);
749 #endif
750 exit(0);
751 }
752 }
753 #endif
754 use_stderr = 0; /* stop writing to stderr now */
755 xx = getpid();
756 if ((xx != 0) && (!backgrd)) {
757 FILE *fp;
758
759 /* write pid to file */
760 unlink(pid_file);
761 fp = fopen(pid_file, "w");
762 if (fp != NULL) {
763 fprintf(fp, "%u\n", xx);
764 if (fflush(fp)) {
765 /* let the bot live since this doesn't appear to be a botchk */
766 printf(EGG_NOWRITE, pid_file);
767 fclose(fp);
768 unlink(pid_file);
769 }
770 fclose(fp);
771 } else
772 printf(EGG_NOWRITE, pid_file);
773 }
774 if (backgrd) {
775 /* ok, try to disassociate from controlling terminal */
776 /* (finger cross) */
777 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
778 setpgid(0, 0);
779 #endif
780 /* close out stdin/out/err */
781 freopen("/dev/null", "r", stdin);
782 freopen("/dev/null", "w", stdout);
783 freopen("/dev/null", "w", stderr);
784 #ifdef CYGWIN_HACKS
785 FreeConsole();
786 #endif
787 /* tcl wants those file handles kept open */
788 /* close(0); close(1); close(2); */
789 }
790 /* terminal emulating dcc chat */
791 if ((!backgrd) && (term_z)) {
792 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
793
794 dcc[n].addr = iptolong(getmyip());
795 dcc[n].sock = STDOUT;
796 dcc[n].timeval = now;
797 dcc[n].u.chat->con_flags = conmask;
798 dcc[n].u.chat->strip_flags = STRIP_ALL;
799 dcc[n].status = STAT_ECHO;
800 strcpy(dcc[n].nick, "HQ");
801 strcpy(dcc[n].host, "llama@console");
802 dcc[n].user = get_user_by_handle(userlist, "HQ");
803 /* make sure there's an innocuous HQ user if needed */
804 if (!dcc[n].user) {
805 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
806 dcc[n].user = get_user_by_handle(userlist, "HQ");
807 }
808 setsock(STDOUT, 0); /* entry in net table */
809 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
810 dcc_chatter(n);
811 }
812 then = now;
813 online_since = now;
814 autolink_cycle(NULL); /* hurry and connect to tandem bots */
815 add_help_reference("cmds1.help");
816 add_help_reference("cmds2.help");
817 add_help_reference("core.help");
818 add_hook(HOOK_SECONDLY, (Function) core_secondly);
819 add_hook(HOOK_MINUTELY, (Function) core_minutely);
820 add_hook(HOOK_HOURLY, (Function) core_hourly);
821 add_hook(HOOK_REHASH, (Function) event_rehash);
822 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
823 add_hook(HOOK_USERFILE, (Function) event_save);
824 add_hook(HOOK_DAILY, (Function) event_logfile);
825
826 debug0("main: entering loop");
827 while (1) {
828 int socket_cleanup = 0;
829
830 Context;
831 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
832 /* process a single tcl event */
833 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
834 #endif
835 /* lets move some of this here, reducing the numer of actual
836 * calls to periodic_timers */
837 now = time(NULL);
838 random(); /* woop, lets really jumble things */
839 if (now != then) { /* once a second */
840 call_hook(HOOK_SECONDLY);
841 then = now;
842 }
843 Context;
844 /* only do this every so often */
845 if (!socket_cleanup) {
846 dcc_remove_lost();
847 /* check for server or dcc activity */
848 dequeue_sockets();
849 socket_cleanup = 5;
850 } else
851 socket_cleanup--;
852 xx = sockgets(buf, &i);
853 if (xx >= 0) { /* non-error */
854 int idx;
855
856 Context;
857 for (idx = 0; idx < dcc_total; idx++)
858 if (dcc[idx].sock == xx) {
859 if (dcc[idx].type && dcc[idx].type->activity)
860 dcc[idx].type->activity(idx, buf, i);
861 else
862 putlog(LOG_MISC, "*",
863 "!!! untrapped dcc activity: type %s, sock %d",
864 dcc[idx].type->name, dcc[idx].sock);
865 break;
866 }
867 } else if (xx == -1) { /* EOF from someone */
868 int idx;
869
870 if ((i == STDOUT) && !backgrd)
871 fatal("END OF FILE ON TERMINAL", 0);
872 Context;
873 for (idx = 0; idx < dcc_total; idx++)
874 if (dcc[idx].sock == i) {
875 if (dcc[idx].type && dcc[idx].type->eof)
876 dcc[idx].type->eof(idx);
877 else {
878 putlog(LOG_MISC, "*",
879 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
880 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
881 killsock(i);
882 lostdcc(idx);
883 }
884 idx = dcc_total + 1;
885 }
886 if (idx == dcc_total) {
887 putlog(LOG_MISC, "*",
888 "(@) EOF socket %d, not a dcc socket, not anything.", i);
889 close(i);
890 killsock(i);
891 }
892 } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
893 Context;
894 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
895 for (i = 0; i < dcc_total; i++) {
896 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
897 putlog(LOG_MISC, "*",
898 "DCC socket %d (type %d, name '%s') expired -- pfft",
899 dcc[i].sock, dcc[i].type, dcc[i].nick);
900 killsock(dcc[i].sock);
901 lostdcc(i);
902 i--;
903 }
904 }
905 } else if (xx == (-3)) {
906 call_hook(HOOK_IDLE);
907 socket_cleanup = 0; /* if we've been idle, cleanup & flush */
908 }
909 if (do_restart) {
910 if (do_restart == -2)
911 rehash();
912 else {
913 /* unload as many modules as possible */
914 int f = 1;
915 module_entry *p;
916 Function x;
917 char xx[256];
918
919 while (f) {
920 f = 0;
921 for (p = module_list; p != NULL; p = p->next) {
922 dependancy *d = dependancy_list;
923 int ok = 1;
924
925 while (ok && d) {
926 if (d->needed == p)
927 ok = 0;
928 d = d->next;
929 }
930 if (ok) {
931 strcpy(xx, p->name);
932 if (module_unload(xx, origbotname) == NULL) {
933 f = 1;
934 break;
935 }
936 }
937 }
938 }
939 p = module_list;
940 if (p && p->next && p->next->next)
941 /* should be only 2 modules now -
942 * blowfish & eggdrop */
943 putlog(LOG_MISC, "*", MOD_STAGNANT);
944 Context;
945 flushlogs();
946 Context;
947 kill_tcl();
948 init_tcl(argc, argv);
949 init_language(0);
950 x = p->funcs[MODCALL_START];
951 x(0);
952 rehash();
953 restart_chons();
954 }
955 do_restart = 0;
956 }
957 }
958 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23