/[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.8 - (hide annotations) (download) (as text)
Wed Nov 10 21:35:03 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.7: +5 -4 lines
File MIME type: text/x-chdr
Resync with 1.4

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 segfault 1.7 char egg_version[1024] = "1.5.0";
87     int egg_numver = 1050000;
88 segfault 1.1
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 fabian 1.8 init_net(), init_modules(), init_tcl(int, char **),
543     init_language(int);
544 segfault 1.1
545     int main(int argc, char **argv)
546     {
547     int xx, i;
548     char buf[520], s[520];
549     FILE *f;
550     struct sigaction sv;
551     struct chanset_t *chan;
552    
553     /* initialise context list */
554     for (i = 0; i < 16; i++) {
555     context;
556     }
557     #include "patch.h"
558     /* version info! */
559     sprintf(ver, "eggdrop v%s", egg_version);
560     sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999 Eggheads", egg_version);
561     /* now add on the patchlevel (for Tcl) */
562 fabian 1.2 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
563 segfault 1.1 strcat(egg_version, egg_xtra);
564     context;
565     #ifdef STOP_UAC
566     {
567     int nvpair[2];
568    
569     nvpair[0] = SSIN_UACPROC;
570     nvpair[1] = UAC_NOPRINT;
571     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
572     }
573     #endif
574     /* set up error traps: */
575     sv.sa_handler = got_bus;
576     sigemptyset(&sv.sa_mask);
577     #ifdef SA_RESETHAND
578     sv.sa_flags = SA_RESETHAND;
579     #else
580     sv.sa_flags = 0;
581     #endif
582     sigaction(SIGBUS, &sv, NULL);
583     sv.sa_handler = got_segv;
584     sigaction(SIGSEGV, &sv, NULL);
585     #ifdef SA_RESETHAND
586     sv.sa_flags = 0;
587     #endif
588     sv.sa_handler = got_fpe;
589     sigaction(SIGFPE, &sv, NULL);
590     sv.sa_handler = got_term;
591     sigaction(SIGTERM, &sv, NULL);
592     sv.sa_handler = got_hup;
593     sigaction(SIGHUP, &sv, NULL);
594     sv.sa_handler = got_quit;
595     sigaction(SIGQUIT, &sv, NULL);
596     sv.sa_handler = SIG_IGN;
597     sigaction(SIGPIPE, &sv, NULL);
598     sv.sa_handler = got_ill;
599     sigaction(SIGILL, &sv, NULL);
600     sv.sa_handler = got_alarm;
601     sigaction(SIGALRM, &sv, NULL);
602     /* initialize variables and stuff */
603     now = time(NULL);
604     chanset = NULL;
605     nowtm = localtime(&now);
606     lastmin = nowtm->tm_min;
607     srandom(now);
608     init_mem();
609     init_language(1);
610     if (argc > 1)
611     for (i = 1; i < argc; i++)
612     do_arg(argv[i]);
613     printf("\n%s\n", version);
614     init_dcc_max();
615     init_userent();
616     init_misc();
617     init_bots();
618     init_net();
619     init_modules();
620 fabian 1.8 init_tcl(argc, argv);
621 segfault 1.1 init_language(0);
622     #ifdef STATIC
623     link_statics();
624     #endif
625     context;
626     strcpy(s, ctime(&now));
627     s[strlen(s) - 1] = 0;
628     strcpy(&s[11], &s[20]);
629     putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
630     chanprog();
631     context;
632     if (encrypt_pass == 0) {
633     printf(MOD_NOCRYPT);
634     exit(1);
635     }
636     i = 0;
637     for (chan = chanset; chan; chan = chan->next)
638     i++;
639 fabian 1.4 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
640 segfault 1.1 botnetnick, i, count_users(userlist));
641     cache_miss = 0;
642     cache_hit = 0;
643     context;
644     sprintf(pid_file, "pid.%s", botnetnick);
645     context;
646     /* check for pre-existing eggdrop! */
647     f = fopen(pid_file, "r");
648     if (f != NULL) {
649     fgets(s, 10, f);
650     xx = atoi(s);
651     kill(xx, SIGCHLD); /* meaningless kill to determine if pid
652     * is used */
653     if (errno != ESRCH) {
654     printf(EGG_RUNNING1, origbotname);
655     printf(EGG_RUNNING2, pid_file);
656     exit(1);
657     }
658     }
659     context;
660 fabian 1.5 #ifndef CYGWIN_HACKS
661 segfault 1.1 /* move into background? */
662     if (backgrd) {
663     xx = fork();
664     if (xx == -1)
665     fatal("CANNOT FORK PROCESS.", 0);
666     if (xx != 0) {
667     FILE *fp;
668    
669     /* need to attempt to write pid now, not later */
670     unlink(pid_file);
671     fp = fopen(pid_file, "w");
672     if (fp != NULL) {
673     fprintf(fp, "%u\n", xx);
674     if (fflush(fp)) {
675     /* kill bot incase a botchk is run from crond */
676     printf(EGG_NOWRITE, pid_file);
677     printf(" Try freeing some disk space\n");
678     fclose(fp);
679     unlink(pid_file);
680     exit(1);
681     }
682     fclose(fp);
683     } else
684     printf(EGG_NOWRITE, pid_file);
685     printf("Launched into the background (pid: %d)\n\n", xx);
686     #if HAVE_SETPGID
687     setpgid(xx, xx);
688     #endif
689     exit(0);
690     }
691     }
692 fabian 1.5 #endif
693 segfault 1.1 use_stderr = 0; /* stop writing to stderr now */
694     xx = getpid();
695     if ((xx != 0) && (!backgrd)) {
696     FILE *fp;
697    
698     /* write pid to file */
699     unlink(pid_file);
700     fp = fopen(pid_file, "w");
701     if (fp != NULL) {
702     fprintf(fp, "%u\n", xx);
703     if (fflush(fp)) {
704     /* let the bot live since this doesn't appear to be a botchk */
705     printf(EGG_NOWRITE, pid_file);
706     fclose(fp);
707     unlink(pid_file);
708     }
709     fclose(fp);
710     } else
711     printf(EGG_NOWRITE, pid_file);
712     }
713     if (backgrd) {
714     /* ok, try to disassociate from controlling terminal */
715     /* (finger cross) */
716 fabian 1.5 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
717 segfault 1.1 setpgid(0, 0);
718     #endif
719     /* close out stdin/out/err */
720     freopen("/dev/null", "r", stdin);
721     freopen("/dev/null", "w", stdout);
722     freopen("/dev/null", "w", stderr);
723 fabian 1.5 #ifdef CYGWIN_HACKS
724     FreeConsole();
725     #endif
726 segfault 1.1 /* tcl wants those file handles kept open */
727     /* close(0); close(1); close(2); */
728     }
729     /* terminal emulating dcc chat */
730     if ((!backgrd) && (term_z)) {
731     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
732    
733     dcc[n].addr = iptolong(getmyip());
734     dcc[n].sock = STDOUT;
735     dcc[n].timeval = now;
736     dcc[n].u.chat->con_flags = conmask;
737     dcc[n].u.chat->strip_flags = STRIP_ALL;
738     dcc[n].status = STAT_ECHO;
739     strcpy(dcc[n].nick, "HQ");
740     strcpy(dcc[n].host, "llama@console");
741     dcc[n].user = get_user_by_handle(userlist, "HQ");
742     /* make sure there's an innocuous HQ user if needed */
743     if (!dcc[n].user) {
744     adduser(userlist, "HQ", "none", "-", USER_PARTY);
745     dcc[n].user = get_user_by_handle(userlist, "HQ");
746     }
747     setsock(STDOUT, 0); /* entry in net table */
748     dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
749     dcc_chatter(n);
750     }
751     then = now;
752     online_since = now;
753     autolink_cycle(NULL); /* hurry and connect to tandem bots */
754     add_help_reference("cmds1.help");
755     add_help_reference("cmds2.help");
756     add_help_reference("core.help");
757     add_hook(HOOK_SECONDLY, core_secondly);
758     add_hook(HOOK_MINUTELY, core_minutely);
759     add_hook(HOOK_HOURLY, core_hourly);
760     add_hook(HOOK_REHASH, event_rehash);
761     add_hook(HOOK_PRE_REHASH, event_prerehash);
762     add_hook(HOOK_USERFILE, event_save);
763     add_hook(HOOK_DAILY, event_logfile);
764    
765     debug0("main: entering loop");
766     while (1) {
767     int socket_cleanup = 0;
768    
769     context;
770 fabian 1.8 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
771 segfault 1.1 /* process a single tcl event */
772     Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
773 fabian 1.5 #endif
774 segfault 1.1 /* lets move some of this here, reducing the numer of actual
775     * calls to periodic_timers */
776     now = time(NULL);
777     random(); /* woop, lets really jumble things */
778     if (now != then) { /* once a second */
779     call_hook(HOOK_SECONDLY);
780     then = now;
781     }
782     context;
783     /* only do this every so often */
784     if (!socket_cleanup) {
785     /* clean up sockets that were just left for dead */
786     for (i = 0; i < dcc_total; i++) {
787     if (dcc[i].type == &DCC_LOST) {
788     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
789     lostdcc(i);
790     i--;
791     }
792     }
793     /* check for server or dcc activity */
794     dequeue_sockets();
795     socket_cleanup = 5;
796     } else
797     socket_cleanup--;
798     /* new net routines: help me mary! */
799     xx = sockgets(buf, &i);
800     if (xx >= 0) { /* non-error */
801     int idx;
802    
803     context;
804     for (idx = 0; idx < dcc_total; idx++)
805     if (dcc[idx].sock == xx) {
806     if (dcc[idx].type && dcc[idx].type->activity)
807     dcc[idx].type->activity(idx, buf, i);
808     else
809     putlog(LOG_MISC, "*",
810     "!!! untrapped dcc activity: type %s, sock %d",
811     dcc[idx].type->name, dcc[idx].sock);
812     break;
813     }
814     } else if (xx == -1) { /* EOF from someone */
815     int idx;
816    
817     if ((i == STDOUT) && !backgrd)
818     fatal("END OF FILE ON TERMINAL", 0);
819     context;
820     for (idx = 0; idx < dcc_total; idx++)
821     if (dcc[idx].sock == i) {
822     if (dcc[idx].type && dcc[idx].type->eof)
823     dcc[idx].type->eof(idx);
824     else {
825     putlog(LOG_MISC, "*",
826     "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
827 fabian 1.2 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
828     killsock(i);
829 segfault 1.1 lostdcc(idx);
830     }
831     idx = dcc_total + 1;
832     }
833     if (idx == dcc_total) {
834     putlog(LOG_MISC, "*",
835 fabian 1.2 "(@) EOF socket %d, not a dcc socket, not anything.", i);
836     close(i);
837     killsock(i);
838 segfault 1.1 }
839     } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
840     context;
841     putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
842     for (i = 0; i < dcc_total; i++) {
843     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
844     if (dcc[i].type == &DCC_LOST)
845     dcc[i].type = (struct dcc_table *) (dcc[i].sock);
846     putlog(LOG_MISC, "*",
847     "DCC socket %d (type %d, name '%s') expired -- pfft",
848     dcc[i].sock, dcc[i].type, dcc[i].nick);
849     killsock(dcc[i].sock);
850     lostdcc(i);
851     i--;
852     }
853     }
854     } else if (xx == (-3)) {
855     call_hook(HOOK_IDLE);
856     socket_cleanup = 0; /* if we've been idle, cleanup & flush */
857     }
858     if (do_restart) {
859     if (do_restart == -2)
860     rehash();
861     else {
862     /* unload as many modules as possible */
863     int f = 1;
864     module_entry *p;
865     Function x;
866     char xx[256];
867    
868     while (f) {
869     f = 0;
870     for (p = module_list; p != NULL; p = p->next) {
871     dependancy *d = dependancy_list;
872     int ok = 1;
873    
874     while (ok && d) {
875     if (d->needed == p)
876     ok = 0;
877     d = d->next;
878     }
879     if (ok) {
880     strcpy(xx, p->name);
881     if (module_unload(xx, origbotname) == NULL) {
882     f = 1;
883     break;
884     }
885     }
886     }
887     }
888     p = module_list;
889     if (p && p->next && p->next->next)
890     /* should be only 2 modules now -
891     * blowfish & eggdrop */
892     putlog(LOG_MISC, "*", MOD_STAGNANT);
893     context;
894     flushlogs();
895     context;
896     kill_tcl();
897 fabian 1.8 init_tcl(argc, argv);
898 segfault 1.1 init_language(0);
899     x = p->funcs[MODCALL_START];
900     x(0);
901     rehash();
902     restart_chons();
903     }
904     do_restart = 0;
905     }
906     }
907     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23