/[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.186 - (show annotations) (download) (as text)
Sun Oct 17 05:14:07 2004 UTC (14 years, 10 months ago) by stdarg
Branch: MAIN
Changes since 1.185: +3 -25 lines
File MIME type: text/x-chdr
* Added a few includs to common.h
* Removed duplicate includes from a bunch of files
* Added tcl date parsing a la "clock scan"
* Fixed some issues with configure (maybe)

1 /* main.c
2 *
3 * Copyright (C) 1997 Robey Pointer
4 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Eggheads Development Team
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifndef lint
22 static const char rcsid[] = "$Id: main.c,v 1.185 2004/09/29 15:38:39 stdarg Exp $";
23 #endif
24
25 #include <eggdrop/eggdrop.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <locale.h>
32 #include <ltdl.h>
33
34 #include "debug.h"
35 #include "core_config.h"
36 #include "core_party.h"
37 #include "core_binds.h"
38 #include "logfile.h"
39 #include "terminal.h"
40 #include "bg.h"
41 #include "main.h"
42
43 #ifdef STOP_UAC /* OSF/1 complains a lot */
44 # include <sys/sysinfo.h>
45 # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
46 #endif
47
48 #ifdef CYGWIN_HACKS
49 # include <windows.h>
50 #endif
51
52 #ifndef _POSIX_SOURCE
53 # define _POSIX_SOURCE 1 /* Solaris needs this */
54 #endif
55
56 #define RUNMODE_NORMAL 1
57 #define RUNMODE_SHUTDOWN 2
58 #define RUNMODE_RESTART 3
59
60 int runmode = 1; /* The most important variable ever! */
61 int backgrd = 1; /* Run in the background? */
62 int make_userfile = 0; /* Start bot in make-userfile mode? */
63 int terminal_mode = 0; /* Terminal mode */
64
65 /* XXX: remove this feature (but not yet) */
66 int debug_run = 0; /* Just init and shutdown */
67
68 const char *configfile = NULL; /* Name of the config file */
69 char pid_file[512]; /* Name of Eggdrop's pid file */
70
71 time_t online_since; /* Time the bot was started */
72 time_t now; /* Current time */
73 egg_timeval_t egg_timeval_now; /* Current time in seconds and microseconds. */
74
75 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
76
77 char version[81];
78
79 static int lastmin = 99;
80 static struct tm nowtm;
81
82 void fatal(const char *errmsg)
83 {
84 putlog(LOG_MISC, "*", "Fatal: %s", errmsg);
85 flushlogs();
86 exit(1);
87 }
88
89 static void got_bus(int z)
90 {
91 fatal("BUS ERROR.");
92 }
93
94 static void got_segv(int z)
95 {
96 fatal("SEGMENT VIOLATION.");
97 }
98
99 static void got_fpe(int z)
100 {
101 fatal("FLOATING POINT ERROR.");
102 }
103
104 static void got_term(int z)
105 {
106 eggdrop_event("sigterm");
107 if (core_config.die_on_sigterm) core_shutdown(SHUTDOWN_SIGTERM, NULL, NULL);
108 else putlog(LOG_MISC, "*", _("Received TERM signal (ignoring)."));
109 }
110
111 static void got_quit(int z)
112 {
113 eggdrop_event("sigquit");
114 putlog(LOG_MISC, "*", _("Received QUIT signal (ignoring)."));
115 return;
116 }
117
118 static void got_hup(int z)
119 {
120 eggdrop_event("sighup");
121 putlog(LOG_MISC, "*", _("Received HUP signal (ignoring)."));
122 return;
123 }
124
125 static void print_version(void)
126 {
127 printf("%s\n", version);
128 }
129
130 static void print_help(char *const *argv)
131 {
132 print_version();
133 printf(_("\nUsage: %s [OPTIONS]... [FILE]\n"), argv[0]);
134 printf(_("\n\
135 -h, --help Print help and exit\n\
136 -v, --version Print version and exit\n\
137 -n, --foreground Don't go into the background (default=off)\n\
138 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
139 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
140 -m --make-userfile Userfile creation mode\n\
141 -d --debug Do a debugging dry-run\n\
142 FILE optional config filename (default 'config.xml')\n"));
143 printf("\n");
144 }
145
146 static void do_args(int argc, char **argv)
147 {
148 int c;
149
150 optarg = 0;
151 optind = 1;
152 opterr = 1;
153 optopt = '?';
154
155 while (1) {
156 int option_index = 0;
157
158 static struct option long_options[] = {
159 { "help", 0, NULL, 'h' },
160 { "version", 0, NULL, 'v' },
161 { "load-module", 1, NULL, 'p' },
162 { "foreground", 0, NULL, 'n' },
163 { "terminal", 0, NULL, 't' },
164 { "make-userfile", 0, NULL, 'm' },
165 { "debug", 0, NULL, 'd' },
166 {NULL, 0, NULL, 0}
167 };
168
169 c = getopt_long(argc, argv, "hvp:nctmd", long_options, &option_index);
170 if (c == -1) break;
171 switch (c) {
172 case 'n':
173 backgrd = 0;
174 break;
175 case 't':
176 terminal_mode = 1;
177 break;
178 case 'm':
179 make_userfile = 1;
180 break;
181 case 'd':
182 backgrd = 0;
183 debug_run = 1;
184 break;
185 case 'v':
186 print_version();
187 exit(0);
188 break; /* this should never be reached */
189 case 'h':
190 print_help(argv);
191 exit(0);
192 break; /* this should never be reached */
193 case 0: /* Long option with no short option */
194 case '?': /* Invalid option. */
195 /* getopt_long() already printed an error message. */
196 exit(1);
197 default: /* bug: option not considered. */
198 fprintf(stderr, _("%s: option unknown: %c\n"), argv[0], c);
199 exit(1);
200 break; /* this should never be reached */
201 }
202 }
203 if (optind < argc) {
204 configfile = argv[optind++];
205 eggdrop_set_params((const char **)&argv[optind], argc-optind);
206 }
207 else configfile = "config.xml";
208 }
209
210 /* Called once a second.
211 */
212 static int core_secondly()
213 {
214 static int cnt = 0;
215 int miltime;
216
217 check_bind_secondly();
218 cnt++;
219 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
220 if (nowtm.tm_min != lastmin) {
221 int i = 0;
222
223 /* Once a minute */
224 lastmin = (lastmin + 1) % 60;
225 eggdrop_event("minutely");
226 check_bind_time(&nowtm);
227 /* In case for some reason more than 1 min has passed: */
228 while (nowtm.tm_min != lastmin) {
229 /* Timer drift, dammit */
230 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
231 i++;
232 lastmin = (lastmin + 1) % 60;
233 eggdrop_event("minutely");
234 }
235 if (i > 1) putlog(LOG_MISC, "*", _("Warning: timer drift (%d minutes)."), i);
236
237 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
238 if (nowtm.tm_min % 5 == 0) { /* 5 minutes */
239 eggdrop_event("5minutely");
240 if (!miltime) { /* At midnight */
241 char s[25];
242
243 strlcpy(s, ctime(&now), sizeof s);
244 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
245 eggdrop_event("backup");
246 eggdrop_event("daily");
247 }
248 }
249 }
250 return(0);
251 }
252
253
254 int file_check(const char *filename)
255 {
256 struct stat sbuf = {0};
257 int r;
258
259 r = stat(filename, &sbuf);
260 if (r) return(-1);
261 errno = 0;
262 if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
263 return(0);
264 }
265
266 static int create_userfile(void)
267 {
268 user_t *owner;
269 char handle[512], password[12];
270 int len;
271
272 printf("\n");
273 if (user_count() != 0) {
274 printf("You are trying to create a new userfile, but the old one still exists (%s)!\n", core_config.userfile);
275 printf("Please remove the userfile, or do not pass the -m option.\n\n");
276 return(-1);
277 }
278
279 printf("Hello! I see you are creating a new userfile.\n\n");
280 printf("Let's create the owner account.\n\n");
281 do {
282 printf("Enter the owner handle: ");
283 fflush(stdout);
284 fgets(handle, sizeof(handle), stdin);
285 for (len = 0; handle[len]; len++) {
286 if (!ispunct(handle[len]) && !isalnum(handle[len])) break;
287 }
288 if (len == 0) printf("Come on, enter a real handle.\n\n");
289 } while (len <= 0);
290 handle[len] = 0;
291
292 owner = user_new(handle);
293 if (!owner) {
294 printf("Failed to create user record! Out of memory?\n\n");
295 return(-1);
296 }
297
298 user_rand_pass(password, sizeof(password));
299 user_set_pass(owner, password);
300 user_set_flags_str(owner, NULL, "+n");
301 printf("Your owner handle is '%s' and your password is '%s' (without the quotes).\n\n", handle, password);
302 memset(password, 0, sizeof(password));
303 str_redup(&core_config.owner, handle);
304 core_config_save();
305 user_save(core_config.userfile);
306
307 return(0);
308 }
309
310 static void init_signals(void)
311 {
312 struct sigaction sv;
313
314
315 sv.sa_handler = got_bus;
316 sigemptyset(&sv.sa_mask);
317 #ifdef SA_RESETHAND
318 sv.sa_flags = SA_RESETHAND;
319 #else
320 sv.sa_flags = 0;
321 #endif
322 sigaction(SIGBUS, &sv, NULL);
323
324 sv.sa_handler = got_segv;
325 //sigaction(SIGSEGV, &sv, NULL);
326 #ifdef SA_RESETHAND
327 sv.sa_flags = 0;
328 #endif
329
330 sv.sa_handler = got_fpe;
331 sigaction(SIGFPE, &sv, NULL);
332
333 sv.sa_handler = got_term;
334 sigaction(SIGTERM, &sv, NULL);
335
336 sv.sa_handler = got_hup;
337 sigaction(SIGHUP, &sv, NULL);
338
339 sv.sa_handler = got_quit;
340 sigaction(SIGQUIT, &sv, NULL);
341
342 sv.sa_handler = SIG_IGN;
343 sigaction(SIGPIPE, &sv, NULL);
344
345 sv.sa_handler = SIG_IGN;
346 sigaction(SIGUSR1, &sv, NULL);
347 }
348
349 static void core_shutdown_or_restart()
350 {
351 /* notifiy shutdown listeners */
352 check_bind_shutdown();
353
354 putlog(LOG_MISC, "*", _("Saving user file..."));
355 user_save(core_config.userfile);
356
357 /* destroy terminal */
358 if (terminal_mode && runmode != RUNMODE_RESTART)
359 terminal_shutdown();
360
361 /* unload core help */
362 //help_unload_by_module("core");
363
364 /* shutdown core binds */
365 core_binds_shutdown();
366
367 /* shutdown logging */
368 logfile_shutdown();
369
370 putlog(LOG_MISC, "*", _("Saving config file..."));
371 core_config_save();
372
373 /* shutdown libeggdrop */
374 eggdrop_shutdown();
375
376 /* force a garbage run */
377 garbage_run();
378
379 /* just remove pid file if didn't restart */
380 if (runmode != RUNMODE_RESTART)
381 unlink(pid_file);
382 }
383
384 int main(int argc, char **argv)
385 {
386 int timeout;
387
388 #ifdef DEBUG
389 {
390 # include <sys/resource.h>
391 struct rlimit cdlim;
392
393 cdlim.rlim_cur = RLIM_INFINITY;
394 cdlim.rlim_max = RLIM_INFINITY;
395 setrlimit(RLIMIT_CORE, &cdlim);
396 }
397 #endif
398
399 init_signals();
400
401 #ifdef ENABLE_NLS
402 setlocale(LC_MESSAGES, "");
403 bindtextdomain(PACKAGE, LOCALEDIR);
404 textdomain(PACKAGE);
405 #endif
406
407 /* Initialize ltdl. */
408 LTDL_SET_PRELOADED_SYMBOLS();
409 if (lt_dlinit()) {
410 printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
411 return(-1);
412 }
413
414 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2004 Eggheads Development Team", VERSION);
415
416 #ifdef STOP_UAC
417 {
418 int nvpair[2];
419
420 nvpair[0] = SSIN_UACPROC;
421 nvpair[1] = UAC_NOPRINT;
422 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
423 }
424 #endif
425
426 timer_update_now(&egg_timeval_now);
427 now = egg_timeval_now.sec;
428 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
429 lastmin = nowtm.tm_min;
430
431 srandom(now % (getpid() + getppid()));
432
433 do_args(argc, argv);
434 printf("\n%s\n", version);
435 printf("WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
436
437 /* we may not run as root */
438 if (((int) getuid() == 0) || ((int) geteuid() == 0)) {
439 fprintf(stderr, "ERROR\n");
440 fprintf(stderr, "\tEggdrop will not run as root!\n\n");
441 return -1;
442 }
443
444 init_flag_map();
445
446 /* config file may not be world read/writeable */
447 if (file_check(configfile)) {
448 fprintf(stderr, "ERROR\n");
449 if (errno) {
450 fprintf(stderr, "\tFailed to load config file '%s': %s\n\n", configfile, strerror(errno));
451 } else {
452 fprintf(stderr, "\tCheck file permissions on your config file '%s'!\n", configfile);
453 fprintf(stderr, "\tMake sure other groups and users cannot read/write it.\n\n");
454 }
455 return -1;
456 }
457
458 /* set uptime */
459 online_since = now;
460
461 do {
462 int tid;
463 egg_timeval_t howlong;
464
465 /* default select timeout is 1 sec */
466 howlong.sec = 1;
467 howlong.usec = 0;
468
469 tid = timer_create_repeater(&howlong, "main loop", core_secondly);
470
471 /* init core */
472 if (core_init() != 0)
473 break;
474
475 /* set normal running mode */
476 runmode = RUNMODE_NORMAL;
477
478 putlog(LOG_DEBUG, "*", "Entering main loop.");
479
480 /* main loop */
481 while (!debug_run && runmode == RUNMODE_NORMAL) {
482 /* Update the time. */
483 timer_update_now(&egg_timeval_now);
484 now = egg_timeval_now.sec;
485
486 /* Run any expired timers. */
487 timer_run();
488
489 /* Figure out how long to wait for activity. */
490 if (timer_get_shortest(&howlong)) timeout = 1000;
491 else timeout = howlong.sec * 1000 + howlong.usec / 1000;
492
493 /* Wait and monitor sockets. */
494 sockbuf_update_all(timeout);
495
496 /* Run any scheduled cleanups. */
497 garbage_run();
498 }
499
500 /* shutdown secondly timer */
501 timer_destroy(tid);
502
503 /* Save user file, config file, ... */
504 core_shutdown_or_restart();
505
506 /* the only chance to loop again is that running = 2, meaning
507 we have a restart */
508 } while (runmode == RUNMODE_RESTART);
509
510 return 0;
511 }
512
513 int core_init()
514 {
515 int i;
516 char *name;
517 void *config_root, *entry;
518 char datetime[25];
519 static int lockfd = -1;
520 struct flock lock;
521
522 /* init libeggdrop */
523 eggdrop_init();
524
525 /* load config */
526 if (core_config_init(configfile) != 0)
527 return (-1);
528
529 /* init logging */
530 logfile_init();
531
532 /* did the user specify -m? */
533 if (make_userfile) {
534 if (create_userfile()) return(-1);
535 make_userfile = 0;
536 }
537
538 /* init background mode and pid file */
539 if (runmode != RUNMODE_RESTART) {
540 if (backgrd) bg_begin_split();
541 }
542
543 /* Check lock file. */
544 if (core_config.lockfile && lockfd == -1) {
545 lockfd = open(core_config.lockfile, O_WRONLY | O_CREAT, S_IWUSR);
546 if (lockfd < 0) {
547 fatal("Could not open lock file!");
548 }
549 memset(&lock, 0, sizeof(lock));
550 lock.l_type = F_WRLCK;
551 if (fcntl(lockfd, F_SETLK, &lock) != 0) {
552 fatal("Lock file is already locked! Is eggdrop already running?");
553 }
554 }
555
556 /* just issue this "Loading Eggdrop" message if we are not
557 * restarting */
558 if (runmode != RUNMODE_RESTART) {
559 strlcpy(datetime, ctime(&now), sizeof(datetime));
560 strcpy(&datetime[11], &datetime[20]);
561 putlog(LOG_ALL, "*", "Loading Eggdrop %s (%s)", VERSION, datetime);
562 }
563
564 /* load userlist */
565 if (core_config.userfile)
566 user_load(core_config.userfile);
567
568 /* init core bindings */
569 core_binds_init();
570
571 /* init core partyline */
572 core_party_init();
573
574 /* Load core help */
575 help_load_by_module ("core");
576
577 /* Put the module directory in the ltdl search path. */
578 if (core_config.module_path) module_add_dir(core_config.module_path);
579
580 /* Initialize help system. */
581 if (core_config.help_path) {
582 help_set_default_path(core_config.help_path);
583 /* Load all help files here... */
584 }
585
586 /* Scan the autoload section of config. */
587 config_root = config_get_root("eggdrop");
588 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
589 name = NULL;
590 config_get_str(&name, entry, NULL);
591 module_load(name);
592 /* For now we'll just load these help files... */
593 help_load_by_module(name);
594 }
595 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
596 name = NULL;
597 config_get_str(&name, entry, NULL);
598 script_load(name);
599 }
600
601 /* notify init listeners */
602 check_bind_init();
603
604 /* start terminal */
605 if (runmode != RUNMODE_RESTART) {
606 if (backgrd) bg_finish_split();
607 if (terminal_mode) terminal_init();
608 }
609
610 return (0);
611 }
612
613 int core_restart(const char *nick)
614 {
615 putlog(LOG_MISC, "*", "Restarting...");
616 runmode = RUNMODE_RESTART;
617 return (0);
618 }
619
620 int core_shutdown(int how, const char *nick, const char *reason)
621 {
622 if (how == SHUTDOWN_SIGTERM)
623 putlog(LOG_MISC, "*", ("Received TERM signal, shutting down."));
624 else if (how != SHUTDOWN_RESTART)
625 putlog(LOG_MISC, "*", ("Shutdown requested by %s: %s"), nick,
626 (reason) ? reason : _("No reason"));
627
628 /* check how to shut down */
629 switch (how) {
630
631 case (SHUTDOWN_RESTART):
632 runmode = RUNMODE_RESTART;
633 break;
634
635 case (SHUTDOWN_GRACEFULL):
636 runmode = RUNMODE_SHUTDOWN;
637 break;
638
639 /* These two cases are treaded special because something
640 * unusual happened and we're better off if we exit() NOW
641 * than doing a gracefull shutdown with freeing all
642 * memory, closing all sockets, ... */
643 case (SHUTDOWN_HARD):
644 case (SHUTDOWN_SIGTERM):
645 runmode = RUNMODE_SHUTDOWN;
646 core_shutdown_or_restart();
647 exit(0);
648 break;
649
650 }
651
652 return BIND_RET_LOG;
653 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23