/[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.7 - (hide annotations) (download) (as text)
Thu Jan 27 01:41:08 2011 UTC (8 years, 4 months ago) by thommey
Branch: MAIN
Changes since 1.6: +5 -5 lines
File MIME type: text/x-chdr
bind evnt return value now ignored for non-signal events as documented

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23