/[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.155 - (hide annotations) (download) (as text)
Tue Dec 16 03:26:42 2003 UTC (15 years, 8 months ago) by wcc
Branch: MAIN
Changes since 1.154: +3 -1 lines
File MIME type: text/x-chdr
* Fixes from last commit.

1 guppy 1.56 /*
2 tothwolf 1.117 * main.c --
3 guppy 1.56 *
4 tothwolf 1.117 * core event handling
5     * signal handling
6     * command line arguments
7 segfault 1.1 */
8 guppy 1.56 /*
9     * Copyright (C) 1997 Robey Pointer
10 wcc 1.153 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Eggheads Development Team
11 guppy 1.56 *
12 fabian 1.14 * This program is free software; you can redistribute it and/or
13     * modify it under the terms of the GNU General Public License
14     * as published by the Free Software Foundation; either version 2
15     * of the License, or (at your option) any later version.
16 guppy 1.56 *
17 segfault 1.1 * This program is distributed in the hope that it will be useful,
18     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     * GNU General Public License for more details.
21 guppy 1.56 *
22 segfault 1.1 * You should have received a copy of the GNU General Public License
23     * along with this program; if not, write to the Free Software
24 fabian 1.14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25     */
26 guppy 1.56 /*
27 segfault 1.1 * The author (Robey Pointer) can be reached at: robey@netcom.com
28 tothwolf 1.117 * NOTE: Robey is no longer working on this code, there is a discussion
29 guppy 1.60 * list available at eggheads@eggheads.org.
30 segfault 1.1 */
31 tothwolf 1.117
32     #ifndef lint
33 wcc 1.155 static const char rcsid[] = "$Id: main.c,v 1.154 2003/12/16 03:13:51 wcc Exp $";
34 tothwolf 1.117 #endif
35 segfault 1.1
36 stdarg 1.151 #include <ctype.h>
37 stdarg 1.145 #include <unistd.h>
38 stdarg 1.125 #include <eggdrop/eggdrop.h>
39 fabian 1.28 #include "main.h"
40 stdarg 1.133 #include "core_config.h"
41 wcc 1.154 #include "bg.h"
42     #include "core_binds.h"
43     #include "logfile.h"
44 fabian 1.27 #include <fcntl.h>
45 wcc 1.154
46 wcc 1.129 #ifdef TIME_WITH_SYS_TIME
47     # include <sys/time.h>
48     # include <time.h>
49 wcc 1.154 #else
50     # ifdef HAVE_SYS_TIME_H
51     # include <sys/time.h>
52     # else
53     # include <time.h>
54     # endif
55 wcc 1.129 #endif
56 wcc 1.154
57 stdarg 1.142 #include <sys/stat.h>
58 segfault 1.1 #include <errno.h>
59     #include <signal.h>
60     #include <netdb.h>
61     #include <setjmp.h>
62 ite 1.76 #include <locale.h>
63 stdarg 1.142 #include <ctype.h>
64 stdarg 1.145 #include <ltdl.h>
65 ite 1.76
66 wcc 1.154 #ifdef STOP_UAC /* OSF/1 complains a lot */
67     # include <sys/sysinfo.h>
68     # define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
69 segfault 1.1 #endif
70 drummer 1.71
71 poptix 1.62 #ifdef CYGWIN_HACKS
72 wcc 1.154 # include <windows.h>
73 poptix 1.62 #endif
74    
75 segfault 1.1 #ifndef _POSIX_SOURCE
76 wcc 1.154 # define _POSIX_SOURCE 1 /* Solaris needs this */
77 segfault 1.1 #endif
78    
79 wcc 1.154 eggdrop_t *egg = NULL; /* Eggdrop's context */
80    
81     int backgrd = 1; /* Run in the background? */
82     int con_chan = 0; /* Foreground: constantly display channel stats? */
83     int term_z = 0; /* Foreground: use the terminal as a partyline? */
84     int make_userfile = 0; /* Start bot in make-userfile mode? */
85    
86     char configfile[121] = "config.xml"; /* Name of the config file */
87     char helpdir[121] = "help/"; /* Directory of help files (if used) */
88     char textdir[121] = "text/"; /* Directory for text files that get dumped */
89     char pid_file[120]; /* Name of Eggdrop's pid file */
90    
91     time_t online_since; /* Time the bot was started */
92     time_t now; /* Current time */
93     egg_timeval_t egg_timeval_now; /* Current time in seconds and microseconds. */
94 segfault 1.1
95 wcc 1.154 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
96 ite 1.109
97 wcc 1.155 char version[81];
98    
99 wcc 1.154 static int lastmin = 99;
100     static struct tm nowtm;
101    
102     void core_party_init();
103     void core_config_init();
104 fabian 1.27
105 fabian 1.44 void fatal(const char *s, int recoverable)
106 segfault 1.1 {
107 wcc 1.154 putlog(LOG_MISC, "*", "Fatal: %s", s);
108     flushlogs();
109     unlink(pid_file);
110     if (!recoverable) {
111     bg_send_quit(BG_ABORT);
112     exit(1);
113     }
114 segfault 1.1 }
115    
116     static void got_bus(int z)
117     {
118 wcc 1.154 fatal("BUS ERROR.", 1);
119 segfault 1.1 #ifdef SA_RESETHAND
120 wcc 1.154 kill(getpid(), SIGBUS);
121 segfault 1.1 #else
122 wcc 1.154 bg_send_quit(BG_ABORT);
123     exit(1);
124 segfault 1.1 #endif
125     }
126    
127     static void got_segv(int z)
128     {
129 wcc 1.154 fatal("SEGMENT VIOLATION.", 1);
130 segfault 1.1 #ifdef SA_RESETHAND
131 wcc 1.154 kill(getpid(), SIGSEGV);
132 segfault 1.1 #else
133 wcc 1.154 bg_send_quit(BG_ABORT);
134     exit(1);
135 segfault 1.1 #endif
136     }
137    
138     static void got_fpe(int z)
139     {
140 wcc 1.154 fatal("FLOATING POINT ERROR.", 0);
141 segfault 1.1 }
142    
143     static void got_term(int z)
144     {
145 wcc 1.154 eggdrop_event("sigterm");
146     if (core_config.die_on_sigterm) {
147     putlog(LOG_MISC, "*", "Saving user file...");
148     user_save(core_config.userfile);
149     putlog(LOG_MISC, "*", "Saving config file...");
150     core_config_save();
151     putlog(LOG_MISC, "*", "Bot shutting down: received TERMINATE signal.");
152     flushlogs();
153     unlink(pid_file);
154     exit(0);
155     }
156     else putlog(LOG_MISC, "*", "Received TERMINATE signal (ignoring).");
157 segfault 1.1 }
158    
159     static void got_quit(int z)
160     {
161 wcc 1.154 eggdrop_event("sigquit");
162     putlog(LOG_MISC, "*", "Received QUIT signal (ignoring).");
163     return;
164 segfault 1.1 }
165    
166     static void got_hup(int z)
167     {
168 wcc 1.154 eggdrop_event("sighup");
169     putlog(LOG_MISC, "*", "Received HUP signal (ignoring).");
170     return;
171 segfault 1.1 }
172    
173     static void got_ill(int z)
174     {
175 wcc 1.154 eggdrop_event("sigill");
176     putlog(LOG_MISC, "*", "Received ILL signal (ignoring).");
177 fabian 1.10 }
178    
179 ite 1.114
180 wcc 1.135 static void print_version(void)
181 segfault 1.1 {
182 wcc 1.154 printf("%s\n", version);
183 ite 1.114 }
184    
185 wcc 1.154 static void print_help(const char **argv)
186     {
187     print_version();
188     printf("\nUsage: %s [OPTIONS]... [FILE]\n", argv[0]);
189     printf(_("\n\
190 ite 1.114 -h, --help Print help and exit\n\
191     -v, --version Print version and exit\n\
192     -n, --foreground Don't go into the background (default=off)\n\
193     -c --channel-stats (with -n) Display channel stats every 10 seconds\n\
194     -t --terminal (with -n) Use terminal to simulate dcc-chat\n\
195     -m --make-userfile Userfile creation mode\n\
196 wcc 1.135 FILE optional config filename (default 'config.xml')\n"));
197 wcc 1.154 printf("\n");
198 ite 1.114 }
199 segfault 1.1
200 wcc 1.154 static void do_args(int argc, const char **argv)
201 ite 1.114 {
202 wcc 1.154 int c;
203 ite 1.114
204 wcc 1.154 optarg = 0;
205     optind = 1;
206     opterr = 1;
207     optopt = '?';
208 ite 1.114
209 wcc 1.154 while (1) {
210     int option_index = 0;
211 ite 1.114
212 wcc 1.154 static struct option long_options[] = {
213     {"help", 0, NULL, 'h'},
214     {"version", 0, NULL, 'v'},
215     {"load-module", 1, NULL, 'p'},
216     {"foreground", 0, NULL, 'n'},
217     {"channel-stats", 0, NULL, 'c'},
218     {"terminal", 0, NULL, 't'},
219     {"make-userfile", 0, NULL, 'm'},
220     {NULL, 0, NULL, 0}
221     };
222    
223     c = getopt_long(argc, argv, "hvp:nctm", long_options, &option_index);
224     if (c == -1) break;
225     switch (c) {
226     case 'n':
227     backgrd = 0;
228     break;
229     case 'c':
230     con_chan = 1;
231     term_z = 0;
232     break;
233     case 't':
234     con_chan = 0;
235     term_z = 1;
236     break;
237     case 'm':
238     make_userfile = 1;
239     break;
240     case 'v':
241     print_version();
242     bg_send_quit(BG_ABORT);
243     exit(0);
244     break; /* this should never be reached */
245     case 'h':
246     print_help(argv);
247     bg_send_quit(BG_ABORT);
248     exit(0);
249     break; /* this should never be reached */
250     case 0: /* Long option with no short option */
251     case '?': /* Invalid option. */
252     /* getopt_long() already printed an error message. */
253     bg_send_quit(BG_ABORT);
254     exit(1);
255     default: /* bug: option not considered. */
256     fprintf(stderr, "%s: option unknown: %c\n", argv[0], c);
257     bg_send_quit(BG_ABORT);
258     exit(1);
259     break; /* this should never be reached */
260     }
261     }
262     if (optind < argc) strlcpy(configfile, argv[optind], sizeof configfile);
263 segfault 1.1 }
264    
265 fabian 1.34 /* Called once a second.
266 fabian 1.27 */
267 stdarg 1.139 static int core_secondly()
268 segfault 1.1 {
269 wcc 1.154 static int cnt = 0;
270     int miltime;
271 segfault 1.1
272 wcc 1.154 check_bind_secondly();
273     cnt++;
274     memcpy(&nowtm, localtime(&now), sizeof(struct tm));
275     if (nowtm.tm_min != lastmin) {
276     int i = 0;
277    
278     /* Once a minute */
279     lastmin = (lastmin + 1) % 60;
280     eggdrop_event("minutely");
281     check_bind_time(&nowtm);
282     /* In case for some reason more than 1 min has passed: */
283     while (nowtm.tm_min != lastmin) {
284     /* Timer drift, dammit */
285     putlog(LOG_DEBUG, "*", "timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
286     i++;
287     lastmin = (lastmin + 1) % 60;
288     eggdrop_event("minutely");
289     }
290     if (i > 1) putlog(LOG_MISC, "*", "Warning: timer drift (%d minutes).", i);
291 segfault 1.1
292 wcc 1.154 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
293     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 minutes */
294     eggdrop_event("5minutely");
295     if (!miltime) { /* At midnight */
296     char s[25];
297    
298     strlcpy(s, ctime(&now), sizeof s);
299     putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
300     eggdrop_event("backup");
301     eggdrop_event("daily");
302     }
303     }
304     }
305     return(0);
306 segfault 1.1 }
307    
308 stdarg 1.140
309 stdarg 1.142 int file_check(const char *filename)
310     {
311     struct stat sbuf = {0};
312     int r;
313    
314     r = stat(filename, &sbuf);
315     if (r) return(-1);
316     errno = 0;
317     if (sbuf.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) return(-1);
318     return(0);
319     }
320    
321 segfault 1.1 int main(int argc, char **argv)
322     {
323 wcc 1.154 int xx, i, timeout;
324     char s[25], *modname, *scriptname;
325     FILE *f;
326     struct sigaction sv;
327     egg_timeval_t howlong;
328     void *config_root, *entry;
329 segfault 1.1
330 tothwolf 1.81 #ifdef DEBUG
331 wcc 1.154 {
332     # include <sys/resource.h>
333     struct rlimit cdlim;
334    
335     cdlim.rlim_cur = RLIM_INFINITY;
336     cdlim.rlim_max = RLIM_INFINITY;
337     setrlimit(RLIMIT_CORE, &cdlim);
338     }
339 fabian 1.15 #endif
340 fabian 1.32
341 ite 1.99 #ifdef ENABLE_NLS
342 wcc 1.154 setlocale(LC_MESSAGES, "");
343     bindtextdomain(PACKAGE, LOCALEDIR);
344     textdomain(PACKAGE);
345 ite 1.99 #endif
346 wcc 1.143
347 stdarg 1.145 /* Initialize ltdl. */
348     LTDL_SET_PRELOADED_SYMBOLS();
349     if (lt_dlinit()) {
350     printf("Fatal error initializing ltdl: %s\n", lt_dlerror());
351     return(-1);
352     }
353    
354 wcc 1.154 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2004 Eggheads Development Team", VERSION);
355    
356 segfault 1.1 #ifdef STOP_UAC
357 wcc 1.154 {
358     int nvpair[2];
359 segfault 1.1
360 wcc 1.154 nvpair[0] = SSIN_UACPROC;
361     nvpair[1] = UAC_NOPRINT;
362     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
363     }
364 segfault 1.1 #endif
365 fabian 1.27
366 wcc 1.154 sv.sa_handler = got_bus;
367     sigemptyset(&sv.sa_mask);
368 segfault 1.1 #ifdef SA_RESETHAND
369 wcc 1.154 sv.sa_flags = SA_RESETHAND;
370 segfault 1.1 #else
371 wcc 1.154 sv.sa_flags = 0;
372 segfault 1.1 #endif
373 wcc 1.154 sigaction(SIGBUS, &sv, NULL);
374    
375     sv.sa_handler = got_segv;
376     sigaction(SIGSEGV, &sv, NULL);
377 segfault 1.1 #ifdef SA_RESETHAND
378 wcc 1.154 sv.sa_flags = 0;
379 segfault 1.1 #endif
380 wcc 1.154
381     sv.sa_handler = got_fpe;
382     sigaction(SIGFPE, &sv, NULL);
383    
384     sv.sa_handler = got_term;
385     sigaction(SIGTERM, &sv, NULL);
386    
387     sv.sa_handler = got_hup;
388     sigaction(SIGHUP, &sv, NULL);
389    
390     sv.sa_handler = got_quit;
391     sigaction(SIGQUIT, &sv, NULL);
392    
393     sv.sa_handler = SIG_IGN;
394     sigaction(SIGPIPE, &sv, NULL);
395    
396     sv.sa_handler = got_ill;
397     sigaction(SIGILL, &sv, NULL);
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 stdarg 1.142
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 guppy 1.53
418 stdarg 1.151 egg = eggdrop_new();
419     eggdrop_init();
420     core_config_init(configfile);
421     logfile_init();
422     if (core_config.userfile) 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 wcc 1.154 if (len == 0) printf("Come on, enter a real handle.\n\n");
446 stdarg 1.151 } 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 stdarg 1.152 user_set_flag_str(owner, NULL, "+n");
456 stdarg 1.151 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 stdarg 1.80
463 wcc 1.154 if (backgrd) bg_prepare_split();
464 stdarg 1.80
465 wcc 1.154 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 fabian 1.27
483 stdarg 1.151 /* Put the module directory in the ltdl search path. */
484     if (core_config.module_path) module_add_dir(core_config.module_path);
485    
486     /* Scan the autoload section of config. */
487     config_root = config_get_root("eggdrop");
488     for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "module", i, NULL)); i++) {
489     modname = NULL;
490     config_get_str(&modname, entry, NULL);
491     module_load(modname);
492     }
493     for (i = 0; (entry = config_exists(config_root, "eggdrop", 0, "autoload", 0, "script", i, NULL)); i++) {
494     scriptname = NULL;
495     config_get_str(&scriptname, entry, NULL);
496     script_load(scriptname);
497     }
498    
499 wcc 1.154 if (backgrd) { /* Move into background? */
500 fabian 1.38 #ifndef CYGWIN_HACKS
501 wcc 1.154 bg_do_split();
502     }
503     else {
504 fabian 1.5 #endif
505 wcc 1.154 xx = getpid();
506     if (xx != 0) {
507     FILE *fp;
508    
509     unlink(pid_file);
510     fp = fopen(pid_file, "w");
511     if (fp != NULL) {
512     fprintf(fp, "%u\n", xx);
513     if (fflush(fp)) {
514     /* Let the bot live since this doesn't appear to be a botchk */
515     printf("WARNING: Could not write pid file '%s'.\n", pid_file);
516     fclose(fp);
517     unlink(pid_file);
518     }
519     else fclose(fp);
520     }
521     else printf("WARNING: Could not write pid file '%s'.\n", pid_file);
522 fabian 1.38 #ifdef CYGWIN_HACKS
523 wcc 1.154 printf("Launched into the background (pid: %d)\n\n", xx);
524 fabian 1.38 #endif
525 wcc 1.154 }
526     }
527 stdarg 1.151
528 wcc 1.154 use_stderr = 0; /* Stop writing to stderr now */
529 fabian 1.38
530 wcc 1.154 if (backgrd) {
531     /* Try to disassociate from controlling terminal (finger cross) */
532 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
533 wcc 1.154 setpgid(0, 0);
534 segfault 1.1 #endif
535 wcc 1.154 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
536     freopen("/dev/null", "r", stdin);
537     freopen("/dev/null", "w", stdout);
538     freopen("/dev/null", "w", stderr);
539 poptix 1.62 #ifdef CYGWIN_HACKS
540 wcc 1.154 FreeConsole();
541 poptix 1.62 #endif
542 wcc 1.154 }
543 fabian 1.27
544 wcc 1.154 online_since = now;
545     howlong.sec = 1;
546     howlong.usec = 0;
547     timer_create_repeater(&howlong, "main loop", core_secondly);
548 segfault 1.1
549 wcc 1.154 putlog(LOG_DEBUG, "*", "Entering main loop.");
550 stdarg 1.139 while (1) {
551     timer_update_now(&egg_timeval_now);
552     now = egg_timeval_now.sec;
553     random(); /* Woop, lets really jumble things */
554     timer_run();
555 wcc 1.154 if (timer_get_shortest(&howlong)) timeout = 1000;
556     else timeout = howlong.sec * 1000 + howlong.usec / 1000;
557 stdarg 1.139 sockbuf_update_all(timeout);
558 stdarg 1.150 garbage_run();
559 stdarg 1.131 }
560 segfault 1.1 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23