/[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.167 - (show annotations) (download) (as text)
Tue Jun 15 11:24:46 2004 UTC (15 years, 4 months ago) by wingman
Branch: MAIN
Changes since 1.166: +9 -4 lines
File MIME type: text/x-chdr
* Assertion outputs shouldn't be done via putlog
* stdin/stdout can now be used with sockbufs (just create a new sockbuf
  with socket fileno(stdout))
* Added terminal mode (--terminal/-t). Though not the best solution yet
  since telnetparty needs to be loaded but it's much easier now to
  debug/test things now.
* Added two new binds:

	init: executed after everything (core,modules,scripts) is loaded
		and right before main loop.

	shutdown: executed before shutting down the bot (currently never
		called since we'll never leave our main loop and there
		are places like party_die which calls exit() manually).

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23