/[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.45 - (show annotations) (download) (as text)
Thu Nov 16 03:07:50 2000 UTC (19 years ago) by guppy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.44: +3 -3 lines
File MIME type: text/x-chdr
guppy: 1.4.6 .. will we ever work on you? I hope not

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.44 2000/07/17 21:00:41 guppy 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.6";
80 int egg_numver = 1040600;
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 /* really BB, this is not QUITE as bad as it seems <G> */
437 /* ONCE A SECOND */
438
439 /* Try to not put any Context lines in here (guppy 21Mar2000) */
440 static void core_secondly()
441 {
442 static int cnt = 0;
443 int miltime;
444
445 do_check_timers(&utimer); /* secondly timers */
446 cnt++;
447 if (cnt >= 10) { /* every 10 seconds */
448 cnt = 0;
449 check_expired_dcc();
450 if (con_chan && !backgrd) {
451 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
452 tell_verbose_status(DP_STDOUT);
453 do_module_report(DP_STDOUT, 0, "server");
454 do_module_report(DP_STDOUT, 0, "channels");
455 tell_mem_status_dcc(DP_STDOUT);
456 }
457 }
458 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
459 if (nowtm.tm_min != lastmin) {
460 int i = 0;
461
462 /* once a minute */
463 lastmin = (lastmin + 1) % 60;
464 call_hook(HOOK_MINUTELY);
465 check_expired_ignores();
466 autolink_cycle(NULL); /* attempt autolinks */
467 /* in case for some reason more than 1 min has passed: */
468 while (nowtm.tm_min != lastmin) {
469 /* timer drift, dammit */
470 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
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 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
479 call_hook(HOOK_5MINUTELY);
480 check_botnet_pings();
481 if (quick_logs == 0) {
482 flushlogs();
483 check_logsize();
484 }
485 if (miltime == 0) { /* at midnight */
486 char s[128];
487 int j;
488
489 s[my_strcpy(s, ctime(&now)) - 1] = 0;
490 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
491 backup_userfile();
492 for (j = 0; j < max_logs; j++) {
493 if (logs[j].filename != NULL && logs[j].f != NULL) {
494 fclose(logs[j].f);
495 logs[j].f = NULL;
496 }
497 }
498 }
499 }
500 if (nowtm.tm_min == notify_users_at)
501 call_hook(HOOK_HOURLY);
502 /* these no longer need checking since they are
503 * all check vs minutely settings and we only
504 * get this far on the minute */
505 if (miltime == switch_logfiles_at) {
506 call_hook(HOOK_DAILY);
507 if (!keep_all_logs) {
508 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
509 for (i = 0; i < max_logs; i++)
510 if (logs[i].filename) {
511 char s[1024];
512
513 if (logs[i].f) {
514 fclose(logs[i].f);
515 logs[i].f = NULL;
516 }
517 simple_sprintf(s, "%s.yesterday", logs[i].filename);
518 unlink(s);
519 movefile(logs[i].filename, s);
520 }
521 }
522 }
523 }
524 }
525
526 static void core_minutely()
527 {
528 check_tcl_time(&nowtm);
529 do_check_timers(&timer);
530 if (quick_logs != 0) {
531 flushlogs();
532 check_logsize();
533 }
534 }
535
536 static void core_hourly()
537 {
538 Context;
539 write_userfile(-1);
540 }
541
542 static void event_rehash()
543 {
544 Context;
545 check_tcl_event("rehash");
546 }
547
548 static void event_prerehash()
549 {
550 Context;
551 check_tcl_event("prerehash");
552 }
553
554 static void event_save()
555 {
556 Context;
557 check_tcl_event("save");
558 }
559
560 static void event_logfile()
561 {
562 Context;
563 check_tcl_event("logfile");
564 }
565
566 void kill_tcl();
567 extern module_entry *module_list;
568 void restart_chons();
569
570 #ifdef STATIC
571 void check_static(char *, char *(*)());
572
573 #include "mod/static.h"
574 #endif
575 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
576 init_net(), init_modules(), init_tcl(int, char **),
577 init_language(int);
578
579 void patch(char *str)
580 {
581 char *p = strchr(egg_version, '+');
582
583 if (!p)
584 p = &egg_version[strlen(egg_version)];
585 sprintf(p, "+%s", str);
586 egg_numver++;
587 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
588 }
589
590 int main(int argc, char **argv)
591 {
592 int xx, i;
593 char buf[520], s[520];
594 FILE *f;
595 struct sigaction sv;
596 struct chanset_t *chan;
597
598 #ifdef DEBUG_MEM
599 {
600 #include <sys/resource.h>
601 struct rlimit cdlim;
602 cdlim.rlim_cur = RLIM_INFINITY;
603 cdlim.rlim_max = RLIM_INFINITY;
604 setrlimit(RLIMIT_CORE, &cdlim);
605 }
606 #endif
607
608 /* initialise context list */
609 for (i = 0; i < 16; i++) {
610 Context;
611 }
612 #include "patch.h"
613 /* version info! */
614 sprintf(ver, "eggdrop v%s", egg_version);
615 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999, 2000 Eggheads",
616 egg_version);
617 /* now add on the patchlevel (for Tcl) */
618 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
619 strcat(egg_version, egg_xtra);
620 Context;
621 #ifdef STOP_UAC
622 {
623 int nvpair[2];
624
625 nvpair[0] = SSIN_UACPROC;
626 nvpair[1] = UAC_NOPRINT;
627 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
628 }
629 #endif
630 /* set up error traps: */
631 sv.sa_handler = got_bus;
632 sigemptyset(&sv.sa_mask);
633 #ifdef SA_RESETHAND
634 sv.sa_flags = SA_RESETHAND;
635 #else
636 sv.sa_flags = 0;
637 #endif
638 sigaction(SIGBUS, &sv, NULL);
639 sv.sa_handler = got_segv;
640 sigaction(SIGSEGV, &sv, NULL);
641 #ifdef SA_RESETHAND
642 sv.sa_flags = 0;
643 #endif
644 sv.sa_handler = got_fpe;
645 sigaction(SIGFPE, &sv, NULL);
646 sv.sa_handler = got_term;
647 sigaction(SIGTERM, &sv, NULL);
648 sv.sa_handler = got_hup;
649 sigaction(SIGHUP, &sv, NULL);
650 sv.sa_handler = got_quit;
651 sigaction(SIGQUIT, &sv, NULL);
652 sv.sa_handler = SIG_IGN;
653 sigaction(SIGPIPE, &sv, NULL);
654 sv.sa_handler = got_ill;
655 sigaction(SIGILL, &sv, NULL);
656 sv.sa_handler = got_alarm;
657 sigaction(SIGALRM, &sv, NULL);
658 /* initialize variables and stuff */
659 now = time(NULL);
660 chanset = NULL;
661 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
662 lastmin = nowtm.tm_min;
663 srandom(now);
664 init_mem();
665 init_language(1);
666 if (argc > 1)
667 for (i = 1; i < argc; i++)
668 do_arg(argv[i]);
669 printf("\n%s\n", version);
670 init_dcc_max();
671 init_userent();
672 init_misc();
673 init_bots();
674 init_net();
675 init_modules();
676 init_tcl(argc, argv);
677 init_language(0);
678 #ifdef STATIC
679 link_statics();
680 #endif
681 Context;
682 strcpy(s, ctime(&now));
683 s[strlen(s) - 1] = 0;
684 strcpy(&s[11], &s[20]);
685 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
686 chanprog();
687 Context;
688 if (encrypt_pass == 0) {
689 printf(MOD_NOCRYPT);
690 exit(1);
691 }
692 i = 0;
693 for (chan = chanset; chan; chan = chan->next)
694 i++;
695 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
696 botnetnick, i, count_users(userlist));
697 cache_miss = 0;
698 cache_hit = 0;
699 Context;
700 sprintf(pid_file, "pid.%s", botnetnick);
701 Context;
702 /* check for pre-existing eggdrop! */
703 f = fopen(pid_file, "r");
704 if (f != NULL) {
705 fgets(s, 10, f);
706 xx = atoi(s);
707 kill(xx, SIGCHLD); /* meaningless kill to determine if pid
708 * is used */
709 if (errno != ESRCH) {
710 printf(EGG_RUNNING1, origbotname);
711 printf(EGG_RUNNING2, pid_file);
712 exit(1);
713 }
714 }
715 Context;
716 #ifndef CYGWIN_HACKS
717 /* move into background? */
718 if (backgrd) {
719 xx = fork();
720 if (xx == -1)
721 fatal("CANNOT FORK PROCESS.", 0);
722 if (xx != 0) {
723 FILE *fp;
724
725 /* need to attempt to write pid now, not later */
726 unlink(pid_file);
727 fp = fopen(pid_file, "w");
728 if (fp != NULL) {
729 fprintf(fp, "%u\n", xx);
730 if (fflush(fp)) {
731 /* kill bot incase a botchk is run from crond */
732 printf(EGG_NOWRITE, pid_file);
733 printf(" Try freeing some disk space\n");
734 fclose(fp);
735 unlink(pid_file);
736 exit(1);
737 }
738 fclose(fp);
739 } else
740 printf(EGG_NOWRITE, pid_file);
741 printf("Launched into the background (pid: %d)\n\n", xx);
742 #if HAVE_SETPGID
743 setpgid(xx, xx);
744 #endif
745 exit(0);
746 }
747 }
748 #endif
749 use_stderr = 0; /* stop writing to stderr now */
750 xx = getpid();
751 if ((xx != 0) && (!backgrd)) {
752 FILE *fp;
753
754 /* write pid to file */
755 unlink(pid_file);
756 fp = fopen(pid_file, "w");
757 if (fp != NULL) {
758 fprintf(fp, "%u\n", xx);
759 if (fflush(fp)) {
760 /* let the bot live since this doesn't appear to be a botchk */
761 printf(EGG_NOWRITE, pid_file);
762 fclose(fp);
763 unlink(pid_file);
764 }
765 fclose(fp);
766 } else
767 printf(EGG_NOWRITE, pid_file);
768 }
769 if (backgrd) {
770 /* ok, try to disassociate from controlling terminal */
771 /* (finger cross) */
772 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
773 setpgid(0, 0);
774 #endif
775 /* close out stdin/out/err */
776 freopen("/dev/null", "r", stdin);
777 freopen("/dev/null", "w", stdout);
778 freopen("/dev/null", "w", stderr);
779 #ifdef CYGWIN_HACKS
780 FreeConsole();
781 #endif
782 /* tcl wants those file handles kept open */
783 /* close(0); close(1); close(2); */
784 }
785 /* terminal emulating dcc chat */
786 if ((!backgrd) && (term_z)) {
787 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
788
789 dcc[n].addr = iptolong(getmyip());
790 dcc[n].sock = STDOUT;
791 dcc[n].timeval = now;
792 dcc[n].u.chat->con_flags = conmask;
793 dcc[n].u.chat->strip_flags = STRIP_ALL;
794 dcc[n].status = STAT_ECHO;
795 strcpy(dcc[n].nick, "HQ");
796 strcpy(dcc[n].host, "llama@console");
797 dcc[n].user = get_user_by_handle(userlist, "HQ");
798 /* make sure there's an innocuous HQ user if needed */
799 if (!dcc[n].user) {
800 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
801 dcc[n].user = get_user_by_handle(userlist, "HQ");
802 }
803 setsock(STDOUT, 0); /* entry in net table */
804 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
805 dcc_chatter(n);
806 }
807 then = now;
808 online_since = now;
809 autolink_cycle(NULL); /* hurry and connect to tandem bots */
810 add_help_reference("cmds1.help");
811 add_help_reference("cmds2.help");
812 add_help_reference("core.help");
813 add_hook(HOOK_SECONDLY, (Function) core_secondly);
814 add_hook(HOOK_MINUTELY, (Function) core_minutely);
815 add_hook(HOOK_HOURLY, (Function) core_hourly);
816 add_hook(HOOK_REHASH, (Function) event_rehash);
817 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
818 add_hook(HOOK_USERFILE, (Function) event_save);
819 add_hook(HOOK_DAILY, (Function) event_logfile);
820
821 debug0("main: entering loop");
822 while (1) {
823 int socket_cleanup = 0;
824
825 Context;
826 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
827 /* process a single tcl event */
828 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
829 #endif
830 /* lets move some of this here, reducing the numer of actual
831 * calls to periodic_timers */
832 now = time(NULL);
833 random(); /* woop, lets really jumble things */
834 if (now != then) { /* once a second */
835 call_hook(HOOK_SECONDLY);
836 then = now;
837 }
838 Context;
839 /* only do this every so often */
840 if (!socket_cleanup) {
841 dcc_remove_lost();
842 /* check for server or dcc activity */
843 dequeue_sockets();
844 socket_cleanup = 5;
845 } else
846 socket_cleanup--;
847 xx = sockgets(buf, &i);
848 if (xx >= 0) { /* non-error */
849 int idx;
850
851 Context;
852 for (idx = 0; idx < dcc_total; idx++)
853 if (dcc[idx].sock == xx) {
854 if (dcc[idx].type && dcc[idx].type->activity)
855 dcc[idx].type->activity(idx, buf, i);
856 else
857 putlog(LOG_MISC, "*",
858 "!!! untrapped dcc activity: type %s, sock %d",
859 dcc[idx].type->name, dcc[idx].sock);
860 break;
861 }
862 } else if (xx == -1) { /* EOF from someone */
863 int idx;
864
865 if ((i == STDOUT) && !backgrd)
866 fatal("END OF FILE ON TERMINAL", 0);
867 Context;
868 for (idx = 0; idx < dcc_total; idx++)
869 if (dcc[idx].sock == i) {
870 if (dcc[idx].type && dcc[idx].type->eof)
871 dcc[idx].type->eof(idx);
872 else {
873 putlog(LOG_MISC, "*",
874 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
875 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
876 killsock(i);
877 lostdcc(idx);
878 }
879 idx = dcc_total + 1;
880 }
881 if (idx == dcc_total) {
882 putlog(LOG_MISC, "*",
883 "(@) EOF socket %d, not a dcc socket, not anything.", i);
884 close(i);
885 killsock(i);
886 }
887 } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
888 Context;
889 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
890 for (i = 0; i < dcc_total; i++) {
891 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
892 putlog(LOG_MISC, "*",
893 "DCC socket %d (type %d, name '%s') expired -- pfft",
894 dcc[i].sock, dcc[i].type, dcc[i].nick);
895 killsock(dcc[i].sock);
896 lostdcc(i);
897 i--;
898 }
899 }
900 } else if (xx == (-3)) {
901 call_hook(HOOK_IDLE);
902 socket_cleanup = 0; /* if we've been idle, cleanup & flush */
903 }
904 if (do_restart) {
905 if (do_restart == -2)
906 rehash();
907 else {
908 /* unload as many modules as possible */
909 int f = 1;
910 module_entry *p;
911 Function x;
912 char xx[256];
913
914 while (f) {
915 f = 0;
916 for (p = module_list; p != NULL; p = p->next) {
917 dependancy *d = dependancy_list;
918 int ok = 1;
919
920 while (ok && d) {
921 if (d->needed == p)
922 ok = 0;
923 d = d->next;
924 }
925 if (ok) {
926 strcpy(xx, p->name);
927 if (module_unload(xx, origbotname) == NULL) {
928 f = 1;
929 break;
930 }
931 }
932 }
933 }
934 p = module_list;
935 if (p && p->next && p->next->next)
936 /* should be only 2 modules now -
937 * blowfish & eggdrop */
938 putlog(LOG_MISC, "*", MOD_STAGNANT);
939 Context;
940 flushlogs();
941 Context;
942 kill_tcl();
943 init_tcl(argc, argv);
944 init_language(0);
945 x = p->funcs[MODCALL_START];
946 x(0);
947 rehash();
948 restart_chons();
949 }
950 do_restart = 0;
951 }
952 }
953 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23