/[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.28 - (hide annotations) (download) (as text)
Tue Feb 1 20:36:18 2000 UTC (19 years, 5 months ago) by fabian
Branch: MAIN
CVS Tags: eggdrop105020
Changes since 1.27: +2 -2 lines
File MIME type: text/x-chdr
stat_fix patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23