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

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

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


Revision 1.51 - (hide annotations) (download) (as text)
Wed Jan 31 05:32:31 2001 UTC (18 years, 6 months ago) by guppy
Branch: MAIN
Changes since 1.50: +25 -4 lines
File MIME type: text/x-chdr
added tclstuff.diff -- finally, we can see our tcl version in a .status <g>

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23