/[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.73 - (hide annotations) (download) (as text)
Fri Aug 10 23:51:20 2001 UTC (17 years, 11 months ago) by ite
Branch: MAIN
Changes since 1.72: +18 -10 lines
File MIME type: text/x-chdr
Gettext phase 1: created a dummy _() macro, replaced old get_lang #defines with the respective string entries in english.*.lang and removed them.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23