/[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.157 - (hide annotations) (download) (as text)
Tue Dec 16 22:36:38 2003 UTC (15 years, 11 months ago) by wcc
Branch: MAIN
Changes since 1.156: +28 -28 lines
File MIME type: text/x-chdr
* src/ gettextification.
* More cleanups.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23