ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 9089
Committed: Sun Oct 27 21:27:12 2019 UTC (4 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 12498 byte(s)
Log Message:
- hash: move initialization of 'hashf_xor_key' into strhash() and kill hash_init()
- hash.c: stylistic changes

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8752 * Copyright (c) 1997-2019 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file ircd.c
23     * \brief Starts up and runs the ircd.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 3347 #include "user.h"
29 michael 1011 #include "list.h"
30 adx 30 #include "ircd.h"
31     #include "channel.h"
32 michael 8089 #include "channel_mode.h"
33 adx 30 #include "client.h"
34     #include "event.h"
35     #include "fdlist.h"
36     #include "hash.h"
37 michael 6161 #include "id.h"
38 adx 30 #include "irc_string.h"
39     #include "ircd_signal.h"
40     #include "motd.h"
41 michael 1632 #include "conf.h"
42 adx 30 #include "hostmask.h"
43     #include "parse.h"
44 michael 3322 #include "res.h"
45 adx 30 #include "restart.h"
46 michael 982 #include "rng_mt.h"
47 adx 30 #include "s_bsd.h"
48 michael 1309 #include "log.h"
49 michael 6481 #include "server.h"
50 michael 8166 #include "server_capab.h"
51 adx 30 #include "send.h"
52     #include "modules.h"
53     #include "memory.h"
54     #include "ircd_getopt.h"
55 michael 1622 #include "conf_db.h"
56 michael 1632 #include "conf_class.h"
57 michael 4325 #include "ipcache.h"
58 michael 6185 #include "isupport.h"
59 michael 8729 #include "patchlevel.h"
60     #include "serno.h"
61 adx 30
62 michael 1858
63 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
64 michael 5602 struct Counter Count;
65     struct ServerState_t server_state;
66     struct ServerStatistics ServerStats;
67 michael 5737 struct Connection meConnection; /* That's also part of me */
68 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
69 adx 30
70 michael 5460 char **myargv;
71 adx 30 const char *logFileName = LPATH;
72     const char *pidFileName = PPATH;
73    
74 michael 8658 bool dorehash;
75     bool doremotd;
76 adx 30
77 michael 8658 static bool printVersion;
78 michael 6735
79     static struct lgetopt myopts[] =
80     {
81     { "configfile", &ConfigGeneral.configfile,
82     STRING, "File to use for ircd.conf" },
83     { "klinefile", &ConfigGeneral.klinefile,
84     STRING, "File to use for kline database" },
85     { "dlinefile", &ConfigGeneral.dlinefile,
86     STRING, "File to use for dline database" },
87     { "xlinefile", &ConfigGeneral.xlinefile,
88     STRING, "File to use for xline database" },
89     { "resvfile", &ConfigGeneral.resvfile,
90     STRING, "File to use for resv database" },
91     { "logfile", &logFileName,
92     STRING, "File to use for ircd.log" },
93     { "pidfile", &pidFileName,
94     STRING, "File to use for process ID" },
95     { "foreground", &server_state.foreground,
96 michael 8862 BOOLEAN, "Run in foreground (don't detach)" },
97 michael 6735 { "version", &printVersion,
98 michael 8862 BOOLEAN, "Print version and exit" },
99 michael 6735 { "help", NULL, USAGE, "Print this text" },
100     { NULL, NULL, STRING, NULL },
101     };
102    
103 michael 4094 static struct event event_cleanup_tklines =
104     {
105     .name = "cleanup_tklines",
106     .handler = cleanup_tklines,
107     .when = CLEANUP_TKLINES_TIME
108     };
109    
110     static struct event event_try_connections =
111     {
112     .name = "try_connections",
113     .handler = try_connections,
114 michael 8982 .when = 5
115 michael 4094 };
116    
117     static struct event event_comm_checktimeouts =
118     {
119     .name = "comm_checktimeouts",
120     .handler = comm_checktimeouts,
121     .when = 1
122     };
123    
124     static struct event event_save_all_databases =
125     {
126     .name = "save_all_databases",
127     .handler = save_all_databases,
128     .when = DATABASE_UPDATE_TIMEOUT
129     };
130    
131     struct event event_write_links_file =
132     {
133     .name = "write_links_file",
134     .handler = write_links_file,
135     };
136    
137    
138 adx 30 static void
139     io_loop(void)
140     {
141 michael 8664 while (true)
142 adx 30 {
143     if (listing_client_list.head)
144     {
145 michael 4815 dlink_node *node = NULL, *node_next = NULL;
146     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
147 michael 8656 safe_list_channels(node->data, false);
148 adx 30 }
149    
150 michael 4094 /* Run pending events */
151     event_run();
152 adx 30
153     comm_select();
154     exit_aborted_clients();
155     free_exited_clients();
156    
157 michael 6735 /* Check to see whether we have to rehash the configuration. */
158 michael 8658 if (dorehash == true)
159 adx 30 {
160 michael 8658 conf_rehash(true);
161     dorehash = false;
162 adx 30 }
163 michael 3215
164 michael 8658 if (doremotd == true)
165 adx 30 {
166 michael 2150 motd_recache();
167 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
168 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
169 michael 8658 doremotd = false;
170 adx 30 }
171     }
172     }
173    
174     /* initalialize_global_set_options()
175     *
176     * inputs - none
177     * output - none
178 michael 2916 * side effects - This sets all global set options needed
179 adx 30 */
180     static void
181     initialize_global_set_options(void)
182     {
183 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
184 michael 8800 GlobalSetOptions.autoconn = true;
185 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
186 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
187     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
188 michael 7858 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
189 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
190     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
191 adx 30 }
192    
193     /* write_pidfile()
194     *
195     * inputs - filename+path of pid file
196     * output - NONE
197     * side effects - write the pid of the ircd to filename
198     */
199     static void
200     write_pidfile(const char *filename)
201     {
202 michael 1325 FILE *fb;
203 adx 30
204 michael 1325 if ((fb = fopen(filename, "w")))
205 adx 30 {
206 michael 6470 char buf[IRCD_BUFSIZE];
207 adx 30 unsigned int pid = (unsigned int)getpid();
208    
209 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
210 michael 1325
211 michael 6470 if (fputs(buf, fb) == -1)
212 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
213     filename, strerror(errno));
214 adx 30
215 michael 1325 fclose(fb);
216 adx 30 }
217     else
218 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
219 michael 4748 filename, strerror(errno));
220 adx 30 }
221    
222     /* check_pidfile()
223     *
224     * inputs - filename+path of pid file
225     * output - none
226     * side effects - reads pid from pidfile and checks if ircd is in process
227     * list. if it is, gracefully exits
228     * -kre
229     */
230     static void
231     check_pidfile(const char *filename)
232     {
233 michael 1325 FILE *fb;
234 michael 6470 char buf[IRCD_BUFSIZE];
235 adx 30
236 michael 1325 if ((fb = fopen(filename, "r")))
237 adx 30 {
238 michael 8702 if (fgets(buf, 20, fb) == NULL)
239 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
240     filename, strerror(errno));
241 adx 30 else
242     {
243 michael 6481 pid_t pid = atoi(buf);
244 adx 30
245 michael 8702 if (kill(pid, 0) == 0)
246 adx 30 {
247     /* log(L_ERROR, "Server is already running"); */
248     printf("ircd: daemon is already running\n");
249 michael 6646 exit(EXIT_FAILURE);
250 adx 30 }
251     }
252    
253 michael 1325 fclose(fb);
254 adx 30 }
255     else if (errno != ENOENT)
256 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
257     filename, strerror(errno));
258 adx 30 }
259    
260     /* setup_corefile()
261     *
262     * inputs - nothing
263     * output - nothing
264     * side effects - setups corefile to system limits.
265     * -kre
266     */
267     static void
268     setup_corefile(void)
269     {
270     struct rlimit rlim; /* resource limits */
271    
272     /* Set corefilesize to maximum */
273 michael 8702 if (getrlimit(RLIMIT_CORE, &rlim) == 0)
274 adx 30 {
275     rlim.rlim_cur = rlim.rlim_max;
276     setrlimit(RLIMIT_CORE, &rlim);
277     }
278     }
279    
280 michael 8774 static void
281     setup_fdlimit(void)
282     {
283     struct rlimit rlim; /* resource limits */
284    
285     if (getrlimit(RLIMIT_NOFILE, &rlim))
286     {
287     fprintf(stderr, "getrlimit: couldn't get maximum number of file descriptors: %s\n",
288     strerror(errno));
289     exit(EXIT_FAILURE);
290     }
291    
292     if (rlim.rlim_max > 0xFFFF)
293     rlim.rlim_max = 0xFFFF;
294     rlim.rlim_cur = rlim.rlim_max;
295    
296     if (setrlimit(RLIMIT_NOFILE, &rlim) == 0)
297     hard_fdlimit = rlim.rlim_cur;
298     else
299     {
300     fprintf(stderr, "setrlimit: couldn't set maximum number of file descriptors: %s\n",
301     strerror(errno));
302     exit(EXIT_FAILURE);
303     }
304     }
305    
306 michael 6735 /*
307     * print_startup - print startup information
308     */
309     static void
310     print_startup(int pid)
311     {
312 michael 8729 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
313 michael 6735 printf("ircd: pid %d\n", pid);
314     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
315     : "foreground", ConfigGeneral.dpath);
316     }
317    
318     static void
319     make_daemon(void)
320     {
321     int pid;
322    
323     if ((pid = fork()) < 0)
324     {
325     perror("fork");
326     exit(EXIT_FAILURE);
327     }
328     else if (pid > 0)
329     {
330     print_startup(pid);
331     exit(EXIT_SUCCESS);
332     }
333    
334     setsid();
335     }
336    
337 adx 30 int
338     main(int argc, char *argv[])
339     {
340 michael 2253 /* Check to see if the user is running us as root, which is a nono */
341 michael 8702 if (geteuid() == 0)
342 adx 30 {
343 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
344 michael 982 return -1;
345 adx 30 }
346    
347     /* Setup corefile size immediately after boot -kre */
348     setup_corefile();
349    
350 michael 8774 setup_fdlimit();
351    
352 michael 5545 /* Save server boot time right away, so getrusage works correctly */
353 michael 8900 event_time_set();
354 adx 30
355 michael 5545 /* It's not random, but it ought to be a little harder to guess */
356 michael 8925 uint32_t seed = (uint32_t)(event_base->time.sec_real ^
357     (event_base->time.sec_monotonic | (getpid() << 16)));
358     init_genrand(seed);
359 michael 982
360 michael 4340 ConfigGeneral.dpath = DPATH;
361     ConfigGeneral.spath = SPATH;
362     ConfigGeneral.mpath = MPATH;
363     ConfigGeneral.configfile = CPATH; /* Server configuration file */
364     ConfigGeneral.klinefile = KPATH; /* Server kline file */
365     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
366     ConfigGeneral.dlinefile = DLPATH; /* dline file */
367     ConfigGeneral.resvfile = RESVPATH; /* resv file */
368 michael 1702
369 adx 30 myargv = argv;
370 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
371 adx 30
372     parseargs(&argc, &argv, myopts);
373    
374 michael 8660 if (printVersion == true)
375 adx 30 {
376 michael 8729 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
377 adx 30 exit(EXIT_SUCCESS);
378     }
379    
380 michael 4340 if (chdir(ConfigGeneral.dpath))
381 adx 30 {
382     perror("chdir");
383     exit(EXIT_FAILURE);
384     }
385    
386 michael 8660 if (server_state.foreground == false)
387 adx 30 {
388     make_daemon();
389 michael 8414 close_standard_fds(); /* this needs to be before comm_select_init()! */
390 adx 30 }
391     else
392     print_startup(getpid());
393    
394     setup_signals();
395    
396     /* We need this to initialise the fd array before anything else */
397     fdlist_init();
398 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
399 michael 4415
400 michael 8414 comm_select_init(); /* This needs to be setup early ! -- adrian */
401 michael 7279 tls_init();
402 michael 2253
403 adx 30 /* Check if there is pidfile and daemon already running */
404     check_pidfile(pidFileName);
405    
406 michael 6185 isupport_init();
407 michael 4319 ipcache_init();
408 michael 1798 client_init();
409 michael 1632 class_init();
410 michael 7569 resolver_init(); /* Needs to be setup before the io loop */
411 michael 1404 modules_init();
412 michael 8656 read_conf_files(true); /* cold start init conf files */
413 michael 8166 capab_init(); /* Set up default_server_capabs */
414 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
415 michael 8089 channel_mode_init();
416 michael 2216 read_links_file();
417 michael 2150 motd_init();
418 michael 6189 user_modes_init();
419 adx 30
420 michael 4340 if (EmptyString(ConfigServerInfo.name))
421 michael 1115 {
422 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
423 michael 1115 exit(EXIT_FAILURE);
424     }
425    
426 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
427 adx 30
428 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
429 michael 4340 if (EmptyString(ConfigServerInfo.description))
430 adx 30 {
431 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
432 adx 30 exit(EXIT_FAILURE);
433     }
434 michael 885
435 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
436 adx 30
437 michael 6156 if (EmptyString(ConfigServerInfo.sid))
438     {
439     ilog(LOG_TYPE_IRCD, "Generating server ID");
440     generate_sid();
441     }
442     else
443     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
444    
445 michael 6464 init_uid();
446    
447 michael 5545 me.from = &me;
448     me.servptr = &me;
449 michael 8919 me.connection->created_real = event_base->time.sec_real;
450     me.connection->created_monotonic = event_base->time.sec_monotonic;
451 adx 30
452     SetMe(&me);
453 michael 8426 server_make(&me);
454 adx 30
455 michael 1115 hash_add_id(&me);
456 adx 30 hash_add_client(&me);
457 michael 2916
458 michael 7963 dlinkAdd(&me, &me.node, &global_server_list);
459 adx 30
460 michael 6928 load_kline_database(ConfigGeneral.klinefile);
461     load_dline_database(ConfigGeneral.dlinefile);
462     load_xline_database(ConfigGeneral.xlinefile);
463     load_resv_database(ConfigGeneral.resvfile);
464 michael 1622
465 michael 8660 load_all_modules(true);
466 adx 30 load_conf_modules();
467 michael 8660 load_core_modules(true);
468 michael 1115
469 adx 30 write_pidfile(pidFileName);
470    
471 michael 4094 event_addish(&event_cleanup_tklines, NULL);
472 adx 30
473     /* We want try_connections to be called as soon as possible now! -- adrian */
474     /* No, 'cause after a restart it would cause all sorts of nick collides */
475 michael 4094 event_addish(&event_try_connections, NULL);
476 adx 30
477     /* Setup the timeout check. I'll shift it later :) -- adrian */
478 michael 4399 event_add(&event_comm_checktimeouts, NULL);
479 adx 30
480 michael 4094 event_addish(&event_save_all_databases, NULL);
481 michael 1625
482 michael 8660 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false)
483 michael 4094 {
484 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
485 michael 6636 event_add(&event_write_links_file, NULL);
486 michael 4094 }
487 adx 30
488 michael 8729 ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s(%s)", PATCHLEVEL, SERIALNUM);
489 adx 30 io_loop();
490 michael 6464
491 michael 885 return 0;
492 adx 30 }

Properties

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