/[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.6 - (hide annotations) (download) (as text)
Tue Oct 26 21:38:37 1999 UTC (19 years, 8 months ago) by fabian
Branch: MAIN
Changes since 1.5: +2 -2 lines
File MIME type: text/x-chdr
resync 940380771-940924890

1 segfault 1.1 /*
2     * main.c -- handles:
3     * changing nicknames when the desired nick is in use
4     * flood detection
5     * signal handling
6     * command line arguments
7     *
8     * dprintf'ized, 15nov1995
9     */
10     /*
11     * This file is part of the eggdrop source code
12     * copyright (c) 1997 Robey Pointer
13     * and is distributed according to the GNU general public license.
14     *
15     * Parts of this eggdrop source code are copyright (c)1999 Eggheads
16     * and is distributed according to the GNU general public license.
17     *
18     * This program is free software; you can redistribute it and/or modify
19     * it under the terms of the GNU General Public License as published by
20     * the Free Software Foundation; either version 2 of the License, or
21     * (at your option) any later version.
22     *
23     * This program is distributed in the hope that it will be useful,
24     * but WITHOUT ANY WARRANTY; without even the implied warranty of
25     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26     * GNU General Public License for more details.
27     *
28     * You should have received a copy of the GNU General Public License
29     * along with this program; if not, write to the Free Software
30     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31     *
32     * The author (Robey Pointer) can be reached at: robey@netcom.com
33     * NOTE: Robey is no long working on this code, there is a discussion
34     * list avaliable at eggheads@eggheads.org.
35     */
36    
37     #include "main.h"
38     #include <time.h>
39     #include <fcntl.h>
40     #include <errno.h>
41     #include <signal.h>
42     #include <netdb.h>
43     #include <setjmp.h>
44     #ifdef STOP_UAC /* osf/1 complains a lot */
45     #include <sys/sysinfo.h>
46     #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
47     #endif
48     /* some systems have a working sys/wait.h even though configure will
49     * decide it's not bsd compatable. oh well. */
50     #include "chan.h"
51     #include "modules.h"
52     #include "tandem.h"
53 fabian 1.5 #ifdef CYGWIN_HACKS
54     #include <windows.h>
55     BOOL FreeConsole(VOID);
56     #endif
57 segfault 1.1
58     #ifndef _POSIX_SOURCE
59     /* solaris needs this */
60     #define _POSIX_SOURCE 1
61     #endif
62    
63     extern char origbotname[];
64     extern int dcc_total;
65     extern struct dcc_t *dcc;
66     extern int conmask;
67     extern struct userrec *userlist;
68     extern int cache_hit, cache_miss;
69     extern char userfile[];
70     extern struct chanset_t *chanset;
71     extern char botnetnick[];
72     extern log_t *logs;
73     extern Tcl_Interp *interp;
74     extern int max_logs;
75     extern tcl_timer_t *timer, *utimer;
76     extern jmp_buf alarmret;
77     extern int quick_logs; /* dw */
78    
79     /*
80     * Please use the PATCH macro instead of directly altering the version
81     * string from now on (it makes it much easier to maintain patches).
82     * Also please read the README file regarding your rights to distribute
83     * modified versions of this bot.
84     */
85    
86     char egg_version[1024] = "1.4.0";
87     int egg_numver = 1040000;
88    
89     char notify_new[121] = ""; /* person to send a note to for new users */
90     int default_flags = 0; /* default user flags and */
91     int default_uflags = 0; /* default userdefinied flags for people */
92     /* who say 'hello' or for .adduser */
93    
94     int backgrd = 1; /* run in the background? */
95     int con_chan = 0; /* foreground: constantly display channel
96     * stats? */
97     int term_z = 0; /* foreground: use the terminal as a party
98     * line? */
99 fabian 1.4 char configfile[121] = "eggdrop.conf"; /* name of the config file */
100 segfault 1.1 char helpdir[121]; /* directory of help files (if used) */
101     char textdir[121] = ""; /* directory for text files that get dumped */
102     int keep_all_logs = 0; /* never erase logfiles, no matter how old
103     * they are? */
104     time_t online_since; /* unix-time that the bot loaded up */
105     int make_userfile = 0; /* using bot in make-userfile mode? (first
106     * user to 'hello' becomes master) */
107     char owner[121] = ""; /* permanent owner(s) of the bot */
108     char pid_file[40]; /* name of the file for the pid to be
109     * stored in */
110     int save_users_at = 0; /* how many minutes past the hour to
111     * save the userfile? */
112     int notify_users_at = 0; /* how many minutes past the hour to
113     * notify users of notes? */
114     int switch_logfiles_at = 300; /* when (military time) to switch logfiles */
115     char version[81]; /* version info (long form) */
116     char ver[41]; /* version info (short form) */
117     char egg_xtra[2048]; /* patch info */
118     int use_stderr = 1; /* send stuff to stderr instead of logfiles? */
119     int do_restart = 0; /* .restart has been called, restart asap */
120     int die_on_sighup = 0; /* die if bot receives SIGHUP */
121     int die_on_sigterm = 0; /* die if bot receives SIGTERM */
122     int resolve_timeout = 15; /* hostname/address lookup timeout */
123     time_t now; /* duh, now :) */
124    
125     /* context storage for fatal crashes */
126     char cx_file[16][30];
127     char cx_note[16][256];
128     int cx_line[16];
129     int cx_ptr = 0;
130    
131     void fatal(char *s, int recoverable)
132     {
133     int i;
134    
135     putlog(LOG_MISC, "*", "* %s", s);
136     flushlogs();
137     for (i = 0; i < dcc_total; i++)
138     killsock(dcc[i].sock);
139     unlink(pid_file);
140     if (!recoverable)
141     exit(1);
142     }
143    
144     int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
145     expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
146     expmem_modules(int), expmem_language(), expmem_tcldcc();
147    
148     /* for mem.c : calculate memory we SHOULD be using */
149     int expected_memory()
150     {
151     int tot;
152    
153     context;
154     tot = expmem_chanprog() + expmem_users() + expmem_misc() +
155     expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
156     expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
157     return tot;
158     }
159    
160     static void check_expired_dcc()
161     {
162     int i;
163    
164     for (i = 0; i < dcc_total; i++)
165     if (dcc[i].type && dcc[i].type->timeout_val &&
166     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
167     if (dcc[i].type->timeout)
168     dcc[i].type->timeout(i);
169     else if (dcc[i].type->eof)
170     dcc[i].type->eof(i);
171     else
172     continue;
173     /* only timeout 1 socket per cycle, too risky for more */
174     return;
175     }
176     }
177    
178     static int nested_debug = 0;
179    
180     void write_debug()
181     {
182     int x;
183     char s[80];
184     int y;
185    
186     if (nested_debug) {
187     /* yoicks, if we have this there's serious trouble */
188     /* all of these are pretty reliable, so we'll try these */
189     /* dont try and display context-notes in here, it's _not_ safe <cybah> */
190     x = creat("DEBUG.DEBUG", 0644);
191     setsock(x, SOCK_NONSOCK);
192     if (x >= 0) {
193     strcpy(s, ctime(&now));
194     dprintf(-x, "Debug (%s) written %s", ver, s);
195     dprintf(-x, "Please report problem to eggheads@eggheads.org");
196     dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
197     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
198     dprintf(-x, "Context: ");
199     cx_ptr = cx_ptr & 15;
200     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
201     dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
202     dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
203     killsock(x);
204     close(x);
205     }
206     exit(1); /* dont even try & tell people about, that may
207     * have caused the fault last time */
208     } else
209     nested_debug = 1;
210     putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
211     cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
212 fabian 1.2 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
213     putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
214    
215     x = creat("DEBUG", 0644);
216 segfault 1.1 setsock(x, SOCK_NONSOCK);
217     if (x < 0) {
218     putlog(LOG_MISC, "*", "* Failed to write DEBUG");
219     } else {
220     strcpy(s, ctime(&now));
221     dprintf(-x, "Debug (%s) written %s", ver, s);
222     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
223     #ifdef STATIC
224     dprintf(-x, "STATICALLY LINKED\n");
225     #endif
226     strcpy(s, "info library");
227     if (interp && (Tcl_Eval(interp, s) == TCL_OK))
228     dprintf(-x, "Using tcl library: %s (header version %s)\n",
229     interp->result, TCL_VERSION);
230     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
231     dprintf(-x, "Link flags : %s\n", LDFLAGS);
232     dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
233     dprintf(-x, "Context: ");
234     cx_ptr = cx_ptr & 15;
235     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
236     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
237     (cx_note[y][0]) ? cx_note[y] : "");
238     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
239     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
240     tell_dcc(-x);
241     dprintf(-x, "\n");
242     debug_mem_to_dcc(-x);
243     killsock(x);
244     close(x);
245     putlog(LOG_MISC, "*", "* Wrote DEBUG");
246     }
247     }
248    
249 fabian 1.2 void assert_failed (const char *module, const char *file, const int line)
250     {
251     write_debug();
252     if (!module) {
253 fabian 1.6 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
254 fabian 1.2 } else {
255 fabian 1.6 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
256 fabian 1.2 }
257     fatal ("ASSERT FAILED -- CRASHING!", 1);
258     exit (1);
259     }
260    
261 segfault 1.1 static void got_bus(int z)
262     {
263     write_debug();
264     fatal("BUS ERROR -- CRASHING!", 1);
265     #ifdef SA_RESETHAND
266     kill(getpid(), SIGBUS);
267     #else
268     exit(1);
269     #endif
270     }
271    
272     static void got_segv(int z)
273     {
274     write_debug();
275     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
276     #ifdef SA_RESETHAND
277     kill(getpid(), SIGSEGV);
278     #else
279     exit(1);
280     #endif
281     }
282    
283     static void got_fpe(int z)
284     {
285     write_debug();
286     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
287     }
288    
289     static void got_term(int z)
290     {
291     write_userfile(-1);
292     check_tcl_event("sigterm");
293     if (die_on_sigterm) {
294     botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
295     fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
296     } else {
297     putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
298     }
299     }
300    
301     static void got_quit(int z)
302     {
303     check_tcl_event("sigquit");
304     putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
305     return;
306     }
307    
308     static void got_hup(int z)
309     {
310     write_userfile(-1);
311     check_tcl_event("sighup");
312     if (die_on_sighup) {
313     fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
314     } else
315     putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
316     do_restart = -2;
317     return;
318     }
319    
320     static void got_alarm(int z)
321     {
322     /* a call to resolver (gethostbyname, etc) timed out */
323     longjmp(alarmret, 1);
324     /* STUPID STUPID STUPID */
325     /* return; */
326     }
327    
328     /* got ILL signal -- log context and continue */
329     static void got_ill(int z)
330     {
331     check_tcl_event("sigill");
332 fabian 1.5 write_debug();
333 segfault 1.1 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
334     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
335 fabian 1.5 fatal ("GOT ILL SIGNAL -- CRASHING!", 1);
336     #ifdef SA_RESETHAND
337     kill(getpid(), SIGILL);
338     #else
339     exit(1);
340     #endif
341 segfault 1.1 }
342    
343     static void do_arg(char *s)
344     {
345     int i;
346    
347     if (s[0] == '-')
348     for (i = 1; i < strlen(s); i++) {
349     if (s[i] == 'n')
350     backgrd = 0;
351     if (s[i] == 'c') {
352     con_chan = 1;
353     term_z = 0;
354     }
355     if (s[i] == 't') {
356     con_chan = 0;
357     term_z = 1;
358     }
359     if (s[i] == 'm')
360     make_userfile = 1;
361     if (s[i] == 'v') {
362     char x[256], *z = x;
363    
364     strcpy(x, egg_version);
365     newsplit(&z);
366     newsplit(&z);
367     printf("%s\n", version);
368     if (z[0])
369     printf(" (patches: %s)\n", z);
370     exit(0);
371     }
372     if (s[i] == 'h') {
373     printf("\n%s\n\n", version);
374     printf(EGG_USAGE);
375     printf("\n");
376     exit(0);
377     }
378     } else
379     strcpy(configfile, s);
380     }
381    
382     void backup_userfile()
383     {
384     char s[150];
385    
386     putlog(LOG_MISC, "*", USERF_BACKUP);
387     strcpy(s, userfile);
388     strcat(s, "~bak");
389     copyfile(userfile, s);
390     }
391    
392     /* timer info: */
393     static int lastmin = 99;
394     static time_t then;
395     static struct tm *nowtm;
396    
397     /* rally BB, this is not QUITE as bad as it seems <G> */
398     /* ONCE A SECOND */
399     static void core_secondly()
400     {
401     static int cnt = 0;
402     int miltime;
403    
404     do_check_timers(&utimer); /* secondly timers */
405     cnt++;
406     if (cnt >= 10) { /* every 10 seconds */
407     cnt = 0;
408     check_expired_dcc();
409     if (con_chan && !backgrd) {
410     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
411     tell_verbose_status(DP_STDOUT);
412     do_module_report(DP_STDOUT, 0, "server");
413     do_module_report(DP_STDOUT, 0, "channels");
414     tell_mem_status_dcc(DP_STDOUT);
415     }
416     }
417     context;
418     nowtm = localtime(&now);
419     if (nowtm->tm_min != lastmin) {
420     int i = 0;
421    
422     /* once a minute */
423     lastmin = (lastmin + 1) % 60;
424     call_hook(HOOK_MINUTELY);
425     check_expired_ignores();
426     autolink_cycle(NULL); /* attempt autolinks */
427     /* in case for some reason more than 1 min has passed: */
428     while (nowtm->tm_min != lastmin) {
429     /* timer drift, dammit */
430     debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm->tm_min);
431     context;
432     i++;
433     lastmin = (lastmin + 1) % 60;
434     call_hook(HOOK_MINUTELY);
435     }
436     if (i > 1)
437     putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
438     miltime = (nowtm->tm_hour * 100) + (nowtm->tm_min);
439     context;
440     if (((int) (nowtm->tm_min / 5) * 5) == (nowtm->tm_min)) { /* 5 min */
441     call_hook(HOOK_5MINUTELY);
442     check_botnet_pings();
443     context;
444     if (quick_logs == 0) {
445     flushlogs();
446     check_logsize();
447     }
448     if (miltime == 0) { /* at midnight */
449     char s[128];
450     int j;
451    
452     s[my_strcpy(s, ctime(&now)) - 1] = 0;
453 fabian 1.4 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
454 segfault 1.1 backup_userfile();
455     for (j = 0; j < max_logs; j++) {
456     if (logs[j].filename != NULL && logs[j].f != NULL) {
457     fclose(logs[j].f);
458     logs[j].f = NULL;
459     }
460     }
461     }
462     }
463     context;
464     if (nowtm->tm_min == notify_users_at)
465     call_hook(HOOK_HOURLY);
466     context; /* these no longer need checking since they are
467     * all check vs minutely settings and we only
468     * get this far on the minute */
469     if (miltime == switch_logfiles_at) {
470     call_hook(HOOK_DAILY);
471     if (!keep_all_logs) {
472     putlog(LOG_MISC, "*", MISC_LOGSWITCH);
473     for (i = 0; i < max_logs; i++)
474     if (logs[i].filename) {
475     char s[1024];
476    
477     if (logs[i].f) {
478     fclose(logs[i].f);
479     logs[i].f = NULL;
480     }
481     simple_sprintf(s, "%s.yesterday", logs[i].filename);
482     unlink(s);
483 fabian 1.2 movefile(logs[i].filename, s);
484 segfault 1.1 }
485     }
486     }
487     }
488     }
489    
490     static void core_minutely()
491     {
492     context;
493     check_tcl_time(nowtm);
494     do_check_timers(&timer);
495     context;
496     if (quick_logs != 0) {
497     flushlogs();
498     check_logsize();
499     }
500     }
501    
502     static void core_hourly()
503     {
504     context;
505     write_userfile(-1);
506     }
507    
508     static void event_rehash()
509     {
510     context;
511     check_tcl_event("rehash");
512     }
513    
514     static void event_prerehash()
515     {
516     context;
517     check_tcl_event("prerehash");
518     }
519    
520     static void event_save()
521     {
522     context;
523     check_tcl_event("save");
524     }
525    
526     static void event_logfile()
527     {
528     context;
529     check_tcl_event("logfile");
530     }
531    
532     void kill_tcl();
533     extern module_entry *module_list;
534     void restart_chons();
535    
536     #ifdef STATIC
537     void check_static(char *, char *(*)());
538    
539     #include "mod/static.h"
540     #endif
541     int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
542     init_net(), init_modules(), init_tcl(), init_language(int);
543    
544     int main(int argc, char **argv)
545     {
546     int xx, i;
547     char buf[520], s[520];
548     FILE *f;
549     struct sigaction sv;
550     struct chanset_t *chan;
551    
552     /* initialise context list */
553     for (i = 0; i < 16; i++) {
554     context;
555     }
556     #include "patch.h"
557     /* version info! */
558     sprintf(ver, "eggdrop v%s", egg_version);
559     sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999 Eggheads", egg_version);
560     /* now add on the patchlevel (for Tcl) */
561 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
562 segfault 1.1 strcat(egg_version, egg_xtra);
563     context;
564     #ifdef STOP_UAC
565     {
566     int nvpair[2];
567    
568     nvpair[0] = SSIN_UACPROC;
569     nvpair[1] = UAC_NOPRINT;
570     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
571     }
572     #endif
573     /* set up error traps: */
574     sv.sa_handler = got_bus;
575     sigemptyset(&sv.sa_mask);
576     #ifdef SA_RESETHAND
577     sv.sa_flags = SA_RESETHAND;
578     #else
579     sv.sa_flags = 0;
580     #endif
581     sigaction(SIGBUS, &sv, NULL);
582     sv.sa_handler = got_segv;
583     sigaction(SIGSEGV, &sv, NULL);
584     #ifdef SA_RESETHAND
585     sv.sa_flags = 0;
586     #endif
587     sv.sa_handler = got_fpe;
588     sigaction(SIGFPE, &sv, NULL);
589     sv.sa_handler = got_term;
590     sigaction(SIGTERM, &sv, NULL);
591     sv.sa_handler = got_hup;
592     sigaction(SIGHUP, &sv, NULL);
593     sv.sa_handler = got_quit;
594     sigaction(SIGQUIT, &sv, NULL);
595     sv.sa_handler = SIG_IGN;
596     sigaction(SIGPIPE, &sv, NULL);
597     sv.sa_handler = got_ill;
598     sigaction(SIGILL, &sv, NULL);
599     sv.sa_handler = got_alarm;
600     sigaction(SIGALRM, &sv, NULL);
601     /* initialize variables and stuff */
602     now = time(NULL);
603     chanset = NULL;
604     nowtm = localtime(&now);
605     lastmin = nowtm->tm_min;
606     srandom(now);
607     init_mem();
608     init_language(1);
609     if (argc > 1)
610     for (i = 1; i < argc; i++)
611     do_arg(argv[i]);
612     printf("\n%s\n", version);
613     init_dcc_max();
614     init_userent();
615     init_misc();
616     init_bots();
617     init_net();
618     init_modules();
619     init_tcl();
620     init_language(0);
621     #ifdef STATIC
622     link_statics();
623     #endif
624     context;
625     strcpy(s, ctime(&now));
626     s[strlen(s) - 1] = 0;
627     strcpy(&s[11], &s[20]);
628     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
629     chanprog();
630     context;
631     if (encrypt_pass == 0) {
632     printf(MOD_NOCRYPT);
633     exit(1);
634     }
635     i = 0;
636     for (chan = chanset; chan; chan = chan->next)
637     i++;
638 fabian 1.4 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
639 segfault 1.1 botnetnick, i, count_users(userlist));
640     cache_miss = 0;
641     cache_hit = 0;
642     context;
643     sprintf(pid_file, "pid.%s", botnetnick);
644     context;
645     /* check for pre-existing eggdrop! */
646     f = fopen(pid_file, "r");
647     if (f != NULL) {
648     fgets(s, 10, f);
649     xx = atoi(s);
650     kill(xx, SIGCHLD); /* meaningless kill to determine if pid
651     * is used */
652     if (errno != ESRCH) {
653     printf(EGG_RUNNING1, origbotname);
654     printf(EGG_RUNNING2, pid_file);
655     exit(1);
656     }
657     }
658     context;
659 fabian 1.5 #ifndef CYGWIN_HACKS
660 segfault 1.1 /* move into background? */
661     if (backgrd) {
662     xx = fork();
663     if (xx == -1)
664     fatal("CANNOT FORK PROCESS.", 0);
665     if (xx != 0) {
666     FILE *fp;
667    
668     /* need to attempt to write pid now, not later */
669     unlink(pid_file);
670     fp = fopen(pid_file, "w");
671     if (fp != NULL) {
672     fprintf(fp, "%u\n", xx);
673     if (fflush(fp)) {
674     /* kill bot incase a botchk is run from crond */
675     printf(EGG_NOWRITE, pid_file);
676     printf(" Try freeing some disk space\n");
677     fclose(fp);
678     unlink(pid_file);
679     exit(1);
680     }
681     fclose(fp);
682     } else
683     printf(EGG_NOWRITE, pid_file);
684     printf("Launched into the background (pid: %d)\n\n", xx);
685     #if HAVE_SETPGID
686     setpgid(xx, xx);
687     #endif
688     exit(0);
689     }
690     }
691 fabian 1.5 #endif
692 segfault 1.1 use_stderr = 0; /* stop writing to stderr now */
693     xx = getpid();
694     if ((xx != 0) && (!backgrd)) {
695     FILE *fp;
696    
697     /* write pid to file */
698     unlink(pid_file);
699     fp = fopen(pid_file, "w");
700     if (fp != NULL) {
701     fprintf(fp, "%u\n", xx);
702     if (fflush(fp)) {
703     /* let the bot live since this doesn't appear to be a botchk */
704     printf(EGG_NOWRITE, pid_file);
705     fclose(fp);
706     unlink(pid_file);
707     }
708     fclose(fp);
709     } else
710     printf(EGG_NOWRITE, pid_file);
711     }
712     if (backgrd) {
713     /* ok, try to disassociate from controlling terminal */
714     /* (finger cross) */
715 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
716 segfault 1.1 setpgid(0, 0);
717     #endif
718     /* close out stdin/out/err */
719     freopen("/dev/null", "r", stdin);
720     freopen("/dev/null", "w", stdout);
721     freopen("/dev/null", "w", stderr);
722 fabian 1.5 #ifdef CYGWIN_HACKS
723     FreeConsole();
724     #endif
725 segfault 1.1 /* tcl wants those file handles kept open */
726     /* close(0); close(1); close(2); */
727     }
728     /* terminal emulating dcc chat */
729     if ((!backgrd) && (term_z)) {
730     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
731    
732     dcc[n].addr = iptolong(getmyip());
733     dcc[n].sock = STDOUT;
734     dcc[n].timeval = now;
735     dcc[n].u.chat->con_flags = conmask;
736     dcc[n].u.chat->strip_flags = STRIP_ALL;
737     dcc[n].status = STAT_ECHO;
738     strcpy(dcc[n].nick, "HQ");
739     strcpy(dcc[n].host, "llama@console");
740     dcc[n].user = get_user_by_handle(userlist, "HQ");
741     /* make sure there's an innocuous HQ user if needed */
742     if (!dcc[n].user) {
743     adduser(userlist, "HQ", "none", "-", USER_PARTY);
744     dcc[n].user = get_user_by_handle(userlist, "HQ");
745     }
746     setsock(STDOUT, 0); /* entry in net table */
747     dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
748     dcc_chatter(n);
749     }
750     then = now;
751     online_since = now;
752     autolink_cycle(NULL); /* hurry and connect to tandem bots */
753     add_help_reference("cmds1.help");
754     add_help_reference("cmds2.help");
755     add_help_reference("core.help");
756     add_hook(HOOK_SECONDLY, core_secondly);
757     add_hook(HOOK_MINUTELY, core_minutely);
758     add_hook(HOOK_HOURLY, core_hourly);
759     add_hook(HOOK_REHASH, event_rehash);
760     add_hook(HOOK_PRE_REHASH, event_prerehash);
761     add_hook(HOOK_USERFILE, event_save);
762     add_hook(HOOK_DAILY, event_logfile);
763    
764     debug0("main: entering loop");
765     while (1) {
766     int socket_cleanup = 0;
767    
768     context;
769 fabian 1.5 #if !defined(HAVE_OLD_TCL) && !defined(HAVE_BUGGY_TCL_THREADS)
770 segfault 1.1 /* process a single tcl event */
771     Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
772 fabian 1.5 #endif
773 segfault 1.1 /* lets move some of this here, reducing the numer of actual
774     * calls to periodic_timers */
775     now = time(NULL);
776     random(); /* woop, lets really jumble things */
777     if (now != then) { /* once a second */
778     call_hook(HOOK_SECONDLY);
779     then = now;
780     }
781     context;
782     /* only do this every so often */
783     if (!socket_cleanup) {
784     /* clean up sockets that were just left for dead */
785     for (i = 0; i < dcc_total; i++) {
786     if (dcc[i].type == &DCC_LOST) {
787     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
788     lostdcc(i);
789     i--;
790     }
791     }
792     /* check for server or dcc activity */
793     dequeue_sockets();
794     socket_cleanup = 5;
795     } else
796     socket_cleanup--;
797     /* new net routines: help me mary! */
798     xx = sockgets(buf, &i);
799     if (xx >= 0) { /* non-error */
800     int idx;
801    
802     context;
803     for (idx = 0; idx < dcc_total; idx++)
804     if (dcc[idx].sock == xx) {
805     if (dcc[idx].type && dcc[idx].type->activity)
806     dcc[idx].type->activity(idx, buf, i);
807     else
808     putlog(LOG_MISC, "*",
809     "!!! untrapped dcc activity: type %s, sock %d",
810     dcc[idx].type->name, dcc[idx].sock);
811     break;
812     }
813     } else if (xx == -1) { /* EOF from someone */
814     int idx;
815    
816     if ((i == STDOUT) && !backgrd)
817     fatal("END OF FILE ON TERMINAL", 0);
818     context;
819     for (idx = 0; idx < dcc_total; idx++)
820     if (dcc[idx].sock == i) {
821     if (dcc[idx].type && dcc[idx].type->eof)
822     dcc[idx].type->eof(idx);
823     else {
824     putlog(LOG_MISC, "*",
825     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
826 fabian 1.2 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
827     killsock(i);
828 segfault 1.1 lostdcc(idx);
829     }
830     idx = dcc_total + 1;
831     }
832     if (idx == dcc_total) {
833     putlog(LOG_MISC, "*",
834 fabian 1.2 "(@) EOF socket %d, not a dcc socket, not anything.", i);
835     close(i);
836     killsock(i);
837 segfault 1.1 }
838     } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
839     context;
840     putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
841     for (i = 0; i < dcc_total; i++) {
842     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
843     if (dcc[i].type == &DCC_LOST)
844     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
845     putlog(LOG_MISC, "*",
846     "DCC socket %d (type %d, name '%s') expired -- pfft",
847     dcc[i].sock, dcc[i].type, dcc[i].nick);
848     killsock(dcc[i].sock);
849     lostdcc(i);
850     i--;
851     }
852     }
853     } else if (xx == (-3)) {
854     call_hook(HOOK_IDLE);
855     socket_cleanup = 0; /* if we've been idle, cleanup & flush */
856     }
857     if (do_restart) {
858     if (do_restart == -2)
859     rehash();
860     else {
861     /* unload as many modules as possible */
862     int f = 1;
863     module_entry *p;
864     Function x;
865     char xx[256];
866    
867     while (f) {
868     f = 0;
869     for (p = module_list; p != NULL; p = p->next) {
870     dependancy *d = dependancy_list;
871     int ok = 1;
872    
873     while (ok && d) {
874     if (d->needed == p)
875     ok = 0;
876     d = d->next;
877     }
878     if (ok) {
879     strcpy(xx, p->name);
880     if (module_unload(xx, origbotname) == NULL) {
881     f = 1;
882     break;
883     }
884     }
885     }
886     }
887     p = module_list;
888     if (p && p->next && p->next->next)
889     /* should be only 2 modules now -
890     * blowfish & eggdrop */
891     putlog(LOG_MISC, "*", MOD_STAGNANT);
892     context;
893     flushlogs();
894     context;
895     kill_tcl();
896     init_tcl();
897     init_language(0);
898     x = p->funcs[MODCALL_START];
899     x(0);
900     rehash();
901     restart_chons();
902     }
903     do_restart = 0;
904     }
905     }
906     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23