]> jfr.im git - solanum.git/blobdiff - ircd/ircd.c
Remove ancient portability code (#361)
[solanum.git] / ircd / ircd.c
index 96dcbce42d1c026b6332ccaa6982875e64894ee8..582964e8c261f6843877ed84c1b29e0368df8098 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  charybdis: A slightly useful ircd.
+ *  Solanum: a slightly advanced ircd
  *  ircd.c: Starts up and runs the ircd.
  *
  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
- *
- *  $Id$
  */
 
-#include "ratbox_lib.h"
+#include "rb_lib.h"
 #include "stdinc.h"
 #include "setup.h"
-#include "config.h"
+#include "defaults.h"
 #include "ircd.h"
 #include "channel.h"
 #include "class.h"
 #include "client.h"
-#include "common.h"
 #include "hash.h"
 #include "match.h"
 #include "ircd_signal.h"
@@ -41,9 +38,7 @@
 #include "hostmask.h"
 #include "numeric.h"
 #include "parse.h"
-#include "res.h"
 #include "restart.h"
-#include "s_auth.h"
 #include "s_conf.h"
 #include "logger.h"
 #include "s_serv.h"            /* try_connections */
 #include "ircd_getopt.h"
 #include "newconf.h"
 #include "reject.h"
-#include "s_conf.h"
 #include "s_newconf.h"
 #include "cache.h"
 #include "monitor.h"
 #include "patchlevel.h"
 #include "serno.h"
 #include "sslproc.h"
+#include "wsproc.h"
 #include "chmode.h"
 #include "privilege.h"
 #include "bandbi.h"
+#include "authproc.h"
+#include "operhash.h"
+
+static void
+ircd_die_cb(const char *str) __attribute__((noreturn));
 
 /* /quote set variables */
 struct SetOptions GlobalSetOptions;
@@ -96,18 +96,15 @@ rb_dlink_list global_serv_list;    /* global servers on the network */
 rb_dlink_list local_oper_list;     /* our opers, duplicated in lclient_list */
 rb_dlink_list oper_list;           /* network opers */
 
-const char *logFileName = LPATH;
-const char *pidFileName = PPATH;
-
-char **myargv;
-int dorehash = 0;
-int dorehashbans = 0;
-int doremotd = 0;
-int kline_queued = 0;
-int server_state_foreground = 0;
-int opers_see_all_users = 0;
-int ssl_ok = 0;
-int zlib_ok = 1;
+char * const *myargv;
+volatile sig_atomic_t dorehash = false;
+volatile sig_atomic_t dorehashbans = false;
+volatile sig_atomic_t doremotd = false;
+bool kline_queued = false;
+bool server_state_foreground = false;
+bool opers_see_all_users = false;
+bool ircd_ssl_ok = false;
+bool ircd_zlib_ok = true;
 
 int testing_conf = 0;
 time_t startup_time;
@@ -120,6 +117,47 @@ int split_users;
 int split_servers;
 int eob_count;
 
+const char *ircd_paths[IRCD_PATH_COUNT] = {
+       [IRCD_PATH_PREFIX] = DPATH,
+       [IRCD_PATH_MODULES] = MODPATH,
+       [IRCD_PATH_AUTOLOAD_MODULES] = AUTOMODPATH,
+       [IRCD_PATH_ETC] = ETCPATH,
+       [IRCD_PATH_LOG] = LOGPATH,
+       [IRCD_PATH_USERHELP] = UHPATH,
+       [IRCD_PATH_OPERHELP] = HPATH,
+       [IRCD_PATH_IRCD_EXEC] = SPATH,
+       [IRCD_PATH_IRCD_CONF] = CPATH,
+       [IRCD_PATH_IRCD_MOTD] = MPATH,
+       [IRCD_PATH_IRCD_LOG] = LPATH,
+       [IRCD_PATH_IRCD_PID] = PPATH,
+       [IRCD_PATH_IRCD_OMOTD] = OPATH,
+       [IRCD_PATH_BANDB] = DBPATH,
+       [IRCD_PATH_BIN] = BINPATH,
+       [IRCD_PATH_LIBEXEC] = PKGLIBEXECDIR,
+};
+
+const char *ircd_pathnames[IRCD_PATH_COUNT] = {
+       [IRCD_PATH_PREFIX] = "prefix",
+       [IRCD_PATH_MODULES] = "modules",
+       [IRCD_PATH_AUTOLOAD_MODULES] = "autoload modules",
+       [IRCD_PATH_ETC] = "config",
+       [IRCD_PATH_LOG] = "log",
+       [IRCD_PATH_USERHELP] = "user help",
+       [IRCD_PATH_OPERHELP] = "oper help",
+       [IRCD_PATH_IRCD_EXEC] = "ircd binary",
+       [IRCD_PATH_IRCD_CONF] = "ircd.conf",
+       [IRCD_PATH_IRCD_MOTD] = "ircd.motd",
+       [IRCD_PATH_IRCD_LOG] = "ircd.log",
+       [IRCD_PATH_IRCD_PID] = "ircd.pid",
+       [IRCD_PATH_IRCD_OMOTD] = "oper motd",
+       [IRCD_PATH_BANDB] = "bandb",
+       [IRCD_PATH_BIN] = "binary dir",
+       [IRCD_PATH_LIBEXEC] = "libexec dir",
+};
+
+const char *logFileName = NULL;
+const char *pidFileName = NULL;
+
 void
 ircd_shutdown(const char *reason)
 {
@@ -149,39 +187,6 @@ ircd_shutdown(const char *reason)
        exit(0);
 }
 
-/*
- * print_startup - print startup information
- */
-static void
-print_startup(int pid)
-{
-       int fd;
-
-       close(1);
-       fd = open("/dev/null", O_RDWR);
-       if (fd == -1) {
-               perror("open /dev/null");
-               exit(EXIT_FAILURE);
-       }
-       if (fd == 0)
-               fd = dup(fd);
-       if (fd != 1)
-               abort();
-
-       inotice("now running in %s mode from %s as pid %d ...",
-              !server_state_foreground ? "background" : "foreground",
-               ConfigFileEntry.dpath, pid);
-
-       /* let the parent process know the initialization was successful
-        * -- jilles */
-       if (!server_state_foreground)
-               write(0, ".", 1);
-       if (dup2(1, 0) == -1)
-               abort();
-       if (dup2(1, 2) == -1)
-               abort();
-}
-
 /*
  * init_sys
  *
@@ -192,7 +197,6 @@ print_startup(int pid)
 static void
 init_sys(void)
 {
-#if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
        struct rlimit limit;
 
        if(!getrlimit(RLIMIT_NOFILE, &limit))
@@ -201,29 +205,29 @@ init_sys(void)
                if(maxconnections <= MAX_BUFFER)
                {
                        fprintf(stderr, "ERROR: Shell FD limits are too low.\n");
-                       fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
+                       fprintf(stderr, "ERROR: solanum reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
                        exit(EXIT_FAILURE);
                }
                return;
        }
-#endif /* RLIMIT_FD_MAX */
        maxconnections = MAXCONNECTIONS;
 }
 
 static int
 make_daemon(void)
 {
-       int pid;
-       int pip[2];
-       char c;
+       int pid, nullfd, fdx;
 
-       if (pipe(pip) < 0)
+       /* The below is approximately what daemon(1, 0) does, but
+          we need control over the parent after forking to print
+          the startup message -- Aaron */
+
+       if((nullfd = open("/dev/null", O_RDWR)) < 0)
        {
-               perror("pipe");
+               perror("open /dev/null");
                exit(EXIT_FAILURE);
        }
-       dup2(pip[1], 0);
-       close(pip[1]);
+
        if((pid = fork()) < 0)
        {
                perror("fork");
@@ -231,22 +235,20 @@ make_daemon(void)
        }
        else if(pid > 0)
        {
-               close(0);
-               /* Wait for initialization to finish, successfully or
-                * unsuccessfully. Until this point the child may still
-                * write to stdout/stderr.
-                * -- jilles */
-               if (read(pip[0], &c, 1) > 0)
-                       exit(EXIT_SUCCESS);
-               else
-                       exit(EXIT_FAILURE);
+               inotice("now running in background mode from %s as pid %d ...",
+                       ConfigFileEntry.dpath, pid);
+
+               exit(EXIT_SUCCESS);
        }
 
-       close(pip[0]);
-       setsid();
-/*     fclose(stdin);
-       fclose(stdout);
-       fclose(stderr); */
+       for(fdx = 0; fdx <= 2; fdx++)
+               if (fdx != nullfd)
+                       (void) dup2(nullfd, fdx);
+
+       if(nullfd > 2)
+               (void) close(nullfd);
+
+       (void) setsid();
 
        return 0;
 }
