/[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.131 - (hide annotations) (download) (as text)
Mon Feb 3 06:42:40 2003 UTC (16 years, 6 months ago) by stdarg
Branch: MAIN
Changes since 1.130: +10 -2 lines
File MIME type: text/x-chdr
*** empty log message ***

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23