diff options
author | onefang | 2021-08-18 23:25:50 +1000 |
---|---|---|
committer | onefang | 2021-08-18 23:25:50 +1000 |
commit | d339481d070b075ac0b512995fc6d173a7d88c70 (patch) | |
tree | af84331e8811316cecfc18262df40fd9392bfa58 | |
parent | bin/.sledjchisl.lua is alink to the one in src. (diff) | |
download | opensim-SC-d339481d070b075ac0b512995fc6d173a7d88c70.zip opensim-SC-d339481d070b075ac0b512995fc6d173a7d88c70.tar.gz opensim-SC-d339481d070b075ac0b512995fc6d173a7d88c70.tar.bz2 opensim-SC-d339481d070b075ac0b512995fc6d173a7d88c70.tar.xz |
Factor out most of the system() calls.
-rw-r--r-- | src/sledjchisl/sledjchisl.c | 141 |
1 files 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; | |||
541 | qlist_t *dbRequests; | 541 | qlist_t *dbRequests; |
542 | 542 | ||
543 | 543 | ||
544 | int shellMeFail(char *format, ...) | ||
545 | { | ||
546 | va_list va, va2; | ||
547 | int len; | ||
548 | char *ret; | ||
549 | int result; | ||
550 | |||
551 | va_start(va, format); | ||
552 | va_copy(va2, va); | ||
553 | // How long is it? | ||
554 | len = vsnprintf(0, 0, format, va); | ||
555 | len++; | ||
556 | va_end(va); | ||
557 | // Allocate and do the sprintf() | ||
558 | ret = xmalloc(len); | ||
559 | vsnprintf(ret, len, format, va2); | ||
560 | va_end(va2); | ||
561 | |||
562 | result = !WIFEXITED(system(ret)); | ||
563 | free(ret); | ||
564 | return result; | ||
565 | } | ||
566 | |||
567 | int shellMe(char *format, ...) | ||
568 | { | ||
569 | va_list va, va2; | ||
570 | int len; | ||
571 | char *ret; | ||
572 | int result; | ||
573 | |||
574 | va_start(va, format); | ||
575 | va_copy(va2, va); | ||
576 | // How long is it? | ||
577 | len = vsnprintf(0, 0, format, va); | ||
578 | len++; | ||
579 | va_end(va); | ||
580 | // Allocate and do the sprintf() | ||
581 | ret = xmalloc(len); | ||
582 | vsnprintf(ret, len, format, va2); | ||
583 | va_end(va2); | ||
584 | |||
585 | result = system(ret); | ||
586 | free(ret); | ||
587 | return result; | ||
588 | } | ||
589 | |||
590 | |||
544 | // 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. | 591 | // 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. |
545 | // Now that we are using spawn-fcgi, all the logs are going to STDERR, which we can capture and write to a file. | 592 | // Now that we are using spawn-fcgi, all the logs are going to STDERR, which we can capture and write to a file. |
546 | // Unfortunately spawn-fcgi in deamon mode sends all the output to /dev/null or something. | 593 | // Unfortunately spawn-fcgi in deamon mode sends all the output to /dev/null or something. |
@@ -1037,11 +1084,9 @@ void doTmuxCmd(char *format, ...) | |||
1037 | vsnprintf(ret, len, format, va2); | 1084 | vsnprintf(ret, len, format, va2); |
1038 | va_end(va2); | 1085 | va_end(va2); |
1039 | 1086 | ||
1040 | char *c = xmprintf("%s %s/%s %s", Tcmd, scRun, Tsocket, ret); | 1087 | if (shellMeFail("%s %s/%s %s", Tcmd, scRun, Tsocket, ret)) |
1088 | E("tmux command failed! %s", ret); | ||
1041 | free(ret); | 1089 | free(ret); |
1042 | if (!WIFEXITED(system(c))) | ||
1043 | E("tmux command failed! %s", c); | ||
1044 | free(c); | ||
1045 | } | 1090 | } |
1046 | 1091 | ||
1047 | void sendTmuxCmd(char *dest, char *cmd) | 1092 | void sendTmuxCmd(char *dest, char *cmd) |
@@ -1421,29 +1466,25 @@ simList *getSims() | |||
1421 | 1466 | ||
1422 | V("Converting %s.ini -> %s", path, newPath); | 1467 | V("Converting %s.ini -> %s", path, newPath); |
1423 | D(cmd); | 1468 | D(cmd); |
1424 | if (!WIFEXITED(system(cmd))) | 1469 | if (shellMeFail(cmd)) |
1425 | E("sed command failed!"); | 1470 | E("sed command failed!"); |
1426 | else | 1471 | else |
1427 | { | 1472 | { |
1428 | free(cmd); | 1473 | if (shellMeFail("chmod ugo+x %s", newPath)) |
1429 | cmd = xmprintf("chmod ugo+x %s", newPath); | ||
1430 | if (!WIFEXITED(system(cmd))) | ||
1431 | E("chmod command failed!"); | 1474 | E("chmod command failed!"); |
1432 | free(cmd); | ||
1433 | 1475 | ||
1434 | char *link = xmprintf("%s/%s.shini", scBin, name); | 1476 | char *link = xmprintf("%s/%s.shini", scBin, name); |
1435 | I("Symlinking %s to %s", newPath, link); | 1477 | I("Symlinking %s to %s", newPath, link); |
1436 | if (qfile_exist(link)) | 1478 | if (qfile_exist(link)) |
1437 | { | 1479 | { |
1438 | cmd = xmprintf("rm %s", link); | 1480 | if (shellMeFail("rm %s", link)) |
1439 | if (!WIFEXITED(system(cmd))) | ||
1440 | E("rm command failed!"); | 1481 | E("rm command failed!"); |
1441 | free(cmd); | ||
1442 | } | 1482 | } |
1443 | if (0 != symlink(newPath, link)) | 1483 | if (0 != symlink(newPath, link)) |
1444 | perror_msg("Symlinking %s to %s", newPath, link); | 1484 | perror_msg("Symlinking %s to %s", newPath, link); |
1445 | free(link); | 1485 | free(link); |
1446 | } | 1486 | } |
1487 | free(cmd); | ||
1447 | } | 1488 | } |
1448 | free(newPath); | 1489 | free(newPath); |
1449 | free(path); | 1490 | free(path); |
@@ -1483,11 +1524,10 @@ simList *getSims() | |||
1483 | // Write the sims.lua_NEW file if needed. | 1524 | // Write the sims.lua_NEW file if needed. |
1484 | if (ourSims->doIt) | 1525 | if (ourSims->doIt) |
1485 | { | 1526 | { |
1486 | char *tnm = xmprintf("mv -f %s/sims.lua %s/sims.lua.BACKUP", scEtc, scEtc); | 1527 | char *tnm; |
1487 | 1528 | ||
1488 | if (!WIFEXITED(system(tnm))) | 1529 | if (shellMeFail("mv -f %s/sims.lua %s/sims.lua.BACKUP", scEtc, scEtc)) |
1489 | E("mv command failed!"); | 1530 | E("mv command failed!"); |
1490 | free(tnm); | ||
1491 | 1531 | ||
1492 | free(file); | 1532 | free(file); |
1493 | file = xmprintf("%s/sims.lua", scEtc); | 1533 | file = xmprintf("%s/sims.lua", scEtc); |
@@ -1735,11 +1775,9 @@ void prepSims(simData *simd, char *sim, char *type, int count, int window, int p | |||
1735 | } | 1775 | } |
1736 | 1776 | ||
1737 | // Create the temporary .ini files. | 1777 | // Create the temporary .ini files. |
1738 | snprintf(toybuf, sizeof(toybuf), "echo 'IDs={' >%s/IDs_%d.lua", scTemp, window); | 1778 | shellMe("echo 'IDs={' >%s/IDs_%d.lua", scTemp, window); |
1739 | system(toybuf); | ||
1740 | doTmuxCmd("list-panes -t %d -F \"['%d #{pane_index}'] = '#{window_id}.#{pane_id}',\" >> %s/IDs_%d.lua", window, window, scTemp, window); | 1779 | doTmuxCmd("list-panes -t %d -F \"['%d #{pane_index}'] = '#{window_id}.#{pane_id}',\" >> %s/IDs_%d.lua", window, window, scTemp, window); |
1741 | snprintf(toybuf, sizeof(toybuf), "echo '}\nreturn IDs' >>%s/IDs_%d.lua", scTemp, window); | 1780 | shellMe("echo '}\nreturn IDs' >>%s/IDs_%d.lua", scTemp, window); |
1742 | system(toybuf); | ||
1743 | 1781 | ||
1744 | findWindow(simd, sim, type, count, window, panes, pane); | 1782 | findWindow(simd, sim, type, count, window, panes, pane); |
1745 | simd->portH = 8004 + count * 2; | 1783 | simd->portH = 8004 + count * 2; |
@@ -1767,7 +1805,7 @@ void prepSims(simData *simd, char *sim, char *type, int count, int window, int p | |||
1767 | simd->portH, | 1805 | simd->portH, |
1768 | path, newPath); | 1806 | path, newPath); |
1769 | D("Writing .ini file %s with ports %d %d", newPath, simd->portH, simd->portU); | 1807 | D("Writing .ini file %s with ports %d %d", newPath, simd->portH, simd->portU); |
1770 | if (!WIFEXITED(system(cmd))) | 1808 | if (shellMeFail(cmd)) |
1771 | E("sed command failed!"); | 1809 | E("sed command failed!"); |
1772 | free(cmd); | 1810 | free(cmd); |
1773 | free(newPath); | 1811 | free(newPath); |
@@ -1819,13 +1857,12 @@ void stopSim(simData *simd, char *sim, char *type, int count, int window, int pa | |||
1819 | V("Attempting to shut down sim %s.", simd->tab); | 1857 | V("Attempting to shut down sim %s.", simd->tab); |
1820 | sendTmuxCmd(simd->paneID, "show users"); | 1858 | sendTmuxCmd(simd->paneID, "show users"); |
1821 | sleep(1); | 1859 | sleep(1); |
1822 | 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", | 1860 | 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", |
1823 | Tcmd, scRun, Tsocket, simd->paneID, scTemp, simd->tab); | 1861 | Tcmd, scRun, Tsocket, simd->paneID, scTemp, simd->tab)) |
1824 | int i = system(cmd); | 1862 | E("Problem with shutting down sim %s - Failed find 'Agents connected'", simd->tab); |
1825 | if (WIFEXITED(i)) | 1863 | else |
1826 | { | 1864 | { |
1827 | free(cmd); | 1865 | char *cmd = xmprintf("%s/STOP_%s", scTemp, simd->tab); |
1828 | cmd = xmprintf("%s/STOP_%s", scTemp, simd->tab); | ||
1829 | char *r = (char *) qfile_load(cmd, NULL); | 1866 | char *r = (char *) qfile_load(cmd, NULL); |
1830 | if (r) | 1867 | if (r) |
1831 | { | 1868 | { |
@@ -1845,10 +1882,8 @@ void stopSim(simData *simd, char *sim, char *type, int count, int window, int pa | |||
1845 | } | 1882 | } |
1846 | else | 1883 | else |
1847 | E("Problem with shutting down sim %s - Failed to read file %s", simd->tab, cmd); | 1884 | E("Problem with shutting down sim %s - Failed to read file %s", simd->tab, cmd); |
1885 | free(cmd); | ||
1848 | } | 1886 | } |
1849 | else | ||
1850 | E("Problem with shutting down sim %s - Failed to run command \n %s", simd->tab, cmd); | ||
1851 | free(cmd); | ||
1852 | 1887 | ||
1853 | // There's three things that might happen - | 1888 | // There's three things that might happen - |
1854 | // Sim will quit, tmux pane will go away before we can see the shutdown message. | 1889 | // Sim will quit, tmux pane will go away before we can see the shutdown message. |
@@ -4814,11 +4849,8 @@ t("Write shs %s", tnm4); | |||
4814 | } | 4849 | } |
4815 | xclose(fd); | 4850 | xclose(fd); |
4816 | I("Sending linky email to %s %s", getStrH(Rd->stuff, "email"), t1); | 4851 | I("Sending linky email to %s %s", getStrH(Rd->stuff, "email"), t1); |
4817 | command = xmprintf("sendmail -oi -t <'%s'", file); | 4852 | if (shellMeFail("sendmail -oi -t <'%s'", file)) |
4818 | int i = system(command); | ||
4819 | if (!WIFEXITED(i)) | ||
4820 | E("sendmail command failed!"); | 4853 | E("sendmail command failed!"); |
4821 | free(command); | ||
4822 | free(file); | 4854 | free(file); |
4823 | free(content); | 4855 | free(content); |
4824 | free(t1); | 4856 | free(t1); |
@@ -7451,14 +7483,12 @@ void forEachMember(char *verb, simFunction func, simFunction not) | |||
7451 | if (BACKUP == currentMode) | 7483 | if (BACKUP == currentMode) |
7452 | { | 7484 | { |
7453 | char *file = xmprintf("%s/.lastTime", scBackup); | 7485 | char *file = xmprintf("%s/.lastTime", scBackup); |
7454 | char *cmd = xmprintf("date +%%s > %s", file); | ||
7455 | 7486 | ||
7456 | last = (char *) qfile_load(file, NULL); | 7487 | last = (char *) qfile_load(file, NULL); |
7457 | if (NULL == last) | 7488 | if (NULL == last) |
7458 | last = xstrdup("0"); | 7489 | last = xstrdup("0"); |
7459 | if (!WIFEXITED(system(cmd))) | 7490 | if (shellMeFail("date +%%s > %s", file)) |
7460 | E("date command failed!"); | 7491 | E("date command failed!"); |
7461 | free(cmd); | ||
7462 | free(file); | 7492 | free(file); |
7463 | } | 7493 | } |
7464 | 7494 | ||
@@ -7529,11 +7559,9 @@ static void cleanup(void) | |||
7529 | { | 7559 | { |
7530 | V("Deleting web socket."); | 7560 | V("Deleting web socket."); |
7531 | free(cmd); | 7561 | free(cmd); |
7532 | cmd = xmprintf("rm %s/sledjchisl.socket", scRun); | 7562 | if (shellMeFail("rm %s/sledjchisl.socket", scRun)) |
7533 | if (!WIFEXITED(system(cmd))) | ||
7534 | E("rm command failed!"); | 7563 | E("rm command failed!"); |
7535 | } | 7564 | } |
7536 | free(cmd); | ||
7537 | 7565 | ||
7538 | if (accountPages) | 7566 | if (accountPages) |
7539 | { | 7567 | { |
@@ -7601,9 +7629,7 @@ static void cleanup(void) | |||
7601 | cmd = xmprintf("%s/%s", scRun, Tsocket); | 7629 | cmd = xmprintf("%s/%s", scRun, Tsocket); |
7602 | if (qfile_exist(cmd)) | 7630 | if (qfile_exist(cmd)) |
7603 | { | 7631 | { |
7604 | free(cmd); | 7632 | if (shellMeFail("rm %s/%s", scRun, Tsocket)) |
7605 | cmd = xmprintf("rm %s/%s", scRun, Tsocket); | ||
7606 | if (!WIFEXITED(system(cmd))) | ||
7607 | E("rm command failed!"); | 7633 | E("rm command failed!"); |
7608 | } | 7634 | } |
7609 | free(cmd); | 7635 | free(cmd); |
@@ -7886,9 +7912,7 @@ Other possibilities - | |||
7886 | // TODO - get rid of RunIt.sh as well. | 7912 | // TODO - get rid of RunIt.sh as well. |
7887 | if (!isTmux) | 7913 | if (!isTmux) |
7888 | { // Let's see if the proper tmux server is even running. | 7914 | { // Let's see if the proper tmux server is even running. |
7889 | memset(toybuf, 0, sizeof(toybuf)); | 7915 | i = shellMe("%s %s/%s -q list-sessions 2>/dev/null | grep -q %s:", Tcmd, scRun, Tsocket, Tconsole); |
7890 | snprintf(toybuf, sizeof(toybuf), "%s %s/%s -q list-sessions 2>/dev/null | grep -q %s:", Tcmd, scRun, Tsocket, Tconsole); | ||
7891 | i = system(toybuf); | ||
7892 | if (WIFEXITED(i)) | 7916 | if (WIFEXITED(i)) |
7893 | { | 7917 | { |
7894 | if (0 != WEXITSTATUS(i)) // No such session, create it. | 7918 | if (0 != WEXITSTATUS(i)) // No such session, create it. |
@@ -7918,14 +7942,11 @@ Other possibilities - | |||
7918 | "split-window -vp 50 -t '%s:' bash -c 'export PATH=%s:$PATH; /usr/bin/valgrind --leak-check=full ./sledjchisl; cd %s/current/bin; bash'", | 7942 | "split-window -vp 50 -t '%s:' bash -c 'export PATH=%s:$PATH; /usr/bin/valgrind --leak-check=full ./sledjchisl; cd %s/current/bin; bash'", |
7919 | scUser, Tcmd, scRun, Tsocket, scBin, Tconsole, Ttab, Tconsole, scBin, scRoot); | 7943 | scUser, Tcmd, scRun, Tsocket, scBin, Tconsole, Ttab, Tconsole, scBin, scRoot); |
7920 | } | 7944 | } |
7921 | i = system(toybuf); | 7945 | if (shellMeFail(toybuf)) |
7922 | if (!WIFEXITED(i)) | ||
7923 | E("tmux new-session command failed! %s", toybuf); | 7946 | E("tmux new-session command failed! %s", toybuf); |
7924 | else | 7947 | else |
7925 | { | 7948 | { |
7926 | snprintf(toybuf, sizeof(toybuf), "%s %s/%s select-pane -t 1 -T 'SledjChisl'", Tcmd, scRun, Tsocket); | 7949 | if (shellMeFail("%s %s/%s select-pane -t 1 -T 'SledjChisl'", Tcmd, scRun, Tsocket)) |
7927 | i = system(toybuf); | ||
7928 | if (!WIFEXITED(i)) | ||
7929 | E("tmux select-pane command failed!"); | 7950 | E("tmux select-pane command failed!"); |
7930 | } | 7951 | } |
7931 | // toybox argument parsing is half working. | 7952 | // toybox argument parsing is half working. |
@@ -7933,10 +7954,7 @@ Other possibilities - | |||
7933 | { | 7954 | { |
7934 | V("Joining the session."); | 7955 | V("Joining the session."); |
7935 | // Join the session. | 7956 | // Join the session. |
7936 | memset(toybuf, 0, sizeof(toybuf)); | 7957 | if (shellMeFail("%s %s/%s select-window -t '%s' \\; attach-session -t '%s'", Tcmd, scRun, Tsocket, Tconsole, Tconsole)) |
7937 | snprintf(toybuf, sizeof(toybuf), "%s %s/%s select-window -t '%s' \\; attach-session -t '%s'", Tcmd, scRun, Tsocket, Tconsole, Tconsole); | ||
7938 | i = system(toybuf); | ||
7939 | if (!WIFEXITED(i)) | ||
7940 | E("tmux attach-session command failed! %s", toybuf); | 7958 | E("tmux attach-session command failed! %s", toybuf); |
7941 | } | 7959 | } |
7942 | else | 7960 | else |
@@ -8011,8 +8029,7 @@ Other possibilities - | |||
8011 | V("Symlinking %s to %s", newPath, tmp); | 8029 | V("Symlinking %s to %s", newPath, tmp); |
8012 | if (qfile_exist(tmp)) | 8030 | if (qfile_exist(tmp)) |
8013 | { | 8031 | { |
8014 | snprintf(toybuf, sizeof(toybuf), "rm %s", tmp); | 8032 | if (shellMeFail("rm %s", tmp)) |
8015 | if (!WIFEXITED(system(toybuf))) | ||
8016 | E("rm command failed!"); | 8033 | E("rm command failed!"); |
8017 | } | 8034 | } |
8018 | if (0 != symlink(newPath, tmp)) | 8035 | if (0 != symlink(newPath, tmp)) |
@@ -8238,18 +8255,14 @@ Other possibilities - | |||
8238 | { | 8255 | { |
8239 | I("Closing all the other windows."); | 8256 | I("Closing all the other windows."); |
8240 | // First figure out what panes and windows are left. | 8257 | // First figure out what panes and windows are left. |
8241 | snprintf(toybuf, sizeof(toybuf), "echo 'IDs={' >%s/IDs_ALL.lua", scTemp); | 8258 | shellMeFail("echo 'IDs={' >%s/IDs_ALL.lua", scTemp); |
8242 | system(toybuf); | ||
8243 | doTmuxCmd("list-panes -s -F '\"#{window_id}.#{pane_id}\",' >> %s/IDs_ALL.lua", scTemp); | 8259 | doTmuxCmd("list-panes -s -F '\"#{window_id}.#{pane_id}\",' >> %s/IDs_ALL.lua", scTemp); |
8244 | snprintf(toybuf, sizeof(toybuf), "echo '}\nreturn IDs' >>%s/IDs_ALL.lua", scTemp); | 8260 | shellMeFail("echo '}\nreturn IDs' >>%s/IDs_ALL.lua", scTemp); |
8245 | system(toybuf); | ||
8246 | snprintf(toybuf, sizeof(toybuf), "%s/IDs_ALL.lua", scTemp); | 8261 | snprintf(toybuf, sizeof(toybuf), "%s/IDs_ALL.lua", scTemp); |
8247 | qtreetbl_t *IDs = Lua2tree(toybuf, "IDs"); | 8262 | qtreetbl_t *IDs = Lua2tree(toybuf, "IDs"); |
8248 | 8263 | ||
8249 | char *c = xmprintf("rm -fr %s/*", scTemp); | 8264 | if (shellMeFail("rm -fr %s/*", scTemp)) |
8250 | if (!WIFEXITED(system(c))) | 8265 | E("rm command failed! %s/*", scTemp); |
8251 | E("rm command failed! %s", c); | ||
8252 | free(c); | ||
8253 | 8266 | ||
8254 | qtreetbl_obj_t obj0; | 8267 | qtreetbl_obj_t obj0; |
8255 | memset((void*)&obj0, 0, sizeof(obj0)); | 8268 | memset((void*)&obj0, 0, sizeof(obj0)); |