/[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.92 - (hide annotations) (download) (as text)
Fri Oct 19 05:26:32 2001 UTC (17 years, 10 months ago) by guppy
Branch: MAIN
Changes since 1.91: +5 -3 lines
File MIME type: text/x-chdr
* applied BarkerJr's small documentation patch
* applied the pidfile patch from eggdrop1.6
* added stdarg to AUTHORS (wish we had a realname <g>)

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23