/[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.41 - (show annotations) (download) (as text)
Thu Aug 24 21:03:49 2000 UTC (18 years, 10 months ago) by fabian
Branch: MAIN
Changes since 1.40: +3 -3 lines
File MIME type: text/x-chdr
fabian: changes to reflect release of 1.5.4

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23