/[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.125 - (hide annotations) (download) (as text)
Thu Oct 10 04:41:59 2002 UTC (16 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.124: +2 -2 lines
File MIME type: text/x-chdr
* Moved bind tables to libeggdrop

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23