@@ -278,22 +280,22 @@ check_rehash(void *unused)
         */
        if(dorehash)
        {
-               rehash(1);
-               dorehash = 0;
+               rehash(true);
+               dorehash = false;
        }
 
        if(dorehashbans)
        {
-               rehash_bans(1);
-               dorehashbans = 0;
+               rehash_bans();
+               dorehashbans = false;
        }
 
        if(doremotd)
        {
-               sendto_realops_snomask(SNO_GENERAL, L_ALL,
+               sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
                                     "Got signal SIGUSR1, reloading ircd motd file");
                cache_user_motd();
-               doremotd = 0;
+               doremotd = false;
        }
 }
 
@@ -347,19 +349,6 @@ initialize_global_set_options(void)
 
 }
 
-/*
- * initialize_server_capabs
- *
- * inputs       - none
- * output       - none
- */
-static void
-initialize_server_capabs(void)
-{
-       default_server_capabs &= ~CAP_ZIP;
-}
-
-
 /*
  * write_pidfile
  *
@@ -376,7 +365,7 @@ write_pidfile(const char *filename)
        {
                unsigned int pid = (unsigned int) getpid();
 
-               rb_snprintf(buff, sizeof(buff), "%u\n", pid);
+               snprintf(buff, sizeof(buff), "%u\n", pid);
                if((fputs(buff, fb) == -1))
                {
                        ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
@@ -413,7 +402,7 @@ check_pidfile(const char *filename)
                if(fgets(buff, 20, fb) != NULL)
                {
                        pidfromfile = atoi(buff);
-                       if(!kill(pidfromfile, 0))
+                       if(!rb_kill(pidfromfile, 0))
                        {
                                printf("ircd: daemon is already running\n");
                                exit(-1);
@@ -434,7 +423,6 @@ check_pidfile(const char *filename)
 static void
 setup_corefile(void)
 {
-#ifdef HAVE_SYS_RESOURCE_H
        struct rlimit rlim;     /* resource limits */
 
        /* Set corefilesize to maximum */
