From d339481d070b075ac0b512995fc6d173a7d88c70 Mon Sep 17 00:00:00 2001 From: onefang Date: Wed, 18 Aug 2021 23:25:50 +1000 Subject: Factor out most of the system() calls. --- src/sledjchisl/sledjchisl.c | 141 ++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 64 deletions(-) diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index 3d94035..91ecb77 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -541,6 +541,53 @@ qhashtbl_t *mimeTypes; qlist_t *dbRequests; +int shellMeFail(char *format, ...) +{ + va_list va, va2; + int len; + char *ret; + int result; + + va_start(va, format); + va_copy(va2, va); + // How long is it? + len = vsnprintf(0, 0, format, va); + len++; + va_end(va); + // Allocate and do the sprintf() + ret = xmalloc(len); + vsnprintf(ret, len, format, va2); + va_end(va2); + + result = !WIFEXITED(system(ret)); + free(ret); + return result; +} + +int shellMe(char *format, ...) +{ + va_list va, va2; + int len; + char *ret; + int result; + + va_start(va, format); + va_copy(va2, va); + // How long is it? + len = vsnprintf(0, 0, format, va); + len++; + va_end(va); + // Allocate and do the sprintf() + ret = xmalloc(len); + vsnprintf(ret, len, format, va2); + va_end(va2); + + result = system(ret); + free(ret); + return result; +} + + // TODO - log to file. The problem is we don't know where to log until after we have loaded the configs, and before that we are spewing log messages. // Now that we are using spawn-fcgi, all the logs are going to STDERR, which we can capture and write to a file. // Unfortunately spawn-fcgi in deamon mode sends all the output to /dev/null or something. @@ -1037,11 +1084,9 @@ void doTmuxCmd(char *format, ...) vsnprintf(ret, len, format, va2); va_end(va2); - char *c = xmprintf("%s %s/%s %s", Tcmd, scRun, Tsocket, ret); + if (shellMeFail("%s %s/%s %s", Tcmd, scRun, Tsocket, ret)) + E("tmux command failed! %s", ret); free(ret); - if (!WIFEXITED(system(c))) - E("tmux command failed! %s", c); - free(c); } void sendTmuxCmd(char *dest, char *cmd) @@ -1421,29 +1466,25 @@ simList *getSims() V("Converting %s.ini -> %s", path, newPath); D(cmd); - if (!WIFEXITED(system(cmd))) + if (shellMeFail(cmd)) E("sed command failed!"); else { - free(cmd); - cmd = xmprintf("chmod ugo+x %s", newPath); - if (!WIFEXITED(system(cmd))) + if (shellMeFail("chmod ugo+x %s", newPath)) E("chmod command failed!"); - free(cmd); char *link = xmprintf("%s/%s.shini", scBin, name); I("Symlinking %s to %s", newPath, link); if (qfile_exist(link)) { - cmd = xmprintf("rm %s", link); - if (!WIFEXITED(system(cmd))) + if (shellMeFail("rm %s", link)) E("rm command failed!"); - free(cmd); } if (0 != symlink(newPath, link)) perror_msg("Symlinking %s to %s", newPath, link); free(link); } + free(cmd); } free(newPath); free(path); @@ -1483,11 +1524,10 @@ simList *getSims() // Write the sims.lua_NEW file if needed. if (ourSims->doIt) { - char *tnm = xmprintf("mv -f %s/sims.lua %s/sims.lua.BACKUP", scEtc, scEtc); + char *tnm; - if (!WIFEXITED(system(tnm))) + if (shellMeFail("mv -f %s/sims.lua %s/sims.lua.BACKUP", scEtc, scEtc)) E("mv command failed!"); - free(tnm); free(file); file = xmprintf("%s/sims.lua", scEtc); @@ -1735,11 +1775,9 @@ void prepSims(simData *simd, char *sim, char *type, int count, int window, int p } // Create the temporary .ini files. - snprintf(toybuf, sizeof(toybuf), "echo 'IDs={' >%s/IDs_%d.lua", scTemp, window); - system(toybuf); + shellMe("echo 'IDs={' >%s/IDs_%d.lua", scTemp, window); doTmuxCmd("list-panes -t %d -F \"['%d #{pane_index}'] = '#{window_id}.#{pane_id}',\" >> %s/IDs_%d.lua", window, window, scTemp, window); - snprintf(toybuf, sizeof(toybuf), "echo '}\nreturn IDs' >>%s/IDs_%d.lua", scTemp, window); - system(toybuf); + shellMe("echo '}\nreturn IDs' >>%s/IDs_%d.lua", scTemp, window); findWindow(simd, sim, type, count, window, panes, pane); simd->portH = 8004 + count * 2; @@ -1767,7 +1805,7 @@ void prepSims(simData *simd, char *sim, char *type, int count, int window, int p simd->portH, path, newPath); D("Writing .ini file %s with ports %d %d", newPath, simd->portH, simd->portU); - if (!WIFEXITED(system(cmd))) + if (shellMeFail(cmd)) E("sed command failed!"); free(cmd); free(newPath); @@ -1819,13 +1857,12 @@ void stopSim(simData *simd, char *sim, char *type, int count, int window, int pa V("Attempting to shut down sim %s.", simd->tab); sendTmuxCmd(simd->paneID, "show users"); sleep(1); - char *cmd = xmprintf("%s %s/%s capture-pane -Jt '%s' -p | grep -E '^Agents connected: [[:digit:]]*[[:space:]]*$' | tail -n 1 | cut -d ' ' -f 3 >%s/STOP_%s", - Tcmd, scRun, Tsocket, simd->paneID, scTemp, simd->tab); - int i = system(cmd); - if (WIFEXITED(i)) + if (shellMeFail("%s %s/%s capture-pane -Jt '%s' -p | grep -E '^Agents connected: [[:digit:]]*[[:space:]]*$' | tail -n 1 | cut -d ' ' -f 3 >%s/STOP_%s", + Tcmd, scRun, Tsocket, simd->paneID, scTemp, simd->tab)) + E("Problem with shutting down sim %s - Failed find 'Agents connected'", simd->tab); + else { - free(cmd); - cmd = xmprintf("%s/STOP_%s", scTemp, simd->tab); + char *cmd = xmprintf("%s/STOP_%s", scTemp, simd->tab); char *r = (char *) qfile_load(cmd, NULL); if (r) { @@ -1845,10 +1882,8 @@ void stopSim(simData *simd, char *sim, char *type, int count, int window, int pa } else E("Problem with shutting down sim %s - Failed to read file %s", simd->tab, cmd); + free(cmd); } - else - E("Problem with shutting down sim %s - Failed to run command \n %s", simd->tab, cmd); - free(cmd); // There's three things that might happen - // Sim will quit, tmux pane will go away before we can see the shutdown message. @@ -4814,11 +4849,8 @@ t("Write shs %s", tnm4); } xclose(fd); I("Sending linky email to %s %s", getStrH(Rd->stuff, "email"), t1); - command = xmprintf("sendmail -oi -t <'%s'", file); - int i = system(command); - if (!WIFEXITED(i)) + if (shellMeFail("sendmail -oi -t <'%s'", file)) E("sendmail command failed!"); - free(command); free(file); free(content); free(t1); @@ -7451,14 +7483,12 @@ void forEachMember(char *verb, simFunction func, simFunction not) if (BACKUP == currentMode) { char *file = xmprintf("%s/.lastTime", scBackup); - char *cmd = xmprintf("date +%%s > %s", file); last = (char *) qfile_load(file, NULL); if (NULL == last) last = xstrdup("0"); - if (!WIFEXITED(system(cmd))) + if (shellMeFail("date +%%s > %s", file)) E("date command failed!"); - free(cmd); free(file); } @@ -7529,11 +7559,9 @@ static void cleanup(void) { V("Deleting web socket."); free(cmd); - cmd = xmprintf("rm %s/sledjchisl.socket", scRun); - if (!WIFEXITED(system(cmd))) + if (shellMeFail("rm %s/sledjchisl.socket", scRun)) E("rm command failed!"); } - free(cmd); if (accountPages) { @@ -7601,9 +7629,7 @@ static void cleanup(void) cmd = xmprintf("%s/%s", scRun, Tsocket); if (qfile_exist(cmd)) { - free(cmd); - cmd = xmprintf("rm %s/%s", scRun, Tsocket); - if (!WIFEXITED(system(cmd))) + if (shellMeFail("rm %s/%s", scRun, Tsocket)) E("rm command failed!"); } free(cmd); @@ -7886,9 +7912,7 @@ Other possibilities - // TODO - get rid of RunIt.sh as well. 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, scRun, Tsocket, Tconsole); - i = system(toybuf); + i = shellMe("%s %s/%s -q list-sessions 2>/dev/null | grep -q %s:", Tcmd, scRun, Tsocket, Tconsole); if (WIFEXITED(i)) { if (0 != WEXITSTATUS(i)) // No such session, create it. @@ -7918,14 +7942,11 @@ Other possibilities - "split-window -vp 50 -t '%s:' bash -c 'export PATH=%s:$PATH; /usr/bin/valgrind --leak-check=full ./sledjchisl; cd %s/current/bin; bash'", scUser, Tcmd, scRun, Tsocket, scBin, Tconsole, Ttab, Tconsole, scBin, scRoot); } - i = system(toybuf); - if (!WIFEXITED(i)) + if (shellMeFail(toybuf)) E("tmux new-session command failed! %s", toybuf); else { - snprintf(toybuf, sizeof(toybuf), "%s %s/%s select-pane -t 1 -T 'SledjChisl'", Tcmd, scRun, Tsocket); - i = system(toybuf); - if (!WIFEXITED(i)) + if (shellMeFail("%s %s/%s select-pane -t 1 -T 'SledjChisl'", Tcmd, scRun, Tsocket)) E("tmux select-pane command failed!"); } // toybox argument parsing is half working. @@ -7933,10 +7954,7 @@ Other possibilities - { V("Joining the session."); // Join the session. - memset(toybuf, 0, sizeof(toybuf)); - 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)) + if (shellMeFail("%s %s/%s select-window -t '%s' \\; attach-session -t '%s'", Tcmd, scRun, Tsocket, Tconsole, Tconsole)) E("tmux attach-session command failed! %s", toybuf); } else @@ -8011,8 +8029,7 @@ Other possibilities - V("Symlinking %s to %s", newPath, tmp); if (qfile_exist(tmp)) { - snprintf(toybuf, sizeof(toybuf), "rm %s", tmp); - if (!WIFEXITED(system(toybuf))) + if (shellMeFail("rm %s", tmp)) E("rm command failed!"); } if (0 != symlink(newPath, tmp)) @@ -8238,18 +8255,14 @@ Other possibilities - { I("Closing all the other windows."); // First figure out what panes and windows are left. - snprintf(toybuf, sizeof(toybuf), "echo 'IDs={' >%s/IDs_ALL.lua", scTemp); - system(toybuf); + shellMeFail("echo 'IDs={' >%s/IDs_ALL.lua", scTemp); doTmuxCmd("list-panes -s -F '\"#{window_id}.#{pane_id}\",' >> %s/IDs_ALL.lua", scTemp); - snprintf(toybuf, sizeof(toybuf), "echo '}\nreturn IDs' >>%s/IDs_ALL.lua", scTemp); - system(toybuf); + shellMeFail("echo '}\nreturn IDs' >>%s/IDs_ALL.lua", scTemp); snprintf(toybuf, sizeof(toybuf), "%s/IDs_ALL.lua", scTemp); qtreetbl_t *IDs = Lua2tree(toybuf, "IDs"); - char *c = xmprintf("rm -fr %s/*", scTemp); - if (!WIFEXITED(system(c))) - E("rm command failed! %s", c); - free(c); + if (shellMeFail("rm -fr %s/*", scTemp)) + E("rm command failed! %s/*", scTemp); qtreetbl_obj_t obj0; memset((void*)&obj0, 0, sizeof(obj0)); -- cgit v1.1