/[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.139 - (hide annotations) (download) (as text)
Tue Feb 25 06:52:19 2003 UTC (16 years, 6 months ago) by stdarg
Branch: MAIN
Changes since 1.138: +21 -99 lines
File MIME type: text/x-chdr
* Well, I think Tcl is finally removed from the core (except for detection/configuration).

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.139 static const char rcsid[] = "$Id: main.c,v 1.138 2003/02/18 10:37:18 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 stdarg 1.139 static int core_secondly()
343 segfault 1.1 {
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 stdarg 1.139 return(0);
392 segfault 1.1 }
393    
394     static void core_minutely()
395     {
396 stdarg 1.85 check_bind_time(&nowtm);
397 segfault 1.1 }
398    
399     static void core_hourly()
400     {
401     write_userfile(-1);
402     }
403    
404     static void event_rehash()
405     {
406 stdarg 1.85 check_bind_event("rehash");
407 segfault 1.1 }
408    
409     static void event_prerehash()
410     {
411 stdarg 1.85 check_bind_event("prerehash");
412 segfault 1.1 }
413    
414     static void event_save()
415     {
416 stdarg 1.85 check_bind_event("save");
417 segfault 1.1 }
418    
419     static void event_logfile()
420     {
421 stdarg 1.85 check_bind_event("logfile");
422 segfault 1.1 }
423    
424 guppy 1.61 static void event_loaded()
425     {
426 stdarg 1.85 check_bind_event("loaded");
427 guppy 1.61 }
428    
429 segfault 1.1 extern module_entry *module_list;
430    
431 stdarg 1.139 int init_userent(), init_net();
432 stdarg 1.130 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 fabian 1.11
438 fabian 1.44 void patch(const char *str)
439 fabian 1.11 {
440     char *p = strchr(egg_version, '+');
441    
442     if (!p)
443     p = &egg_version[strlen(egg_version)];
444     sprintf(p, "+%s", str);
445     egg_numver++;
446     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
447     }
448 segfault 1.1
449 stdarg 1.100 /*
450 fabian 1.45 static inline void garbage_collect(void)
451     {
452     static u_8bit_t run_cnt = 0;
453    
454     if (run_cnt == 3)
455     garbage_collect_tclhash();
456     else
457     run_cnt++;
458     }
459 stdarg 1.100 */
460 fabian 1.45
461 segfault 1.1 int main(int argc, char **argv)
462     {
463     int xx, i;
464 stdarg 1.138 char s[25];
465 segfault 1.1 FILE *f;
466     struct sigaction sv;
467 stdarg 1.82 egg_timeval_t howlong;
468 stdarg 1.131 int timeout;
469 stdarg 1.137 void *config_root, *entry;
470 stdarg 1.134 char *modname, *scriptname;
471 segfault 1.1
472 tothwolf 1.81 #ifdef DEBUG
473 fabian 1.21 /* Make sure it can write core, if you make debug. Else it's pretty
474     * useless (dw)
475     */
476 fabian 1.15 {
477     #include <sys/resource.h>
478     struct rlimit cdlim;
479 fabian 1.21
480 fabian 1.15 cdlim.rlim_cur = RLIM_INFINITY;
481     cdlim.rlim_max = RLIM_INFINITY;
482 fabian 1.32 setrlimit(RLIMIT_CORE, &cdlim);
483 fabian 1.15 }
484     #endif
485 fabian 1.32
486 ite 1.99 #ifdef ENABLE_NLS
487 ite 1.76 setlocale(LC_MESSAGES, "");
488     bindtextdomain(PACKAGE, LOCALEDIR);
489     textdomain(PACKAGE);
490 ite 1.99 #endif
491 ite 1.76
492 segfault 1.1 #include "patch.h"
493 fabian 1.27 /* Version info! */
494 ite 1.114 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
495 wcc 1.128 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
496 guppy 1.56 egg_version);
497 fabian 1.27 /* Now add on the patchlevel (for Tcl) */
498 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
499 segfault 1.1 strcat(egg_version, egg_xtra);
500     #ifdef STOP_UAC
501     {
502     int nvpair[2];
503    
504     nvpair[0] = SSIN_UACPROC;
505     nvpair[1] = UAC_NOPRINT;
506     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
507     }
508     #endif
509 fabian 1.27
510     /* Set up error traps: */
511 segfault 1.1 sv.sa_handler = got_bus;
512     sigemptyset(&sv.sa_mask);
513     #ifdef SA_RESETHAND
514     sv.sa_flags = SA_RESETHAND;
515     #else
516     sv.sa_flags = 0;
517     #endif
518     sigaction(SIGBUS, &sv, NULL);
519     sv.sa_handler = got_segv;
520     sigaction(SIGSEGV, &sv, NULL);
521     #ifdef SA_RESETHAND
522     sv.sa_flags = 0;
523     #endif
524     sv.sa_handler = got_fpe;
525     sigaction(SIGFPE, &sv, NULL);
526     sv.sa_handler = got_term;
527     sigaction(SIGTERM, &sv, NULL);
528     sv.sa_handler = got_hup;
529     sigaction(SIGHUP, &sv, NULL);
530     sv.sa_handler = got_quit;
531     sigaction(SIGQUIT, &sv, NULL);
532     sv.sa_handler = SIG_IGN;
533     sigaction(SIGPIPE, &sv, NULL);
534     sv.sa_handler = got_ill;
535     sigaction(SIGILL, &sv, NULL);
536     sv.sa_handler = got_alarm;
537     sigaction(SIGALRM, &sv, NULL);
538 fabian 1.27
539     /* Initialize variables and stuff */
540 stdarg 1.121 timer_update_now(&egg_timeval_now);
541 stdarg 1.108 now = egg_timeval_now.sec;
542 segfault 1.1 chanset = NULL;
543 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
544 fabian 1.17 lastmin = nowtm.tm_min;
545 poptix 1.63 srandom(now % (getpid() + getppid()));
546 ite 1.114
547     do_args(argc, argv);
548    
549 segfault 1.1 printf("\n%s\n", version);
550 guppy 1.93 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
551 guppy 1.53
552     /* Don't allow eggdrop to run as root */
553     if (((int) getuid() == 0) || ((int) geteuid() == 0))
554 ite 1.76 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
555 guppy 1.53
556 ite 1.109 egg = eggdrop_new();
557 stdarg 1.136 config_init();
558 stdarg 1.134 core_config_init(configfile);
559 stdarg 1.130 egglog_init();
560     logfile_init();
561 ite 1.110 script_init();
562 stdarg 1.124 user_init();
563 stdarg 1.133 if (core_config.userfile) user_load(core_config.userfile);
564 stdarg 1.126 partyline_init();
565 stdarg 1.130 core_party_init();
566 stdarg 1.126 telnet_init();
567 ite 1.109 modules_init();
568 stdarg 1.88 dns_init();
569 stdarg 1.139 egg_net_init();
570 stdarg 1.85 core_binds_init();
571 segfault 1.1 init_userent();
572 wingman 1.119 traffic_init();
573 stdarg 1.80
574 fabian 1.43 if (backgrd)
575     bg_prepare_split();
576 stdarg 1.80
577 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
578 segfault 1.1 strcpy(&s[11], &s[20]);
579     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
580 stdarg 1.134
581     /* Scan the autoload section of config. */
582     config_root = config_get_root("eggdrop");
583 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
584 stdarg 1.134 modname = NULL;
585 stdarg 1.137 config_get_str(&modname, entry, NULL);
586 stdarg 1.134 module_load(modname);
587     }
588 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
589 stdarg 1.134 scriptname = NULL;
590 stdarg 1.137 config_get_str(&scriptname, entry, NULL);
591 stdarg 1.134 script_load(scriptname);
592     }
593    
594    
595 segfault 1.1 cache_miss = 0;
596     cache_hit = 0;
597 guppy 1.92
598     if (!pid_file[0])
599 stdarg 1.139 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
600 fabian 1.27
601     /* Check for pre-existing eggdrop! */
602 segfault 1.1 f = fopen(pid_file, "r");
603     if (f != NULL) {
604     fgets(s, 10, f);
605     xx = atoi(s);
606 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
607     is used */
608 segfault 1.1 if (errno != ESRCH) {
609 stdarg 1.139 printf(_("I detect %s already running from this directory.\n"), core_config.botname);
610 ite 1.73 printf(_("If this is incorrect, erase the %s\n"), pid_file);
611 fabian 1.43 bg_send_quit(BG_ABORT);
612 segfault 1.1 exit(1);
613     }
614     }
615 fabian 1.27
616     /* Move into background? */
617 segfault 1.1 if (backgrd) {
618 fabian 1.38 #ifndef CYGWIN_HACKS
619 fabian 1.43 bg_do_split();
620 fabian 1.38 } else { /* !backgrd */
621 fabian 1.5 #endif
622 fabian 1.38 xx = getpid();
623     if (xx != 0) {
624     FILE *fp;
625    
626     /* Write pid to file */
627     unlink(pid_file);
628     fp = fopen(pid_file, "w");
629     if (fp != NULL) {
630     fprintf(fp, "%u\n", xx);
631     if (fflush(fp)) {
632     /* Let the bot live since this doesn't appear to be a botchk */
633 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
634 fabian 1.38 fclose(fp);
635     unlink(pid_file);
636     } else
637     fclose(fp);
638 fabian 1.27 } else
639 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
640 fabian 1.38 #ifdef CYGWIN_HACKS
641 guppy 1.68 printf("Launched into the background (pid: %d)\n\n", xx);
642 fabian 1.38 #endif
643     }
644 segfault 1.1 }
645 fabian 1.38
646     use_stderr = 0; /* Stop writing to stderr now */
647 segfault 1.1 if (backgrd) {
648 fabian 1.27 /* Ok, try to disassociate from controlling terminal (finger cross) */
649 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
650 segfault 1.1 setpgid(0, 0);
651     #endif
652 fabian 1.38 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
653 segfault 1.1 freopen("/dev/null", "r", stdin);
654     freopen("/dev/null", "w", stdout);
655     freopen("/dev/null", "w", stderr);
656 poptix 1.62 #ifdef CYGWIN_HACKS
657     FreeConsole();
658     #endif
659 segfault 1.1 }
660 fabian 1.27
661 segfault 1.1 online_since = now;
662     add_help_reference("cmds1.help");
663     add_help_reference("cmds2.help");
664     add_help_reference("core.help");
665 stdarg 1.82 howlong.sec = 1;
666     howlong.usec = 0;
667 stdarg 1.139 timer_create_repeater(&howlong, core_secondly);
668 wingman 1.119
669     /* init time'd hooks */
670 fabian 1.22 add_hook(HOOK_MINUTELY, (Function) core_minutely);
671     add_hook(HOOK_HOURLY, (Function) core_hourly);
672     add_hook(HOOK_REHASH, (Function) event_rehash);
673     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
674     add_hook(HOOK_USERFILE, (Function) event_save);
675 guppy 1.69 add_hook(HOOK_BACKUP, (Function) backup_userfile);
676 fabian 1.22 add_hook(HOOK_DAILY, (Function) event_logfile);
677 guppy 1.61 add_hook(HOOK_LOADED, (Function) event_loaded);
678    
679     call_hook(HOOK_LOADED);
680 segfault 1.1
681     debug0("main: entering loop");
682 stdarg 1.139 while (1) {
683     timer_update_now(&egg_timeval_now);
684     now = egg_timeval_now.sec;
685     random(); /* Woop, lets really jumble things */
686     timer_run();
687    
688     if (timer_get_shortest(&howlong)) {
689     timeout = 1000;
690     }
691     else {
692     timeout = howlong.sec * 1000 + howlong.usec / 1000;
693     }
694     sockbuf_update_all(timeout);
695 stdarg 1.131 }
696 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23