/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Contents of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.148 - (show annotations) (download) (as text)
Sun May 11 02:33:32 2003 UTC (16 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.147: +1 -4 lines
File MIME type: text/x-chdr
* stray code removal heh

1 /*
2 * main.c --
3 *
4 * core event handling
5 * signal handling
6 * command line arguments
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Eggheads Development Team
11 *
12 * 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 *
17 * 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 *
22 * 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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26 /*
27 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 * NOTE: Robey is no longer working on this code, there is a discussion
29 * list available at eggheads@eggheads.org.
30 */
31
32 #ifndef lint
33 static const char rcsid[] = "$Id: main.c,v 1.147 2003/05/11 02:30:54 stdarg Exp $";
34 #endif
35
36 #include <unistd.h>
37 #include <eggdrop/eggdrop.h>
38 #include "main.h"
39 #include "core_config.h"
40 #include <fcntl.h>
41 #ifdef TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 # else
48 # include <time.h>
49 # endif
50 #endif
51 #include <sys/stat.h>
52 #include <errno.h>
53 #include <signal.h>
54 #include <netdb.h>
55 #include <setjmp.h>
56 #include <locale.h>
57 #include <ctype.h>
58 #include <ltdl.h>
59
60 #ifdef STOP_UAC /* osf/1 complains a lot */
61 #include <sys/sysinfo.h>
62 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
63 #endif
64 /* Some systems have a working sys/wait.h even though configure will
65 * decide it's not bsd compatable. Oh well.
66 */
67
68 #include "bg.h"
69 #include "core_binds.h"
70 #include "logfile.h"
71
72 #ifdef CYGWIN_HACKS
73 #include <windows.h>
74 #endif
75
76 #ifndef _POSIX_SOURCE
77 /* Solaris needs this */
78 #define _POSIX_SOURCE 1
79 #endif
80
81 char egg_version[1024] = VERSION;
82 int egg_numver = VERSION_NUM;
83
84 eggdrop_t *egg = NULL; /* Eggdrop's context */
85
86 char notify_new[121] = ""; /* Person to send a note to for new users */
87 int default_flags = 0; /* Default user flags and */
88 int default_uflags = 0; /* Default userdefinied flags for people
89 who say 'hello' or for .adduser */
90
91 int backgrd = 1; /* Run in the background? */
92 int con_chan = 0; /* Foreground: constantly display channel
93 stats? */
94 int term_z = 0; /* Foreground: use the terminal as a party
95 line? */
96 char configfile[121] = "config.xml"; /* Name of the config file */
97 char helpdir[121] = "help/"; /* Directory of help files (if used) */
98 char textdir[121] = "text/"; /* Directory for text files that get dumped */
99 time_t online_since; /* Unix-time that the bot loaded up */
100 int make_userfile = 0; /* Using bot in make-userfile mode? (first
101 user to 'hello' becomes master) */
102 char pid_file[120]; /* Name of the file for the pid to be
103 stored in */
104 int save_users_at = 0; /* How many minutes past the hour to
105 save the userfile? */
106 int notify_users_at = 0; /* How many minutes past the hour to
107 notify users of notes? */
108 char version[81]; /* Version info (long form) */
109 char ver[41]; /* Version info (short form) */
110 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
111 int do_restart = 0; /* .restart has been called, restart asap */
112 int die_on_sighup = 0; /* die if bot receives SIGHUP */
113 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
114 int resolve_timeout = 15; /* hostname/address lookup timeout */
115 char quit_msg[1024]; /* quit message */
116 time_t now; /* duh, now :) */
117 egg_timeval_t egg_timeval_now; /* Same thing, but seconds and microseconds. */
118
119 void fatal(const char *s, int recoverable)
120 {
121 putlog(LOG_MISC, "*", "* %s", s);
122 flushlogs();
123 unlink(pid_file);
124 if (!recoverable) {
125 bg_send_quit(BG_ABORT);
126 exit(1);
127 }
128 }
129
130 static void got_bus(int z)
131 {
132 fatal("BUS ERROR -- CRASHING!", 1);
133 #ifdef SA_RESETHAND
134 kill(getpid(), SIGBUS);
135 #else
136 bg_send_quit(BG_ABORT);
137 exit(1);
138 #endif
139 }
140
141 static void got_segv(int z)
142 {
143 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
144 #ifdef SA_RESETHAND
145 kill(getpid(), SIGSEGV);
146 #else
147 bg_send_quit(BG_ABORT);
148 exit(1);
149 #endif
150 }
151
152 static void got_fpe(int z)
153 {
154 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
155 }
156
157 static void got_term(int z)
158 {
159 check_bind_event("sigterm");
160 if (die_on_sigterm) {
161 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
162 } else {
163 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
164 }
165 }
166
167 static void got_quit(int z)
168 {
169 check_bind_event("sigquit");
170 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
171 return;
172 }
173
174 static void got_hup(int z)
175 {
176 check_bind_event("sighup");
177 if (die_on_sighup) {
178 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
179 } else
180 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
181 do_restart = -2;
182 return;
183 }
184
185 /* Got ILL signal
186 */
187 static void got_ill(int z)
188 {
189 check_bind_event("sigill");
190 }
191
192
193 static void print_version(void)
194 {
195 char x[1024];
196
197 strlcpy(x, egg_version, sizeof x);
198 printf("%s\n", version);
199 }
200
201 static void print_help(void)
202 {
203 print_version ();
204 printf("\n\
205 Usage: %s [OPTIONS]... [FILE]\n", PACKAGE);
206 printf(_("\n\
207 -h, --help Print help and exit\n\
208 -v, --version Print version and exit\n\
209 -n, --foreground Don't go into the background (default=off)\n\
210 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
211 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
212 -m --make-userfile Userfile creation mode\n\
213 FILE optional config filename (default 'config.xml')\n"));
214 printf("\n");
215 }
216
217 static void do_args(int argc, char * const *argv)
218 {
219 int c;
220
221 optarg = 0;
222 optind = 1;
223 opterr = 1;
224 optopt = '?';
225
226 while (1) {
227 int option_index = 0;
228 static struct option long_options[] = {
229 { "help", 0, NULL, 'h' },
230 { "version", 0, NULL, 'v' },
231 { "load-module", 1, NULL, 'p' },
232 { "foreground", 0, NULL, 'n' },
233 { "channel-stats", 0, NULL, 'c' },
234 { "terminal", 0, NULL, 't' },
235 { "make-userfile", 0, NULL, 'm' },
236 { NULL, 0, NULL, 0 }
237 };
238
239 c = getopt_long (argc, argv, "hvp:nctm", long_options, &option_index);
240
241 if (c == -1) break;
242
243 switch (c) {
244 case 'n':
245 backgrd = 0;
246 break;
247
248 case 'c':
249 con_chan = 1;
250 term_z = 0;
251 break;
252
253 case 't':
254 con_chan = 0;
255 term_z = 1;
256 break;
257
258 case 'm':
259 make_userfile = 1;
260 break;
261
262 case 'v':
263 print_version();
264 bg_send_quit(BG_ABORT);
265 exit(0);
266 break; /* this should never be reached */
267
268 case 'h':
269 print_help();
270 bg_send_quit(BG_ABORT);
271 exit(0);
272 break; /* this should never be reached */
273
274 case 0: /* Long option with no short option */
275 case '?': /* Invalid option. */
276 /* getopt_long() already printed an error message. */
277 bg_send_quit(BG_ABORT);
278 exit(1);
279
280 default: /* bug: option not considered. */
281 fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
282 bg_send_quit(BG_ABORT);
283 exit(1);
284 break; /* this should never be reached */
285 }
286 }
287 if (optind < argc)
288 strlcpy(configfile, argv[optind], sizeof configfile);
289 }
290
291 /* Timer info */
292 static int lastmin = 99;
293 static struct tm nowtm;
294
295 /* Called once a second.
296 */
297 static int core_secondly()
298 {
299 static int cnt = 0;
300 int miltime;
301
302 check_bind_secondly();
303 cnt++;
304 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
305 if (nowtm.tm_min != lastmin) {
306 int i = 0;
307
308 /* Once a minute */
309 lastmin = (lastmin + 1) % 60;
310 check_bind_event("minutely");
311 check_bind_time(&nowtm);
312 /* In case for some reason more than 1 min has passed: */
313 while (nowtm.tm_min != lastmin) {
314 /* Timer drift, dammit */
315 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin,
316 nowtm.tm_min);
317 i++;
318 lastmin = (lastmin + 1) % 60;
319 check_bind_event("minutely");
320 }
321 if (i > 1)
322 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
323 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
324 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
325 check_bind_event("5minutely");
326 if (!miltime) { /* At midnight */
327 char s[25];
328
329 strlcpy(s, ctime(&now), sizeof s);
330 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
331 check_bind_event("backup");
332 check_bind_event("daily");
333 }
334 }
335 if (nowtm.tm_min == notify_users_at) check_bind_event("hourly");
336 }
337 return(0);
338 }
339
340 void core_party_init();
341 void core_config_init();
342 void telnet_init();
343
344 int file_check(const char *filename)
345 {
346 struct stat sbuf = {0};
347 int r;
348
349 r = stat(filename, &sbuf);
350 if (r) return(-1);
351 errno = 0;
352 if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
353 return(0);
354 }
355
356 int main(int argc, char **argv)
357 {
358 int xx, i;
359 char s[25];
360 FILE *f;
361 struct sigaction sv;
362 egg_timeval_t howlong;
363 int timeout;
364 void *config_root, *entry;
365 char *modname, *scriptname;
366
367 #ifdef DEBUG
368 /* Make sure it can write core, if you make debug. Else it's pretty
369 * useless (dw)
370 */
371 {
372 #include <sys/resource.h>
373 struct rlimit cdlim;
374
375 cdlim.rlim_cur = RLIM_INFINITY;
376 cdlim.rlim_max = RLIM_INFINITY;
377 setrlimit(RLIMIT_CORE, &cdlim);
378 }
379 #endif
380
381 #ifdef ENABLE_NLS
382 setlocale(LC_MESSAGES, "");
383 bindtextdomain(PACKAGE, LOCALEDIR);
384 textdomain(PACKAGE);
385 #endif
386
387 /* Initialize ltdl. */
388 LTDL_SET_PRELOADED_SYMBOLS();
389 if (lt_dlinit()) {
390 printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
391 return(-1);
392 }
393
394 /* Version info! */
395 snprintf(ver, sizeof ver, "%s v%s", PACKAGE, egg_version);
396 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2003 Eggheads",
397 egg_version);
398 /* Now add on the patchlevel (for Tcl) */
399 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
400 #ifdef STOP_UAC
401 {
402 int nvpair[2];
403
404 nvpair[0] = SSIN_UACPROC;
405 nvpair[1] = UAC_NOPRINT;
406 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
407 }
408 #endif
409
410 /* Set up error traps: */
411 sv.sa_handler = got_bus;
412 sigemptyset(&sv.sa_mask);
413 #ifdef SA_RESETHAND
414 sv.sa_flags = SA_RESETHAND;
415 #else
416 sv.sa_flags = 0;
417 #endif
418 sigaction(SIGBUS, &sv, NULL);
419 sv.sa_handler = got_segv;
420 sigaction(SIGSEGV, &sv, NULL);
421 #ifdef SA_RESETHAND
422 sv.sa_flags = 0;
423 #endif
424 sv.sa_handler = got_fpe;
425 sigaction(SIGFPE, &sv, NULL);
426 sv.sa_handler = got_term;
427 sigaction(SIGTERM, &sv, NULL);
428 sv.sa_handler = got_hup;
429 sigaction(SIGHUP, &sv, NULL);
430 sv.sa_handler = got_quit;
431 sigaction(SIGQUIT, &sv, NULL);
432 sv.sa_handler = SIG_IGN;
433 sigaction(SIGPIPE, &sv, NULL);
434 sv.sa_handler = got_ill;
435 sigaction(SIGILL, &sv, NULL);
436
437 /* Initialize variables and stuff */
438 timer_update_now(&egg_timeval_now);
439 now = egg_timeval_now.sec;
440 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
441 lastmin = nowtm.tm_min;
442 srandom(now % (getpid() + getppid()));
443
444 do_args(argc, argv);
445
446 printf("\n%s\n", version);
447 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
448
449 /* Don't allow eggdrop to run as root */
450 if (((int) getuid() == 0) || ((int) geteuid() == 0))
451 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
452
453 if (file_check(configfile)) {
454 if (errno) perror(configfile);
455 else fprintf(stderr, "ERROR\n\nCheck file permissions on your config file!\nMake sure other groups and users cannot read/write it.\n");
456 exit(-1);
457 }
458
459 egg = eggdrop_new();
460 config_init();
461 core_config_init(configfile);
462 egglog_init();
463 logfile_init();
464 script_init();
465 user_init();
466 if (core_config.userfile) user_load(core_config.userfile);
467 partyline_init();
468 core_party_init();
469 telnet_init();
470 module_init();
471 egg_net_init();
472 core_binds_init();
473
474 if (backgrd)
475 bg_prepare_split();
476
477 strlcpy(s, ctime(&now), sizeof s);
478 strcpy(&s[11], &s[20]);
479 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
480
481 /* Put the module directory in the ltdl search path. */
482 if (core_config.module_path) module_add_dir(core_config.module_path);
483
484 /* Scan the autoload section of config. */
485 config_root = config_get_root("eggdrop");
486 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
487 modname = NULL;
488 config_get_str(&modname, entry, NULL);
489 module_load(modname);
490 }
491 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
492 scriptname = NULL;
493 config_get_str(&scriptname, entry, NULL);
494 script_load(scriptname);
495 }
496
497
498
499 if (!pid_file[0])
500 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
501
502 /* Check for pre-existing eggdrop! */
503 f = fopen(pid_file, "r");
504 if (f != NULL) {
505 fgets(s, 10, f);
506 xx = atoi(s);
507 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
508 is used */
509 if (errno != ESRCH) {
510 printf(_("I detect %s already running from this directory.\n"), core_config.botname);
511 printf(_("If this is incorrect, erase the %s\n"), pid_file);
512 bg_send_quit(BG_ABORT);
513 exit(1);
514 }
515 }
516
517 /* Move into background? */
518 if (backgrd) {
519 #ifndef CYGWIN_HACKS
520 bg_do_split();
521 } else { /* !backgrd */
522 #endif
523 xx = getpid();
524 if (xx != 0) {
525 FILE *fp;
526
527 /* Write pid to file */
528 unlink(pid_file);
529 fp = fopen(pid_file, "w");
530 if (fp != NULL) {
531 fprintf(fp, "%u\n", xx);
532 if (fflush(fp)) {
533 /* Let the bot live since this doesn't appear to be a botchk */
534 printf(_("* Warning! Could not write %s file!\n"), pid_file);
535 fclose(fp);
536 unlink(pid_file);
537 } else
538 fclose(fp);
539 } else
540 printf(_("* Warning! Could not write %s file!\n"), pid_file);
541 #ifdef CYGWIN_HACKS
542 printf("Launched into the background (pid: %d)\n\n", xx);
543 #endif
544 }
545 }
546
547 use_stderr = 0; /* Stop writing to stderr now */
548 if (backgrd) {
549 /* Ok, try to disassociate from controlling terminal (finger cross) */
550 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
551 setpgid(0, 0);
552 #endif
553 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
554 freopen("/dev/null", "r", stdin);
555 freopen("/dev/null", "w", stdout);
556 freopen("/dev/null", "w", stderr);
557 #ifdef CYGWIN_HACKS
558 FreeConsole();
559 #endif
560 }
561
562 online_since = now;
563 howlong.sec = 1;
564 howlong.usec = 0;
565 timer_create_repeater(&howlong, "main loop", core_secondly);
566
567 putlog(LOG_DEBUG, "*", "main: entering loop");
568 while (1) {
569 timer_update_now(&egg_timeval_now);
570 now = egg_timeval_now.sec;
571 random(); /* Woop, lets really jumble things */
572 timer_run();
573
574 if (timer_get_shortest(&howlong)) {
575 timeout = 1000;
576 }
577 else {
578 timeout = howlong.sec * 1000 + howlong.usec / 1000;
579 }
580 sockbuf_update_all(timeout);
581 }
582 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23