/[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.111 - (hide annotations) (download) (as text)
Mon Apr 1 13:33:33 2002 UTC (17 years, 4 months ago) by ite
Branch: MAIN
Changes since 1.110: +5 -5 lines
File MIME type: text/x-chdr
* Got rid of strncpyz() in favour of BSD's strlcpy(). Fallback function provided.
* BSD's strlcat() provided as fallback function.
* Fallback functions weren't linked as expected when needed.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23