ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 2881
Committed: Mon Jan 20 17:15:39 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 17061 byte(s)
Log Message:
- Use the i/o subsystem to execute scheduled writes. Patch provided by Adam.

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

Properties

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