@@ -443,26 +431,25 @@ setup_corefile(void)
                rlim.rlim_cur = rlim.rlim_max;
                setrlimit(RLIMIT_CORE, &rlim);
        }
-#endif
 }
 
 static void
 ircd_log_cb(const char *str)
 {
-       ilog(L_MAIN, "libratbox reports: %s", str);
+       ilog(L_MAIN, "librb reports: %s", str);
 }
 
 static void
 ircd_restart_cb(const char *str)
 {
-       inotice("libratbox has called the restart callback: %s", str);
+       inotice("librb has called the restart callback: %s", str);
        restart(str);
 }
 
 /*
  * Why EXIT_FAILURE here?
  * Because if ircd_die_cb() is called it's because of a fatal
- * error inside libcharybdis, and we don't know how to handle the
+ * error inside libsolanum, and we don't know how to handle the
  * exception, so it is logical to return a FAILURE exit code here.
  *    --nenolod
  */
@@ -472,11 +459,11 @@ ircd_die_cb(const char *str)
        if(str != NULL)
        {
                /* Try to get the message out to currently logged in operators. */
-               sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "libratbox has called the die callback..aborting: %s", str);
-               inotice("libratbox has called the die callback..aborting: %s", str);
+               sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str);
+               inotice("librb has called the die callback..aborting: %s", str);
        }
        else
-               inotice("libratbox has called the die callback..aborting");
+               inotice("librb has called the die callback..aborting");
 
        unlink(pidFileName);
        exit(EXIT_FAILURE);
@@ -536,7 +523,7 @@ seed_random(void *unused)
  * Side Effects - this is where the ircd gets going right now
  */
 int
-charybdis_main(int argc, char *argv[])
+solanum_main(int argc, char * const argv[])
 {
        int fd;
 
@@ -547,12 +534,15 @@ charybdis_main(int argc, char *argv[])
                return -1;
        }
 
-       init_sys();
+       logFileName = ircd_paths[IRCD_PATH_IRCD_LOG];
+       pidFileName = ircd_paths[IRCD_PATH_IRCD_PID];
 
-       ConfigFileEntry.dpath = DPATH;
-       ConfigFileEntry.configfile = CPATH;     /* Server configuration file */
+       ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX];
+       ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF];   /* Server configuration file */
        ConfigFileEntry.connect_timeout = 30;   /* Default to 30 */
 
+       init_sys();
+
        umask(077);             /* better safe than sorry --SRB */
 
        myargv = argv;
@@ -571,11 +561,15 @@ charybdis_main(int argc, char *argv[])
         */
        setup_corefile();
 
