/[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.71 - (hide annotations) (download) (as text)
Thu Jul 26 17:04:33 2001 UTC (18 years ago) by drummer
Branch: MAIN
Changes since 1.70: +4 -2 lines
File MIME type: text/x-chdr
added adns dir

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23