ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 8752
Committed: Tue Jan 1 11:07:01 2019 UTC (7 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 12788 byte(s)
Log Message:
- Update copyright years

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

Properties

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