+       /* initialise operhash fairly early. */
+       init_operhash();
+
        memset(&me, 0, sizeof(me));
        memset(&meLocalUser, 0, sizeof(meLocalUser));
        me.localClient = &meLocalUser;
 
        /* Make sure all lists are zeroed */
+       memset(&global_client_list, 0, sizeof(global_client_list));
        memset(&unknown_list, 0, sizeof(unknown_list));
        memset(&lclient_list, 0, sizeof(lclient_list));
        memset(&serv_list, 0, sizeof(serv_list));
@@ -603,7 +597,7 @@ charybdis_main(int argc, char *argv[])
        setup_signals();
 
        if (testing_conf)
-               server_state_foreground = 1;
+               server_state_foreground = true;
 
        /* Make sure fd 0, 1 and 2 are in use -- jilles */
        do
@@ -620,10 +614,11 @@ charybdis_main(int argc, char *argv[])
        {
                check_pidfile(pidFileName);
 
-               if(!server_state_foreground)
-                       make_daemon();
                inotice("starting %s ...", ircd_version);
                inotice("%s", rb_lib_version());
+
+               if(!server_state_foreground)
+                       make_daemon();
        }
 
        /* Init the event subsystem */
@@ -649,35 +644,34 @@ charybdis_main(int argc, char *argv[])
        init_hook();
        init_channels();
        initclass();
-       initwhowas();
+       whowas_init();
        init_reject();
        init_cache();
        init_monitor();
 
         construct_cflags_strings();
 
-       load_all_modules(1);
-       load_core_modules(1);
+       init_authd();           /* Start up authd. */
+       init_dns();             /* Start up DNS query system */
+       init_modules();         /* Start up modules system */
 
-       init_auth();            /* Initialise the auth code */
-       init_resolver();        /* Needs to be setup before the io loop */
        privilegeset_set_new("default", "", 0);
 
        if (testing_conf)
                fprintf(stderr, "\nBeginning config test\n");
-       read_conf_files(YES);   /* cold start init conf files */
+       read_conf_files(true);  /* cold start init conf files */
 
-       mod_add_path(MODULE_DIR);
-       mod_add_path(MODULE_DIR "/autoload");
+       load_all_modules(1);
+       load_core_modules(1);
 
        init_isupport();
 
        init_bandb();
        init_ssld();
+       init_wsockd();
 
-       rehash_bans(0);
+       rehash_bans();
 
-       initialize_server_capabs();     /* Set up default_server_capabs */
        initialize_global_set_options();
 
        if(ServerInfo.name == NULL)
@@ -692,7 +686,7 @@ charybdis_main(int argc, char *argv[])
                ierror("no server sid specified in serverinfo block.");
                return -2;
        }
-       strcpy(me.id, ServerInfo.sid);
+       rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
        init_uid();
 
        /* serverinfo{} description must exist.  If not, error out. */
@@ -703,23 +697,16 @@ charybdis_main(int argc, char *argv[])
        }
        rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
 
-       if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL)
+       if(ServerInfo.ssl_cert != NULL)
        {
                /* just do the rb_setup_ssl_server to validate the config */
                if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list))
                {
                        ilog(L_MAIN, "WARNING: Unable to setup SSL.");
-                       ssl_ok = 0;
+                       ircd_ssl_ok = false;
                }
                else
-                       ssl_ok = 1;
-       }
-
-       if (testing_conf)
-       {
-               fprintf(stderr, "\nConfig testing complete.\n");
-               fflush(stderr);
-               return 0;       /* Why? We want the launcher to exit out. */
+                       ircd_ssl_ok = true;
        }
 
        me.from = &me;
@@ -735,11 +722,20 @@ charybdis_main(int argc, char *argv[])
 
        construct_umodebuf();
 
+       if (testing_conf)
+       {
+               fprintf(stderr, "\nConfig testing complete.\n");
+               fflush(stderr);
+               return 0;       /* Why? We want the launcher to exit out. */
+       }
+
        check_class();
        write_pidfile(pidFileName);
        load_help();
        open_logfiles();
 
+       configure_authd();
+
        ilog(L_MAIN, "Server Ready");
 
        /* We want try_connections to be called as soon as possible now! -- adrian */
@@ -755,7 +751,9 @@ charybdis_main(int argc, char *argv[])
        if(splitmode)
                check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
 
-       print_startup(getpid());
+       if(server_state_foreground)
+               inotice("now running in foreground mode from %s as pid %ld ...",
+                       ConfigFileEntry.dpath, (long)getpid());
 
        rb_lib_loop(0);