/[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.169 - (show annotations) (download) (as text)
Thu Jun 17 13:32:44 2004 UTC (15 years, 1 month ago) by wingman
Branch: MAIN
Changes since 1.168: +9 -5 lines
File MIME type: text/x-chdr
* Added new TAG: DDC for "this feature is document". After we are sure
  that all source code variables/commands/functions/ are tagged with either
  DDD or DDC we'll remove them.

* Made module Makefile.am's more centralized by moving common stuff
  to acconfig/module.mk.

* Introducted new help system. It's now on per module basis and localized.
  Default language is "en_EN" else not otherwise specified. Beside the
  %b formattings of 1.6 it has all features of the 1.6 help system.

  It's also more intelligent since help files are automaticly bound to
  module load/unload events. You don't need to manually load module help
  files any more. (Though unloading not tested yet... 8-))

* Fixed some tweaks here and there.

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.168 2004/06/15 11:54:33 wingman Exp $";
23 #endif
24
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <eggdrop/eggdrop.h>
30 #include <ctype.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <netdb.h>
39 #include <setjmp.h>
40 #include <locale.h>
41 #include <ctype.h>
42 #include <ltdl.h>
43
44 #include <eggdrop/stat.h>
45
46 #ifdef TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # ifdef HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 # else
53 # include <time.h>
54 # endif
55 #endif
56
57 #include "lib/compat/compat.h"
58 #include "debug.h"
59 #include "core_config.h"
60 #include "core_party.h"
61 #include "core_binds.h"
62 #include "logfile.h"
63 #include "bg.h"
64 #include "main.h"
65
66 #ifdef STOP_UAC /* OSF/1 complains a lot */
67 # include <sys/sysinfo.h>
68 # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
69 #endif
70
71 #ifdef CYGWIN_HACKS
72 # include <windows.h>
73 #endif
74
75 #ifndef _POSIX_SOURCE
76 # define _POSIX_SOURCE 1 /* Solaris needs this */
77 #endif
78
79 int backgrd = 1; /* Run in the background? */
80 int con_chan = 0; /* Foreground: constantly display channel stats? */
81 int make_userfile = 0; /* Start bot in make-userfile mode? */
82
83 const char *configfile = NULL; /* Name of the config file */
84 char pid_file[512]; /* Name of Eggdrop's pid file */
85
86 time_t online_since; /* Time the bot was started */
87 time_t now; /* Current time */
88 egg_timeval_t egg_timeval_now; /* Current time in seconds and microseconds. */
89
90 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
91
92 char version[81];
93
94 static int lastmin = 99;
95 static struct tm nowtm;
96
97 void core_config_init();
98
99 void fatal(const char *s, int recoverable)
100 {
101 putlog(LOG_MISC, "*", "Fatal: %s", s);
102 flushlogs();
103 unlink(pid_file);
104 if (!recoverable) {
105 bg_send_quit(BG_ABORT);
106 exit(1);
107 }
108 }
109
110 static void got_bus(int z)
111 {
112 fatal("BUS ERROR.", 1);
113 #ifdef SA_RESETHAND
114 kill(getpid(), SIGBUS);
115 #else
116 bg_send_quit(BG_ABORT);
117 exit(1);
118 #endif
119 }
120
121 static void got_segv(int z)
122 {
123 fatal("SEGMENT VIOLATION.", 1);
124 #ifdef SA_RESETHAND
125 kill(getpid(), SIGSEGV);
126 #else
127 bg_send_quit(BG_ABORT);
128 exit(1);
129 #endif
130 }
131
132 static void got_fpe(int z)
133 {
134 fatal("FLOATING POINT ERROR.", 0);
135 }
136
137 static void got_term(int z)
138 {
139 eggdrop_event("sigterm");
140 if (core_config.die_on_sigterm) {
141 putlog(LOG_MISC, "*", _("Saving user file..."));
142 user_save(core_config.userfile);
143 putlog(LOG_MISC, "*", _("Saving config file..."));
144 core_config_save();
145 putlog(LOG_MISC, "*", _("Bot shutting down: received TERMINATE signal."));
146 flushlogs();
147 unlink(pid_file);
148 exit(0);
149 }
150 else putlog(LOG_MISC, "*", _("Received TERMINATE signal (ignoring)."));
151 }
152
153 static void got_quit(int z)
154 {
155 eggdrop_event("sigquit");
156 putlog(LOG_MISC, "*", _("Received QUIT signal (ignoring)."));
157 return;
158 }
159
160 static void got_hup(int z)
161 {
162 eggdrop_event("sighup");
163 putlog(LOG_MISC, "*", _("Received HUP signal (ignoring)."));
164 return;
165 }
166
167 #if 0
168 static void got_ill(int z)
169 {
170 eggdrop_event("sigill");
171 putlog(LOG_MISC, "*", _("Received ILL signal (ignoring)."));
172 }
173 #endif
174
175
176 static void print_version(void)
177 {
178 printf("%s\n", version);
179 }
180
181 static void print_help(char *const *argv)
182 {
183 print_version();
184 printf(_("\nUsage: %s [OPTIONS]... [FILE]\n"), argv[0]);
185 printf(_("\n\
186 -h, --help Print help and exit\n\
187 -v, --version Print version and exit\n\
188 -n, --foreground Don't go into the background (default=off)\n\
189 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
190 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
191 -m --make-userfile Userfile creation mode\n\
192 FILE optional config filename (default 'config.xml')\n"));
193 printf("\n");
194 }
195
196 static void do_args(int argc, char *const *argv)
197 {
198 int c;
199
200 optarg = 0;
201 optind = 1;
202 opterr = 1;
203 optopt = '?';
204
205 while (1) {
206 int option_index = 0;
207
208 static struct option long_options[] = {
209 {"help", 0, NULL, 'h'},
210 {"version", 0, NULL, 'v'},
211 {"load-module", 1, NULL, 'p'},
212 {"foreground", 0, NULL, 'n'},
213 {"channel-stats", 0, NULL, 'c'},
214 {"terminal", 0, NULL, 't'},
215 {"make-userfile", 0, NULL, 'm'},
216 {NULL, 0, NULL, 0}
217 };
218
219 c = getopt_long(argc, argv, "hvp:nctm", long_options, &option_index);
220 if (c == -1) break;
221 switch (c) {
222 case 'n':
223 backgrd = 0;
224 break;
225 case 'c':
226 con_chan = 1;
227 partyline_terminal_mode = 0;
228 break;
229 case 't':
230 con_chan = 0;
231 partyline_terminal_mode = 1;
232 break;
233 case 'm':
234 make_userfile = 1;
235 break;
236 case 'v':
237 print_version();
238 bg_send_quit(BG_ABORT);
239 exit(0);
240 break; /* this should never be reached */
241 case 'h':
242 print_help(argv);
243 bg_send_quit(BG_ABORT);
244 exit(0);
245 break; /* this should never be reached */
246 case 0: /* Long option with no short option */
247 case '?': /* Invalid option. */
248 /* getopt_long() already printed an error message. */
249 bg_send_quit(BG_ABORT);
250 exit(1);
251 default: /* bug: option not considered. */
252 fprintf(stderr, _("%s: option unknown: %c\n"), argv[0], c);
253 bg_send_quit(BG_ABORT);
254 exit(1);
255 break; /* this should never be reached */
256 }
257 }
258 if (optind < argc) configfile = argv[optind];
259 else configfile = "config.xml";
260 }
261
262 /* Called once a second.
263 */
264 static int core_secondly()
265 {
266 static int cnt = 0;
267 int miltime;
268
269 check_bind_secondly();
270 cnt++;
271 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
272 if (nowtm.tm_min != lastmin) {
273 int i = 0;
274
275 /* Once a minute */
276 lastmin = (lastmin + 1) % 60;
277 eggdrop_event("minutely");
278 check_bind_time(&nowtm);
279 /* In case for some reason more than 1 min has passed: */
280 while (nowtm.tm_min != lastmin) {
281 /* Timer drift, dammit */
282 putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
283 i++;
284 lastmin = (lastmin + 1) % 60;
285 eggdrop_event("minutely");
286 }
287 if (i > 1) putlog(LOG_MISC, "*", _("Warning: timer drift (%d minutes)."), i);
288
289 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
290 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 minutes */
291 eggdrop_event("5minutely");
292 if (!miltime) { /* At midnight */
293 char s[25];
294
295 strlcpy(s, ctime(&now), sizeof s);
296 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
297 eggdrop_event("backup");
298 eggdrop_event("daily");
299 }
300 }
301 }
302 return(0);
303 }
304
305
306 int file_check(const char *filename)
307 {
308 struct stat sbuf = {0};
309 int r;
310
311 r = stat(filename, &sbuf);
312 if (r) return(-1);
313 errno = 0;
314 if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
315 return(0);
316 }
317
318 int main(int argc, char **argv)
319 {
320 int xx, i, timeout;
321 char s[25], *modname, *scriptname;
322 FILE *f;
323 struct sigaction sv;
324 egg_timeval_t howlong;
325 void *config_root, *entry;
326
327 #ifdef DEBUG
328 {
329 # include <sys/resource.h>
330 struct rlimit cdlim;
331
332 cdlim.rlim_cur = RLIM_INFINITY;
333 cdlim.rlim_max = RLIM_INFINITY;
334 setrlimit(RLIMIT_CORE, &cdlim);
335 }
336 #endif
337
338 #ifdef ENABLE_NLS
339 setlocale(LC_MESSAGES, "");
340 bindtextdomain(PACKAGE, LOCALEDIR);
341 textdomain(PACKAGE);
342 #endif
343
344 /* Initialize ltdl. */
345 LTDL_SET_PRELOADED_SYMBOLS();
346 if (lt_dlinit()) {
347 printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
348 return(-1);
349 }
350
351 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2004 Eggheads Development Team", VERSION);
352
353 #ifdef STOP_UAC
354 {
355 int nvpair[2];
356
357 nvpair[0] = SSIN_UACPROC;
358 nvpair[1] = UAC_NOPRINT;
359 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
360 }
361 #endif
362
363 sv.sa_handler = got_bus;
364 sigemptyset(&sv.sa_mask);
365 #ifdef SA_RESETHAND
366 sv.sa_flags = SA_RESETHAND;
367 #else
368 sv.sa_flags = 0;
369 #endif
370 sigaction(SIGBUS, &sv, NULL);
371
372 sv.sa_handler = got_segv;
373 sigaction(SIGSEGV, &sv, NULL);
374 #ifdef SA_RESETHAND
375 sv.sa_flags = 0;
376 #endif
377
378 sv.sa_handler = got_fpe;
379 sigaction(SIGFPE, &sv, NULL);
380
381 sv.sa_handler = got_term;
382 sigaction(SIGTERM, &sv, NULL);
383
384 sv.sa_handler = got_hup;
385 sigaction(SIGHUP, &sv, NULL);
386
387 sv.sa_handler = got_quit;
388 sigaction(SIGQUIT, &sv, NULL);
389
390 sv.sa_handler = SIG_IGN;
391 sigaction(SIGPIPE, &sv, NULL);
392
393 /* XXX: why is this disabled? */
394 #if 0
395 sv.sa_handler = got_ill;
396 sigaction(SIGILL, &sv, NULL);
397 #endif
398
399 timer_update_now(&egg_timeval_now);
400 now = egg_timeval_now.sec;
401 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
402 lastmin = nowtm.tm_min;
403
404 srandom(now % (getpid() + getppid()));
405
406 do_args(argc, argv);
407 printf("\n%s\n", version);
408 printf("WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
409
410 if (((int) getuid() == 0) || ((int) geteuid() == 0)) fatal("Eggdrop will not run as root!", 0);
411
412 if (file_check(configfile)) {
413 if (errno) perror(configfile);
414 else fprintf(stderr, "ERROR\n\nCheck file permissions on your config file!\nMake sure other groups and users cannot read/write it.\n");
415 exit(-1);
416 }
417
418 eggdrop_init();
419 core_config_init(configfile);
420 logfile_init();
421 if (core_config.userfile)
422 user_load(core_config.userfile);
423 core_party_init();
424 core_binds_init();
425
426 if (make_userfile) {
427 user_t *owner;
428 char handle[512], password[12];
429 int len;
430
431 if (user_count() != 0) {
432 printf("\n\n\nYou are trying to create a new userfile, but the old one still exists (%s)!\n\nPlease remove the userfile, or do not pass the -m option.\n\n", core_config.userfile);
433 exit(1);
434 }
435
436 printf("\n\n\nHello! I see you are creating a new userfile.\n\n");
437 printf("Let's create the owner account.\n\n");
438 do {
439 printf("Enter the owner handle: ");
440 fflush(stdout);
441 fgets(handle, sizeof(handle), stdin);
442 for (len = 0; handle[len]; len++) {
443 if (!ispunct(handle[len]) && !isalnum(handle[len])) break;
444 }
445 if (len == 0) printf("Come on, enter a real handle.\n\n");
446 } while (len <= 0);
447 handle[len] = 0;
448 owner = user_new(handle);
449 if (!owner) {
450 printf("Failed to create user record! Out of memory?\n\n");
451 exit(1);
452 }
453 user_rand_pass(password, sizeof(password));
454 user_set_pass(owner, password);
455 user_set_flags_str(owner, NULL, "+n");
456 printf("Your owner handle is '%s' and your password is '%s' (without the quotes).\n\n", handle, password);
457 memset(password, 0, sizeof(password));
458 str_redup(&core_config.owner, handle);
459 core_config_save();
460 user_save(core_config.userfile);
461 }
462
463 if (backgrd) bg_prepare_split();
464
465 strlcpy(s, ctime(&now), sizeof s);
466 strcpy(&s[11], &s[20]);
467 putlog(LOG_ALL, "*", "Loading Eggdrop %s (%s)", VERSION, s);
468 snprintf(pid_file, sizeof pid_file, "pid.%s", core_config.botname);
469
470 f = fopen(pid_file, "r");
471 if (f != NULL) {
472 fgets(s, 10, f);
473 xx = atoi(s);
474 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid is used */
475 if (errno != ESRCH) {
476 printf("I detect %s already running from this directory.\n", core_config.botname);
477 printf("If this is incorrect, please erase '%s'.\n", pid_file);
478 bg_send_quit(BG_ABORT);
479 exit(1);
480 }
481 }
482
483 /* Load core help */
484 help_load_by_module ("core");
485
486 /* Put the module directory in the ltdl search path. */
487 if (core_config.module_path)
488 module_add_dir(core_config.module_path);
489
490 /* Scan the autoload section of config. */
491 config_root = config_get_root("eggdrop");
492 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
493 modname = NULL;
494 config_get_str(&modname, entry, NULL);
495 module_load(modname);
496 }
497 for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
498 scriptname = NULL;
499 config_get_str(&scriptname, entry, NULL);
500 script_load(scriptname);
501 }
502
503 if (backgrd) { /* Move into background? */
504 #ifndef CYGWIN_HACKS
505 bg_do_split();
506 }
507 else {
508 #endif
509 xx = getpid();
510 if (xx != 0) {
511 FILE *fp;
512
513 unlink(pid_file);
514 fp = fopen(pid_file, "w");
515 if (fp != NULL) {
516 fprintf(fp, "%u\n", xx);
517 if (fflush(fp)) {
518 /* Let the bot live since this doesn't appear to be a botchk */
519 printf("WARNING: Could not write pid file '%s'.\n", pid_file);
520 fclose(fp);
521 unlink(pid_file);
522 }
523 else fclose(fp);
524 }
525 else printf("WARNING: Could not write pid file '%s'.\n", pid_file);
526 #ifdef CYGWIN_HACKS
527 printf("Launched into the background (pid: %d).\n\n", xx);
528 #endif
529 }
530 }
531
532 use_stderr = 0; /* Stop writing to stderr now */
533
534 if (backgrd) {
535 /* Try to disassociate from controlling terminal (finger cross) */
536 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
537 setpgid(0, 0);
538 #endif
539 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
540 freopen("/dev/null", "r", stdin);
541 freopen("/dev/null", "w", stdout);
542 freopen("/dev/null", "w", stderr);
543 #ifdef CYGWIN_HACKS
544 FreeConsole();
545 #endif
546 }
547
548 online_since = now;
549 howlong.sec = 1;
550 howlong.usec = 0;
551 timer_create_repeater(&howlong, "main loop", core_secondly);
552
553 putlog(LOG_DEBUG, "*", "Entering main loop.");
554
555 check_bind_init ();
556
557 while (1) {
558 timer_update_now(&egg_timeval_now);
559 now = egg_timeval_now.sec;
560 random(); /* Woop, lets really jumble things */
561 timer_run();
562 if (timer_get_shortest(&howlong)) timeout = 1000;
563 else timeout = howlong.sec * 1000 + howlong.usec / 1000;
564 sockbuf_update_all(timeout);
565 garbage_run();
566 }
567
568 check_bind_shutdown ();
569 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23