ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8925
Committed: Mon Apr 22 10:31:49 2019 UTC (6 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 12536 byte(s)
Log Message:
- ircd.c:main(): improve seed generation

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2019 ircd-hybrid development team
5 *
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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file ircd.c
23 * \brief Starts up and runs the ircd.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "user.h"
29 #include "list.h"
30 #include "ircd.h"
31 #include "channel.h"
32 #include "channel_mode.h"
33 #include "client.h"
34 #include "event.h"
35 #include "fdlist.h"
36 #include "hash.h"
37 #include "id.h"
38 #include "irc_string.h"
39 #include "ircd_signal.h"
40 #include "motd.h"
41 #include "conf.h"
42 #include "hostmask.h"
43 #include "parse.h"
44 #include "res.h"
45 #include "restart.h"
46 #include "rng_mt.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "server.h"
50 #include "server_capab.h"
51 #include "send.h"
52 #include "modules.h"
53 #include "memory.h"
54 #include "ircd_getopt.h"
55 #include "conf_db.h"
56 #include "conf_class.h"
57 #include "ipcache.h"
58 #include "isupport.h"
59 #include "patchlevel.h"
60 #include "serno.h"
61
62
63 struct SetOptions GlobalSetOptions; /* /quote set variables */
64 struct Counter Count;
65 struct ServerState_t server_state;
66 struct ServerStatistics ServerStats;
67 struct Connection meConnection; /* That's also part of me */
68 struct Client me = { .connection = &meConnection }; /* That's me */
69
70 char **myargv;
71 const char *logFileName = LPATH;
72 const char *pidFileName = PPATH;
73
74 bool dorehash;
75 bool doremotd;
76
77 static bool printVersion;
78
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 BOOLEAN, "Run in foreground (don't detach)" },
97 { "version", &printVersion,
98 BOOLEAN, "Print version and exit" },
99 { "help", NULL, USAGE, "Print this text" },
100 { NULL, NULL, STRING, NULL },
101 };
102
103 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 .when = STARTUP_CONNECTIONS_TIME
115 };
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 static void
139 io_loop(void)
140 {
141 while (true)
142 {
143 if (listing_client_list.head)
144 {
145 dlink_node *node = NULL, *node_next = NULL;
146 DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
147 safe_list_channels(node->data, false);
148 }
149
150 /* Run pending events */
151 event_run();
152
153 comm_select();
154 exit_aborted_clients();
155 free_exited_clients();
156
157 /* Check to see whether we have to rehash the configuration. */
158 if (dorehash == true)
159 {
160 conf_rehash(true);
161 dorehash = false;
162 }
163
164 if (doremotd == true)
165 {
166 motd_recache();
167 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
168 "Got signal SIGUSR1, reloading motd file(s)");
169 doremotd = false;
170 }
171 }
172 }
173
174 /* initalialize_global_set_options()
175 *
176 * inputs - none
177 * output - none
178 * side effects - This sets all global set options needed
179 */
180 static void
181 initialize_global_set_options(void)
182 {
183 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
184 GlobalSetOptions.autoconn = true;
185 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
186 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
187 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
188 GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime;
189 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
190 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
191 }
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 FILE *fb;
203
204 if ((fb = fopen(filename, "w")))
205 {
206 char buf[IRCD_BUFSIZE];
207 unsigned int pid = (unsigned int)getpid();
208
209 snprintf(buf, sizeof(buf), "%u\n", pid);
210
211 if (fputs(buf, fb) == -1)
212 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
213 filename, strerror(errno));
214
215 fclose(fb);
216 }
217 else
218 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
219 filename, strerror(errno));
220 }
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 FILE *fb;
234 char buf[IRCD_BUFSIZE];
235
236 if ((fb = fopen(filename, "r")))
237 {
238 if (fgets(buf, 20, fb) == NULL)
239 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
240 filename, strerror(errno));
241 else
242 {
243 pid_t pid = atoi(buf);
244
245 if (kill(pid, 0) == 0)
246 {
247 /* log(L_ERROR, "Server is already running"); */
248 printf("ircd: daemon is already running\n");
249 exit(EXIT_FAILURE);
250 }
251 }
252
253 fclose(fb);
254 }
255 else if (errno != ENOENT)
256 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
257 filename, strerror(errno));
258 }
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 if (getrlimit(RLIMIT_CORE, &rlim) == 0)
274 {
275 rlim.rlim_cur = rlim.rlim_max;
276 setrlimit(RLIMIT_CORE, &rlim);
277 }
278 }
279
280 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 /*
307 * print_startup - print startup information
308 */
309 static void
310 print_startup(int pid)
311 {
312 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
313 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 int
338 main(int argc, char *argv[])
339 {
340 /* Check to see if the user is running us as root, which is a nono */
341 if (geteuid() == 0)
342 {
343 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
344 return -1;
345 }
346
347 /* Setup corefile size immediately after boot -kre */
348 setup_corefile();
349
350 setup_fdlimit();
351
352 /* Save server boot time right away, so getrusage works correctly */
353 event_time_set();
354
355 /* It's not random, but it ought to be a little harder to guess */
356 uint32_t seed = (uint32_t)(event_base->time.sec_real ^
357 (event_base->time.sec_monotonic | (getpid() << 16)));
358 init_genrand(seed);
359
360 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
369 myargv = argv;
370 umask(077); /* umask 077: u=rwx,g=,o= */
371
372 parseargs(&argc, &argv, myopts);
373
374 if (printVersion == true)
375 {
376 printf("ircd: version %s(%s)\n", PATCHLEVEL, SERIALNUM);
377 exit(EXIT_SUCCESS);
378 }
379
380 if (chdir(ConfigGeneral.dpath))
381 {
382 perror("chdir");
383 exit(EXIT_FAILURE);
384 }
385
386 if (server_state.foreground == false)
387 {
388 make_daemon();
389 close_standard_fds(); /* this needs to be before comm_select_init()! */
390 }
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 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
399
400 comm_select_init(); /* This needs to be setup early ! -- adrian */
401 tls_init();
402
403 /* Check if there is pidfile and daemon already running */
404 check_pidfile(pidFileName);
405
406 isupport_init();
407 hash_init();
408 ipcache_init();
409 client_init();
410 class_init();
411 resolver_init(); /* Needs to be setup before the io loop */
412 modules_init();
413 read_conf_files(true); /* cold start init conf files */
414 capab_init(); /* Set up default_server_capabs */
415 initialize_global_set_options(); /* Has to be called after read_conf_files() */
416 channel_mode_init();
417 read_links_file();
418 motd_init();
419 user_modes_init();
420
421 if (EmptyString(ConfigServerInfo.name))
422 {
423 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
424 exit(EXIT_FAILURE);
425 }
426
427 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
428
429 /* serverinfo {} description must exist. If not, error out.*/
430 if (EmptyString(ConfigServerInfo.description))
431 {
432 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
433 exit(EXIT_FAILURE);
434 }
435
436 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
437
438 if (EmptyString(ConfigServerInfo.sid))
439 {
440 ilog(LOG_TYPE_IRCD, "Generating server ID");
441 generate_sid();
442 }
443 else
444 strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
445
446 init_uid();
447
448 me.from = &me;
449 me.servptr = &me;
450 me.connection->created_real = event_base->time.sec_real;
451 me.connection->created_monotonic = event_base->time.sec_monotonic;
452
453 SetMe(&me);
454 server_make(&me);
455
456 hash_add_id(&me);
457 hash_add_client(&me);
458
459 dlinkAdd(&me, &me.node, &global_server_list);
460
461 load_kline_database(ConfigGeneral.klinefile);
462 load_dline_database(ConfigGeneral.dlinefile);
463 load_xline_database(ConfigGeneral.xlinefile);
464 load_resv_database(ConfigGeneral.resvfile);
465
466 load_all_modules(true);
467 load_conf_modules();
468 load_core_modules(true);
469
470 write_pidfile(pidFileName);
471
472 event_addish(&event_cleanup_tklines, NULL);
473
474 /* We want try_connections to be called as soon as possible now! -- adrian */
475 /* No, 'cause after a restart it would cause all sorts of nick collides */
476 event_addish(&event_try_connections, NULL);
477
478 /* Setup the timeout check. I'll shift it later :) -- adrian */
479 event_add(&event_comm_checktimeouts, NULL);
480
481 event_addish(&event_save_all_databases, NULL);
482
483 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false)
484 {
485 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
486 event_add(&event_write_links_file, NULL);
487 }
488
489 ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s(%s)", PATCHLEVEL, SERIALNUM);
490 io_loop();
491
492 return 0;
493 }

Properties

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