ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 948
Committed: Tue Jul 21 17:34:06 2009 UTC (16 years, 1 month ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/ircd.c
File size: 18054 byte(s)
Log Message:
- lp64\llp64\ilp32 portability fixes

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * ircd.c: Starts up and runs the ircd.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26     #include "s_user.h"
27     #include "tools.h"
28     #include "ircd.h"
29     #include "channel.h"
30     #include "channel_mode.h"
31     #include "client.h"
32     #include "common.h"
33     #include "event.h"
34     #include "fdlist.h"
35     #include "hash.h"
36     #include "irc_string.h"
37     #include "sprintf_irc.h"
38     #include "ircd_signal.h"
39     #include "list.h"
40     #include "s_gline.h"
41     #include "motd.h"
42     #include "ircd_handler.h"
43     #include "msg.h" /* msgtab */
44     #include "hostmask.h"
45     #include "numeric.h"
46     #include "packet.h"
47     #include "parse.h"
48     #include "irc_res.h"
49     #include "restart.h"
50     #include "s_auth.h"
51     #include "s_bsd.h"
52     #include "s_conf.h"
53     #include "s_log.h"
54     #include "s_misc.h"
55     #include "s_serv.h" /* try_connections */
56     #include "send.h"
57     #include "whowas.h"
58     #include "modules.h"
59     #include "memory.h"
60     #include "hook.h"
61     #include "ircd_getopt.h"
62     #include "balloc.h"
63     #include "motd.h"
64     #include "supported.h"
65 michael 876 #include "watch.h"
66 adx 30
67     /* Try and find the correct name to use with getrlimit() for setting the max.
68     * number of files allowed to be open by this process.
69     */
70    
71     /* /quote set variables */
72     struct SetOptions GlobalSetOptions;
73    
74     /* configuration set from ircd.conf */
75     struct config_file_entry ConfigFileEntry;
76     /* server info set from ircd.conf */
77     struct server_info ServerInfo;
78     /* admin info set from ircd.conf */
79     struct admin_info AdminInfo = { NULL, NULL, NULL };
80     struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
81     struct ServerState_t server_state = { 0 };
82     struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
83 michael 896 struct ServerStatistics ServerStats;
84 adx 30 struct timeval SystemTime;
85     struct Client me; /* That's me */
86     struct LocalUser meLocalUser; /* That's also part of me */
87    
88     static unsigned long initialVMTop = 0; /* top of virtual memory at init */
89     const char *logFileName = LPATH;
90     const char *pidFileName = PPATH;
91    
92     char **myargv;
93     char ircd_platform[PLATFORMLEN];
94    
95     int dorehash = 0;
96     int doremotd = 0;
97     time_t nextconnect = 1; /* time for next try_connections call */
98    
99     /* Set to zero because it should be initialized later using
100     * initialize_server_capabs
101     */
102     int default_server_capabs = 0;
103    
104     #ifdef HAVE_LIBCRYPTO
105     int bio_spare_fd = -1;
106     #endif
107    
108     int splitmode;
109     int splitchecking;
110     int split_users;
111     unsigned int split_servers;
112    
113     /* Do klines the same way hybrid-6 did them, i.e. at the
114     * top of the next io_loop instead of in the same loop as
115     * the klines are being applied.
116     *
117     * This should fix strange CPU starvation as very indirectly reported.
118     * (Why do you people not email bug reports? WHY? WHY?)
119     *
120     * - Dianora
121     */
122    
123     int rehashed_klines = 0;
124    
125    
126     /*
127     * print_startup - print startup information
128     */
129     static void
130     print_startup(int pid)
131     {
132     printf("ircd: version %s\n", ircd_version);
133     printf("ircd: pid %d\n", pid);
134     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
135     : "foreground", ConfigFileEntry.dpath);
136     }
137    
138     static void
139     make_daemon(void)
140     {
141     int pid;
142    
143     if ((pid = fork()) < 0)
144     {
145     perror("fork");
146     exit(EXIT_FAILURE);
147     }
148     else if (pid > 0)
149     {
150     print_startup(pid);
151     exit(EXIT_SUCCESS);
152     }
153    
154     setsid();
155     }
156     #endif
157    
158     static int printVersion = 0;
159    
160     struct lgetopt myopts[] = {
161     {"dlinefile", &ConfigFileEntry.dlinefile,
162     STRING, "File to use for dline.conf"},
163     {"configfile", &ConfigFileEntry.configfile,
164     STRING, "File to use for ircd.conf"},
165     {"klinefile", &ConfigFileEntry.klinefile,
166     STRING, "File to use for kline.conf"},
167     {"xlinefile", &ConfigFileEntry.xlinefile,
168     STRING, "File to use for xline.conf"},
169     {"logfile", &logFileName,
170     STRING, "File to use for ircd.log"},
171     {"pidfile", &pidFileName,
172     STRING, "File to use for process ID"},
173     {"foreground", &server_state.foreground,
174     YESNO, "Run in foreground (don't detach)"},
175     {"version", &printVersion,
176     YESNO, "Print version and exit"},
177     {"help", NULL, USAGE, "Print this text"},
178     {NULL, NULL, STRING, NULL},
179     };
180    
181     void
182     set_time(void)
183     {
184     static char to_send[200];
185     struct timeval newtime;
186     #ifdef _WIN32
187     FILETIME ft;
188    
189     GetSystemTimeAsFileTime(&ft);
190     if (ft.dwLowDateTime < 0xd53e8000)
191     ft.dwHighDateTime--;
192     ft.dwLowDateTime -= 0xd53e8000;
193     ft.dwHighDateTime -= 0x19db1de;
194    
195     newtime.tv_sec = (*(uint64_t *) &ft) / 10000000;
196     newtime.tv_usec = (*(uint64_t *) &ft) / 10 % 1000000;
197     #else
198     newtime.tv_sec = 0;
199     newtime.tv_usec = 0;
200    
201     if (gettimeofday(&newtime, NULL) == -1)
202     {
203     ilog(L_ERROR, "Clock Failure (%s), TS can be corrupted",
204     strerror(errno));
205     sendto_realops_flags(UMODE_ALL, L_ALL,
206     "Clock Failure (%s), TS can be corrupted",
207     strerror(errno));
208     restart("Clock Failure");
209     }
210     #endif
211    
212     if (newtime.tv_sec < CurrentTime)
213     {
214     ircsprintf(to_send, "System clock is running backwards - (%lu < %lu)",
215     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
216     report_error(L_ALL, to_send, me.name, 0);
217     set_back_events(CurrentTime - newtime.tv_sec);
218     }
219    
220     SystemTime.tv_sec = newtime.tv_sec;
221     SystemTime.tv_usec = newtime.tv_usec;
222     }
223    
224     static void
225     io_loop(void)
226     {
227     while (1 == 1)
228     {
229     /*
230     * Maybe we want a flags word?
231     * ie. if (REHASHED_KLINES(global_flags))
232     * SET_REHASHED_KLINES(global_flags)
233     * CLEAR_REHASHED_KLINES(global_flags)
234     *
235     * - Dianora
236     */
237     if (rehashed_klines)
238     {
239     check_conf_klines();
240     rehashed_klines = 0;
241     }
242    
243     if (listing_client_list.head)
244     {
245     dlink_node *ptr = NULL, *ptr_next = NULL;
246     DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
247     {
248     struct Client *client_p = ptr->data;
249     assert(client_p->localClient->list_task);
250 michael 896 safe_list_channels(client_p, client_p->localClient->list_task, 0);
251 adx 30 }
252     }
253    
254     /* Run pending events, then get the number of seconds to the next
255     * event
256     */
257     while (eventNextTime() <= CurrentTime)
258     eventRun();
259    
260     comm_select();
261     exit_aborted_clients();
262     free_exited_clients();
263     send_queued_all();
264    
265     /* Check to see whether we have to rehash the configuration .. */
266     if (dorehash)
267     {
268     rehash(1);
269     dorehash = 0;
270     }
271     if (doremotd)
272     {
273     read_message_file(&ConfigFileEntry.motd);
274     sendto_realops_flags(UMODE_ALL, L_ALL,
275     "Got signal SIGUSR1, reloading ircd motd file");
276     doremotd = 0;
277     }
278     }
279     }
280    
281     /* initalialize_global_set_options()
282     *
283     * inputs - none
284     * output - none
285     * side effects - This sets all global set options needed
286     */
287     static void
288     initialize_global_set_options(void)
289     {
290     memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
291    
292     GlobalSetOptions.autoconn = 1;
293     GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
294     GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
295    
296     if (ConfigFileEntry.default_floodcount)
297     GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
298     else
299     GlobalSetOptions.floodcount = 10;
300    
301     /* XXX I have no idea what to try here - Dianora */
302     GlobalSetOptions.joinfloodcount = 16;
303     GlobalSetOptions.joinfloodtime = 8;
304    
305     split_servers = ConfigChannel.default_split_server_count;
306     split_users = ConfigChannel.default_split_user_count;
307    
308     if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
309     ConfigChannel.no_join_on_split))
310     {
311     splitmode = 1;
312     splitchecking = 1;
313     }
314    
315     GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
316     GlobalSetOptions.idletime = ConfigFileEntry.idletime;
317     /* End of global set options */
318     }
319    
320     /* initialize_message_files()
321     *
322     * inputs - none
323     * output - none
324     * side effects - Set up all message files needed, motd etc.
325     */
326     static void
327     initialize_message_files(void)
328     {
329     init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd);
330     init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
331     init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
332    
333     read_message_file(&ConfigFileEntry.motd);
334     read_message_file(&ConfigFileEntry.opermotd);
335     read_message_file(&ConfigFileEntry.linksfile);
336    
337     init_isupport();
338     }
339    
340     /* initialize_server_capabs()
341     *
342     * inputs - none
343     * output - none
344     */
345     static void
346     initialize_server_capabs(void)
347     {
348     add_capability("QS", CAP_QS, 1);
349     add_capability("EOB", CAP_EOB, 1);
350 michael 885
351 adx 30 if (ServerInfo.sid != NULL) /* only enable TS6 if we have an SID */
352     add_capability("TS6", CAP_TS6, 0);
353 michael 885
354 adx 30 add_capability("ZIP", CAP_ZIP, 0);
355     add_capability("CLUSTER", CAP_CLUSTER, 1);
356     #ifdef HALFOPS
357     add_capability("HOPS", CAP_HOPS, 1);
358     #endif
359     }
360    
361     /* write_pidfile()
362     *
363     * inputs - filename+path of pid file
364     * output - NONE
365     * side effects - write the pid of the ircd to filename
366     */
367     static void
368     write_pidfile(const char *filename)
369     {
370     FBFILE *fb;
371    
372     if ((fb = fbopen(filename, "w")))
373     {
374     char buff[32];
375     unsigned int pid = (unsigned int)getpid();
376     size_t nbytes = ircsprintf(buff, "%u\n", pid);
377    
378     if ((fbputs(buff, fb, nbytes) == -1))
379     ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
380     pid, filename, strerror(errno));
381    
382     fbclose(fb);
383     return;
384     }
385     else
386     {
387     ilog(L_ERROR, "Error opening pid file %s", filename);
388     }
389     }
390    
391     /* check_pidfile()
392     *
393     * inputs - filename+path of pid file
394     * output - none
395     * side effects - reads pid from pidfile and checks if ircd is in process
396     * list. if it is, gracefully exits
397     * -kre
398     */
399     static void
400     check_pidfile(const char *filename)
401     {
402     #ifndef _WIN32
403     FBFILE *fb;
404     char buff[32];
405     pid_t pidfromfile;
406    
407     /* Don't do logging here, since we don't have log() initialised */
408     if ((fb = fbopen(filename, "r")))
409     {
410     if (fbgets(buff, 20, fb) == NULL)
411     {
412     /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
413     * strerror(errno));
414     */
415     }
416     else
417     {
418     pidfromfile = atoi(buff);
419    
420     if (!kill(pidfromfile, 0))
421     {
422     /* log(L_ERROR, "Server is already running"); */
423     printf("ircd: daemon is already running\n");
424     exit(-1);
425     }
426     }
427    
428     fbclose(fb);
429     }
430     else if (errno != ENOENT)
431     {
432     /* log(L_ERROR, "Error opening pid file %s", filename); */
433     }
434     #endif
435     }
436    
437     /* setup_corefile()
438     *
439     * inputs - nothing
440     * output - nothing
441     * side effects - setups corefile to system limits.
442     * -kre
443     */
444     static void
445     setup_corefile(void)
446     {
447     #ifdef HAVE_SYS_RESOURCE_H
448     struct rlimit rlim; /* resource limits */
449    
450     /* Set corefilesize to maximum */
451     if (!getrlimit(RLIMIT_CORE, &rlim))
452     {
453     rlim.rlim_cur = rlim.rlim_max;
454     setrlimit(RLIMIT_CORE, &rlim);
455     }
456     #endif
457     }
458    
459     /* init_ssl()
460     *
461     * inputs - nothing
462     * output - nothing
463     * side effects - setups SSL context.
464     */
465     static void
466     init_ssl(void)
467     {
468     #ifdef HAVE_LIBCRYPTO
469     SSL_load_error_strings();
470     SSLeay_add_ssl_algorithms();
471    
472     ServerInfo.ctx = SSL_CTX_new(SSLv23_server_method());
473     if (!ServerInfo.ctx)
474     {
475     const char *s;
476    
477     fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
478     s = ERR_lib_error_string(ERR_get_error()));
479     ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
480     }
481    
482     SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_NO_SSLv2);
483     SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
484     SSL_CTX_set_verify(ServerInfo.ctx, SSL_VERIFY_NONE, NULL);
485    
486     bio_spare_fd = save_spare_fd("SSL private key validation");
487     #endif /* HAVE_LIBCRYPTO */
488     }
489    
490     /* init_callbacks()
491     *
492     * inputs - nothing
493     * output - nothing
494     * side effects - setups standard hook points
495     */
496     static void
497     init_callbacks(void)
498     {
499 adx 163 iorecv_cb = register_callback("iorecv", iorecv_default);
500     iosend_cb = register_callback("iosend", iosend_default);
501 adx 30 iorecvctrl_cb = register_callback("iorecvctrl", NULL);
502     iosendctrl_cb = register_callback("iosendctrl", NULL);
503     }
504    
505     int
506     main(int argc, char *argv[])
507     {
508     /* Check to see if the user is running
509     * us as root, which is a nono
510     */
511     #ifndef _WIN32
512     if (geteuid() == 0)
513     {
514     fprintf(stderr, "Don't run ircd as root!!!\n");
515     return(-1);
516     }
517    
518     /* Setup corefile size immediately after boot -kre */
519     setup_corefile();
520     #endif
521    
522     /* save server boot time right away, so getrusage works correctly */
523     set_time();
524    
525     /* It ain't random, but it ought to be a little harder to guess */
526     srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
527     memset(&me, 0, sizeof(me));
528     memset(&meLocalUser, 0, sizeof(meLocalUser));
529     me.localClient = &meLocalUser;
530     dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
531     of Client list */
532    
533     memset(&ServerInfo, 0, sizeof(ServerInfo));
534 michael 896 memset(&ServerStats, 0, sizeof(ServerStats));
535 adx 30
536     /* Initialise the channel capability usage counts... */
537     init_chcap_usage_counts();
538    
539     ConfigFileEntry.dpath = DPATH;
540     ConfigFileEntry.configfile = CPATH; /* Server configuration file */
541     ConfigFileEntry.klinefile = KPATH; /* Server kline file */
542     ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
543     ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
544     ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
545     ConfigFileEntry.dlinefile = DLPATH; /* dline file */
546     ConfigFileEntry.glinefile = GPATH; /* gline log file */
547     ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
548     ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
549     myargv = argv;
550     umask(077); /* better safe than sorry --SRB */
551    
552     parseargs(&argc, &argv, myopts);
553    
554     if (printVersion)
555     {
556     printf("ircd: version %s\n", ircd_version);
557     exit(EXIT_SUCCESS);
558     }
559    
560     if (chdir(ConfigFileEntry.dpath))
561     {
562     perror("chdir");
563     exit(EXIT_FAILURE);
564     }
565    
566     init_ssl();
567    
568     #ifndef _WIN32
569     if (!server_state.foreground)
570     {
571     make_daemon();
572     close_standard_fds(); /* this needs to be before init_netio()! */
573     }
574     else
575     print_startup(getpid());
576    
577     setup_signals();
578     #endif
579    
580     get_ircd_platform(ircd_platform);
581    
582     /* Init the event subsystem */
583     eventInit();
584     /* We need this to initialise the fd array before anything else */
585     fdlist_init();
586     init_log(logFileName);
587     check_can_use_v6();
588     init_comm(); /* This needs to be setup early ! -- adrian */
589     /* Check if there is pidfile and daemon already running */
590     check_pidfile(pidFileName);
591    
592     #ifndef NOBALLOC
593     initBlockHeap();
594     #endif
595     init_dlink_nodes();
596     init_callbacks();
597     initialize_message_files();
598     dbuf_init();
599     init_hash();
600     init_ip_hash_table(); /* client host ip hash table */
601     init_host_hash(); /* Host-hashtable. */
602     clear_tree_parse();
603     init_client();
604     init_class();
605     init_whowas();
606 michael 876 watch_init();
607 adx 30 read_conf_files(1); /* cold start init conf files */
608     me.id[0] = '\0';
609     init_uid();
610     init_auth(); /* Initialise the auth code */
611     #ifndef _WIN32
612     init_resolver(); /* Needs to be setup before the io loop */
613     #endif
614     initialize_server_capabs(); /* Set up default_server_capabs */
615     initialize_global_set_options();
616     init_channels();
617    
618     if (ServerInfo.name == NULL)
619     {
620     ilog(L_CRIT, "No server name specified in serverinfo block.");
621     exit(EXIT_FAILURE);
622     }
623 michael 885
624 adx 30 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
625    
626     /* serverinfo{} description must exist. If not, error out.*/
627     if (ServerInfo.description == NULL)
628     {
629     ilog(L_CRIT,
630     "ERROR: No server description specified in serverinfo block.");
631     exit(EXIT_FAILURE);
632     }
633 michael 885
634 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
635    
636     me.from = &me;
637     me.servptr = &me;
638    
639     SetMe(&me);
640     make_server(&me);
641    
642     me.lasttime = me.since = me.firsttime = CurrentTime;
643     hash_add_client(&me);
644    
645     /* add ourselves to global_serv_list */
646     dlinkAdd(&me, make_dlink_node(), &global_serv_list);
647    
648     check_class();
649    
650     #ifndef STATIC_MODULES
651     if (chdir(MODPATH))
652     {
653     ilog (L_CRIT, "Could not load core modules. Terminating!");
654     exit(EXIT_FAILURE);
655     }
656    
657     load_all_modules(1);
658     load_conf_modules();
659     load_core_modules(1);
660     /* Go back to DPATH after checking to see if we can chdir to MODPATH */
661     chdir(ConfigFileEntry.dpath);
662     #else
663     load_all_modules(1);
664     #endif
665     /*
666     * assemble_umode_buffer() has to be called after
667     * reading conf/loading modules.
668     */
669     assemble_umode_buffer();
670    
671     write_pidfile(pidFileName);
672    
673     ilog(L_NOTICE, "Server Ready");
674    
675     eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
676     eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
677    
678     /* We want try_connections to be called as soon as possible now! -- adrian */
679     /* No, 'cause after a restart it would cause all sorts of nick collides */
680     eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
681    
682     eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
683    
684     /* Setup the timeout check. I'll shift it later :) -- adrian */
685     eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
686    
687     if (ConfigServerHide.links_delay > 0)
688     eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
689     else
690     ConfigServerHide.links_disabled = 1;
691    
692     if (splitmode)
693     eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
694    
695     io_loop();
696 michael 885 return 0;
697 adx 30 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision