/[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.66 - (hide annotations) (download) (as text)
Sun Jul 15 05:15:14 2001 UTC (18 years, 3 months ago) by guppy
Branch: MAIN
CVS Tags: eggdrop1_6_6
Changes since 1.65: +3 -3 lines
File MIME type: text/x-chdr
here is 1.6.6 ...

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23