ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
Revision: 1286
Committed: Sun Feb 5 15:20:03 2012 UTC (12 years, 1 month ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/ircd.c
File size: 17385 byte(s)
Log Message:
- ircd.c: fix compile warning

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

Properties

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