/[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.6.2.3 - (hide annotations) (download) (as text)
Wed Jan 12 13:54:00 2011 UTC (8 years, 4 months ago) by pseudo
Branch: gettext
Changes since 1.6.2.2: +3 -3 lines
File MIME type: text/x-chdr
Removed memcpy(), memset() and strftime() compatibility replacements.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23