/[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.142 - (hide annotations) (download) (as text)
Mon Mar 24 02:11:39 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.141: +21 -3 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.142 static const char rcsid[] = "$Id: main.c,v 1.141 2003/03/04 22:02:27 wcc 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 stdarg 1.142 #include <sys/stat.h>
51 segfault 1.1 #include <errno.h>
52     #include <signal.h>
53     #include <netdb.h>
54     #include <setjmp.h>
55 ite 1.76 #include <locale.h>
56 stdarg 1.142 #include <ctype.h>
57 ite 1.76
58 fabian 1.27 #ifdef STOP_UAC /* osf/1 complains a lot */
59 segfault 1.1 #include <sys/sysinfo.h>
60     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
61     #endif
62 fabian 1.27 /* Some systems have a working sys/wait.h even though configure will
63     * decide it's not bsd compatable. Oh well.
64     */
65 guppy 1.49
66 segfault 1.1 #include "chan.h"
67     #include "modules.h"
68 fabian 1.43 #include "bg.h"
69 stdarg 1.85 #include "core_binds.h"
70 ite 1.105 #include "logfile.h"
71     #include "misc.h"
72 wingman 1.119 #include "traffic.h"
73 drummer 1.71
74 poptix 1.62 #ifdef CYGWIN_HACKS
75     #include <windows.h>
76     #endif
77    
78 segfault 1.1 #ifndef _POSIX_SOURCE
79 fabian 1.27 /* Solaris needs this */
80 segfault 1.1 #define _POSIX_SOURCE 1
81     #endif
82    
83 stdarg 1.140 extern int conmask;
84 wcc 1.132 extern struct dcc_t *dcc;
85     extern struct userrec *userlist;
86 fabian 1.27
87 wingman 1.116 #ifndef MAKING_MODS
88     extern struct dcc_table DCC_CHAT;
89     #endif /* MAKING_MODS */
90 segfault 1.1
91 guppy 1.56 /*
92 segfault 1.1 * Please use the PATCH macro instead of directly altering the version
93     * string from now on (it makes it much easier to maintain patches).
94     * Also please read the README file regarding your rights to distribute
95     * modified versions of this bot.
96     */
97    
98 ite 1.76 char egg_version[1024] = VERSION;
99     int egg_numver = VERSION_NUM;
100 segfault 1.1
101 ite 1.109 eggdrop_t *egg = NULL; /* Eggdrop's context */
102    
103 fabian 1.27 char notify_new[121] = ""; /* Person to send a note to for new users */
104     int default_flags = 0; /* Default user flags and */
105     int default_uflags = 0; /* Default userdefinied flags for people
106     who say 'hello' or for .adduser */
107    
108     int backgrd = 1; /* Run in the background? */
109     int con_chan = 0; /* Foreground: constantly display channel
110     stats? */
111     int term_z = 0; /* Foreground: use the terminal as a party
112     line? */
113 stdarg 1.134 char configfile[121] = "config.xml"; /* Name of the config file */
114 wcc 1.127 char helpdir[121] = "help/"; /* Directory of help files (if used) */
115     char textdir[121] = "text/"; /* Directory for text files that get dumped */
116 fabian 1.27 time_t online_since; /* Unix-time that the bot loaded up */
117     int make_userfile = 0; /* Using bot in make-userfile mode? (first
118     user to 'hello' becomes master) */
119     char owner[121] = ""; /* Permanent owner(s) of the bot */
120 guppy 1.92 char pid_file[120]; /* Name of the file for the pid to be
121 fabian 1.27 stored in */
122     int save_users_at = 0; /* How many minutes past the hour to
123     save the userfile? */
124     int notify_users_at = 0; /* How many minutes past the hour to
125     notify users of notes? */
126     char version[81]; /* Version info (long form) */
127     char ver[41]; /* Version info (short form) */
128     char egg_xtra[2048]; /* Patch info */
129     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
130     int do_restart = 0; /* .restart has been called, restart asap */
131     int die_on_sighup = 0; /* die if bot receives SIGHUP */
132 guppy 1.52 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
133 fabian 1.27 int resolve_timeout = 15; /* hostname/address lookup timeout */
134 guppy 1.72 char quit_msg[1024]; /* quit message */
135 fabian 1.27 time_t now; /* duh, now :) */
136 stdarg 1.108 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
137 fabian 1.27
138 fabian 1.44 void fatal(const char *s, int recoverable)
139 segfault 1.1 {
140     putlog(LOG_MISC, "*", "* %s", s);
141     flushlogs();
142     unlink(pid_file);
143 fabian 1.43 if (!recoverable) {
144     bg_send_quit(BG_ABORT);
145 segfault 1.1 exit(1);
146 fabian 1.43 }
147 segfault 1.1 }
148    
149     static void got_bus(int z)
150     {
151     fatal("BUS ERROR -- CRASHING!", 1);
152     #ifdef SA_RESETHAND
153     kill(getpid(), SIGBUS);
154     #else
155 fabian 1.43 bg_send_quit(BG_ABORT);
156 segfault 1.1 exit(1);
157     #endif
158     }
159    
160     static void got_segv(int z)
161     {
162     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
163     #ifdef SA_RESETHAND
164     kill(getpid(), SIGSEGV);
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_fpe(int z)
172     {
173     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
174     }
175    
176     static void got_term(int z)
177     {
178 stdarg 1.85 check_bind_event("sigterm");
179 segfault 1.1 if (die_on_sigterm) {
180     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
181     } else {
182     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
183     }
184     }
185    
186     static void got_quit(int z)
187     {
188 stdarg 1.85 check_bind_event("sigquit");
189 segfault 1.1 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
190     return;
191     }
192    
193     static void got_hup(int z)
194     {
195 stdarg 1.85 check_bind_event("sighup");
196 segfault 1.1 if (die_on_sighup) {
197     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
198     } else
199     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
200     do_restart = -2;
201     return;
202     }
203    
204 guppy 1.101 /* Got ILL signal
205 fabian 1.27 */
206 segfault 1.1 static void got_ill(int z)
207     {
208 stdarg 1.85 check_bind_event("sigill");
209 fabian 1.10 }
210    
211 ite 1.114
212 wcc 1.135 static void print_version(void)
213 segfault 1.1 {
214 guppy 1.49 char x[1024], *z = x;
215 ite 1.114
216     strlcpy(x, egg_version, sizeof x);
217     newsplit(&z);
218     newsplit(&z);
219     printf("%s\n", version);
220     if (z[0])
221     printf(" (patches: %s)\n", z);
222     }
223    
224 wcc 1.135 static void print_help(void)
225 ite 1.114 {
226     print_version ();
227     printf("\n\
228     Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
229     printf(_("\n\
230     -h, --help Print help and exit\n\
231     -v, --version Print version and exit\n\
232     -n, --foreground Don't go into the background (default=off)\n\
233     -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
234     -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
235     -m --make-userfile Userfile creation mode\n\
236 wcc 1.135 FILE optional config filename (default 'config.xml')\n"));
237 ite 1.114 printf("\n");
238     }
239 segfault 1.1
240 ite 1.114 static void do_args(int argc, char * const *argv)
241     {
242     int c;
243    
244     optarg = 0;
245     optind = 1;
246     opterr = 1;
247     optopt = '?';
248    
249     while (1) {
250     int option_index = 0;
251     static struct option long_options[] = {
252     { "help", 0, NULL, 'h' },
253     { "version", 0, NULL, 'v' },
254     { "load-module", 1, NULL, 'p' },
255     { "foreground", 0, NULL, 'n' },
256     { "channel-stats", 0, NULL, 'c' },
257     { "terminal", 0, NULL, 't' },
258     { "make-userfile", 0, NULL, 'm' },
259     { NULL, 0, NULL, 0 }
260     };
261    
262     c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
263    
264     if (c == -1) break;
265    
266     switch (c) {
267     case 'n':
268 segfault 1.1 backgrd = 0;
269 ite 1.114 break;
270    
271     case 'c':
272 segfault 1.1 con_chan = 1;
273     term_z = 0;
274 ite 1.114 break;
275    
276     case 't':
277 segfault 1.1 con_chan = 0;
278     term_z = 1;
279 ite 1.114 break;
280    
281     case 'm':
282 segfault 1.1 make_userfile = 1;
283 ite 1.114 break;
284    
285     case 'v':
286     print_version();
287 fabian 1.43 bg_send_quit(BG_ABORT);
288 segfault 1.1 exit(0);
289 ite 1.114 break; /* this should never be reached */
290    
291     case 'h':
292     print_help();
293 fabian 1.43 bg_send_quit(BG_ABORT);
294 segfault 1.1 exit(0);
295 ite 1.114 break; /* this should never be reached */
296    
297     case 0: /* Long option with no short option */
298     case '?': /* Invalid option. */
299     /* getopt_long() already printed an error message. */
300     bg_send_quit(BG_ABORT);
301     exit(1);
302    
303     default: /* bug: option not considered. */
304     fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
305     bg_send_quit(BG_ABORT);
306     exit(1);
307     break; /* this should never be reached */
308     }
309     }
310     if (optind < argc)
311     strlcpy(configfile, argv[optind], sizeof configfile);
312 segfault 1.1 }
313    
314 fabian 1.27 /* Timer info */
315     static int lastmin = 99;
316     static struct tm nowtm;
317 segfault 1.1
318 fabian 1.34 /* Called once a second.
319 fabian 1.27 */
320 stdarg 1.139 static int core_secondly()
321 segfault 1.1 {
322     static int cnt = 0;
323     int miltime;
324    
325 stdarg 1.82 call_hook(HOOK_SECONDLY); /* Will be removed later */
326 segfault 1.1 cnt++;
327 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
328 fabian 1.17 if (nowtm.tm_min != lastmin) {
329 segfault 1.1 int i = 0;
330    
331 fabian 1.27 /* Once a minute */
332 segfault 1.1 lastmin = (lastmin + 1) % 60;
333     call_hook(HOOK_MINUTELY);
334 fabian 1.27 /* In case for some reason more than 1 min has passed: */
335 fabian 1.17 while (nowtm.tm_min != lastmin) {
336 fabian 1.27 /* Timer drift, dammit */
337 wcc 1.141 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin,
338     nowtm.tm_min);
339 segfault 1.1 i++;
340     lastmin = (lastmin + 1) % 60;
341     call_hook(HOOK_MINUTELY);
342     }
343     if (i > 1)
344     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
345 fabian 1.17 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
346     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
347 segfault 1.1 call_hook(HOOK_5MINUTELY);
348 guppy 1.49 if (!miltime) { /* At midnight */
349     char s[25];
350 segfault 1.1
351 ite 1.111 strlcpy(s, ctime(&now), sizeof s);
352 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
353 guppy 1.69 call_hook(HOOK_BACKUP);
354 stdarg 1.90 call_hook(HOOK_DAILY);
355 segfault 1.1 }
356     }
357 fabian 1.17 if (nowtm.tm_min == notify_users_at)
358 segfault 1.1 call_hook(HOOK_HOURLY);
359     }
360 stdarg 1.139 return(0);
361 segfault 1.1 }
362    
363     static void core_minutely()
364     {
365 stdarg 1.85 check_bind_time(&nowtm);
366 segfault 1.1 }
367    
368     static void core_hourly()
369     {
370     }
371    
372     static void event_rehash()
373     {
374 stdarg 1.85 check_bind_event("rehash");
375 segfault 1.1 }
376    
377     static void event_prerehash()
378     {
379 stdarg 1.85 check_bind_event("prerehash");
380 segfault 1.1 }
381    
382     static void event_save()
383     {
384 stdarg 1.85 check_bind_event("save");
385 segfault 1.1 }
386    
387     static void event_logfile()
388     {
389 stdarg 1.85 check_bind_event("logfile");
390 segfault 1.1 }
391    
392 guppy 1.61 static void event_loaded()
393     {
394 stdarg 1.85 check_bind_event("loaded");
395 guppy 1.61 }
396    
397 segfault 1.1 extern module_entry *module_list;
398    
399 stdarg 1.130 void core_party_init();
400 stdarg 1.133 void core_config_init();
401 stdarg 1.130 void telnet_init();
402 stdarg 1.85 void binds_init();
403 fabian 1.11
404 fabian 1.44 void patch(const char *str)
405 fabian 1.11 {
406     char *p = strchr(egg_version, '+');
407    
408     if (!p)
409     p = &egg_version[strlen(egg_version)];
410     sprintf(p, "+%s", str);
411     egg_numver++;
412     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
413     }
414 segfault 1.1
415 stdarg 1.100 /*
416 fabian 1.45 static inline void garbage_collect(void)
417     {
418     static u_8bit_t run_cnt = 0;
419    
420     if (run_cnt == 3)
421     garbage_collect_tclhash();
422     else
423     run_cnt++;
424     }
425 stdarg 1.100 */
426 fabian 1.45
427 stdarg 1.140 int owner_check(const char *handle)
428     {
429     int len;
430     char *powner;
431    
432     len = strlen(handle);
433     if (!len) return(-1);
434    
435     powner = core_config.owner;
436     while (powner) {
437     while (*powner && !isalnum(*powner)) powner++;
438     if (!*powner) break;
439     if (!strncasecmp(powner, handle, len)) {
440     powner += len;
441     if (!*powner || isalnum(*powner)) return(0);
442     }
443     while (*powner && isalnum(*powner)) powner++;
444     }
445     return(-1);
446     }
447    
448 stdarg 1.142 int file_check(const char *filename)
449     {
450     struct stat sbuf = {0};
451     int r;
452    
453     r = stat(filename, &sbuf);
454     if (r) return(-1);
455     errno = 0;
456     if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
457     return(0);
458     }
459    
460 segfault 1.1 int main(int argc, char **argv)
461     {
462     int xx, i;
463 stdarg 1.138 char s[25];
464 segfault 1.1 FILE *f;
465     struct sigaction sv;
466 stdarg 1.82 egg_timeval_t howlong;
467 stdarg 1.131 int timeout;
468 stdarg 1.137 void *config_root, *entry;
469 stdarg 1.134 char *modname, *scriptname;
470 segfault 1.1
471 tothwolf 1.81 #ifdef DEBUG
472 fabian 1.21 /* Make sure it can write core, if you make debug. Else it's pretty
473     * useless (dw)
474     */
475 fabian 1.15 {
476     #include <sys/resource.h>
477     struct rlimit cdlim;
478 fabian 1.21
479 fabian 1.15 cdlim.rlim_cur = RLIM_INFINITY;
480     cdlim.rlim_max = RLIM_INFINITY;
481 fabian 1.32 setrlimit(RLIMIT_CORE, &cdlim);
482 fabian 1.15 }
483     #endif
484 fabian 1.32
485 ite 1.99 #ifdef ENABLE_NLS
486 ite 1.76 setlocale(LC_MESSAGES, "");
487     bindtextdomain(PACKAGE, LOCALEDIR);
488     textdomain(PACKAGE);
489 ite 1.99 #endif
490 ite 1.76
491 segfault 1.1 #include "patch.h"
492 fabian 1.27 /* Version info! */
493 ite 1.114 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
494 wcc 1.128 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
495 guppy 1.56 egg_version);
496 fabian 1.27 /* Now add on the patchlevel (for Tcl) */
497 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
498 segfault 1.1 strcat(egg_version, egg_xtra);
499     #ifdef STOP_UAC
500     {
501     int nvpair[2];
502    
503     nvpair[0] = SSIN_UACPROC;
504     nvpair[1] = UAC_NOPRINT;
505     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
506     }
507     #endif
508 fabian 1.27
509     /* Set up error traps: */
510 segfault 1.1 sv.sa_handler = got_bus;
511     sigemptyset(&sv.sa_mask);
512     #ifdef SA_RESETHAND
513     sv.sa_flags = SA_RESETHAND;
514     #else
515     sv.sa_flags = 0;
516     #endif
517     sigaction(SIGBUS, &sv, NULL);
518     sv.sa_handler = got_segv;
519     sigaction(SIGSEGV, &sv, NULL);
520     #ifdef SA_RESETHAND
521     sv.sa_flags = 0;
522     #endif
523     sv.sa_handler = got_fpe;
524     sigaction(SIGFPE, &sv, NULL);
525     sv.sa_handler = got_term;
526     sigaction(SIGTERM, &sv, NULL);
527     sv.sa_handler = got_hup;
528     sigaction(SIGHUP, &sv, NULL);
529     sv.sa_handler = got_quit;
530     sigaction(SIGQUIT, &sv, NULL);
531     sv.sa_handler = SIG_IGN;
532     sigaction(SIGPIPE, &sv, NULL);
533     sv.sa_handler = got_ill;
534     sigaction(SIGILL, &sv, NULL);
535 fabian 1.27
536     /* Initialize variables and stuff */
537 stdarg 1.121 timer_update_now(&egg_timeval_now);
538 stdarg 1.108 now = egg_timeval_now.sec;
539 tothwolf 1.79 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
540 fabian 1.17 lastmin = nowtm.tm_min;
541 poptix 1.63 srandom(now % (getpid() + getppid()));
542 ite 1.114
543     do_args(argc, argv);
544    
545 segfault 1.1 printf("\n%s\n", version);
546 guppy 1.93 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
547 guppy 1.53
548     /* Don't allow eggdrop to run as root */
549     if (((int) getuid() == 0) || ((int) geteuid() == 0))
550 ite 1.76 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
551 stdarg 1.142
552     if (file_check(configfile)) {
553     if (errno) perror(configfile);
554     else fprintf(stderr, "ERROR\n\nCheck file permissions on your config file!\nMake sure other groups and users cannot read/write it.\n");
555     exit(-1);
556     }
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.139 egg_net_init();
571 stdarg 1.85 core_binds_init();
572 stdarg 1.80
573 fabian 1.43 if (backgrd)
574     bg_prepare_split();
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 stdarg 1.134
580     /* Scan the autoload section of config. */
581     config_root = config_get_root("eggdrop");
582 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
583 stdarg 1.134 modname = NULL;
584 stdarg 1.137 config_get_str(&modname, entry, NULL);
585 stdarg 1.134 module_load(modname);
586     }
587 stdarg 1.137 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
588 stdarg 1.134 scriptname = NULL;
589 stdarg 1.137 config_get_str(&scriptname, entry, NULL);
590 stdarg 1.134 script_load(scriptname);
591     }
592    
593    
594 guppy 1.92
595     if (!pid_file[0])
596 stdarg 1.139 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
597 fabian 1.27
598     /* Check for pre-existing eggdrop! */
599 segfault 1.1 f = fopen(pid_file, "r");
600     if (f != NULL) {
601     fgets(s, 10, f);
602     xx = atoi(s);
603 fabian 1.27 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
604     is used */
605 segfault 1.1 if (errno != ESRCH) {
606 stdarg 1.139 printf(_("I detect %s already running from this directory.\n"), core_config.botname);
607 ite 1.73 printf(_("If this is incorrect, erase the %s\n"), pid_file);
608 fabian 1.43 bg_send_quit(BG_ABORT);
609 segfault 1.1 exit(1);
610     }
611     }
612 fabian 1.27
613     /* Move into background? */
614 segfault 1.1 if (backgrd) {
615 fabian 1.38 #ifndef CYGWIN_HACKS
616 fabian 1.43 bg_do_split();
617 fabian 1.38 } else { /* !backgrd */
618 fabian 1.5 #endif
619 fabian 1.38 xx = getpid();
620     if (xx != 0) {
621     FILE *fp;
622    
623     /* Write pid to file */
624     unlink(pid_file);
625     fp = fopen(pid_file, "w");
626     if (fp != NULL) {
627     fprintf(fp, "%u\n", xx);
628     if (fflush(fp)) {
629     /* Let the bot live since this doesn't appear to be a botchk */
630 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
631 fabian 1.38 fclose(fp);
632     unlink(pid_file);
633     } else
634     fclose(fp);
635 fabian 1.27 } else
636 ite 1.73 printf(_("* Warning! Could not write %s file!\n"), pid_file);
637 fabian 1.38 #ifdef CYGWIN_HACKS
638 guppy 1.68 printf("Launched into the background (pid: %d)\n\n", xx);
639 fabian 1.38 #endif
640     }
641 segfault 1.1 }
642 fabian 1.38
643     use_stderr = 0; /* Stop writing to stderr now */
644 segfault 1.1 if (backgrd) {
645 fabian 1.27 /* Ok, try to disassociate from controlling terminal (finger cross) */
646 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
647 segfault 1.1 setpgid(0, 0);
648     #endif
649 fabian 1.38 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
650 segfault 1.1 freopen("/dev/null", "r", stdin);
651     freopen("/dev/null", "w", stdout);
652     freopen("/dev/null", "w", stderr);
653 poptix 1.62 #ifdef CYGWIN_HACKS
654     FreeConsole();
655     #endif
656 segfault 1.1 }
657 fabian 1.27
658 segfault 1.1 online_since = now;
659 stdarg 1.82 howlong.sec = 1;
660     howlong.usec = 0;
661 stdarg 1.139 timer_create_repeater(&howlong, core_secondly);
662 wingman 1.119
663     /* init time'd hooks */
664 fabian 1.22 add_hook(HOOK_MINUTELY, (Function) core_minutely);
665     add_hook(HOOK_HOURLY, (Function) core_hourly);
666     add_hook(HOOK_REHASH, (Function) event_rehash);
667     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
668     add_hook(HOOK_USERFILE, (Function) event_save);
669     add_hook(HOOK_DAILY, (Function) event_logfile);
670 guppy 1.61 add_hook(HOOK_LOADED, (Function) event_loaded);
671    
672     call_hook(HOOK_LOADED);
673 segfault 1.1
674 wcc 1.141 putlog(LOG_DEBUG, "*", "main: entering loop");
675 stdarg 1.139 while (1) {
676     timer_update_now(&egg_timeval_now);
677     now = egg_timeval_now.sec;
678     random(); /* Woop, lets really jumble things */
679     timer_run();
680    
681     if (timer_get_shortest(&howlong)) {
682     timeout = 1000;
683     }
684     else {
685     timeout = howlong.sec * 1000 + howlong.usec / 1000;
686     }
687     sockbuf_update_all(timeout);
688 stdarg 1.131 }
689 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23