/[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.122 - (hide annotations) (download) (as text)
Fri Sep 20 21:41:49 2002 UTC (16 years, 10 months ago) by stdarg
Branch: MAIN
Changes since 1.121: +1 -9 lines
File MIME type: text/x-chdr
* Mo-Ize removed the botnet code, thanks

1 guppy 1.56 /*
2 tothwolf 1.117 * main.c --
3 guppy 1.56 *
4 tothwolf 1.117 * core event handling
5     * signal handling
6     * command line arguments
7 segfault 1.1 */
8 guppy 1.56 /*
9     * Copyright (C) 1997 Robey Pointer
10 wcc 1.106 * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
11 guppy 1.56 *
12 fabian 1.14 * This program is free software; you can redistribute it and/or
13     * modify it under the terms of the GNU General Public License
14     * as published by the Free Software Foundation; either version 2
15     * of the License, or (at your option) any later version.
16 guppy 1.56 *
17 segfault 1.1 * This program is distributed in the hope that it will be useful,
18     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     * GNU General Public License for more details.
21 guppy 1.56 *
22 segfault 1.1 * You should have received a copy of the GNU General Public License
23     * along with this program; if not, write to the Free Software
24 fabian 1.14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25     */
26 guppy 1.56 /*
27 segfault 1.1 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 tothwolf 1.117 * NOTE: Robey is no longer working on this code, there is a discussion
29 guppy 1.60 * list available at eggheads@eggheads.org.
30 segfault 1.1 */
31 tothwolf 1.117
32     #ifndef lint
33 stdarg 1.122 static const char rcsid[] = "$Id: main.c,v 1.121 2002/09/20 02:06:25 stdarg Exp $";
34 tothwolf 1.117 #endif
35 segfault 1.1
36 fabian 1.28 #include "main.h"
37 fabian 1.27 #include <fcntl.h>
38 segfault 1.1 #include <time.h>
39     #include <errno.h>
40     #include <signal.h>
41     #include <netdb.h>
42     #include <setjmp.h>
43 wingman 1.116 #include "users.h" /* check_expired_ignores,
44     get_user_by_handle */
45     #include "chanprog.h" /* tell_verbose_status,
46     chanprog, rehash */
47     #include "net.h" /* killsock, setsock,
48     dequeue_sockets, sockgets */
49     #include "userrec.h" /* write_userfile, count_users */
50 ite 1.76 #include <locale.h>
51    
52 fabian 1.27 #ifdef STOP_UAC /* osf/1 complains a lot */
53 segfault 1.1 #include <sys/sysinfo.h>
54     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
55     #endif
56 fabian 1.27 /* Some systems have a working sys/wait.h even though configure will
57     * decide it's not bsd compatable. Oh well.
58     */
59 guppy 1.49
60 segfault 1.1 #include "chan.h"
61     #include "modules.h"
62 fabian 1.43 #include "bg.h"
63 stdarg 1.85 #include "core_binds.h"
64 ite 1.105 #include "logfile.h"
65     #include "misc.h"
66 wingman 1.119 #include "traffic.h"
67 wingman 1.116 #include "dccutil.h" /* dprintf_eggdrop, new_dcc,
68     dcc_chatter, dcc_remove_lost,
69     lostdcc */
70 fabian 1.38
71 ite 1.96 #include "lib/adns/adns.h"
72 drummer 1.71
73 poptix 1.62 #ifdef CYGWIN_HACKS
74     #include <windows.h>
75     #endif
76    
77 segfault 1.1 #ifndef _POSIX_SOURCE
78 fabian 1.27 /* Solaris needs this */
79 segfault 1.1 #define _POSIX_SOURCE 1
80     #endif
81    
82 ite 1.104 extern char userfile[], botnetnick[];
83     extern int dcc_total, conmask, cache_hit, cache_miss;
84 fabian 1.27 extern struct dcc_t *dcc;
85     extern struct userrec *userlist;
86     extern struct chanset_t *chanset;
87     extern jmp_buf alarmret;
88    
89 wingman 1.116 #ifndef MAKING_MODS
90     extern struct dcc_table DCC_CHAT;
91     #endif /* MAKING_MODS */
92 segfault 1.1
93 guppy 1.56 /*
94 segfault 1.1 * Please use the PATCH macro instead of directly altering the version
95     * string from now on (it makes it much easier to maintain patches).
96     * Also please read the README file regarding your rights to distribute
97     * modified versions of this bot.
98     */
99    
100 ite 1.76 char egg_version[1024] = VERSION;
101     int egg_numver = VERSION_NUM;
102 segfault 1.1
103 ite 1.109 eggdrop_t *egg = NULL; /* Eggdrop's context */
104    
105 fabian 1.27 char notify_new[121] = ""; /* Person to send a note to for new users */
106     int default_flags = 0; /* Default user flags and */
107     int default_uflags = 0; /* Default userdefinied flags for people
108     who say 'hello' or for .adduser */
109    
110     int backgrd = 1; /* Run in the background? */
111     int con_chan = 0; /* Foreground: constantly display channel
112     stats? */
113     int term_z = 0; /* Foreground: use the terminal as a party
114     line? */
115     char configfile[121] = "eggdrop.conf"; /* Name of the config file */
116 ite 1.113 static char preload_module[121] = ""; /* Name of the module to preload */
117 fabian 1.27 char helpdir[121]; /* Directory of help files (if used) */
118     char textdir[121] = ""; /* Directory for text files that get dumped */
119     time_t online_since; /* Unix-time that the bot loaded up */
120     int make_userfile = 0; /* Using bot in make-userfile mode? (first
121     user to 'hello' becomes master) */
122     char owner[121] = ""; /* Permanent owner(s) of the bot */
123 guppy 1.92 char pid_file[120]; /* Name of the file for the pid to be
124 fabian 1.27 stored in */
125     int save_users_at = 0; /* How many minutes past the hour to
126     save the userfile? */
127     int notify_users_at = 0; /* How many minutes past the hour to
128     notify users of notes? */
129     char version[81]; /* Version info (long form) */
130     char ver[41]; /* Version info (short form) */
131     char egg_xtra[2048]; /* Patch info */
132     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
133     int do_restart = 0; /* .restart has been called, restart asap */
134     int die_on_sighup = 0; /* die if bot receives SIGHUP */
135 guppy 1.52 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
136 fabian 1.27 int resolve_timeout = 15; /* hostname/address lookup timeout */
137 guppy 1.72 char quit_msg[1024]; /* quit message */
138 fabian 1.27 time_t now; /* duh, now :) */
139 stdarg 1.108 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
140 fabian 1.27
141 fabian 1.44 void fatal(const char *s, int recoverable)
142 segfault 1.1 {
143     int i;
144    
145     putlog(LOG_MISC, "*", "* %s", s);
146     flushlogs();
147     for (i = 0; i < dcc_total; i++)
148 fabian 1.19 if (dcc[i].sock >= 0)
149 fabian 1.15 killsock(dcc[i].sock);
150 segfault 1.1 unlink(pid_file);
151 fabian 1.43 if (!recoverable) {
152     bg_send_quit(BG_ABORT);
153 segfault 1.1 exit(1);
154 fabian 1.43 }
155 segfault 1.1 }
156    
157     static void check_expired_dcc()
158     {
159     int i;
160    
161     for (i = 0; i < dcc_total; i++)
162     if (dcc[i].type && dcc[i].type->timeout_val &&
163     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
164     if (dcc[i].type->timeout)
165     dcc[i].type->timeout(i);
166     else if (dcc[i].type->eof)
167     dcc[i].type->eof(i);
168     else
169     continue;
170 fabian 1.27 /* Only timeout 1 socket per cycle, too risky for more */
171 segfault 1.1 return;
172     }
173     }
174    
175     static void got_bus(int z)
176     {
177     fatal("BUS ERROR -- CRASHING!", 1);
178     #ifdef SA_RESETHAND
179     kill(getpid(), SIGBUS);
180     #else
181 fabian 1.43 bg_send_quit(BG_ABORT);
182 segfault 1.1 exit(1);
183     #endif
184     }
185    
186     static void got_segv(int z)
187     {
188     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
189     #ifdef SA_RESETHAND
190     kill(getpid(), SIGSEGV);
191     #else
192 fabian 1.43 bg_send_quit(BG_ABORT);
193 segfault 1.1 exit(1);
194     #endif
195     }
196    
197     static void got_fpe(int z)
198     {
199     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
200     }
201    
202     static void got_term(int z)
203     {
204     write_userfile(-1);
205 stdarg 1.85 check_bind_event("sigterm");
206 segfault 1.1 if (die_on_sigterm) {
207     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
208     } else {
209     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
210     }
211     }
212    
213     static void got_quit(int z)
214     {
215 stdarg 1.85 check_bind_event("sigquit");
216 segfault 1.1 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
217     return;
218     }
219    
220     static void got_hup(int z)
221     {
222     write_userfile(-1);
223 stdarg 1.85 check_bind_event("sighup");
224 segfault 1.1 if (die_on_sighup) {
225     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
226     } else
227     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
228     do_restart = -2;
229     return;
230     }
231    
232 fabian 1.27 /* A call to resolver (gethostbyname, etc) timed out
233     */
234 segfault 1.1 static void got_alarm(int z)
235     {
236     longjmp(alarmret, 1);
237 fabian 1.27
238     /* -Never reached- */
239 segfault 1.1 }
240    
241 guppy 1.101 /* Got ILL signal
242 fabian 1.27 */
243 segfault 1.1 static void got_ill(int z)
244     {
245 stdarg 1.85 check_bind_event("sigill");
246 fabian 1.10 }
247    
248 ite 1.114
249     static void print_version (void)
250 segfault 1.1 {
251 guppy 1.49 char x[1024], *z = x;
252 ite 1.114
253     strlcpy(x, egg_version, sizeof x);
254     newsplit(&z);
255     newsplit(&z);
256     printf("%s\n", version);
257     if (z[0])
258     printf(" (patches: %s)\n", z);
259     }
260    
261     static void print_help (void)
262     {
263     print_version ();
264     printf("\n\
265     Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
266     printf(_("\n\
267     -h, --help Print help and exit\n\
268     -v, --version Print version and exit\n\
269     -n, --foreground Don't go into the background (default=off)\n\
270     -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
271     -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
272     -m --make-userfile Userfile creation mode\n\
273     -p --load-module=STRING Modules to load before parsing any configuration\n\
274     file\n\
275     FILE optional config filename (default 'eggdrop.conf')\n"));
276     printf("\n");
277     }
278 segfault 1.1
279 ite 1.114 static void do_args(int argc, char * const *argv)
280     {
281     int c;
282    
283     optarg = 0;
284     optind = 1;
285     opterr = 1;
286     optopt = '?';
287    
288     while (1) {
289     int option_index = 0;
290     static struct option long_options[] = {
291     { "help", 0, NULL, 'h' },
292     { "version", 0, NULL, 'v' },
293     { "load-module", 1, NULL, 'p' },
294     { "foreground", 0, NULL, 'n' },
295     { "channel-stats", 0, NULL, 'c' },
296     { "terminal", 0, NULL, 't' },
297     { "make-userfile", 0, NULL, 'm' },
298     { NULL, 0, NULL, 0 }
299     };
300    
301     c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
302    
303     if (c == -1) break;
304    
305     switch (c) {
306     case 'n':
307 segfault 1.1 backgrd = 0;
308 ite 1.114 break;
309    
310     case 'c':
311 segfault 1.1 con_chan = 1;
312     term_z = 0;
313 ite 1.114 break;
314    
315     case 't':
316 segfault 1.1 con_chan = 0;
317     term_z = 1;
318 ite 1.114 break;
319    
320     case 'm':
321 segfault 1.1 make_userfile = 1;
322 ite 1.114 break;
323    
324     case 'p':
325     strlcpy(preload_module, optarg, sizeof preload_module);
326     break;
327    
328     case 'v':
329     print_version();
330 fabian 1.43 bg_send_quit(BG_ABORT);
331 segfault 1.1 exit(0);
332 ite 1.114 break; /* this should never be reached */
333    
334     case 'h':
335     print_help();
336 fabian 1.43 bg_send_quit(BG_ABORT);
337 segfault 1.1 exit(0);
338 ite 1.114 break; /* this should never be reached */
339    
340     case 0: /* Long option with no short option */
341     case '?': /* Invalid option. */
342     /* getopt_long() already printed an error message. */
343     bg_send_quit(BG_ABORT);
344     exit(1);
345    
346     default: /* bug: option not considered. */
347     fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
348     bg_send_quit(BG_ABORT);
349     exit(1);
350     break; /* this should never be reached */
351     }
352     }
353     if (optind < argc)
354     strlcpy(configfile, argv[optind], sizeof configfile);
355 segfault 1.1 }
356    
357 fabian 1.27 /* Timer info */
358     static int lastmin = 99;
359     static struct tm nowtm;
360 segfault 1.1
361 fabian 1.34 /* Called once a second.
362 fabian 1.27 */
363 segfault 1.1 static void core_secondly()
364     {
365     static int cnt = 0;
366     int miltime;
367    
368 stdarg 1.82 call_hook(HOOK_SECONDLY); /* Will be removed later */
369 segfault 1.1 cnt++;
370 fabian 1.27 if (cnt >= 10) { /* Every 10 seconds */
371 segfault 1.1 cnt = 0;
372     check_expired_dcc();
373     if (con_chan && !backgrd) {
374     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
375     tell_verbose_status(DP_STDOUT);
376     do_module_report(DP_STDOUT, 0, "server");
377     do_module_report(DP_STDOUT, 0, "channels");
378     }
379     }
380 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
381 fabian 1.17 if (nowtm.tm_min != lastmin) {
382 segfault 1.1 int i = 0;
383    
384 fabian 1.27 /* Once a minute */
385 segfault 1.1 lastmin = (lastmin + 1) % 60;
386     call_hook(HOOK_MINUTELY);
387     check_expired_ignores();
388 fabian 1.27 /* In case for some reason more than 1 min has passed: */
389 fabian 1.17 while (nowtm.tm_min != lastmin) {
390 fabian 1.27 /* Timer drift, dammit */
391 fabian 1.17 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
392 segfault 1.1 i++;
393     lastmin = (lastmin + 1) % 60;
394     call_hook(HOOK_MINUTELY);
395     }
396     if (i > 1)
397     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
398 fabian 1.17 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
399     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
400 segfault 1.1 call_hook(HOOK_5MINUTELY);
401 guppy 1.49 if (!miltime) { /* At midnight */
402     char s[25];
403 segfault 1.1
404 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
405 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
406 guppy 1.69 call_hook(HOOK_BACKUP);
407 stdarg 1.90 call_hook(HOOK_DAILY);
408 segfault 1.1 }
409     }
410 fabian 1.17 if (nowtm.tm_min == notify_users_at)
411 segfault 1.1 call_hook(HOOK_HOURLY);
412     }
413     }
414    
415     static void core_minutely()
416     {
417 stdarg 1.85 check_bind_time(&nowtm);
418 segfault 1.1 }
419    
420     static void core_hourly()
421     {
422     write_userfile(-1);
423     }
424    
425     static void event_rehash()
426     {
427 stdarg 1.85 check_bind_event("rehash");
428 segfault 1.1 }
429    
430     static void event_prerehash()
431     {
432 stdarg 1.85 check_bind_event("prerehash");
433 segfault 1.1 }
434    
435     static void event_save()
436     {
437 stdarg 1.85 check_bind_event("save");
438 segfault 1.1 }
439    
440     static void event_logfile()
441     {
442 stdarg 1.85 check_bind_event("logfile");
443 segfault 1.1 }
444    
445 guppy 1.61 static void event_loaded()
446     {
447 stdarg 1.85 check_bind_event("loaded");
448 guppy 1.61 }
449    
450 segfault 1.1 void kill_tcl();
451     extern module_entry *module_list;
452    
453 tothwolf 1.97 int init_userent(), init_net(),
454 ite 1.109 init_tcl(int, char **);
455 stdarg 1.88 void dns_init();
456 stdarg 1.85 void binds_init();
457 stdarg 1.94 void dcc_init();
458 fabian 1.11
459 fabian 1.44 void patch(const char *str)
460 fabian 1.11 {
461     char *p = strchr(egg_version, '+');
462    
463     if (!p)
464     p = &egg_version[strlen(egg_version)];
465     sprintf(p, "+%s", str);
466     egg_numver++;
467     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
468     }
469 segfault 1.1
470 stdarg 1.100 /*
471 fabian 1.45 static inline void garbage_collect(void)
472     {
473     static u_8bit_t run_cnt = 0;
474    
475     if (run_cnt == 3)
476     garbage_collect_tclhash();
477     else
478     run_cnt++;
479     }
480 stdarg 1.100 */
481 fabian 1.45
482 segfault 1.1 int main(int argc, char **argv)
483     {
484     int xx, i;
485 guppy 1.49 char buf[520], s[25];
486 segfault 1.1 FILE *f;
487     struct sigaction sv;
488     struct chanset_t *chan;
489 stdarg 1.82 egg_timeval_t howlong;
490 segfault 1.1
491 tothwolf 1.81 #ifdef DEBUG
492 fabian 1.21 /* Make sure it can write core, if you make debug. Else it's pretty
493     * useless (dw)
494     */
495 fabian 1.15 {
496     #include <sys/resource.h>
497     struct rlimit cdlim;
498 fabian 1.21
499 fabian 1.15 cdlim.rlim_cur = RLIM_INFINITY;
500     cdlim.rlim_max = RLIM_INFINITY;
501 fabian 1.32 setrlimit(RLIMIT_CORE, &cdlim);
502 fabian 1.15 }
503     #endif
504 fabian 1.32
505 ite 1.99 #ifdef ENABLE_NLS
506 ite 1.76 setlocale(LC_MESSAGES, "");
507     bindtextdomain(PACKAGE, LOCALEDIR);
508     textdomain(PACKAGE);
509 ite 1.99 #endif
510 ite 1.76
511 segfault 1.1 #include "patch.h"
512 fabian 1.27 /* Version info! */
513 ite 1.114 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
514 wcc 1.106 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2002 Eggheads",
515 guppy 1.56 egg_version);
516 fabian 1.27 /* Now add on the patchlevel (for Tcl) */
517 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
518 segfault 1.1 strcat(egg_version, egg_xtra);
519     #ifdef STOP_UAC
520     {
521     int nvpair[2];
522    
523     nvpair[0] = SSIN_UACPROC;
524     nvpair[1] = UAC_NOPRINT;
525     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
526     }
527     #endif
528 fabian 1.27
529     /* Set up error traps: */
530 segfault 1.1 sv.sa_handler = got_bus;
531     sigemptyset(&sv.sa_mask);
532     #ifdef SA_RESETHAND
533     sv.sa_flags = SA_RESETHAND;
534     #else
535     sv.sa_flags = 0;
536     #endif
537     sigaction(SIGBUS, &sv, NULL);
538     sv.sa_handler = got_segv;
539     sigaction(SIGSEGV, &sv, NULL);
540     #ifdef SA_RESETHAND
541     sv.sa_flags = 0;
542     #endif
543     sv.sa_handler = got_fpe;
544     sigaction(SIGFPE, &sv, NULL);
545     sv.sa_handler = got_term;
546     sigaction(SIGTERM, &sv, NULL);
547     sv.sa_handler = got_hup;
548     sigaction(SIGHUP, &sv, NULL);
549     sv.sa_handler = got_quit;
550     sigaction(SIGQUIT, &sv, NULL);
551     sv.sa_handler = SIG_IGN;
552     sigaction(SIGPIPE, &sv, NULL);
553     sv.sa_handler = got_ill;
554     sigaction(SIGILL, &sv, NULL);
555     sv.sa_handler = got_alarm;
556     sigaction(SIGALRM, &sv, NULL);
557 fabian 1.27
558     /* Initialize variables and stuff */
559 stdarg 1.121 timer_update_now(&egg_timeval_now);
560 stdarg 1.108 now = egg_timeval_now.sec;
561 segfault 1.1 chanset = NULL;
562 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
563 fabian 1.17 lastmin = nowtm.tm_min;
564 poptix 1.63 srandom(now % (getpid() + getppid()));
565 ite 1.114
566     do_args(argc, argv);
567    
568 segfault 1.1 printf("\n%s\n", version);
569 guppy 1.93 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
570 guppy 1.53
571     /* Don't allow eggdrop to run as root */
572     if (((int) getuid() == 0) || ((int) geteuid() == 0))
573 ite 1.76 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
574 guppy 1.53
575 ite 1.109 egg = eggdrop_new();
576 ite 1.110 script_init();
577 stdarg 1.98 binds_init();
578 ite 1.109 modules_init();
579 stdarg 1.90 logfile_init();
580 stdarg 1.89 timer_init();
581 stdarg 1.88 dns_init();
582 stdarg 1.121 egg_dns_init();
583 stdarg 1.85 core_binds_init();
584 stdarg 1.94 dcc_init();
585 stdarg 1.112 user_init();
586 segfault 1.1 init_userent();
587     init_net();
588 wingman 1.119 traffic_init();
589 stdarg 1.80
590 fabian 1.43 if (backgrd)
591     bg_prepare_split();
592 stdarg 1.80
593 fabian 1.8 init_tcl(argc, argv);
594 stdarg 1.80
595 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
596 segfault 1.1 strcpy(&s[11], &s[20]);
597     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
598 ite 1.113 if (preload_module[0])
599     module_load(preload_module);
600 segfault 1.1 chanprog();
601 guppy 1.49 if (!encrypt_pass) {
602 ite 1.73 printf(_("You have installed modules but have not selected an encryption\n\
603     module, please consult the default config file for info.\n"));
604 fabian 1.43 bg_send_quit(BG_ABORT);
605 segfault 1.1 exit(1);
606     }
607     i = 0;
608     for (chan = chanset; chan; chan = chan->next)
609     i++;
610 fabian 1.4 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
611 segfault 1.1 botnetnick, i, count_users(userlist));
612     cache_miss = 0;
613     cache_hit = 0;
614 guppy 1.92
615     if (!pid_file[0])
616     snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
617 fabian 1.27
618     /* Check for pre-existing eggdrop! */
619 segfault 1.1 f = fopen(pid_file, "r");
620     if (f != NULL) {
621     fgets(s, 10, f);
622     xx = atoi(s);
623 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
624     is used */
625 segfault 1.1 if (errno != ESRCH) {
626 guppy 1.103 printf(_("I detect %s already running from this directory.\n"), botnetnick);
627 ite 1.73 printf(_("If this is incorrect, erase the %s\n"), pid_file);
628 fabian 1.43 bg_send_quit(BG_ABORT);
629 segfault 1.1 exit(1);
630     }
631     }
632 fabian 1.27
633     /* Move into background? */
634 segfault 1.1 if (backgrd) {
635 fabian 1.38 #ifndef CYGWIN_HACKS
636 fabian 1.43 bg_do_split();
637 fabian 1.38 } else { /* !backgrd */
638 fabian 1.5 #endif
639 fabian 1.38 xx = getpid();
640     if (xx != 0) {
641     FILE *fp;
642    
643     /* Write pid to file */
644     unlink(pid_file);
645     fp = fopen(pid_file, "w");
646     if (fp != NULL) {
647     fprintf(fp, "%u\n", xx);
648     if (fflush(fp)) {
649     /* Let the bot live since this doesn't appear to be a botchk */
650 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
651 fabian 1.38 fclose(fp);
652     unlink(pid_file);
653     } else
654     fclose(fp);
655 fabian 1.27 } else
656 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
657 fabian 1.38 #ifdef CYGWIN_HACKS
658 guppy 1.68 printf("Launched into the background (pid: %d)\n\n", xx);
659 fabian 1.38 #endif
660     }
661 segfault 1.1 }
662 fabian 1.38
663     use_stderr = 0; /* Stop writing to stderr now */
664 segfault 1.1 if (backgrd) {
665 fabian 1.27 /* Ok, try to disassociate from controlling terminal (finger cross) */
666 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
667 segfault 1.1 setpgid(0, 0);
668     #endif
669 fabian 1.38 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
670 segfault 1.1 freopen("/dev/null", "r", stdin);
671     freopen("/dev/null", "w", stdout);
672     freopen("/dev/null", "w", stderr);
673 poptix 1.62 #ifdef CYGWIN_HACKS
674     FreeConsole();
675     #endif
676 segfault 1.1 }
677 fabian 1.27
678     /* Terminal emulating dcc chat */
679 fabian 1.42 if (!backgrd && term_z) {
680 segfault 1.1 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
681    
682 drummer 1.71 dcc[n].addr[0] = '\0';
683 segfault 1.1 dcc[n].sock = STDOUT;
684     dcc[n].timeval = now;
685     dcc[n].u.chat->con_flags = conmask;
686     dcc[n].status = STAT_ECHO;
687     strcpy(dcc[n].nick, "HQ");
688     strcpy(dcc[n].host, "llama@console");
689 ite 1.107 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
690 fabian 1.27 /* Make sure there's an innocuous HQ user if needed */
691 segfault 1.1 if (!dcc[n].user) {
692 ite 1.107 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
693     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
694 segfault 1.1 }
695 fabian 1.27 setsock(STDOUT, 0); /* Entry in net table */
696 segfault 1.1 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
697     dcc_chatter(n);
698     }
699 fabian 1.27
700 segfault 1.1 online_since = now;
701     add_help_reference("cmds1.help");
702     add_help_reference("cmds2.help");
703     add_help_reference("core.help");
704 stdarg 1.82 howlong.sec = 1;
705     howlong.usec = 0;
706     timer_create_repeater(&howlong, (Function) core_secondly);
707 wingman 1.119
708     /* init time'd hooks */
709 fabian 1.22 add_hook(HOOK_MINUTELY, (Function) core_minutely);
710     add_hook(HOOK_HOURLY, (Function) core_hourly);
711     add_hook(HOOK_REHASH, (Function) event_rehash);
712     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
713     add_hook(HOOK_USERFILE, (Function) event_save);
714 guppy 1.69 add_hook(HOOK_BACKUP, (Function) backup_userfile);
715 fabian 1.22 add_hook(HOOK_DAILY, (Function) event_logfile);
716 guppy 1.61 add_hook(HOOK_LOADED, (Function) event_loaded);
717    
718     call_hook(HOOK_LOADED);
719 segfault 1.1
720     debug0("main: entering loop");
721     while (1) {
722     int socket_cleanup = 0;
723    
724 fabian 1.43 #if !defined(HAVE_PRE7_5_TCL)
725 fabian 1.27 /* Process a single tcl event */
726 segfault 1.1 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
727 fabian 1.5 #endif
728 fabian 1.45
729 fabian 1.27 /* Lets move some of this here, reducing the numer of actual
730     * calls to periodic_timers
731     */
732 stdarg 1.108 timer_get_time(&egg_timeval_now);
733     now = egg_timeval_now.sec;
734 fabian 1.27 random(); /* Woop, lets really jumble things */
735 stdarg 1.82 timer_run();
736 fabian 1.45
737     /* Only do this every so often. */
738 segfault 1.1 if (!socket_cleanup) {
739 fabian 1.45 socket_cleanup = 5;
740    
741     /* Remove dead dcc entries. */
742 fabian 1.16 dcc_remove_lost();
743 fabian 1.45
744     /* Check for server or dcc activity. */
745 segfault 1.1 dequeue_sockets();
746     } else
747     socket_cleanup--;
748 fabian 1.45
749     /* Free unused structures. */
750 stdarg 1.100 /* garbage_collect(); */
751 fabian 1.45
752 stdarg 1.121 sockbuf_update_all(0);
753 segfault 1.1 xx = sockgets(buf, &i);
754 stdarg 1.121 timer_update_now(&egg_timeval_now);
755 fabian 1.27 if (xx >= 0) { /* Non-error */
756 segfault 1.1 int idx;
757    
758     for (idx = 0; idx < dcc_total; idx++)
759     if (dcc[idx].sock == xx) {
760 fabian 1.13 if (dcc[idx].type && dcc[idx].type->activity) {
761 wingman 1.119
762     /* update traffic stats */
763     traffic_update_in(dcc[idx].type, (strlen(buf) + 1));
764    
765 guppy 1.56 dcc[idx].type->activity(idx, buf, i);
766 fabian 1.13 } else
767 segfault 1.1 putlog(LOG_MISC, "*",
768     "!!! untrapped dcc activity: type %s, sock %d",
769     dcc[idx].type->name, dcc[idx].sock);
770     break;
771     }
772     } else if (xx == -1) { /* EOF from someone */
773     int idx;
774    
775 fabian 1.45 if (i == STDOUT && !backgrd)
776 segfault 1.1 fatal("END OF FILE ON TERMINAL", 0);
777     for (idx = 0; idx < dcc_total; idx++)
778     if (dcc[idx].sock == i) {
779     if (dcc[idx].type && dcc[idx].type->eof)
780     dcc[idx].type->eof(idx);
781     else {
782     putlog(LOG_MISC, "*",
783     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
784 fabian 1.2 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
785     killsock(i);
786 segfault 1.1 lostdcc(idx);
787     }
788     idx = dcc_total + 1;
789     }
790     if (idx == dcc_total) {
791     putlog(LOG_MISC, "*",
792 fabian 1.2 "(@) EOF socket %d, not a dcc socket, not anything.", i);
793     close(i);
794     killsock(i);
795 segfault 1.1 }
796 fabian 1.45 } else if (xx == -2 && errno != EINTR) { /* select() error */
797 segfault 1.1 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
798     for (i = 0; i < dcc_total; i++) {
799     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
800     putlog(LOG_MISC, "*",
801     "DCC socket %d (type %d, name '%s') expired -- pfft",
802     dcc[i].sock, dcc[i].type, dcc[i].nick);
803     killsock(dcc[i].sock);
804     lostdcc(i);
805     i--;
806     }
807     }
808 fabian 1.45 } else if (xx == -3) {
809 segfault 1.1 call_hook(HOOK_IDLE);
810 fabian 1.27 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
811 segfault 1.1 }
812 fabian 1.27
813 segfault 1.1 if (do_restart) {
814     if (do_restart == -2)
815     rehash();
816     else {
817 fabian 1.27 /* Unload as many modules as possible */
818 segfault 1.1 int f = 1;
819     module_entry *p;
820     Function x;
821     char xx[256];
822 guppy 1.59
823     /* oops, I guess we should call this event before tcl is restarted */
824 stdarg 1.85 check_bind_event("prerestart");
825 segfault 1.1
826     while (f) {
827     f = 0;
828     for (p = module_list; p != NULL; p = p->next) {
829     dependancy *d = dependancy_list;
830     int ok = 1;
831    
832     while (ok && d) {
833     if (d->needed == p)
834     ok = 0;
835     d = d->next;
836     }
837     if (ok) {
838     strcpy(xx, p->name);
839 guppy 1.103 if (module_unload(xx, botnetnick) == NULL) {
840 segfault 1.1 f = 1;
841     break;
842     }
843     }
844     }
845     }
846     p = module_list;
847     if (p && p->next && p->next->next)
848 fabian 1.45 /* Should be only 2 modules now - blowfish (or some other
849     encryption module) and eggdrop. */
850 ite 1.73 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
851 segfault 1.1 flushlogs();
852     kill_tcl();
853 stdarg 1.87 timer_destroy_all(); /* Destroy all timers. */
854 fabian 1.8 init_tcl(argc, argv);
855 fabian 1.30 /* We expect the encryption module as the current module pointed
856     * to by `module_list'.
857     */
858 segfault 1.1 x = p->funcs[MODCALL_START];
859 fabian 1.30 /* `NULL' indicates that we just recently restarted. The module
860     * is expected to re-initialise as needed.
861     */
862     x(NULL);
863 segfault 1.1 rehash();
864 guppy 1.61 call_hook(HOOK_LOADED);
865 segfault 1.1 }
866     do_restart = 0;
867     }
868     }
869     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23