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

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

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


Revision 1.3 - (hide annotations) (download) (as text)
Sun Oct 10 21:24:43 2010 UTC (8 years, 8 months ago) by pseudo
Branch: MAIN
Changes since 1.2: +13 -1 lines
File MIME type: text/x-chdr
Removed the unrecognized options warning when configuring modules.
Modified -v output and .status display configure options.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23