From caef5b34f9e94a0375d3a8e8b5fb4040b3a0406a Mon Sep 17 00:00:00 2001 From: onefang Date: Tue, 17 Mar 2020 20:52:32 +1000 Subject: Switch to using FHS style directories, and create them all at startup. --- src/.sledjChisl.conf.lua | 4 +- src/sledjchisl/sledjchisl.c | 127 +++++++++++++++++++++++++++++++------------- 2 files changed, 91 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/.sledjChisl.conf.lua b/src/.sledjChisl.conf.lua index bc04835..f7289de 100644 --- a/src/.sledjChisl.conf.lua +++ b/src/.sledjChisl.conf.lua @@ -12,11 +12,11 @@ config = ["scRoot"] = "/opt/opensim_SC"; ["scUser"] = "opensimsc"; ["Tconsole"] = "SledjChisl"; - ["Tsocket"] = "caches/opensim-tmux.socket"; + ["Tsocket"] = "opensim-tmux.socket"; ["Ttab"] = "SC"; ["loadAverageInc"] = 0.7; ["simTimeOut"] = 45; -- seconds - ["webRoot"] = "/var/www"; + ["webRoot"] = "/var/www/html"; ["URL"] = "sledjchisl.fcgi"; ["seshTimeOut"] = 30 * 60; -- seconds ["idleTimeOut"] = 24 * 60 * 60; -- seconds diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index 9808245..60102f3 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -20,24 +20,6 @@ config SLEDJCHISL // TODO - do all the setup on first run, and check if needed on each start up, to be self healing. -// TODO - create any missing directories when we need them. - -// TODO - use a FHS compatible setup - -// scRoot = / /usr/local /opt/opensim_SC -// /etc/opensim_SC /usr/local/etc/opensim_SC /opt/opensim_SC/etc -// /run/opensim_SC /run/opensim_SC /run/opensim_SC -// /usr/bin /usr/local/bin /opt/opensim_SC/bin -// The problem here is that OpenSim already uses opensim/bin for all of it's crap, where they mix it all together. -// Don't want it in the path. -// Should put the .dlls into ../lib.. -// Most of the rest is config files or common assets. -// /usr/lib/opensim_SC /usr/local/lib/opensim_SC /opt/opensim_SC/lib -// /var/backups/opensim_SC /var/local/backups/opensim_SC /opt/opensim_SC/var/backups -// /var/cache/opensim_SC /var/local/cache/opensim_SC /opt/opensim_SC/var/cache -// /var/lib/opensim_SC /var/local/lib/opensim_SC /opt/opensim_SC/var/lib -// /var/log/opensim_SC /var/local/log/opensim_SC /opt/opensim_SC/var/log -// /var/www/opensim_SC /var/local/www/opensim_SC /opt/opensim_SC/var/www - // TODO - pepper could be entered on the console on startup if it's not defined, as a master password sort of thing. // I'd go as far as protecting the database credentials that way, but legacy OpenSim needs it unprotected. // Also keep in mind, people want autostart of their services without having to enter passwords on each boot. @@ -456,11 +438,19 @@ boolean isWeb = 0; char *pwd = ""; char *scRoot = "/opt/opensim_SC"; char *scUser = "opensimsc"; +char *scBin = ""; +char *scEtc = ""; +char *scLib = ""; +char *scRun = ""; +char *scBackup = ""; +char *scCache = ""; +char *scData = ""; +char *scLog = ""; char *Tconsole = "SledjChisl"; -char *Tsocket = "caches/opensim-tmux.socket"; +char *Tsocket = "opensim-tmux.socket"; char *Ttab = "SC"; char *Tcmd = "tmux -S"; -char *webRoot = "/opt/opensim_SC/web"; +char *webRoot = "/var/www/html"; char *URL = "fcgi-bin/sledjchisl.fcgi"; char *ToS = "Be good."; int seshTimeOut = 30 * 60; @@ -592,7 +582,7 @@ void PrintTable(lua_State *L) int sendTmuxKeys(char *dest, char *keys) { int ret = 0, i; - char *c = xmprintf("%s %s/%s send-keys -t %s:%s '%s'", Tcmd, scRoot, Tsocket, Tconsole, dest, keys); + char *c = xmprintf("%s %s/%s send-keys -t %s:%s '%s'", Tcmd, scRun, Tsocket, Tconsole, dest, keys); i = system(c); if (!WIFEXITED(i)) @@ -604,7 +594,7 @@ int sendTmuxKeys(char *dest, char *keys) int sendTmuxCmd(char *dest, char *cmd) { int ret = 0, i; - char *c = xmprintf("%s %s/%s send-keys -t %s:'%s' '%s' Enter", Tcmd, scRoot, Tsocket, Tconsole, dest, cmd); + char *c = xmprintf("%s %s/%s send-keys -t %s:'%s' '%s' Enter", Tcmd, scRun, Tsocket, Tconsole, dest, cmd); i = system(c); if (!WIFEXITED(i)) @@ -616,7 +606,7 @@ int sendTmuxCmd(char *dest, char *cmd) void waitTmuxText(char *dest, char *text) { int i; - char *c = xmprintf("sleep 5; %s %s/%s capture-pane -t %s:'%s' -p | grep -E '%s' 2>&1 > /dev/null", Tcmd, scRoot, Tsocket, Tconsole, dest, text); + char *c = xmprintf("sleep 5; %s %s/%s capture-pane -t %s:'%s' -p | grep -E '%s' 2>&1 > /dev/null", Tcmd, scRun, Tsocket, Tconsole, dest, text); D("Waiting for '%s'.", text); do @@ -2725,10 +2715,10 @@ static void freeSesh(reqData *Rd, boolean linky, boolean wipe) if (linky) { shs = Rd->lnk; - file = xmprintf("%s/caches/sessions/%s.linky", getStrH(Rd->configs, "scRoot"), shs->leaf); + file = xmprintf("%s/sessions/%s.linky", scCache, shs->leaf); } else - file = xmprintf("%s/caches/sessions/%s.lua", getStrH(Rd->configs, "scRoot"), shs->leaf); + file = xmprintf("%s/sessions/%s.lua", scCache, shs->leaf); if (wipe) I("Wiping session %s.", file); @@ -2784,11 +2774,11 @@ static void setToken_n_munchie(reqData *Rd, boolean linky) if (linky) { shs = Rd->lnk; - file = xmprintf("%s/caches/sessions/%s.linky", getStrH(Rd->configs, "scRoot"), shs->leaf); + file = xmprintf("%s/sessions/%s.linky", scCache, shs->leaf); } else { - file = xmprintf("%s/caches/sessions/%s.lua", getStrH(Rd->configs, "scRoot"), shs->leaf); + file = xmprintf("%s/sessions/%s.lua", scCache, shs->leaf); if (NULL != Rd->lnk) link = Rd->lnk->hashish; } @@ -2844,7 +2834,7 @@ static void setToken_n_munchie(reqData *Rd, boolean linky) static void createUser(reqData *Rd) { - char *file = xmprintf("%s/var/lib/users/%s.lua", getStrH(Rd->configs, "scRoot"), getStrH(Rd->stuff, "UUID")); + char *file = xmprintf("%s/users/%s.lua", scData, getStrH(Rd->stuff, "UUID")); char *tnm = xmprintf( "user = \n" "{\n" " ['name']='%s',\n" @@ -2898,7 +2888,7 @@ static void createUser(reqData *Rd) else { char *name = Rd->stuff->getstr(Rd->stuff, "name", true); - char *nm = xmprintf("%s/var/lib/users/%s.lua", getStrH(Rd->configs, "scRoot"), qstrreplace("tr", name, " ", "_")); + char *nm = xmprintf("%s/users/%s.lua", scData, qstrreplace("tr", name, " ", "_")); free(file); file = xmprintf("%s.lua", getStrH(Rd->stuff, "UUID")); @@ -3180,9 +3170,9 @@ static int validateSesh(reqData *Rd, qhashtbl_t *data) leaf = myHMACkey(getStrH(Rd->configs, "pepper"), hashish, TRUE); //D("leaf %s", leaf); if (linky) - t0 = xmprintf("%s/caches/sessions/%s.linky", getStrH(Rd->configs, "scRoot"), leaf); + t0 = xmprintf("%s/sessions/%s.linky", scCache, leaf); else - t0 = xmprintf("%s/caches/sessions/%s.lua", getStrH(Rd->configs, "scRoot"), leaf); + t0 = xmprintf("%s/sessions/%s.lua", scCache, leaf); qhashtbl_t *tnm = qhashtbl(0, 0); ret = LuaToHash(Rd, t0, "toke_n_munchie", tnm, ret, &st, &now, "session"); @@ -3516,7 +3506,7 @@ static int validateName(reqData *Rd, qhashtbl_t *data) int rt = 0; if (s) {s--; *s = '_'; s++;} - where = xmprintf("%s/var/lib/users/%s.lua", getStrH(Rd->configs, "scRoot"), name); + where = xmprintf("%s/users/%s.lua", scData, name); rt = LuaToHash(Rd, where, "user", tnm, ret, &st, &now, "user"); if (s) {s--; *s = '\0'; s++;} free(where); @@ -4489,10 +4479,71 @@ jit library is loaded or the JIT compiler will not be activated. if ((tmp = configs->getstr(configs, "ToS", false)) != NULL) {ToS = tmp; D("Setting ToS = %s", ToS);} + // Use a FHS compatible setup - + if (strcmp("/", scRoot) == 0) + { + scBin = "/bin"; + scEtc = "/etc/opensim_SC"; + scLib = "/usr/lib/opensim_SC"; + scRun = "/run/opensim_SC"; + scBackup = "/var/backups/opensim_SC"; + scCache = "/var/cache/opensim_SC"; + scData = "/var/lib/opensim_SC"; + scLog = "/var/log/opensim_SC"; + } + else if (strcmp("/usr/local", scRoot) == 0) + { + scBin = "/usr/local/bin"; + scEtc = "/usr/local/etc/opensim_SC"; + scLib = "/usr/local/lib/opensim_SC"; + scRun = "/run/opensim_SC"; + scBackup = "/var/local/backups/opensim_SC"; + scCache = "/var/local/cache/opensim_SC"; + scData = "/var/local/lib/opensim_SC"; + scLog = "/var/local/log/opensim_SC"; + } + else // A place for everything to live, like /opt/opensim_SC + { + char *slsh = ""; + + if ('/' != scRoot[0]) + { + E("scRoot is not an absolute path - %s.", scRoot); + slsh = "/"; + } + // The problem here is that OpenSim already uses /opt/opensim_SC/current/bin for all of it's crap, where they mix everything together. + // Don't want it in the path. + // Could put the .dlls into lib. Most of the rest is config files or common assets. + // Or just slowly migrate opensim stuff to FHS. + scBin = xmprintf("%s%s/bin", slsh, scRoot); + scEtc = xmprintf("%s%s/etc", slsh, scRoot); + scLib = xmprintf("%s%s/lib", slsh, scRoot); + scRun = xmprintf("%s%s/var/run", slsh, scRoot); + scBackup = xmprintf("%s%s/var/backups", slsh, scRoot); + scCache = xmprintf("%s%s/var/cache", slsh, scRoot); + scData = xmprintf("%s%s/var/lib", slsh, scRoot); + scLog = xmprintf("%s%s/var/log", slsh, scRoot); + } + if (isTmux || isWeb) { char *d; + if ((! qfile_exist(scBin)) && (! qfile_mkdir(scBin, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scBin); + if ((! qfile_exist(scEtc)) && (! qfile_mkdir(scEtc, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scEtc); + if ((! qfile_exist(scLib)) && (! qfile_mkdir(scLib, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scLib); + if ((! qfile_exist(scRun)) && (! qfile_mkdir(scRun, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP, true))) C("Unable to create path %s", scRun); + if ((! qfile_exist(scBackup)) && (! qfile_mkdir(scBackup, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scBackup); + if ((! qfile_exist(scCache)) && (! qfile_mkdir(scCache, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scCache); + if ((! qfile_exist(scData)) && (! qfile_mkdir(scData, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scData); + if ((! qfile_exist(scLog)) && (! qfile_mkdir(scLog, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", scLog); + tmp = xmprintf("%s/sessions", scCache); + if ((! qfile_exist(tmp)) && (! qfile_mkdir(tmp, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", tmp); + tmp = xmprintf("%s/users", scData); + if ((! qfile_exist(tmp)) && (! qfile_mkdir(tmp, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, true))) C("Unable to create path %s", tmp); + free(tmp); + + mimeTypes = qhashtbl(0, 0); mimeTypes->putstr(mimeTypes, "gz", "application/gzip"); mimeTypes->putstr(mimeTypes, "js", "application/javascript"); @@ -4746,7 +4797,7 @@ T("BODY"); I("Started SledjChisl FCGI web %s request ROLE = %s, body is %s bytes, pid %d.", Rd->Method, Role, Length, getpid()); - I(" %s://%s%s -> %s/html%s", Rd->Scheme, Rd->Host, Rd->RUri, webRoot, Path); + I(" %s://%s%s -> %s%s", Rd->Scheme, Rd->Host, Rd->RUri, webRoot, Path); /* TODO - other headers may include - @@ -4780,7 +4831,7 @@ T("BODY"); } memset(toybuf, 0, sizeof(toybuf)); - snprintf(toybuf, sizeof(toybuf), "%s/html%s", webRoot, Path); + snprintf(toybuf, sizeof(toybuf), "%s%s", webRoot, Path); HTMLfile *thisFile = checkHTMLcache(toybuf); if (NULL == thisFile) { @@ -4935,7 +4986,7 @@ fcgiDone: if (!isTmux) { // Let's see if the proper tmux server is even running. memset(toybuf, 0, sizeof(toybuf)); - snprintf(toybuf, sizeof(toybuf), "%s %s/%s -q list-sessions 2>/dev/null | grep -q %s:", Tcmd, scRoot, Tsocket, Tconsole); + snprintf(toybuf, sizeof(toybuf), "%s %s/%s -q list-sessions 2>/dev/null | grep -q %s:", Tcmd, scRun, Tsocket, Tconsole); i = system(toybuf); if (WIFEXITED(i)) { @@ -4946,14 +4997,14 @@ fcgiDone: // After the session is created, we rely on the caches directory to be group sticky, so that anyone in the opensim group can attach to the tmux socket. snprintf(toybuf, sizeof(toybuf), "sudo -Hu %s %s %s/%s new-session -d -s %s -n '%s' \\; split-window -bhp 50 -t '%s:' bash -c './sledjchisl; cd %s; bash'", - scUser, Tcmd, scRoot, Tsocket, Tconsole, Ttab, Tconsole, scRoot); + scUser, Tcmd, scRun, Tsocket, Tconsole, Ttab, Tconsole, scRoot); i = system(toybuf); if (!WIFEXITED(i)) E("tmux new-session command failed!"); } // Join the session. memset(toybuf, 0, sizeof(toybuf)); - snprintf(toybuf, sizeof(toybuf), "%s %s/%s select-window -t '%s' \\; attach-session -t '%s'", Tcmd, scRoot, Tsocket, Tconsole, Tconsole); + snprintf(toybuf, sizeof(toybuf), "%s %s/%s select-window -t '%s' \\; attach-session -t '%s'", Tcmd, scRun, Tsocket, Tconsole, Tconsole); i = system(toybuf); if (!WIFEXITED(i)) E("tmux attach-session command failed!"); @@ -5001,7 +5052,7 @@ fcgiDone: I("%s is starting up.", name); memset(toybuf, 0, sizeof(toybuf)); snprintf(toybuf, sizeof(toybuf), "%s %s/%s new-window -dn '%s' -t '%s:%d' 'cd %s/current/bin; mono OpenSim.exe -inidirectory=%s/config/%s'", - Tcmd, scRoot, Tsocket, name, Tconsole, i + 1, scRoot, scRoot, sim); + Tcmd, scRun, Tsocket, name, Tconsole, i + 1, scRoot, scRoot, sim); int r = system(toybuf); if (!WIFEXITED(r)) E("tmux new-window command failed!"); -- cgit v1.1