/[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.38 - (hide annotations) (download) (as text)
Sat May 13 20:20:29 2000 UTC (19 years, 3 months ago) by fabian
Branch: MAIN
Changes since 1.37: +28 -27 lines
File MIME type: text/x-chdr
Tothwolf's cygwin-fixes-07May2000 patch (with changes by Fabian)

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23