/[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.81 - (hide annotations) (download) (as text)
Fri Oct 12 15:50:26 2001 UTC (17 years, 9 months ago) by tothwolf
Branch: MAIN
Changes since 1.80: +2 -18 lines
File MIME type: text/x-chdr
* DEBUG_ASSERT renamed to DEBUG
* Assert macro removed, replaced with ANSI assert support
* Removed more kludge code

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23