/[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.47 - (hide annotations) (download) (as text)
Thu Nov 16 03:27:01 2000 UTC (18 years, 9 months ago) by guppy
Branch: MAIN
Changes since 1.46: +3 -3 lines
File MIME type: text/x-chdr
guppy: 1.6.1 here we go

1 segfault 1.1 /*
2     * main.c -- handles:
3 fabian 1.14 * core event handling
4     * signal handling
5     * command line arguments
6     * context and assert debugging
7 segfault 1.1 *
8 guppy 1.47 * $Id: main.c,v 1.46 2000/11/06 04:06:41 guppy Exp $
9 segfault 1.1 */
10 fabian 1.9 /*
11 fabian 1.14 * Copyright (C) 1997 Robey Pointer
12 fabian 1.25 * Copyright (C) 1999, 2000 Eggheads
13 fabian 1.9 *
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 segfault 1.1 *
19     * 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 fabian 1.9 *
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     /*
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     * list avaliable at eggheads@eggheads.org.
32     */
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 segfault 1.1 #include "chan.h"
50     #include "modules.h"
51     #include "tandem.h"
52 fabian 1.43 #include "bg.h"
53 fabian 1.38
54 fabian 1.5 #ifdef CYGWIN_HACKS
55     #include <windows.h>
56     #endif
57 segfault 1.1
58     #ifndef _POSIX_SOURCE
59 fabian 1.27 /* Solaris needs this */
60 segfault 1.1 #define _POSIX_SOURCE 1
61     #endif
62    
63 fabian 1.27 extern char origbotname[], userfile[], botnetnick[];
64     extern int dcc_total, conmask, cache_hit, cache_miss,
65     max_logs, quick_logs;
66     extern struct dcc_t *dcc;
67     extern struct userrec *userlist;
68     extern struct chanset_t *chanset;
69     extern log_t *logs;
70     extern Tcl_Interp *interp;
71     extern tcl_timer_t *timer,
72     *utimer;
73     extern jmp_buf alarmret;
74    
75 segfault 1.1
76 fabian 1.14 /*
77 segfault 1.1 * Please use the PATCH macro instead of directly altering the version
78     * string from now on (it makes it much easier to maintain patches).
79     * Also please read the README file regarding your rights to distribute
80     * modified versions of this bot.
81     */
82    
83 guppy 1.47 char egg_version[1024] = "1.6.1";
84     int egg_numver = 1060100;
85 segfault 1.1
86 fabian 1.27 char notify_new[121] = ""; /* Person to send a note to for new users */
87     int default_flags = 0; /* Default user flags and */
88     int default_uflags = 0; /* Default userdefinied flags for people
89     who say 'hello' or for .adduser */
90    
91     int backgrd = 1; /* Run in the background? */
92     int con_chan = 0; /* Foreground: constantly display channel
93     stats? */
94     int term_z = 0; /* Foreground: use the terminal as a party
95     line? */
96     char configfile[121] = "eggdrop.conf"; /* Name of the config file */
97     char helpdir[121]; /* Directory of help files (if used) */
98     char textdir[121] = ""; /* Directory for text files that get dumped */
99     int keep_all_logs = 0; /* Never erase logfiles, no matter how old
100     they are? */
101 fabian 1.40 char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix. */
102 fabian 1.27 time_t online_since; /* Unix-time that the bot loaded up */
103     int make_userfile = 0; /* Using bot in make-userfile mode? (first
104     user to 'hello' becomes master) */
105     char owner[121] = ""; /* Permanent owner(s) of the bot */
106     char pid_file[40]; /* Name of the file for the pid to be
107     stored in */
108     int save_users_at = 0; /* How many minutes past the hour to
109     save the userfile? */
110     int notify_users_at = 0; /* How many minutes past the hour to
111     notify users of notes? */
112     int switch_logfiles_at = 300; /* When (military time) to switch logfiles */
113     char version[81]; /* Version info (long form) */
114     char ver[41]; /* Version info (short form) */
115     char egg_xtra[2048]; /* Patch info */
116     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
117     int do_restart = 0; /* .restart has been called, restart asap */
118     int die_on_sighup = 0; /* die if bot receives SIGHUP */
119     int die_on_sigterm = 0; /* die if bot receives SIGTERM */
120     int resolve_timeout = 15; /* hostname/address lookup timeout */
121     time_t now; /* duh, now :) */
122    
123     /* Traffic stats
124     */
125 fabian 1.33 unsigned long otraffic_irc = 0;
126     unsigned long otraffic_irc_today = 0;
127     unsigned long otraffic_bn = 0;
128     unsigned long otraffic_bn_today = 0;
129     unsigned long otraffic_dcc = 0;
130     unsigned long otraffic_dcc_today = 0;
131     unsigned long otraffic_filesys = 0;
132     unsigned long otraffic_filesys_today = 0;
133     unsigned long otraffic_trans = 0;
134     unsigned long otraffic_trans_today = 0;
135     unsigned long otraffic_unknown = 0;
136     unsigned long otraffic_unknown_today = 0;
137     unsigned long itraffic_irc = 0;
138     unsigned long itraffic_irc_today = 0;
139     unsigned long itraffic_bn = 0;
140     unsigned long itraffic_bn_today = 0;
141     unsigned long itraffic_dcc = 0;
142     unsigned long itraffic_dcc_today = 0;
143     unsigned long itraffic_trans = 0;
144     unsigned long itraffic_trans_today = 0;
145     unsigned long itraffic_unknown = 0;
146     unsigned long itraffic_unknown_today = 0;
147 segfault 1.1
148 fabian 1.11 #ifdef DEBUG_CONTEXT
149 fabian 1.27 /* Context storage for fatal crashes */
150     char cx_file[16][30];
151     char cx_note[16][256];
152     int cx_line[16];
153     int cx_ptr = 0;
154 fabian 1.10 #endif
155 segfault 1.1
156 fabian 1.27
157 fabian 1.44 void fatal(const char *s, int recoverable)
158 segfault 1.1 {
159     int i;
160    
161     putlog(LOG_MISC, "*", "* %s", s);
162     flushlogs();
163     for (i = 0; i < dcc_total; i++)
164 fabian 1.19 if (dcc[i].sock >= 0)
165 fabian 1.15 killsock(dcc[i].sock);
166 segfault 1.1 unlink(pid_file);
167 fabian 1.43 if (!recoverable) {
168     bg_send_quit(BG_ABORT);
169 segfault 1.1 exit(1);
170 fabian 1.43 }
171 segfault 1.1 }
172    
173 fabian 1.27
174 segfault 1.1 int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
175     expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
176     expmem_modules(int), expmem_language(), expmem_tcldcc();
177    
178 fabian 1.27 /* For mem.c : calculate memory we SHOULD be using
179     */
180 fabian 1.44 int expected_memory(void)
181 segfault 1.1 {
182     int tot;
183    
184 fabian 1.10 Context;
185 segfault 1.1 tot = expmem_chanprog() + expmem_users() + expmem_misc() +
186     expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
187     expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
188     return tot;
189     }
190    
191     static void check_expired_dcc()
192     {
193     int i;
194    
195     for (i = 0; i < dcc_total; i++)
196     if (dcc[i].type && dcc[i].type->timeout_val &&
197     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
198     if (dcc[i].type->timeout)
199     dcc[i].type->timeout(i);
200     else if (dcc[i].type->eof)
201     dcc[i].type->eof(i);
202     else
203     continue;
204 fabian 1.27 /* Only timeout 1 socket per cycle, too risky for more */
205 segfault 1.1 return;
206     }
207     }
208    
209 fabian 1.11 #ifdef DEBUG_CONTEXT
210 fabian 1.27 static int nested_debug = 0;
211 segfault 1.1
212     void write_debug()
213     {
214     int x;
215     char s[80];
216     int y;
217    
218     if (nested_debug) {
219 fabian 1.27 /* Yoicks, if we have this there's serious trouble!
220     * All of these are pretty reliable, so we'll try these.
221     *
222     * NOTE: dont try and display context-notes in here, it's
223     * _not_ safe <cybah>
224     */
225 segfault 1.1 x = creat("DEBUG.DEBUG", 0644);
226     setsock(x, SOCK_NONSOCK);
227     if (x >= 0) {
228     strcpy(s, ctime(&now));
229     dprintf(-x, "Debug (%s) written %s", ver, s);
230     dprintf(-x, "Please report problem to eggheads@eggheads.org");
231     dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
232     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
233     dprintf(-x, "Context: ");
234     cx_ptr = cx_ptr & 15;
235     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
236     dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
237     dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
238     killsock(x);
239     close(x);
240     }
241 fabian 1.43 bg_send_quit(BG_ABORT);
242 fabian 1.27 exit(1); /* Dont even try & tell people about, that may
243     have caused the fault last time. */
244 segfault 1.1 } else
245     nested_debug = 1;
246     putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
247     cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
248 fabian 1.2 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
249     putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
250 fabian 1.9 x = creat("DEBUG", 0644);
251 segfault 1.1 setsock(x, SOCK_NONSOCK);
252     if (x < 0) {
253     putlog(LOG_MISC, "*", "* Failed to write DEBUG");
254     } else {
255     strcpy(s, ctime(&now));
256     dprintf(-x, "Debug (%s) written %s", ver, s);
257     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
258     #ifdef STATIC
259     dprintf(-x, "STATICALLY LINKED\n");
260     #endif
261     strcpy(s, "info library");
262     if (interp && (Tcl_Eval(interp, s) == TCL_OK))
263     dprintf(-x, "Using tcl library: %s (header version %s)\n",
264     interp->result, TCL_VERSION);
265     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
266     dprintf(-x, "Link flags : %s\n", LDFLAGS);
267     dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
268     dprintf(-x, "Context: ");
269     cx_ptr = cx_ptr & 15;
270     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
271     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
272     (cx_note[y][0]) ? cx_note[y] : "");
273     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
274     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
275     tell_dcc(-x);
276     dprintf(-x, "\n");
277     debug_mem_to_dcc(-x);
278     killsock(x);
279     close(x);
280     putlog(LOG_MISC, "*", "* Wrote DEBUG");
281     }
282     }
283 fabian 1.10 #endif
284 fabian 1.2
285 segfault 1.1 static void got_bus(int z)
286     {
287 fabian 1.11 #ifdef DEBUG_CONTEXT
288 segfault 1.1 write_debug();
289 fabian 1.10 #endif
290 segfault 1.1 fatal("BUS ERROR -- CRASHING!", 1);
291     #ifdef SA_RESETHAND
292     kill(getpid(), SIGBUS);
293     #else
294 fabian 1.43 bg_send_quit(BG_ABORT);
295 segfault 1.1 exit(1);
296     #endif
297     }
298    
299     static void got_segv(int z)
300     {
301 fabian 1.11 #ifdef DEBUG_CONTEXT
302 segfault 1.1 write_debug();
303 fabian 1.10 #endif
304 segfault 1.1 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
305     #ifdef SA_RESETHAND
306     kill(getpid(), SIGSEGV);
307     #else
308 fabian 1.43 bg_send_quit(BG_ABORT);
309 segfault 1.1 exit(1);
310     #endif
311     }
312    
313     static void got_fpe(int z)
314     {
315 fabian 1.11 #ifdef DEBUG_CONTEXT
316 segfault 1.1 write_debug();
317 fabian 1.10 #endif
318 segfault 1.1 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
319     }
320    
321     static void got_term(int z)
322     {
323     write_userfile(-1);
324     check_tcl_event("sigterm");
325     if (die_on_sigterm) {
326     botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
327     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
328     } else {
329     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
330     }
331     }
332    
333     static void got_quit(int z)
334     {
335     check_tcl_event("sigquit");
336     putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
337     return;
338     }
339    
340     static void got_hup(int z)
341     {
342     write_userfile(-1);
343     check_tcl_event("sighup");
344     if (die_on_sighup) {
345     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
346     } else
347     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
348     do_restart = -2;
349     return;
350     }
351    
352 fabian 1.27 /* A call to resolver (gethostbyname, etc) timed out
353     */
354 segfault 1.1 static void got_alarm(int z)
355     {
356     longjmp(alarmret, 1);
357 fabian 1.27
358     /* -Never reached- */
359 segfault 1.1 }
360    
361 fabian 1.27 /* Got ILL signal -- log context and continue
362     */
363 segfault 1.1 static void got_ill(int z)
364     {
365     check_tcl_event("sigill");
366 fabian 1.11 #ifdef DEBUG_CONTEXT
367 segfault 1.1 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
368     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
369 fabian 1.5 #endif
370 segfault 1.1 }
371    
372 fabian 1.11 #ifdef DEBUG_CONTEXT
373 fabian 1.10 /* Context */
374 fabian 1.44 void eggContext(const char *file, int line, const char *module)
375 fabian 1.10 {
376 fabian 1.39 char x[31], *p;
377 fabian 1.10
378 fabian 1.39 p = strrchr(file, '/');
379     if (!module) {
380     strncpy(x, p ? p + 1 : file, 30);
381     x[30] = 0;
382     } else
383     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
384 fabian 1.10 cx_ptr = ((cx_ptr + 1) & 15);
385     strcpy(cx_file[cx_ptr], x);
386     cx_line[cx_ptr] = line;
387     cx_note[cx_ptr][0] = 0;
388     }
389    
390 fabian 1.27 /* Called from the ContextNote macro.
391     */
392 fabian 1.44 void eggContextNote(const char *file, int line, const char *module,
393     const char *note)
394 fabian 1.10 {
395 fabian 1.39 char x[31], *p;
396 fabian 1.10
397 fabian 1.39 p = strrchr(file, '/');
398     if (!module) {
399     strncpy(x, p ? p + 1 : file, 30);
400     x[30] = 0;
401     } else
402     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
403 fabian 1.10 cx_ptr = ((cx_ptr + 1) & 15);
404     strcpy(cx_file[cx_ptr], x);
405     cx_line[cx_ptr] = line;
406     strncpy(cx_note[cx_ptr], note, 255);
407     cx_note[cx_ptr][255] = 0;
408     }
409 fabian 1.11 #endif
410 fabian 1.10
411 fabian 1.11 #ifdef DEBUG_ASSERT
412 fabian 1.27 /* Called from the Assert macro.
413     */
414 fabian 1.44 void eggAssert(const char *file, int line, const char *module)
415 fabian 1.10 {
416 fabian 1.11 #ifdef DEBUG_CONTEXT
417 fabian 1.44 write_debug();
418 fabian 1.11 #endif
419 fabian 1.44 if (!module)
420     putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
421     else
422     putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
423     fatal("ASSERT FAILED -- CRASHING!", 1);
424 fabian 1.10 }
425     #endif
426    
427 segfault 1.1 static void do_arg(char *s)
428     {
429     int i;
430    
431     if (s[0] == '-')
432     for (i = 1; i < strlen(s); i++) {
433     if (s[i] == 'n')
434     backgrd = 0;
435     if (s[i] == 'c') {
436     con_chan = 1;
437     term_z = 0;
438     }
439     if (s[i] == 't') {
440     con_chan = 0;
441     term_z = 1;
442     }
443     if (s[i] == 'm')
444     make_userfile = 1;
445     if (s[i] == 'v') {
446     char x[256], *z = x;
447    
448     strcpy(x, egg_version);
449     newsplit(&z);
450     newsplit(&z);
451     printf("%s\n", version);
452     if (z[0])
453     printf(" (patches: %s)\n", z);
454 fabian 1.43 bg_send_quit(BG_ABORT);
455 segfault 1.1 exit(0);
456     }
457     if (s[i] == 'h') {
458     printf("\n%s\n\n", version);
459     printf(EGG_USAGE);
460     printf("\n");
461 fabian 1.43 bg_send_quit(BG_ABORT);
462 segfault 1.1 exit(0);
463     }
464     } else
465     strcpy(configfile, s);
466     }
467    
468 fabian 1.44 void backup_userfile(void)
469 segfault 1.1 {
470     char s[150];
471    
472     putlog(LOG_MISC, "*", USERF_BACKUP);
473     strcpy(s, userfile);
474     strcat(s, "~bak");
475     copyfile(userfile, s);
476     }
477    
478 fabian 1.27 /* Timer info */
479     static int lastmin = 99;
480     static time_t then;
481     static struct tm nowtm;
482 segfault 1.1
483 fabian 1.34 /* Called once a second.
484     *
485     * Note: Try to not put any Context lines in here (guppy 21Mar2000).
486 fabian 1.27 */
487 segfault 1.1 static void core_secondly()
488     {
489     static int cnt = 0;
490     int miltime;
491    
492 fabian 1.27 do_check_timers(&utimer); /* Secondly timers */
493 segfault 1.1 cnt++;
494 fabian 1.27 if (cnt >= 10) { /* Every 10 seconds */
495 segfault 1.1 cnt = 0;
496     check_expired_dcc();
497     if (con_chan && !backgrd) {
498     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
499     tell_verbose_status(DP_STDOUT);
500     do_module_report(DP_STDOUT, 0, "server");
501     do_module_report(DP_STDOUT, 0, "channels");
502     tell_mem_status_dcc(DP_STDOUT);
503     }
504     }
505 fabian 1.35 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
506 fabian 1.17 if (nowtm.tm_min != lastmin) {
507 segfault 1.1 int i = 0;
508    
509 fabian 1.27 /* Once a minute */
510 segfault 1.1 lastmin = (lastmin + 1) % 60;
511     call_hook(HOOK_MINUTELY);
512     check_expired_ignores();
513 fabian 1.27 autolink_cycle(NULL); /* Attempt autolinks */
514     /* In case for some reason more than 1 min has passed: */
515 fabian 1.17 while (nowtm.tm_min != lastmin) {
516 fabian 1.27 /* Timer drift, dammit */
517 fabian 1.17 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
518 segfault 1.1 i++;
519     lastmin = (lastmin + 1) % 60;
520     call_hook(HOOK_MINUTELY);
521     }
522     if (i > 1)
523     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
524 fabian 1.17 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
525     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
526 segfault 1.1 call_hook(HOOK_5MINUTELY);
527     check_botnet_pings();
528     if (quick_logs == 0) {
529     flushlogs();
530     check_logsize();
531     }
532 fabian 1.27 if (miltime == 0) { /* At midnight */
533 segfault 1.1 char s[128];
534     int j;
535    
536     s[my_strcpy(s, ctime(&now)) - 1] = 0;
537 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
538 segfault 1.1 backup_userfile();
539     for (j = 0; j < max_logs; j++) {
540     if (logs[j].filename != NULL && logs[j].f != NULL) {
541     fclose(logs[j].f);
542     logs[j].f = NULL;
543     }
544     }
545     }
546     }
547 fabian 1.17 if (nowtm.tm_min == notify_users_at)
548 segfault 1.1 call_hook(HOOK_HOURLY);
549 fabian 1.27 /* These no longer need checking since they are all check vs minutely
550     * settings and we only get this far on the minute.
551     */
552 segfault 1.1 if (miltime == switch_logfiles_at) {
553     call_hook(HOOK_DAILY);
554     if (!keep_all_logs) {
555     putlog(LOG_MISC, "*", MISC_LOGSWITCH);
556     for (i = 0; i < max_logs; i++)
557     if (logs[i].filename) {
558     char s[1024];
559    
560     if (logs[i].f) {
561     fclose(logs[i].f);
562     logs[i].f = NULL;
563     }
564     simple_sprintf(s, "%s.yesterday", logs[i].filename);
565     unlink(s);
566 fabian 1.2 movefile(logs[i].filename, s);
567 segfault 1.1 }
568     }
569     }
570     }
571     }
572    
573     static void core_minutely()
574     {
575 fabian 1.17 check_tcl_time(&nowtm);
576 segfault 1.1 do_check_timers(&timer);
577     if (quick_logs != 0) {
578     flushlogs();
579     check_logsize();
580     }
581     }
582    
583     static void core_hourly()
584     {
585 fabian 1.10 Context;
586 segfault 1.1 write_userfile(-1);
587     }
588    
589     static void event_rehash()
590     {
591 fabian 1.10 Context;
592 segfault 1.1 check_tcl_event("rehash");
593     }
594    
595     static void event_prerehash()
596     {
597 fabian 1.10 Context;
598 segfault 1.1 check_tcl_event("prerehash");
599     }
600    
601     static void event_save()
602     {
603 fabian 1.10 Context;
604 segfault 1.1 check_tcl_event("save");
605     }
606    
607     static void event_logfile()
608     {
609 fabian 1.10 Context;
610 segfault 1.1 check_tcl_event("logfile");
611     }
612    
613 fabian 1.13 static void event_resettraffic()
614     {
615     Context;
616     otraffic_irc += otraffic_irc_today;
617     itraffic_irc += itraffic_irc_today;
618     otraffic_bn += otraffic_bn_today;
619     itraffic_bn += itraffic_bn_today;
620     otraffic_dcc += otraffic_dcc_today;
621     itraffic_dcc += itraffic_dcc_today;
622     otraffic_unknown += otraffic_unknown_today;
623     itraffic_unknown += itraffic_unknown_today;
624     otraffic_trans += otraffic_trans_today;
625     itraffic_trans += itraffic_trans_today;
626     otraffic_irc_today = otraffic_bn_today = 0;
627     otraffic_dcc_today = otraffic_unknown_today = 0;
628     itraffic_irc_today = itraffic_bn_today = 0;
629     itraffic_dcc_today = itraffic_unknown_today = 0;
630 fabian 1.18 itraffic_trans_today = otraffic_trans_today = 0;
631 fabian 1.13 }
632    
633 segfault 1.1 void kill_tcl();
634     extern module_entry *module_list;
635     void restart_chons();
636    
637     #ifdef STATIC
638     void check_static(char *, char *(*)());
639    
640     #include "mod/static.h"
641     #endif
642     int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
643 fabian 1.8 init_net(), init_modules(), init_tcl(int, char **),
644     init_language(int);
645 fabian 1.11
646 fabian 1.44 void patch(const char *str)
647 fabian 1.11 {
648     char *p = strchr(egg_version, '+');
649    
650     if (!p)
651     p = &egg_version[strlen(egg_version)];
652     sprintf(p, "+%s", str);
653     egg_numver++;
654     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
655     }
656 segfault 1.1
657 fabian 1.45 static inline void garbage_collect(void)
658     {
659     static u_8bit_t run_cnt = 0;
660    
661     if (run_cnt == 3)
662     garbage_collect_tclhash();
663     else
664     run_cnt++;
665     }
666    
667 segfault 1.1 int main(int argc, char **argv)
668     {
669     int xx, i;
670     char buf[520], s[520];
671     FILE *f;
672     struct sigaction sv;
673     struct chanset_t *chan;
674    
675 fabian 1.15 #ifdef DEBUG_MEM
676 fabian 1.21 /* Make sure it can write core, if you make debug. Else it's pretty
677     * useless (dw)
678     */
679 fabian 1.15 {
680     #include <sys/resource.h>
681     struct rlimit cdlim;
682 fabian 1.21
683 fabian 1.15 cdlim.rlim_cur = RLIM_INFINITY;
684     cdlim.rlim_max = RLIM_INFINITY;
685 fabian 1.32 setrlimit(RLIMIT_CORE, &cdlim);
686 fabian 1.15 }
687     #endif
688    
689 fabian 1.27 /* Initialise context list */
690 fabian 1.32 for (i = 0; i < 16; i++)
691 fabian 1.10 Context;
692 fabian 1.32
693 segfault 1.1 #include "patch.h"
694 fabian 1.27 /* Version info! */
695 segfault 1.1 sprintf(ver, "eggdrop v%s", egg_version);
696 fabian 1.25 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999, 2000 Eggheads",
697     egg_version);
698 fabian 1.27 /* Now add on the patchlevel (for Tcl) */
699 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
700 segfault 1.1 strcat(egg_version, egg_xtra);
701 fabian 1.10 Context;
702 segfault 1.1 #ifdef STOP_UAC
703     {
704     int nvpair[2];
705    
706     nvpair[0] = SSIN_UACPROC;
707     nvpair[1] = UAC_NOPRINT;
708     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
709     }
710     #endif
711 fabian 1.27
712     /* Set up error traps: */
713 segfault 1.1 sv.sa_handler = got_bus;
714     sigemptyset(&sv.sa_mask);
715     #ifdef SA_RESETHAND
716     sv.sa_flags = SA_RESETHAND;
717     #else
718     sv.sa_flags = 0;
719     #endif
720     sigaction(SIGBUS, &sv, NULL);
721     sv.sa_handler = got_segv;
722     sigaction(SIGSEGV, &sv, NULL);
723     #ifdef SA_RESETHAND
724     sv.sa_flags = 0;
725     #endif
726     sv.sa_handler = got_fpe;
727     sigaction(SIGFPE, &sv, NULL);
728     sv.sa_handler = got_term;
729     sigaction(SIGTERM, &sv, NULL);
730     sv.sa_handler = got_hup;
731     sigaction(SIGHUP, &sv, NULL);
732     sv.sa_handler = got_quit;
733     sigaction(SIGQUIT, &sv, NULL);
734     sv.sa_handler = SIG_IGN;
735     sigaction(SIGPIPE, &sv, NULL);
736     sv.sa_handler = got_ill;
737     sigaction(SIGILL, &sv, NULL);
738     sv.sa_handler = got_alarm;
739     sigaction(SIGALRM, &sv, NULL);
740 fabian 1.27
741     /* Initialize variables and stuff */
742 segfault 1.1 now = time(NULL);
743     chanset = NULL;
744 fabian 1.35 egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
745 fabian 1.17 lastmin = nowtm.tm_min;
746 segfault 1.1 srandom(now);
747     init_mem();
748     init_language(1);
749     if (argc > 1)
750     for (i = 1; i < argc; i++)
751     do_arg(argv[i]);
752     printf("\n%s\n", version);
753     init_dcc_max();
754     init_userent();
755     init_misc();
756     init_bots();
757     init_net();
758     init_modules();
759 fabian 1.43 if (backgrd)
760     bg_prepare_split();
761 fabian 1.8 init_tcl(argc, argv);
762 segfault 1.1 init_language(0);
763     #ifdef STATIC
764     link_statics();
765     #endif
766 fabian 1.10 Context;
767 segfault 1.1 strcpy(s, ctime(&now));
768     s[strlen(s) - 1] = 0;
769     strcpy(&s[11], &s[20]);
770     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
771     chanprog();
772 fabian 1.10 Context;
773 segfault 1.1 if (encrypt_pass == 0) {
774     printf(MOD_NOCRYPT);
775 fabian 1.43 bg_send_quit(BG_ABORT);
776 segfault 1.1 exit(1);
777     }
778     i = 0;
779     for (chan = chanset; chan; chan = chan->next)
780     i++;
781 fabian 1.4 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
782 segfault 1.1 botnetnick, i, count_users(userlist));
783     cache_miss = 0;
784     cache_hit = 0;
785     sprintf(pid_file, "pid.%s", botnetnick);
786 fabian 1.10 Context;
787 fabian 1.27
788     /* Check for pre-existing eggdrop! */
789 segfault 1.1 f = fopen(pid_file, "r");
790     if (f != NULL) {
791     fgets(s, 10, f);
792     xx = atoi(s);
793 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
794     is used */
795 segfault 1.1 if (errno != ESRCH) {
796     printf(EGG_RUNNING1, origbotname);
797     printf(EGG_RUNNING2, pid_file);
798 fabian 1.43 bg_send_quit(BG_ABORT);
799 segfault 1.1 exit(1);
800     }
801     }
802 fabian 1.10 Context;
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     printf(EGG_NOWRITE, pid_file);
822     fclose(fp);
823     unlink(pid_file);
824     } else
825     fclose(fp);
826 fabian 1.27 } else
827 fabian 1.38 printf(EGG_NOWRITE, pid_file);
828     #ifdef CYGWIN_HACKS
829     printf("Launched (pid: %d)\n\n", xx);
830     #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 fabian 1.5 #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     dcc[n].addr = iptolong(getmyip());
854     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     add_hook(HOOK_DAILY, (Function) event_logfile);
885     add_hook(HOOK_DAILY, (Function) event_resettraffic);
886 segfault 1.1
887     debug0("main: entering loop");
888     while (1) {
889     int socket_cleanup = 0;
890    
891 fabian 1.10 Context;
892 fabian 1.43 #if !defined(HAVE_PRE7_5_TCL)
893 fabian 1.27 /* Process a single tcl event */
894 segfault 1.1 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
895 fabian 1.5 #endif
896 fabian 1.45
897 fabian 1.27 /* Lets move some of this here, reducing the numer of actual
898     * calls to periodic_timers
899     */
900 segfault 1.1 now = time(NULL);
901 fabian 1.27 random(); /* Woop, lets really jumble things */
902     if (now != then) { /* Once a second */
903 segfault 1.1 call_hook(HOOK_SECONDLY);
904     then = now;
905     }
906 fabian 1.45
907 fabian 1.10 Context;
908 fabian 1.45 /* Only do this every so often. */
909 segfault 1.1 if (!socket_cleanup) {
910 fabian 1.45 socket_cleanup = 5;
911    
912     /* Remove dead dcc entries. */
913 fabian 1.16 dcc_remove_lost();
914 fabian 1.45
915     /* Check for server or dcc activity. */
916 segfault 1.1 dequeue_sockets();
917     } else
918     socket_cleanup--;
919 fabian 1.45
920     /* Free unused structures. */
921     garbage_collect();
922    
923 segfault 1.1 xx = sockgets(buf, &i);
924 fabian 1.27 if (xx >= 0) { /* Non-error */
925 segfault 1.1 int idx;
926    
927     for (idx = 0; idx < dcc_total; idx++)
928     if (dcc[idx].sock == xx) {
929 fabian 1.13 if (dcc[idx].type && dcc[idx].type->activity) {
930 fabian 1.27 /* Traffic stats */
931     if (dcc[idx].type->name) {
932     if (!strncmp(dcc[idx].type->name, "BOT", 3))
933     itraffic_bn_today += strlen(buf) + 1;
934     else if (!strcmp(dcc[idx].type->name, "SERVER"))
935     itraffic_irc_today += strlen(buf) + 1;
936     else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
937     itraffic_dcc_today += strlen(buf) + 1;
938     else if (!strncmp(dcc[idx].type->name, "FILES", 5))
939     itraffic_dcc_today += strlen(buf) + 1;
940     else if (!strcmp(dcc[idx].type->name, "SEND"))
941     itraffic_trans_today += strlen(buf) + 1;
942     else if (!strncmp(dcc[idx].type->name, "GET", 3))
943     itraffic_trans_today += strlen(buf) + 1;
944     else
945     itraffic_unknown_today += strlen(buf) + 1;
946     }
947     dcc[idx].type->activity(idx, buf, i);
948 fabian 1.13 } else
949 segfault 1.1 putlog(LOG_MISC, "*",
950     "!!! untrapped dcc activity: type %s, sock %d",
951     dcc[idx].type->name, dcc[idx].sock);
952     break;
953     }
954     } else if (xx == -1) { /* EOF from someone */
955     int idx;
956    
957 fabian 1.45 if (i == STDOUT && !backgrd)
958 segfault 1.1 fatal("END OF FILE ON TERMINAL", 0);
959 fabian 1.10 Context;
960 segfault 1.1 for (idx = 0; idx < dcc_total; idx++)
961     if (dcc[idx].sock == i) {
962     if (dcc[idx].type && dcc[idx].type->eof)
963     dcc[idx].type->eof(idx);
964     else {
965     putlog(LOG_MISC, "*",
966     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
967 fabian 1.2 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
968     killsock(i);
969 segfault 1.1 lostdcc(idx);
970     }
971     idx = dcc_total + 1;
972     }
973     if (idx == dcc_total) {
974     putlog(LOG_MISC, "*",
975 fabian 1.2 "(@) EOF socket %d, not a dcc socket, not anything.", i);
976     close(i);
977     killsock(i);
978 segfault 1.1 }
979 fabian 1.45 } else if (xx == -2 && errno != EINTR) { /* select() error */
980 fabian 1.10 Context;
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    
1007     while (f) {
1008     f = 0;
1009     for (p = module_list; p != NULL; p = p->next) {
1010     dependancy *d = dependancy_list;
1011     int ok = 1;
1012    
1013     while (ok && d) {
1014     if (d->needed == p)
1015     ok = 0;
1016     d = d->next;
1017     }
1018     if (ok) {
1019     strcpy(xx, p->name);
1020     if (module_unload(xx, origbotname) == NULL) {
1021     f = 1;
1022     break;
1023     }
1024     }
1025     }
1026     }
1027     p = module_list;
1028     if (p && p->next && p->next->next)
1029 fabian 1.45 /* Should be only 2 modules now - blowfish (or some other
1030     encryption module) and eggdrop. */
1031 segfault 1.1 putlog(LOG_MISC, "*", MOD_STAGNANT);
1032 fabian 1.10 Context;
1033 segfault 1.1 flushlogs();
1034 fabian 1.10 Context;
1035 segfault 1.1 kill_tcl();
1036 fabian 1.8 init_tcl(argc, argv);
1037 segfault 1.1 init_language(0);
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     }
1049     do_restart = 0;
1050     }
1051     }
1052     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23