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

Annotation of /eggdrop1.9/src/main.c

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


Revision 1.10 - (hide annotations) (download) (as text)
Sat Dec 11 18:58:03 1999 UTC (19 years, 8 months ago) by fabian
Branch: MAIN
Changes since 1.9: +89 -46 lines
File MIME type: text/x-chdr
tothwolf's remove-context-bloat patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23