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

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

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


Revision 1.158 - (hide annotations) (download) (as text)
Wed Dec 17 08:12:43 2003 UTC (15 years, 8 months ago) by wcc
Branch: MAIN
Changes since 1.157: +20 -17 lines
File MIME type: text/x-chdr
* Should do it..

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23