/[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.50 - (hide annotations) (download) (as text)
Mon Jan 15 03:28:10 2001 UTC (18 years, 6 months ago) by guppy
Branch: MAIN
Changes since 1.49: +3 -3 lines
File MIME type: text/x-chdr
welcome to 1.6.3 land boys and girls

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23