/[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.118 - (hide annotations) (download) (as text)
Sun May 12 15:35:44 2002 UTC (17 years, 2 months ago) by ite
Branch: MAIN
Changes since 1.117: +1 -2 lines
File MIME type: text/x-chdr
* Now always preforking
* More minor configure macros cleanup
* Let's try to detect JS_NewObject() instead of JS_Now() for javascript library detection.
* Js headers to be corectly detected when --with-jsinc isn't passed to ./configure

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23