/[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.138 - (hide annotations) (download) (as text)
Tue Feb 18 10:37:18 2003 UTC (16 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.137: +3 -125 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.138 static const char rcsid[] = "$Id: main.c,v 1.137 2003/02/18 10:13:17 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 stdarg 1.133 #include "core_config.h"
39 fabian 1.27 #include <fcntl.h>
40 wcc 1.129 #ifdef TIME_WITH_SYS_TIME
41     # include <sys/time.h>
42     # include <time.h>
43     #else
44     # ifdef HAVE_SYS_TIME_H
45     # include <sys/time.h>
46     # else
47     # include <time.h>
48     # endif
49     #endif
50 segfault 1.1 #include <errno.h>
51     #include <signal.h>
52     #include <netdb.h>
53     #include <setjmp.h>
54 wingman 1.116 #include "users.h" /* check_expired_ignores,
55     get_user_by_handle */
56     #include "chanprog.h" /* tell_verbose_status,
57     chanprog, rehash */
58     #include "userrec.h" /* write_userfile, count_users */
59 ite 1.76 #include <locale.h>
60    
61 fabian 1.27 #ifdef STOP_UAC /* osf/1 complains a lot */
62 segfault 1.1 #include <sys/sysinfo.h>
63     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
64     #endif
65 fabian 1.27 /* Some systems have a working sys/wait.h even though configure will
66     * decide it's not bsd compatable. Oh well.
67     */
68 guppy 1.49
69 segfault 1.1 #include "chan.h"
70     #include "modules.h"
71 fabian 1.43 #include "bg.h"
72 stdarg 1.85 #include "core_binds.h"
73 ite 1.105 #include "logfile.h"
74     #include "misc.h"
75 wingman 1.119 #include "traffic.h"
76 wingman 1.116 #include "dccutil.h" /* dprintf_eggdrop, new_dcc,
77     dcc_chatter, dcc_remove_lost,
78     lostdcc */
79 fabian 1.38
80 ite 1.96 #include "lib/adns/adns.h"
81 drummer 1.71
82 poptix 1.62 #ifdef CYGWIN_HACKS
83     #include <windows.h>
84     #endif
85    
86 segfault 1.1 #ifndef _POSIX_SOURCE
87 fabian 1.27 /* Solaris needs this */
88 segfault 1.1 #define _POSIX_SOURCE 1
89     #endif
90    
91 wcc 1.132 extern char userfile[], myname[];
92     extern int dcc_total, conmask, cache_hit, cache_miss;
93     extern struct dcc_t *dcc;
94     extern struct userrec *userlist;
95 fabian 1.27 extern struct chanset_t *chanset;
96 wcc 1.132 extern jmp_buf alarmret;
97 fabian 1.27
98 wingman 1.116 #ifndef MAKING_MODS
99     extern struct dcc_table DCC_CHAT;
100     #endif /* MAKING_MODS */
101 segfault 1.1
102 guppy 1.56 /*
103 segfault 1.1 * Please use the PATCH macro instead of directly altering the version
104     * string from now on (it makes it much easier to maintain patches).
105     * Also please read the README file regarding your rights to distribute
106     * modified versions of this bot.
107     */
108    
109 ite 1.76 char egg_version[1024] = VERSION;
110     int egg_numver = VERSION_NUM;
111 segfault 1.1
112 ite 1.109 eggdrop_t *egg = NULL; /* Eggdrop's context */
113    
114 fabian 1.27 char notify_new[121] = ""; /* Person to send a note to for new users */
115     int default_flags = 0; /* Default user flags and */
116     int default_uflags = 0; /* Default userdefinied flags for people
117     who say 'hello' or for .adduser */
118    
119     int backgrd = 1; /* Run in the background? */
120     int con_chan = 0; /* Foreground: constantly display channel
121     stats? */
122     int term_z = 0; /* Foreground: use the terminal as a party
123     line? */
124 stdarg 1.134 char configfile[121] = "config.xml"; /* Name of the config file */
125 wcc 1.127 char helpdir[121] = "help/"; /* Directory of help files (if used) */
126     char textdir[121] = "text/"; /* Directory for text files that get dumped */
127 fabian 1.27 time_t online_since; /* Unix-time that the bot loaded up */
128     int make_userfile = 0; /* Using bot in make-userfile mode? (first
129     user to 'hello' becomes master) */
130     char owner[121] = ""; /* Permanent owner(s) of the bot */
131 guppy 1.92 char pid_file[120]; /* Name of the file for the pid to be
132 fabian 1.27 stored in */
133     int save_users_at = 0; /* How many minutes past the hour to
134     save the userfile? */
135     int notify_users_at = 0; /* How many minutes past the hour to
136     notify users of notes? */
137     char version[81]; /* Version info (long form) */
138     char ver[41]; /* Version info (short form) */
139     char egg_xtra[2048]; /* Patch info */
140     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
141     int do_restart = 0; /* .restart has been called, restart asap */
142     int die_on_sighup = 0; /* die if bot receives SIGHUP */
143 guppy 1.52 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
144 fabian 1.27 int resolve_timeout = 15; /* hostname/address lookup timeout */
145 guppy 1.72 char quit_msg[1024]; /* quit message */
146 fabian 1.27 time_t now; /* duh, now :) */
147 stdarg 1.108 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
148 fabian 1.27
149 fabian 1.44 void fatal(const char *s, int recoverable)
150 segfault 1.1 {
151     putlog(LOG_MISC, "*", "* %s", s);
152     flushlogs();
153     unlink(pid_file);
154 fabian 1.43 if (!recoverable) {
155     bg_send_quit(BG_ABORT);
156 segfault 1.1 exit(1);
157 fabian 1.43 }
158 segfault 1.1 }
159    
160     static void got_bus(int z)
161     {
162     fatal("BUS ERROR -- CRASHING!", 1);
163     #ifdef SA_RESETHAND
164     kill(getpid(), SIGBUS);
165     #else
166 fabian 1.43 bg_send_quit(BG_ABORT);
167 segfault 1.1 exit(1);
168     #endif
169     }
170    
171     static void got_segv(int z)
172     {
173     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
174     #ifdef SA_RESETHAND
175     kill(getpid(), SIGSEGV);
176     #else
177 fabian 1.43 bg_send_quit(BG_ABORT);
178 segfault 1.1 exit(1);
179     #endif
180     }
181    
182     static void got_fpe(int z)
183     {
184     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
185     }
186    
187     static void got_term(int z)
188     {
189     write_userfile(-1);
190 stdarg 1.85 check_bind_event("sigterm");
191 segfault 1.1 if (die_on_sigterm) {
192     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
193     } else {
194     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
195     }
196     }
197    
198     static void got_quit(int z)
199     {
200 stdarg 1.85 check_bind_event("sigquit");
201 segfault 1.1 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
202     return;
203     }
204    
205     static void got_hup(int z)
206     {
207     write_userfile(-1);
208 stdarg 1.85 check_bind_event("sighup");
209 segfault 1.1 if (die_on_sighup) {
210     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
211     } else
212     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
213     do_restart = -2;
214     return;
215     }
216    
217 fabian 1.27 /* A call to resolver (gethostbyname, etc) timed out
218     */
219 segfault 1.1 static void got_alarm(int z)
220     {
221     longjmp(alarmret, 1);
222 fabian 1.27
223     /* -Never reached- */
224 segfault 1.1 }
225    
226 guppy 1.101 /* Got ILL signal
227 fabian 1.27 */
228 segfault 1.1 static void got_ill(int z)
229     {
230 stdarg 1.85 check_bind_event("sigill");
231 fabian 1.10 }
232    
233 ite 1.114
234 wcc 1.135 static void print_version(void)
235 segfault 1.1 {
236 guppy 1.49 char x[1024], *z = x;
237 ite 1.114
238     strlcpy(x, egg_version, sizeof x);
239     newsplit(&z);
240     newsplit(&z);
241     printf("%s\n", version);
242     if (z[0])
243     printf(" (patches: %s)\n", z);
244     }
245    
246 wcc 1.135 static void print_help(void)
247 ite 1.114 {
248     print_version ();
249     printf("\n\
250     Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
251     printf(_("\n\
252     -h, --help Print help and exit\n\
253     -v, --version Print version and exit\n\
254     -n, --foreground Don't go into the background (default=off)\n\
255     -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
256     -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
257     -m --make-userfile Userfile creation mode\n\
258 wcc 1.135 FILE optional config filename (default 'config.xml')\n"));
259 ite 1.114 printf("\n");
260     }
261 segfault 1.1
262 ite 1.114 static void do_args(int argc, char * const *argv)
263     {
264     int c;
265    
266     optarg = 0;
267     optind = 1;
268     opterr = 1;
269     optopt = '?';
270    
271     while (1) {
272     int option_index = 0;
273     static struct option long_options[] = {
274     { "help", 0, NULL, 'h' },
275     { "version", 0, NULL, 'v' },
276     { "load-module", 1, NULL, 'p' },
277     { "foreground", 0, NULL, 'n' },
278     { "channel-stats", 0, NULL, 'c' },
279     { "terminal", 0, NULL, 't' },
280     { "make-userfile", 0, NULL, 'm' },
281     { NULL, 0, NULL, 0 }
282     };
283    
284     c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
285    
286     if (c == -1) break;
287    
288     switch (c) {
289     case 'n':
290 segfault 1.1 backgrd = 0;
291 ite 1.114 break;
292    
293     case 'c':
294 segfault 1.1 con_chan = 1;
295     term_z = 0;
296 ite 1.114 break;
297    
298     case 't':
299 segfault 1.1 con_chan = 0;
300     term_z = 1;
301 ite 1.114 break;
302    
303     case 'm':
304 segfault 1.1 make_userfile = 1;
305 ite 1.114 break;
306    
307     case 'v':
308     print_version();
309 fabian 1.43 bg_send_quit(BG_ABORT);
310 segfault 1.1 exit(0);
311 ite 1.114 break; /* this should never be reached */
312    
313     case 'h':
314     print_help();
315 fabian 1.43 bg_send_quit(BG_ABORT);
316 segfault 1.1 exit(0);
317 ite 1.114 break; /* this should never be reached */
318    
319     case 0: /* Long option with no short option */
320     case '?': /* Invalid option. */
321     /* getopt_long() already printed an error message. */
322     bg_send_quit(BG_ABORT);
323     exit(1);
324    
325     default: /* bug: option not considered. */
326     fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
327     bg_send_quit(BG_ABORT);
328     exit(1);
329     break; /* this should never be reached */
330     }
331     }
332     if (optind < argc)
333     strlcpy(configfile, argv[optind], sizeof configfile);
334 segfault 1.1 }
335    
336 fabian 1.27 /* Timer info */
337     static int lastmin = 99;
338     static struct tm nowtm;
339 segfault 1.1
340 fabian 1.34 /* Called once a second.
341 fabian 1.27 */
342 segfault 1.1 static void core_secondly()
343     {
344     static int cnt = 0;
345     int miltime;
346    
347 stdarg 1.82 call_hook(HOOK_SECONDLY); /* Will be removed later */
348 segfault 1.1 cnt++;
349 fabian 1.27 if (cnt >= 10) { /* Every 10 seconds */
350 segfault 1.1 cnt = 0;
351     if (con_chan && !backgrd) {
352     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
353 stdarg 1.124 //tell_verbose_status(DP_STDOUT);
354 segfault 1.1 do_module_report(DP_STDOUT, 0, "server");
355     do_module_report(DP_STDOUT, 0, "channels");
356     }
357     }
358 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
359 fabian 1.17 if (nowtm.tm_min != lastmin) {
360 segfault 1.1 int i = 0;
361    
362 fabian 1.27 /* Once a minute */
363 segfault 1.1 lastmin = (lastmin + 1) % 60;
364     call_hook(HOOK_MINUTELY);
365     check_expired_ignores();
366 fabian 1.27 /* In case for some reason more than 1 min has passed: */
367 fabian 1.17 while (nowtm.tm_min != lastmin) {
368 fabian 1.27 /* Timer drift, dammit */
369 fabian 1.17 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
370 segfault 1.1 i++;
371     lastmin = (lastmin + 1) % 60;
372     call_hook(HOOK_MINUTELY);
373     }
374     if (i > 1)
375     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
376 fabian 1.17 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
377     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
378 segfault 1.1 call_hook(HOOK_5MINUTELY);
379 guppy 1.49 if (!miltime) { /* At midnight */
380     char s[25];
381 segfault 1.1
382 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
383 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
384 guppy 1.69 call_hook(HOOK_BACKUP);
385 stdarg 1.90 call_hook(HOOK_DAILY);
386 segfault 1.1 }
387     }
388 fabian 1.17 if (nowtm.tm_min == notify_users_at)
389 segfault 1.1 call_hook(HOOK_HOURLY);
390     }
391     }
392    
393     static void core_minutely()
394     {
395 stdarg 1.85 check_bind_time(&nowtm);
396 segfault 1.1 }
397    
398     static void core_hourly()
399     {
400     write_userfile(-1);
401     }
402    
403     static void event_rehash()
404     {
405 stdarg 1.85 check_bind_event("rehash");
406 segfault 1.1 }
407    
408     static void event_prerehash()
409     {
410 stdarg 1.85 check_bind_event("prerehash");
411 segfault 1.1 }
412    
413     static void event_save()
414     {
415 stdarg 1.85 check_bind_event("save");
416 segfault 1.1 }
417    
418     static void event_logfile()
419     {
420 stdarg 1.85 check_bind_event("logfile");
421 segfault 1.1 }
422    
423 guppy 1.61 static void event_loaded()
424     {
425 stdarg 1.85 check_bind_event("loaded");
426 guppy 1.61 }
427    
428 segfault 1.1 void kill_tcl();
429     extern module_entry *module_list;
430    
431 stdarg 1.130 int init_userent(), init_net(), init_tcl(int, char **);
432     void core_party_init();
433 stdarg 1.133 void core_config_init();
434 stdarg 1.130 void telnet_init();
435 stdarg 1.88 void dns_init();
436 stdarg 1.85 void binds_init();
437 stdarg 1.94 void dcc_init();
438 fabian 1.11
439 fabian 1.44 void patch(const char *str)
440 fabian 1.11 {
441     char *p = strchr(egg_version, '+');
442    
443     if (!p)
444     p = &egg_version[strlen(egg_version)];
445     sprintf(p, "+%s", str);
446     egg_numver++;
447     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
448     }
449 segfault 1.1
450 stdarg 1.100 /*
451 fabian 1.45 static inline void garbage_collect(void)
452     {
453     static u_8bit_t run_cnt = 0;
454    
455     if (run_cnt == 3)
456     garbage_collect_tclhash();
457     else
458     run_cnt++;
459     }
460 stdarg 1.100 */
461 fabian 1.45
462 segfault 1.1 int main(int argc, char **argv)
463     {
464     int xx, i;
465 stdarg 1.138 char s[25];
466 segfault 1.1 FILE *f;
467     struct sigaction sv;
468     struct chanset_t *chan;
469 stdarg 1.82 egg_timeval_t howlong;
470 stdarg 1.131 int timeout;
471 stdarg 1.137 void *config_root, *entry;
472 stdarg 1.134 char *modname, *scriptname;
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 ite 1.114 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
497 wcc 1.128 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 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.121 timer_update_now(&egg_timeval_now);
543 stdarg 1.108 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 ite 1.114
549     do_args(argc, argv);
550    
551 segfault 1.1 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 stdarg 1.136 config_init();
560 stdarg 1.134 core_config_init(configfile);
561 stdarg 1.130 egglog_init();
562     logfile_init();
563 ite 1.110 script_init();
564 stdarg 1.124 user_init();
565 stdarg 1.133 if (core_config.userfile) user_load(core_config.userfile);
566 stdarg 1.126 partyline_init();
567 stdarg 1.130 core_party_init();
568 stdarg 1.126 telnet_init();
569 ite 1.109 modules_init();
570 stdarg 1.88 dns_init();
571 stdarg 1.121 egg_dns_init();
572 stdarg 1.85 core_binds_init();
573 segfault 1.1 init_userent();
574 wingman 1.119 traffic_init();
575 stdarg 1.80
576 fabian 1.43 if (backgrd)
577     bg_prepare_split();
578 stdarg 1.80
579 fabian 1.8 init_tcl(argc, argv);
580 stdarg 1.80
581 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
582 segfault 1.1 strcpy(&s[11], &s[20]);
583     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
584 stdarg 1.134
585     /* Scan the autoload section of config. */
586     config_root = config_get_root("eggdrop");
587 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
588 stdarg 1.134 modname = NULL;
589 stdarg 1.137 config_get_str(&modname, entry, NULL);
590 stdarg 1.134 module_load(modname);
591     }
592 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
593 stdarg 1.134 scriptname = NULL;
594 stdarg 1.137 config_get_str(&scriptname, entry, NULL);
595 stdarg 1.134 script_load(scriptname);
596     }
597    
598    
599 segfault 1.1 i = 0;
600     for (chan = chanset; chan; chan = chan->next)
601     i++;
602 stdarg 1.134 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.", core_config.botname, i,
603 wcc 1.132 count_users(userlist));
604 segfault 1.1 cache_miss = 0;
605     cache_hit = 0;
606 guppy 1.92
607     if (!pid_file[0])
608 wcc 1.132 snprintf(pid_file, sizeof pid_file, "pid.%s", myname);
609 fabian 1.27
610     /* Check for pre-existing eggdrop! */
611 segfault 1.1 f = fopen(pid_file, "r");
612     if (f != NULL) {
613     fgets(s, 10, f);
614     xx = atoi(s);
615 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
616     is used */
617 segfault 1.1 if (errno != ESRCH) {
618 wcc 1.132 printf(_("I detect %s already running from this directory.\n"), myname);
619 ite 1.73 printf(_("If this is incorrect, erase the %s\n"), pid_file);
620 fabian 1.43 bg_send_quit(BG_ABORT);
621 segfault 1.1 exit(1);
622     }
623     }
624 fabian 1.27
625     /* Move into background? */
626 segfault 1.1 if (backgrd) {
627 fabian 1.38 #ifndef CYGWIN_HACKS
628 fabian 1.43 bg_do_split();
629 fabian 1.38 } else { /* !backgrd */
630 fabian 1.5 #endif
631 fabian 1.38 xx = getpid();
632     if (xx != 0) {
633     FILE *fp;
634    
635     /* Write pid to file */
636     unlink(pid_file);
637     fp = fopen(pid_file, "w");
638     if (fp != NULL) {
639     fprintf(fp, "%u\n", xx);
640     if (fflush(fp)) {
641     /* Let the bot live since this doesn't appear to be a botchk */
642 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
643 fabian 1.38 fclose(fp);
644     unlink(pid_file);
645     } else
646     fclose(fp);
647 fabian 1.27 } else
648 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
649 fabian 1.38 #ifdef CYGWIN_HACKS
650 guppy 1.68 printf("Launched into the background (pid: %d)\n\n", xx);
651 fabian 1.38 #endif
652     }
653 segfault 1.1 }
654 fabian 1.38
655     use_stderr = 0; /* Stop writing to stderr now */
656 segfault 1.1 if (backgrd) {
657 fabian 1.27 /* Ok, try to disassociate from controlling terminal (finger cross) */
658 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
659 segfault 1.1 setpgid(0, 0);
660     #endif
661 fabian 1.38 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
662 segfault 1.1 freopen("/dev/null", "r", stdin);
663     freopen("/dev/null", "w", stdout);
664     freopen("/dev/null", "w", stderr);
665 poptix 1.62 #ifdef CYGWIN_HACKS
666     FreeConsole();
667     #endif
668 segfault 1.1 }
669 fabian 1.27
670 segfault 1.1 online_since = now;
671     add_help_reference("cmds1.help");
672     add_help_reference("cmds2.help");
673     add_help_reference("core.help");
674 stdarg 1.82 howlong.sec = 1;
675     howlong.usec = 0;
676     timer_create_repeater(&howlong, (Function) core_secondly);
677 wingman 1.119
678     /* init time'd hooks */
679 fabian 1.22 add_hook(HOOK_MINUTELY, (Function) core_minutely);
680     add_hook(HOOK_HOURLY, (Function) core_hourly);
681     add_hook(HOOK_REHASH, (Function) event_rehash);
682     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
683     add_hook(HOOK_USERFILE, (Function) event_save);
684 guppy 1.69 add_hook(HOOK_BACKUP, (Function) backup_userfile);
685 fabian 1.22 add_hook(HOOK_DAILY, (Function) event_logfile);
686 guppy 1.61 add_hook(HOOK_LOADED, (Function) event_loaded);
687    
688     call_hook(HOOK_LOADED);
689 segfault 1.1
690     debug0("main: entering loop");
691     while (1) {
692 fabian 1.43 #if !defined(HAVE_PRE7_5_TCL)
693 fabian 1.27 /* Process a single tcl event */
694 segfault 1.1 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
695 fabian 1.5 #endif
696 fabian 1.45
697 fabian 1.27 /* Lets move some of this here, reducing the numer of actual
698     * calls to periodic_timers
699     */
700 stdarg 1.108 timer_get_time(&egg_timeval_now);
701     now = egg_timeval_now.sec;
702 fabian 1.27 random(); /* Woop, lets really jumble things */
703 stdarg 1.82 timer_run();
704 fabian 1.45
705     /* Free unused structures. */
706 stdarg 1.100 /* garbage_collect(); */
707 fabian 1.45
708 stdarg 1.131 if (timer_get_shortest(&howlong)) {
709     timeout = 1000;
710     }
711     else {
712     timeout = howlong.sec * 1000 + howlong.usec / 1000;
713     }
714     sockbuf_update_all(timeout);
715    
716 stdarg 1.138 timer_update_now(&egg_timeval_now);
717 fabian 1.27
718 segfault 1.1 if (do_restart) {
719     if (do_restart == -2)
720     rehash();
721     else {
722 fabian 1.27 /* Unload as many modules as possible */
723 segfault 1.1 int f = 1;
724     module_entry *p;
725     Function x;
726     char xx[256];
727 guppy 1.59
728     /* oops, I guess we should call this event before tcl is restarted */
729 stdarg 1.85 check_bind_event("prerestart");
730 segfault 1.1
731     while (f) {
732     f = 0;
733     for (p = module_list; p != NULL; p = p->next) {
734     dependancy *d = dependancy_list;
735     int ok = 1;
736    
737     while (ok && d) {
738     if (d->needed == p)
739     ok = 0;
740     d = d->next;
741     }
742     if (ok) {
743     strcpy(xx, p->name);
744 wcc 1.132 if (module_unload(xx, myname) == NULL) {
745 segfault 1.1 f = 1;
746     break;
747     }
748     }
749     }
750     }
751     p = module_list;
752     if (p && p->next && p->next->next)
753 fabian 1.45 /* Should be only 2 modules now - blowfish (or some other
754     encryption module) and eggdrop. */
755 ite 1.73 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
756 segfault 1.1 flushlogs();
757     kill_tcl();
758 stdarg 1.87 timer_destroy_all(); /* Destroy all timers. */
759 fabian 1.8 init_tcl(argc, argv);
760 fabian 1.30 /* We expect the encryption module as the current module pointed
761     * to by `module_list'.
762     */
763 segfault 1.1 x = p->funcs[MODCALL_START];
764 fabian 1.30 /* `NULL' indicates that we just recently restarted. The module
765     * is expected to re-initialise as needed.
766     */
767     x(NULL);
768 segfault 1.1 rehash();
769 guppy 1.61 call_hook(HOOK_LOADED);
770 segfault 1.1 }
771     do_restart = 0;
772     }
773     }
774     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23