]> jfr.im git - solanum.git/blobdiff - ircd/ircd.c
getopt: don't modify argv as it breaks restart()
[solanum.git] / ircd / ircd.c
index 4634f141d4017c8b7057fa34ec6533af5afb169b..3a16a40c7690df6ec5ba9d6cfc27a9acf3d09286 100644 (file)
@@ -31,7 +31,6 @@
 #include "channel.h"
 #include "class.h"
 #include "client.h"
-#include "common.h"
 #include "hash.h"
 #include "match.h"
 #include "ircd_signal.h"
@@ -40,7 +39,6 @@
 #include "numeric.h"
 #include "parse.h"
 #include "restart.h"
-#include "s_auth.h"
 #include "s_conf.h"
 #include "logger.h"
 #include "s_serv.h"            /* try_connections */
 #include "patchlevel.h"
 #include "serno.h"
 #include "sslproc.h"
+#include "wsproc.h"
 #include "chmode.h"
 #include "privilege.h"
 #include "bandbi.h"
-#include "authd.h"
+#include "authproc.h"
 #include "operhash.h"
 
 /* /quote set variables */
@@ -95,13 +94,10 @@ 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;
-bool dorehash = false;
-bool dorehashbans = false;
-bool doremotd = false;
+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;
@@ -119,6 +115,28 @@ 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 *logFileName = NULL;
+const char *pidFileName = NULL;
+
 void
 ircd_shutdown(const char *reason)
 {
@@ -168,7 +186,7 @@ print_startup(int pid)
        if (fd != 1)
                abort();
 #endif
-
+       inotice("runtime path: %s", rb_path_to_self());
        inotice("now running in %s mode from %s as pid %d ...",
               !server_state_foreground ? "background" : "foreground",
                ConfigFileEntry.dpath, pid);
@@ -178,7 +196,13 @@ print_startup(int pid)
         * -- jilles */
        if (!server_state_foreground)
        {
-               (void) write(0, ".", 1);
+               /* GCC complains on Linux if we don't check the value of write pedantically.
+                * Technically you're supposed to check the value, yes, but it probably can't fail.
+                * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
+                * --Elizfaox
+                */
+               if(write(0, ".", 1) < 1)
+                       abort();
        }
        if (dup2(1, 0) == -1)
                abort();
@@ -284,13 +308,13 @@ check_rehash(void *unused)
         */
        if(dorehash)
        {
-               rehash(1);
+               rehash(true);
                dorehash = false;
        }
 
        if(dorehashbans)
        {
-               rehash_bans(1);
+               rehash_bans();
                dorehashbans = false;
        }
 
@@ -365,6 +389,85 @@ initialize_server_capabs(void)
        default_server_capabs &= ~CAP_ZIP;
 }
 
+/*
+ * relocate_paths
+ *
+ * inputs       - none
+ * output       - none
+ * side effects - items in ircd_paths[] array are relocated
+ */
+#ifdef _WIN32
+static void
+relocate_paths(void)
+{
+       char prefix[PATH_MAX], workbuf[PATH_MAX];
+       char *p;
+
+       rb_strlcpy(prefix, rb_path_to_self(), sizeof prefix);
+
+       ircd_paths[IRCD_PATH_IRCD_EXEC] = rb_strdup(prefix);
+
+       /* if we're running from inside the source tree, we probably do not want to relocate any other paths */
+       if (strstr(prefix, ".libs") != NULL)
+               return;
+
+       /* prefix = /home/kaniini/ircd/bin/ircd */
+       p = strrchr(prefix, RB_PATH_SEPARATOR);
+       if (rb_unlikely(p == NULL))
+               return;
+       *p = 0;
+
+       /* prefix = /home/kaniini/ircd/bin */
+       p = strrchr(prefix, RB_PATH_SEPARATOR);
+       if (rb_unlikely(p == NULL))
+               return;
+       *p = 0;
+
+       /* prefix = /home/kaniini/ircd */
+       ircd_paths[IRCD_PATH_PREFIX] = rb_strdup(prefix);
+
+       /* now that we have our prefix, we can relocate the other paths... */
+       snprintf(workbuf, sizeof workbuf, "%s%cmodules", prefix, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_MODULES] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cmodules%cautoload", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc", prefix, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_ETC] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%clog", prefix, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_LOG] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%chelp%cusers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%chelp%copers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.conf", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_IRCD_CONF] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc%copers.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc%cban.db", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_BANDB] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.pid", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%clogs%circd.log", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(workbuf);
+
+       snprintf(workbuf, sizeof workbuf, "%s%cbin", prefix, RB_PATH_SEPARATOR);
+       ircd_paths[IRCD_PATH_BIN] = rb_strdup(workbuf);
+       ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(workbuf);
+}
+#endif
 
 /*
  * write_pidfile
@@ -542,7 +645,7 @@ seed_random(void *unused)
  * Side Effects - this is where the ircd gets going right now
  */
 int
-charybdis_main(int argc, char *argv[])
+charybdis_main(int argc, char * const argv[])
 {
        int fd;
 
@@ -555,12 +658,19 @@ charybdis_main(int argc, char *argv[])
        }
 #endif
 
-       init_sys();
+#ifdef _WIN32
+       relocate_paths();
+#endif
+
+       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;
@@ -669,10 +779,6 @@ charybdis_main(int argc, char *argv[])
 
         construct_cflags_strings();
 
-       load_all_modules(1);
-       load_core_modules(1);
-
-       init_auth();            /* Initialise the auth code */
        init_authd();           /* Start up authd. */
        init_dns();             /* Start up DNS query system */
 
@@ -684,13 +790,16 @@ charybdis_main(int argc, char *argv[])
 
        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();
@@ -755,6 +864,8 @@ charybdis_main(int argc, char *argv[])
        load_help();
        open_logfiles();
 
+       configure_authd();
+
        ilog(L_MAIN, "Server Ready");
 
        /* We want try_connections to be called as soon as possible now! -- adrian */