/[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.24 - (hide annotations) (download) (as text)
Mon Jan 17 21:59:11 2000 UTC (19 years, 7 months ago) by fabian
Branch: MAIN
Changes since 1.23: +4 -10 lines
File MIME type: text/x-chdr
resync with 1.4, 17Jan2000

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23