/[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.77 - (hide annotations) (download) (as text)
Thu Sep 20 19:50:19 2001 UTC (17 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.76: +12 -2 lines
File MIME type: text/x-chdr
STill removing tcl from the bind table.
Created first non-tcl bind entry ("event") and bind2/unbind2.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23