/[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.2 - (hide annotations) (download) (as text)
Thu Aug 5 18:12:05 2010 UTC (8 years, 10 months ago) by pseudo
Branch: MAIN
Changes since 1.1: +2 -2 lines
File MIME type: text/x-chdr
Added new, full IPv6 support to eggdrop.

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.2 * $Id: main.c,v 1.1.1.1 2010/07/26 21:11:06 simple 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    
298     #ifdef CCFLAGS
299     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
300     #endif
301    
302     #ifdef LDFLAGS
303     dprintf(-x, "Link flags: %s\n", LDFLAGS);
304     #endif
305    
306     #ifdef STRIPFLAGS
307     dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
308     #endif
309    
310     dprintf(-x, "Context: ");
311     cx_ptr = cx_ptr & 15;
312     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
313     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
314     (cx_note[y][0]) ? cx_note[y] : "");
315     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
316     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
317     tell_dcc(-x);
318     dprintf(-x, "\n");
319     debug_mem_to_dcc(-x);
320     killsock(x);
321     close(x);
322     putlog(LOG_MISC, "*", "* Wrote DEBUG");
323     }
324     }
325     #endif /* DEBUG_CONTEXT */
326    
327     static void got_bus(int z)
328     {
329     write_debug();
330     fatal("BUS ERROR -- CRASHING!", 1);
331     #ifdef SA_RESETHAND
332     kill(getpid(), SIGBUS);
333     #else
334     bg_send_quit(BG_ABORT);
335     exit(1);
336     #endif
337     }
338    
339     static void got_segv(int z)
340     {
341     write_debug();
342     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
343     #ifdef SA_RESETHAND
344     kill(getpid(), SIGSEGV);
345     #else
346     bg_send_quit(BG_ABORT);
347     exit(1);
348     #endif
349     }
350    
351     static void got_fpe(int z)
352     {
353     write_debug();
354     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
355     }
356    
357     static void got_term(int z)
358     {
359     write_userfile(-1);
360     check_tcl_event("sigterm");
361     if (die_on_sigterm) {
362     botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
363     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
364     } else
365     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
366     }
367    
368     static void got_quit(int z)
369     {
370     check_tcl_event("sigquit");
371     putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
372     return;
373     }
374    
375     static void got_hup(int z)
376     {
377     write_userfile(-1);
378     check_tcl_event("sighup");
379     if (die_on_sighup) {
380     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
381     } else
382     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
383     do_restart = -2;
384     return;
385     }
386    
387     /* A call to resolver (gethostbyname, etc) timed out
388     */
389     static void got_alarm(int z)
390     {
391     siglongjmp(alarmret, 1);
392    
393     /* -Never reached- */
394     }
395    
396     /* Got ILL signal -- log context and continue
397     */
398     static void got_ill(int z)
399     {
400     check_tcl_event("sigill");
401     #ifdef DEBUG_CONTEXT
402     putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
403     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
404     #endif
405     }
406    
407     #ifdef DEBUG_CONTEXT
408     /* Called from the Context macro.
409     */
410     void eggContext(const char *file, int line, const char *module)
411     {
412     char x[31], *p;
413    
414     p = strrchr(file, '/');
415     if (!module) {
416     strncpyz(x, p ? p + 1 : file, sizeof x);
417     } else
418     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
419     cx_ptr = ((cx_ptr + 1) & 15);
420     strcpy(cx_file[cx_ptr], x);
421     cx_line[cx_ptr] = line;
422     cx_note[cx_ptr][0] = 0;
423     }
424    
425     /* Called from the ContextNote macro.
426     */
427     void eggContextNote(const char *file, int line, const char *module,
428     const char *note)
429     {
430     char x[31], *p;
431    
432     p = strrchr(file, '/');
433     if (!module)
434     strncpyz(x, p ? p + 1 : file, sizeof x);
435     else
436     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
437     cx_ptr = ((cx_ptr + 1) & 15);
438     strcpy(cx_file[cx_ptr], x);
439     cx_line[cx_ptr] = line;
440     strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
441     }
442     #endif /* DEBUG_CONTEXT */
443    
444     #ifdef DEBUG_ASSERT
445     /* Called from the Assert macro.
446     */
447     void eggAssert(const char *file, int line, const char *module)
448     {
449     write_debug();
450     if (!module)
451     putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
452     else
453     putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
454     fatal("ASSERT FAILED -- CRASHING!", 1);
455     }
456     #endif
457    
458     static void do_arg(char *s)
459     {
460     char x[512], *z = x;
461     int i;
462    
463     if (s[0] == '-')
464     for (i = 1; i < strlen(s); i++) {
465     switch (s[i]) {
466     case 'n':
467     backgrd = 0;
468     break;
469     case 'c':
470     con_chan = 1;
471     term_z = 0;
472     break;
473     case 't':
474     con_chan = 0;
475     term_z = 1;
476     break;
477     case 'm':
478     make_userfile = 1;
479     break;
480     case 'v':
481     strncpyz(x, egg_version, sizeof x);
482     newsplit(&z);
483     newsplit(&z);
484     printf("%s\n", version);
485     if (z[0])
486     printf(" (patches: %s)\n", z);
487     bg_send_quit(BG_ABORT);
488     exit(0);
489     break; /* this should never be reached */
490     case 'h':
491     printf("\n%s\n\n", version);
492     printf(EGG_USAGE);
493     printf("\n");
494     bg_send_quit(BG_ABORT);
495     exit(0);
496     break; /* this should never be reached */
497     }
498     } else
499     strncpyz(configfile, s, sizeof configfile);
500     }
501    
502     void backup_userfile(void)
503     {
504     char s[125];
505    
506     if (quiet_save < 2)
507     putlog(LOG_MISC, "*", USERF_BACKUP);
508     egg_snprintf(s, sizeof s, "%s~bak", userfile);
509     copyfile(userfile, s);
510     }
511    
512     /* Timer info */
513     static int lastmin = 99;
514     static time_t then;
515     static struct tm nowtm;
516    
517     /* Called once a second.
518     *
519     * Note: Try to not put any Context lines in here (guppy 21Mar2000).
520     */
521     static void core_secondly()
522     {
523     static int cnt = 0;
524     int miltime;
525    
526     do_check_timers(&utimer); /* Secondly timers */
527     cnt++;
528     if (cnt >= 10) { /* Every 10 seconds */
529     cnt = 0;
530     check_expired_dcc();
531     if (con_chan && !backgrd) {
532     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
533     tell_verbose_status(DP_STDOUT);
534     do_module_report(DP_STDOUT, 0, "server");
535     do_module_report(DP_STDOUT, 0, "channels");
536     tell_mem_status_dcc(DP_STDOUT);
537     }
538     }
539     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
540     if (nowtm.tm_min != lastmin) {
541     int i = 0;
542    
543     /* Once a minute */
544     lastmin = (lastmin + 1) % 60;
545     call_hook(HOOK_MINUTELY);
546     check_expired_ignores();
547     autolink_cycle(NULL); /* Attempt autolinks */
548     /* In case for some reason more than 1 min has passed: */
549     while (nowtm.tm_min != lastmin) {
550     /* Timer drift, dammit */
551     debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
552     i++;
553     lastmin = (lastmin + 1) % 60;
554     call_hook(HOOK_MINUTELY);
555     }
556     if (i > 1)
557     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
558     miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
559     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
560     call_hook(HOOK_5MINUTELY);
561     check_botnet_pings();
562     if (!quick_logs) {
563     flushlogs();
564     check_logsize();
565     }
566     if (!miltime) { /* At midnight */
567     char s[25];
568     int j;
569    
570     strncpyz(s, ctime(&now), sizeof s);
571     if (quiet_save < 3)
572     putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
573     call_hook(HOOK_BACKUP);
574     for (j = 0; j < max_logs; j++) {
575     if (logs[j].filename != NULL && logs[j].f != NULL) {
576     fclose(logs[j].f);
577     logs[j].f = NULL;
578     }
579     }
580     }
581     }
582     if (nowtm.tm_min == notify_users_at)
583     call_hook(HOOK_HOURLY);
584     /* These no longer need checking since they are all check vs minutely
585     * settings and we only get this far on the minute.
586     */
587     if (miltime == switch_logfiles_at) {
588     call_hook(HOOK_DAILY);
589     if (!keep_all_logs) {
590     if (quiet_save < 3)
591     putlog(LOG_MISC, "*", MISC_LOGSWITCH);
592     for (i = 0; i < max_logs; i++)
593     if (logs[i].filename) {
594     char s[1024];
595    
596     if (logs[i].f) {
597     fclose(logs[i].f);
598     logs[i].f = NULL;
599     }
600     egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
601     unlink(s);
602     movefile(logs[i].filename, s);
603     }
604     }
605     }
606     }
607     }
608    
609     static void core_minutely()
610     {
611     check_tcl_time(&nowtm);
612     check_tcl_cron(&nowtm);
613     do_check_timers(&timer);
614     if (quick_logs != 0) {
615     flushlogs();
616     check_logsize();
617     }
618     }
619    
620     static void core_hourly()
621     {
622     write_userfile(-1);
623     }
624    
625     static void event_rehash()
626     {
627     check_tcl_event("rehash");
628     }
629    
630     static void event_prerehash()
631     {
632     check_tcl_event("prerehash");
633     }
634    
635     static void event_save()
636     {
637     check_tcl_event("save");
638     }
639    
640     static void event_logfile()
641     {
642     check_tcl_event("logfile");
643     }
644    
645     static void event_resettraffic()
646     {
647     otraffic_irc += otraffic_irc_today;
648     itraffic_irc += itraffic_irc_today;
649     otraffic_bn += otraffic_bn_today;
650     itraffic_bn += itraffic_bn_today;
651     otraffic_dcc += otraffic_dcc_today;
652     itraffic_dcc += itraffic_dcc_today;
653     otraffic_unknown += otraffic_unknown_today;
654     itraffic_unknown += itraffic_unknown_today;
655     otraffic_trans += otraffic_trans_today;
656     itraffic_trans += itraffic_trans_today;
657     otraffic_irc_today = otraffic_bn_today = 0;
658     otraffic_dcc_today = otraffic_unknown_today = 0;
659     itraffic_irc_today = itraffic_bn_today = 0;
660     itraffic_dcc_today = itraffic_unknown_today = 0;
661     itraffic_trans_today = otraffic_trans_today = 0;
662     }
663    
664     static void event_loaded()
665     {
666     check_tcl_event("loaded");
667     }
668    
669     void kill_tcl();
670     extern module_entry *module_list;
671     void restart_chons();
672    
673     #ifdef STATIC
674     void check_static(char *, char *(*)());
675    
676     #include "mod/static.h"
677     #endif
678     int init_threaddata(int);
679     int init_mem();
680     int init_userent();
681     int init_misc();
682     int init_bots();
683     int init_modules();
684     int init_tcl(int, char **);
685     int init_language(int);
686    
687     static void patch(const char *str)
688     {
689     char *p = strchr(egg_version, '+');
690    
691     if (!p)
692     p = &egg_version[strlen(egg_version)];
693     sprintf(p, "+%s", str);
694     egg_numver++;
695     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
696     }
697    
698     static inline void garbage_collect(void)
699     {
700     static u_8bit_t run_cnt = 0;
701    
702     if (run_cnt == 3)
703     garbage_collect_tclhash();
704     else
705     run_cnt++;
706     }
707    
708     int mainloop(int toplevel)
709     {
710     static int socket_cleanup = 0;
711     int xx, i, eggbusy = 1, tclbusy = 0;
712     char buf[520];
713    
714     /* Lets move some of this here, reducing the numer of actual
715     * calls to periodic_timers
716     */
717     now = time(NULL);
718     /*
719     * FIXME: Get rid of this, it's ugly and wastes lots of cpu.
720     *
721     * pre-1.3.0 Eggdrop had random() in the once a second block below.
722     *
723     * This attempts to keep random() more random by constantly
724     * calling random() and updating the state information.
725     */
726     random(); /* Woop, lets really jumble things */
727    
728     /* If we want to restart, we have to unwind to the toplevel.
729     * Tcl will Panic if we kill the interp with Tcl_Eval in progress.
730     * This is done by returning -1 in tickle_WaitForEvent.
731     */
732     if (do_restart && do_restart != -2 && !toplevel)
733     return -1;
734    
735     /* Once a second */
736     if (now != then) {
737     call_hook(HOOK_SECONDLY);
738     then = now;
739     }
740    
741     /* Only do this every so often. */
742     if (!socket_cleanup) {
743     socket_cleanup = 5;
744    
745     /* Remove dead dcc entries. */
746     dcc_remove_lost();
747    
748     /* Check for server or dcc activity. */
749     dequeue_sockets();
750     } else
751     socket_cleanup--;
752    
753     /* Free unused structures. */
754     garbage_collect();
755    
756     xx = sockgets(buf, &i);
757     if (xx >= 0) { /* Non-error */
758     int idx;
759    
760     for (idx = 0; idx < dcc_total; idx++)
761     if (dcc[idx].sock == xx) {
762     if (dcc[idx].type && dcc[idx].type->activity) {
763     /* Traffic stats */
764     if (dcc[idx].type->name) {
765     if (!strncmp(dcc[idx].type->name, "BOT", 3))
766     itraffic_bn_today += strlen(buf) + 1;
767     else if (!strcmp(dcc[idx].type->name, "SERVER"))
768     itraffic_irc_today += strlen(buf) + 1;
769     else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
770     itraffic_dcc_today += strlen(buf) + 1;
771     else if (!strncmp(dcc[idx].type->name, "FILES", 5))
772     itraffic_dcc_today += strlen(buf) + 1;
773     else if (!strcmp(dcc[idx].type->name, "SEND"))
774     itraffic_trans_today += strlen(buf) + 1;
775     else if (!strncmp(dcc[idx].type->name, "GET", 3))
776     itraffic_trans_today += strlen(buf) + 1;
777     else
778     itraffic_unknown_today += strlen(buf) + 1;
779     }
780     dcc[idx].type->activity(idx, buf, i);
781     } else
782     putlog(LOG_MISC, "*",
783     "!!! untrapped dcc activity: type %s, sock %d",
784     dcc[idx].type->name, dcc[idx].sock);
785     break;
786     }
787     } else if (xx == -1) { /* EOF from someone */
788     int idx;
789    
790     if (i == STDOUT && !backgrd)
791     fatal("END OF FILE ON TERMINAL", 0);
792     for (idx = 0; idx < dcc_total; idx++)
793     if (dcc[idx].sock == i) {
794     if (dcc[idx].type && dcc[idx].type->eof)
795     dcc[idx].type->eof(idx);
796     else {
797     putlog(LOG_MISC, "*",
798     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
799     i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
800     killsock(i);
801     lostdcc(idx);
802     }
803     idx = dcc_total + 1;
804     }
805     if (idx == dcc_total) {
806     putlog(LOG_MISC, "*",
807     "(@) EOF socket %d, not a dcc socket, not anything.", i);
808     close(i);
809     killsock(i);
810     }
811     } else if (xx == -2 && errno != EINTR) { /* select() error */
812     putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
813     for (i = 0; i < dcc_total; i++) {
814     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) {
815     putlog(LOG_MISC, "*",
816     "DCC socket %d (type %d, name '%s') expired -- pfft",
817     dcc[i].sock, dcc[i].type, dcc[i].nick);
818     killsock(dcc[i].sock);
819     lostdcc(i);
820     i--;
821     }
822     }
823     } else if (xx == -3) {
824     call_hook(HOOK_IDLE);
825     socket_cleanup = 0; /* If we've been idle, cleanup & flush */
826     eggbusy = 0;
827     } else if (xx == -5) {
828     eggbusy = 0;
829     tclbusy = 1;
830     }
831    
832     if (do_restart) {
833     if (do_restart == -2)
834     rehash();
835     else if (!toplevel)
836     return -1; /* Unwind to toplevel before restarting */
837     else {
838     /* Unload as many modules as possible */
839     int f = 1;
840     module_entry *p;
841     Function startfunc;
842     char name[256];
843    
844     /* oops, I guess we should call this event before tcl is restarted */
845     check_tcl_event("prerestart");
846    
847     while (f) {
848     f = 0;
849     for (p = module_list; p != NULL; p = p->next) {
850     dependancy *d = dependancy_list;
851     int ok = 1;
852    
853     while (ok && d) {
854     if (d->needed == p)
855     ok = 0;
856     d = d->next;
857     }
858     if (ok) {
859     strcpy(name, p->name);
860     if (module_unload(name, botnetnick) == NULL) {
861     f = 1;
862     break;
863     }
864     }
865     }
866     }
867    
868     /* Make sure we don't have any modules left hanging around other than
869     * "eggdrop" and the two that are supposed to be.
870     */
871     for (f = 0, p = module_list; p; p = p->next) {
872     if (strcmp(p->name, "eggdrop") && strcmp(p->name, "encryption") &&
873     strcmp(p->name, "uptime")) {
874     f++;
875     }
876     }
877     if (f != 0) {
878     putlog(LOG_MISC, "*", MOD_STAGNANT);
879     }
880    
881     flushlogs();
882     kill_tcl();
883     init_tcl(argc, argv);
884     init_language(0);
885    
886     /* this resets our modules which we didn't unload (encryption and uptime) */
887     for (p = module_list; p; p = p->next) {
888     if (p->funcs) {
889     startfunc = p->funcs[MODCALL_START];
890     startfunc(NULL);
891     }
892     }
893    
894     rehash();
895     restart_chons();
896     call_hook(HOOK_LOADED);
897     }
898     eggbusy = 1;
899     do_restart = 0;
900     }
901    
902     #ifdef USE_TCL_EVENTS
903     if (!eggbusy) {
904     /* Process all pending tcl events */
905     # ifdef REPLACE_NOTIFIER
906     if (Tcl_ServiceAll())
907     tclbusy = 1;
908     # else
909     while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS))
910     tclbusy = 1;
911     # endif /* REPLACE_NOTIFIER */
912     #endif /* USE_TCL_EVENTS */
913     }
914    
915     return (eggbusy || tclbusy);
916     }
917    
918     int main(int arg_c, char **arg_v)
919     {
920     int i, xx;
921     char s[25];
922     FILE *f;
923     struct sigaction sv;
924     struct chanset_t *chan;
925     #ifdef DEBUG
926     struct rlimit cdlim;
927     #endif
928     #ifdef STOP_UAC
929     int nvpair[2];
930     #endif
931    
932     /* Make sure it can write core, if you make debug. Else it's pretty
933     * useless (dw)
934     *
935     * Only allow unlimited size core files when compiled with DEBUG defined.
936     * This is not a good idea for normal builds -- in these cases, use the
937     * default system resource limits instead.
938     */
939     #ifdef DEBUG
940     cdlim.rlim_cur = RLIM_INFINITY;
941     cdlim.rlim_max = RLIM_INFINITY;
942     setrlimit(RLIMIT_CORE, &cdlim);
943     #endif
944    
945     #ifdef DEBUG_CONTEXT
946     /* Initialise context list */
947     for (i = 0; i < 16; i++)
948     Context;
949     #endif
950    
951     /* Include patch.h header for patch("...") */
952     #include "patch.h"
953    
954     argc = arg_c;
955     argv = arg_v;
956    
957     /* Version info! */
958     egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
959     egg_snprintf(version, sizeof version,
960     "Eggdrop v%s (C) 1997 Robey Pointer (C) 2010 Eggheads",
961     egg_version);
962     /* Now add on the patchlevel (for Tcl) */
963     sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
964     strcat(egg_version, egg_xtra);
965    
966     /* For OSF/1 */
967     #ifdef STOP_UAC
968     /* Don't print "unaligned access fixup" warning to the user */
969     nvpair[0] = SSIN_UACPROC;
970     nvpair[1] = UAC_NOPRINT;
971     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
972     #endif
973    
974     /* Set up error traps: */
975     sv.sa_handler = got_bus;
976     sigemptyset(&sv.sa_mask);
977     #ifdef SA_RESETHAND
978     sv.sa_flags = SA_RESETHAND;
979     #else
980     sv.sa_flags = 0;
981     #endif
982     sigaction(SIGBUS, &sv, NULL);
983     sv.sa_handler = got_segv;
984     sigaction(SIGSEGV, &sv, NULL);
985     #ifdef SA_RESETHAND
986     sv.sa_flags = 0;
987     #endif
988     sv.sa_handler = got_fpe;
989     sigaction(SIGFPE, &sv, NULL);
990     sv.sa_handler = got_term;
991     sigaction(SIGTERM, &sv, NULL);
992     sv.sa_handler = got_hup;
993     sigaction(SIGHUP, &sv, NULL);
994     sv.sa_handler = got_quit;
995     sigaction(SIGQUIT, &sv, NULL);
996     sv.sa_handler = SIG_IGN;
997     sigaction(SIGPIPE, &sv, NULL);
998     sv.sa_handler = got_ill;
999     sigaction(SIGILL, &sv, NULL);
1000     sv.sa_handler = got_alarm;
1001     sigaction(SIGALRM, &sv, NULL);
1002    
1003     /* Initialize variables and stuff */
1004     now = time(NULL);
1005     chanset = NULL;
1006     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
1007     lastmin = nowtm.tm_min;
1008     srandom((unsigned int) (now % (getpid() + getppid())));
1009     init_mem();
1010     init_language(1);
1011     if (argc > 1)
1012     for (i = 1; i < argc; i++)
1013     do_arg(argv[i]);
1014    
1015     printf("\n%s\n", version);
1016    
1017     #ifndef CYGWIN_HACKS
1018     /* Don't allow eggdrop to run as root
1019     * This check isn't useful under cygwin and has been
1020     * reported to cause trouble in some situations.
1021     */
1022     if (((int) getuid() == 0) || ((int) geteuid() == 0))
1023     fatal("ERROR: Eggdrop will not run as root!", 0);
1024     #endif
1025    
1026     #ifndef REPLACE_NOTIFIER
1027     init_threaddata(1);
1028     #endif
1029     init_userent();
1030     init_misc();
1031     init_bots();
1032     init_modules();
1033     if (backgrd)
1034     bg_prepare_split();
1035     init_tcl(argc, argv);
1036     init_language(0);
1037     #ifdef STATIC
1038     link_statics();
1039     #endif
1040     strncpyz(s, ctime(&now), sizeof s);
1041     strcpy(&s[11], &s[20]);
1042     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
1043     chanprog();
1044     if (!encrypt_pass) {
1045     printf(MOD_NOCRYPT);
1046     bg_send_quit(BG_ABORT);
1047     exit(1);
1048     }
1049     i = 0;
1050     for (chan = chanset; chan; chan = chan->next)
1051     i++;
1052     putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
1053     botnetnick, i, count_users(userlist));
1054     cache_miss = 0;
1055     cache_hit = 0;
1056     if (!pid_file[0])
1057     egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
1058    
1059     /* Check for pre-existing eggdrop! */
1060     f = fopen(pid_file, "r");
1061     if (f != NULL) {
1062     fgets(s, 10, f);
1063     xx = atoi(s);
1064     i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
1065     * is used */
1066     if (i == 0 || errno != ESRCH) {
1067     printf(EGG_RUNNING1, botnetnick);
1068     printf(EGG_RUNNING2, pid_file);
1069     bg_send_quit(BG_ABORT);
1070     exit(1);
1071     }
1072     }
1073    
1074     /* Move into background? */
1075     if (backgrd) {
1076     bg_do_split();
1077     } else { /* !backgrd */
1078     xx = getpid();
1079     if (xx != 0) {
1080     FILE *fp;
1081    
1082     /* Write pid to file */
1083     unlink(pid_file);
1084     fp = fopen(pid_file, "w");
1085     if (fp != NULL) {
1086     fprintf(fp, "%u\n", xx);
1087     if (fflush(fp)) {
1088     /* Let the bot live since this doesn't appear to be a botchk */
1089     printf(EGG_NOWRITE, pid_file);
1090     fclose(fp);
1091     unlink(pid_file);
1092     } else
1093     fclose(fp);
1094     } else
1095     printf(EGG_NOWRITE, pid_file);
1096     }
1097     }
1098    
1099     use_stderr = 0; /* Stop writing to stderr now */
1100     if (backgrd) {
1101     /* Ok, try to disassociate from controlling terminal (finger cross) */
1102     #ifdef HAVE_SETPGID
1103     setpgid(0, 0);
1104     #endif
1105     /* Tcl wants the stdin, stdout and stderr file handles kept open. */
1106     freopen("/dev/null", "r", stdin);
1107     freopen("/dev/null", "w", stdout);
1108     freopen("/dev/null", "w", stderr);
1109     #ifdef CYGWIN_HACKS
1110     FreeConsole();
1111     #endif
1112     }
1113    
1114     /* Terminal emulating dcc chat */
1115     if (!backgrd && term_z) {
1116     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
1117    
1118 pseudo 1.2 getvhost(&dcc[n].sockname, AF_INET);
1119 simple 1.1 dcc[n].sock = STDOUT;
1120     dcc[n].timeval = now;
1121     dcc[n].u.chat->con_flags = conmask;
1122     dcc[n].u.chat->strip_flags = STRIP_ALL;
1123     dcc[n].status = STAT_ECHO;
1124     strcpy(dcc[n].nick, "HQ");
1125     strcpy(dcc[n].host, "llama@console");
1126     /* HACK: Workaround not to pass literal "HQ" as a non-const arg */
1127     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1128     /* Make sure there's an innocuous HQ user if needed */
1129     if (!dcc[n].user) {
1130     userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
1131     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1132     }
1133     setsock(STDOUT, 0); /* Entry in net table */
1134     dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
1135     dcc_chatter(n);
1136     }
1137    
1138     then = now;
1139     online_since = now;
1140     autolink_cycle(NULL); /* Hurry and connect to tandem bots */
1141     add_help_reference("cmds1.help");
1142     add_help_reference("cmds2.help");
1143     add_help_reference("core.help");
1144     add_hook(HOOK_SECONDLY, (Function) core_secondly);
1145     add_hook(HOOK_MINUTELY, (Function) core_minutely);
1146     add_hook(HOOK_HOURLY, (Function) core_hourly);
1147     add_hook(HOOK_REHASH, (Function) event_rehash);
1148     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
1149     add_hook(HOOK_USERFILE, (Function) event_save);
1150     add_hook(HOOK_BACKUP, (Function) backup_userfile);
1151     add_hook(HOOK_DAILY, (Function) event_logfile);
1152     add_hook(HOOK_DAILY, (Function) event_resettraffic);
1153     add_hook(HOOK_LOADED, (Function) event_loaded);
1154    
1155     call_hook(HOOK_LOADED);
1156    
1157     debug0("main: entering loop");
1158     while (1) {
1159     mainloop(1);
1160     }
1161     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23