/[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.97 - (hide annotations) (download) (as text)
Wed Oct 31 04:02:51 2001 UTC (17 years, 8 months ago) by tothwolf
Branch: MAIN
Changes since 1.96: +2 -4 lines
File MIME type: text/x-chdr
* 39 less gcc warnings...

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23