/[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.161 - (show annotations) (download) (as text)
Thu Dec 18 06:50:47 2003 UTC (15 years, 7 months ago) by wcc
Branch: MAIN
Changes since 1.160: +3 -2 lines
File MIME type: text/x-chdr
* More GPL/header/rcsid stuff.

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.160 2003/12/18 03:54:46 wcc 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 "main.h"
59 #include "debug.h"
60 #include "core_config.h"
61 #include "bg.h"
62 #include "core_binds.h"
63 #include "logfile.h"
64
65 #ifdef STOP_UAC /* OSF/1 complains a lot */
66 # include <sys/sysinfo.h>
67 # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
68 #endif
69
70 #ifdef CYGWIN_HACKS
71 # include <windows.h>
72 #endif
73
74 #ifndef _POSIX_SOURCE
75 # define _POSIX_SOURCE 1 /* Solaris needs this */
76 #endif
77
78 int backgrd = 1; /* Run in the background? */
79 int con_chan = 0; /* Foreground: constantly display channel stats? */
80 int term_z = 0; /* Foreground: use the terminal as a partyline? */
81 int make_userfile = 0; /* Start bot in make-userfile mode? */
82
83 char configfile[121] = "config.xml"; /* Name of the config file */
84 char helpdir[121] = "help/"; /* Directory of help files (if used) */
85 char textdir[121] = "text/"; /* Directory for text files that get dumped */
86 char pid_file[120]; /* Name of Eggdrop's pid file */
87
88 time_t online_since; /* Time the bot was started */
89 time_t now; /* Current time */
90 egg_timeval_t egg_timeval_now; /* Current time in seconds and microseconds. */
91
92 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
93
94 char version[81];
95
96 static int lastmin = 99;
97 static struct tm nowtm;
98
99 void core_party_init();
100 void core_config_init();
101
102 void fatal(const char *s, int recoverable)
103 {
104 putlog(LOG_MISC, "*", "Fatal: %s", s);
105 flushlogs();
106 unlink(pid_file);
107 if (!recoverable) {
108 bg_send_quit(BG_ABORT);
109 exit(1);
110 }
111 }
112
113 static void got_bus(int z)
114 {
115 fatal("BUS ERROR.", 1);
116 #ifdef SA_RESETHAND
117 kill(getpid(), SIGBUS);
118 #else
119 bg_send_quit(BG_ABORT);
120 exit(1);
121 #endif
122 }
123
124 static void got_segv(int z)
125 {
126 fatal("SEGMENT VIOLATION.", 1);
127 #ifdef SA_RESETHAND
128 kill(getpid(), SIGSEGV);
129 #else
130 bg_send_quit(BG_ABORT);
131 exit(1);
132 #endif
133 }
134
135 static void got_fpe(int z)
136 {
137 fatal("FLOATING POINT ERROR.", 0);
138 }
139
140 static void got_term(int z)
141 {
142 eggdrop_event("sigterm");
143 if (core_config.die_on_sigterm) {
144 putlog(LOG_MISC, "*", _("Saving user file..."));
145 user_save(core_config.userfile);
146 putlog(LOG_MISC, "*", _("Saving config file..."));
147 core_config_save();
148 putlog(LOG_MISC, "*", _("Bot shutting down: received TERMINATE signal."));
149 flushlogs();
150 unlink(pid_file);
151 exit(0);
152 }
153 else putlog(LOG_MISC, "*", _("Received TERMINATE signal (ignoring)."));
154 }
155
156 static void got_quit(int z)
157 {
158 eggdrop_event("sigquit");
159 putlog(LOG_MISC, "*", _("Received QUIT signal (ignoring)."));
160 return;
161 }
162
163 static void got_hup(int z)
164 {
165 eggdrop_event("sighup");
166 putlog(LOG_MISC, "*", _("Received HUP signal (ignoring)."));
167 return;
168 }
169
170 static void got_ill(int z)
171 {
172 eggdrop_event("sigill");
173 putlog(LOG_MISC, "*", _("Received ILL signal (ignoring)."));
174 }
175
176
177 static void print_version(void)
178 {
179 printf("%s\n", version);
180 }
181
182 static void print_help(const char **argv)
183 {
184 print_version();
185 printf(_("\nUsage: %s [OPTIONS]... [FILE]\n"), argv[0]);
186 printf(_("\n\
187 -h, --help Print help and exit\n\
188 -v, --version Print version and exit\n\
189 -n, --foreground Don't go into the background (default=off)\n\
190 -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
191 -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
192 -m --make-userfile Userfile creation mode\n\
193 FILE optional config filename (default 'config.xml')\n"));
194 printf("\n");
195 }
196
197 static void do_args(int argc, char *const *argv)
198 {
199 int c;
200
201 optarg = 0;
202 optind = 1;
203 opterr = 1;
204 optopt = '?';
205
206 while (1) {
207 int option_index = 0;
208
209 static struct option long_options[] = {
210 {"help", 0, NULL, 'h'},
211 {"version", 0, NULL, 'v'},
212 {"load-module", 1, NULL, 'p'},
213 {"foreground", 0, NULL, 'n'},
214 {"channel-stats", 0, NULL, 'c'},
215 {"terminal", 0, NULL, 't'},
216 {"make-userfile", 0, NULL, 'm'},
217 {NULL, 0, NULL, 0}
218 };
219
220 c = getopt_long(argc, argv, "hvp:nctm", long_options, &option_index);
221 if (c == -1) break;
222 switch (c) {
223 case 'n':
224 backgrd = 0;
225 break;
226 case 'c':
227 con_chan = 1;
228 term_z = 0;
229 break;
230 case 't':
231 con_chan = 0;
232 term_z = 1;
233 break;
234 case 'm':
235 make_userfile = 1;
236 break;
237 case 'v':
238 print_version();
239 bg_send_quit(BG_ABORT);
240 exit(0);
241 break; /* this should never be reached */
242 case 'h':
243 print_help(argv);
244 bg_send_quit(BG_ABORT);
245 exit(0);
246 break; /* this should never be reached */
247 case 0: /* Long option with no short option */
248 case '?': /* Invalid option. */
249 /* getopt_long() already printed an error message. */
250 bg_send_quit(BG_ABORT);
251 exit(1);
252 default: /* bug: option not considered. */
253 fprintf(stderr, _("%s: option unknown: %c\n"), argv[0], c);
254 bg_send_quit(BG_ABORT);
255 exit(1);
256 break; /* this should never be reached */
257 }
258 }
259 if (optind < argc) strlcpy(configfile, argv[optind], sizeof configfile);
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 sv.sa_handler = got_ill;
394 sigaction(SIGILL, &sv, NULL);
395
396 timer_update_now(&egg_timeval_now);
397 now = egg_timeval_now.sec;
398 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
399 lastmin = nowtm.tm_min;
400
401 srandom(now % (getpid() + getppid()));
402
403 do_args(argc, argv);
404 printf("\n%s\n", version);
405 printf("WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
406
407 if (((int) getuid() == 0) || ((int) geteuid() == 0)) fatal("Eggdrop will not run as root!", 0);
408
409 if (file_check(configfile)) {
410 if (errno) perror(configfile);
411 else fprintf(stderr, "ERROR\n\nCheck file permissions on your config file!\nMake sure other groups and users cannot read/write it.\n");
412 exit(-1);
413 }
414
415 eggdrop_init();
416 core_config_init(configfile);
417 logfile_init();
418 if (core_config.userfile) user_load(core_config.userfile);
419 core_party_init();
420 core_binds_init();
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 while (1) {
547 timer_update_now(&egg_timeval_now);
548 now = egg_timeval_now.sec;
549 random(); /* Woop, lets really jumble things */
550 timer_run();
551 if (timer_get_shortest(&howlong)) timeout = 1000;
552 else timeout = howlong.sec * 1000 + howlong.usec / 1000;
553 sockbuf_update_all(timeout);
554 garbage_run();
555 }
556 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23