diff --git a/src/lib66/write/deps-lib/deps b/src/lib66/write/deps-lib/deps index bd387c825832bef684f40f92c78d2218bc10ae47..d07d1f64466ea03a219318abadb3e78242d979e9 100644 --- a/src/lib66/write/deps-lib/deps +++ b/src/lib66/write/deps-lib/deps @@ -2,7 +2,6 @@ write_classic.o write_common.o write_environ.o write_execute_scripts.o -write_execute_scripts_user.o write_logger.o write_oneshot.o write_service.o diff --git a/src/lib66/write/write_classic.c b/src/lib66/write/write_classic.c index e582c0a743b4038b20d67f8edc3dbeede5433b55..6b38afcd51ac2ac2f614bbae6ca2dbf2f3ab2f69 100644 --- a/src/lib66/write/write_classic.c +++ b/src/lib66/write/write_classic.c @@ -30,23 +30,23 @@ void write_classic(resolve_service_t *res, char const *dst, uint8_t force) write_common(res, dst) ; /** run file */ - write_execute_scripts(res, &res->execute.run, "run", dst) ; + write_execute_scripts("run", res->sa.s + res->execute.run.run, dst) ; /** finish file */ if (res->execute.finish.run_user) - write_execute_scripts(res, &res->execute.finish, "finish", dst) ; + write_execute_scripts("finish", res->sa.s + res->execute.finish.run, dst) ; /** run.user file */ - write_execute_scripts_user(res, &res->execute.run, "run.user", dst) ; + write_execute_scripts( "run.user", res->sa.s + res->execute.run.run_user, dst) ; /** finish.user file */ if (res->execute.finish.run_user) - write_execute_scripts_user(res, &res->execute.finish, "finish.user", dst) ; + write_execute_scripts("finish.user", res->sa.s + res->execute.finish.run_user, dst) ; /** logger */ if (res->logger.name) { - char destination[strlen(dst)] ; + char destination[strlen(dst) + 1] ; if (!ob_dirname(destination, dst)) log_dieu(LOG_EXIT_SYS, "get dirname of: ", dst) ; diff --git a/src/lib66/write/write_common.c b/src/lib66/write/write_common.c index e0d351049ab89c832b6a7200e529744a79a9ef64..3c8c48b82d790b22763adc106c3a644c999a5171 100644 --- a/src/lib66/write/write_common.c +++ b/src/lib66/write/write_common.c @@ -36,9 +36,6 @@ void write_common(resolve_service_t *res, char const *dst) { log_flow() ; - char *time = 0 ; - size_t dstlen = strlen(dst) ; - /** down file */ if (res->execute.down) if (!file_create_empty(dst, "down", 0644)) @@ -100,7 +97,6 @@ void write_common(resolve_service_t *res, char const *dst) FOREACH_SASTR(&sa, pos) { char *what = sa.s + pos ; - size_t whatlen = strlen(what) ; char tmp[SS_MAX_PATH_LEN + 1] ; char basedir[srclen + 1] ; diff --git a/src/lib66/write/write_execute_scripts.c b/src/lib66/write/write_execute_scripts.c index 4bab4205b09b778192819416cfb4b7f9f6345bc8..d4bb5e8a426c71a54440e05e6dcada03f3390ff4 100644 --- a/src/lib66/write/write_execute_scripts.c +++ b/src/lib66/write/write_execute_scripts.c @@ -19,86 +19,25 @@ #include <oblibs/string.h> #include <oblibs/files.h> -#include <66/service.h> -#include <66/config.h> -#include <66/constants.h> -#include <66/enum.h> - -#include <s6/config.h> - #ifndef FAKELEN #define FAKELEN strlen(run) #endif -void write_execute_scripts(resolve_service_t *res, resolve_service_addon_scripts_t *scripts, char const *file, char const *dst) +void write_execute_scripts(char const *file, char const *contents, char const *dst) { + log_flow() ; char write[strlen(dst) + 1 + strlen(file) + 1] ; - char *shebang = "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ; - size_t shebanglen = strlen(shebang) ; - char run[shebanglen + strlen(res->sa.s + res->live.fdholderdir) + SS_FDHOLDER_PIPENAME_LEN + strlen(res->sa.s + res->name) + SS_LOG_SUFFIX_LEN + strlen(S6_BINPREFIX) + strlen(res->sa.s + scripts->runas) + strlen(res->sa.s + res->environ.envdir) + SS_SYM_VERSION_LEN + (SS_MAX_PATH*2) + SS_MAX_PATH + strlen(file) + 117 + 1] ; auto_strings(write, dst, "/", file) ; - /** shebang */ - auto_strings(run, shebang) ; - - if (res->logger.name && res->type != TYPE_ONESHOT) - auto_strings(run + FAKELEN, \ - "fdmove 1 0\n", \ - "s6-fdholder-retrieve ", \ - res->sa.s + res->live.fdholderdir, "/s ", \ - "\"" SS_FDHOLDER_PIPENAME "w-", \ - res->sa.s + res->name, SS_LOG_SUFFIX "\"\n", \ - "fdswap 0 1\n") ; - - - /** environ */ - if (res->environ.env) - auto_strings(run + FAKELEN, res->sa.s + res->environ.envdir, SS_SYM_VERSION "\n") ; - - /** log redirection for oneshot service */ - if (res->logger.name && res->type == TYPE_ONESHOT) { - - char verbo[UINT_FMT] ; - verbo[uint_fmt(verbo, VERBOSITY)] = 0 ; - - size_t namelen = strlen(res->sa.s + res->name) ; - size_t syslen = res->owner ? strlen(res->sa.s + res->path.home) + 1 + strlen(SS_LOGGER_USERDIR) : strlen(SS_LOGGER_SYSDIR) ; - size_t dstlen = res->logger.destination ? strlen(res->sa.s + res->logger.destination) : strlen(SS_LOGGER_SYSDIR) ; - - char dstlog[syslen + dstlen + namelen + 1] ; - - if (!res->logger.destination) { - - if (res->owner) - - auto_strings(dstlog, res->sa.s + res->path.home, "/", SS_LOGGER_USERDIR, res->sa.s + res->name) ; - - else - - auto_strings(dstlog, SS_LOGGER_SYSDIR, res->sa.s + res->name) ; - - } else { - - auto_strings(dstlog, res->sa.s + res->logger.destination) ; - } - - auto_strings(run + FAKELEN, "execl-toc -v", verbo, " -d ", dstlog, " -m 0755\n") ; - auto_strings(run + FAKELEN, "redirfd -a 1 ", dstlog, "/current\n") ; - } - - /** runas */ - if (!res->owner && scripts->runas) - auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", res->sa.s + scripts->runas, "\n") ; - - auto_strings(run + FAKELEN, "./", file, ".user") ; + char run[strlen(contents) + 1] ; + auto_strings(run, contents) ; if (!file_write_unsafe(dst, file, run, FAKELEN)) log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/", file) ; if (chmod(write, 0755) < 0) log_dieusys(LOG_EXIT_SYS, "chmod", write) ; - } diff --git a/src/lib66/write/write_execute_scripts_user.c b/src/lib66/write/write_execute_scripts_user.c index 65ee0fff23ae116501642278de56de63752c4689..7ddd99a8928b069f368399fc35b12251272f7c30 100644 --- a/src/lib66/write/write_execute_scripts_user.c +++ b/src/lib66/write/write_execute_scripts_user.c @@ -34,7 +34,7 @@ void write_execute_scripts_user(resolve_service_t *res, resolve_service_addon_sc char *shebang = scripts->shebang ? res->sa.s + scripts->shebang : SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ; size_t shebanglen = strlen(shebang) ; size_t scriptlen = strlen(res->sa.s + scripts->run_user) ; - char run[2 + shebanglen + 15 + scriptlen + 1] ; + char run[shebanglen + scriptlen + 4 + 1] ; int build = !strcmp(res->sa.s + scripts->build, "custom") ? 1 : 0 ; char write[strlen(dst) + 1 + strlen(file) + 1] ; @@ -45,11 +45,9 @@ void write_execute_scripts_user(resolve_service_t *res, resolve_service_addon_sc if (build && scripts->shebang) auto_strings(run + FAKELEN, res->sa.s + scripts->shebang, "\n") ; else - auto_strings(run + FAKELEN, \ - SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n", - "fdmove -c 2 1\n") ; + auto_strings(run + FAKELEN, shebang, "\n") ; - auto_strings(run + FAKELEN, res->sa.s + scripts->run_user) ; + auto_strings(run + FAKELEN, res->sa.s + scripts->run_user, "\n") ; if (!file_write_unsafe(dst, file, run, FAKELEN)) log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/", file) ; diff --git a/src/lib66/write/write_logger.c b/src/lib66/write/write_logger.c index 46bcd69be5db14e4978c87978dafa0a9dbe28f9a..f30150e4b3a9bf42d83f63757c02f351bf08c222 100644 --- a/src/lib66/write/write_logger.c +++ b/src/lib66/write/write_logger.c @@ -49,18 +49,10 @@ void write_logger(resolve_service_t *res, char const *destination, uint8_t force gid_t log_gid ; uint8_t owner = res->owner ; - int build = !strcmp(res->sa.s + res->logger.execute.run.build, "custom") ? 1 : 0 ; - - char *pmax = 0 ; - char *pback = 0 ; - char max[UINT32_FMT] ; - char back[UINT32_FMT] ; - char *timestamp = 0 ; - int itimestamp = SS_LOGGER_TIMESTAMP ; char *logrunner = res->logger.execute.run.runas ? res->sa.s + res->logger.execute.run.runas : SS_LOGGER_RUNNER ; - char dst[strlen(destination) + 1 + strlen(res->sa.s + res->logger.name) + 1] ; - auto_strings(dst, destination, "/", res->sa.s + res->logger.name) ; + char dst[strlen(destination) + strlen(res->sa.s + res->logger.name) + 1] ; + auto_strings(dst, destination, res->sa.s + res->logger.name) ; r = scan_mode(dst, S_IFDIR) ; if (r && force) { @@ -83,133 +75,37 @@ void write_logger(resolve_service_t *res, char const *destination, uint8_t force if (res->logger.execute.timeout.finish) write_uint(dst, "timeout-finish", res->logger.execute.timeout.finish) ; - /** timestamp */ - if (res->logger.timestamp != 3) - timestamp = res->logger.timestamp == TIME_NONE ? "" : res->logger.timestamp == TIME_ISO ? "T" : "t" ; - else - timestamp = itimestamp == TIME_NONE ? "" : itimestamp == TIME_ISO ? "T" : "t" ; - - /** backup */ - if (res->logger.backup) { - - back[uint32_fmt(back,res->logger.backup)] = 0 ; - pback = back ; - - } else - pback = "3" ; - - /** file size */ - if (res->logger.maxsize) { - - max[uint32_fmt(max,res->logger.maxsize)] = 0 ; - pmax = max ; - - } else - pmax = "1000000" ; + /** notification */ + write_uint(dst, "notification-fd", 3) ; /** log destination */ - size_t namelen = strlen(res->sa.s + res->name) ; - size_t syslen = res->owner ? strlen(res->sa.s + res->path.home) + 1 + strlen(SS_LOGGER_USERDIR) : strlen(SS_LOGGER_SYSDIR) ; - size_t dstlen = res->logger.destination ? strlen(res->sa.s + res->logger.destination) : strlen(SS_LOGGER_SYSDIR) ; - - char dstlog[syslen + dstlen + namelen + 1] ; - - if (!res->logger.destination) { - - if (res->owner) - - auto_strings(dstlog, res->sa.s + res->path.home, "/", SS_LOGGER_USERDIR, res->sa.s + res->name) ; - - else - - auto_strings(dstlog, SS_LOGGER_SYSDIR, res->sa.s + res->name) ; - - } else { - - auto_strings(dstlog, res->sa.s + res->logger.destination) ; - } - - if (!dir_create_parent(dstlog, 0755)) - log_dieusys(LOG_EXIT_SYS, "create directory: ", dstlog) ; + if (!dir_create_parent(res->sa.s + res->logger.destination, 0755)) + log_dieusys(LOG_EXIT_SYS, "create directory: ", res->sa.s + res->logger.destination) ; if (!owner && ((res->logger.execute.run.build == BUILD_AUTO) || (!res->logger.execute.run.build))) { if (!youruid(&log_uid, logrunner) || !yourgid(&log_gid, log_uid)) log_dieusys(LOG_EXIT_SYS, "get uid and gid of: ", logrunner) ; - if (chown(dstlog, log_uid, log_gid) == -1) - log_dieusys(LOG_EXIT_SYS, "chown: ", dstlog) ; + if (chown(res->sa.s + res->logger.destination, log_uid, log_gid) == -1) + log_dieusys(LOG_EXIT_SYS, "chown: ", res->sa.s + res->logger.destination) ; } - { - /** dst/run file */ - char *shebang = res->logger.execute.run.shebang ? res->sa.s + res->logger.execute.run.shebang : "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ; - - char run[strlen(shebang) + strlen(res->sa.s + res->live.fdholderdir) + SS_FDHOLDER_PIPENAME_LEN + strlen(res->sa.s + res->logger.name) + strlen(S6_BINPREFIX) + strlen(res->sa.s + res->logger.execute.run.runas) + 53 + 1] ; - - auto_strings(run, \ - shebang, \ - "s6-fdholder-retrieve ", \ - res->sa.s + res->live.fdholderdir, "/s ", \ - "\"" SS_FDHOLDER_PIPENAME "r-", \ - res->sa.s + res->logger.name, "\"\n") ; - - /** runas */ - if (!res->owner && res->logger.execute.run.runas) - auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", res->sa.s + res->logger.execute.run.runas, "\n") ; - - auto_strings(run + FAKELEN, "./run.user\n") ; - - if (!file_write_unsafe(dst, "run", run, FAKELEN)) - log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ; - - char write[strlen(dst) + 5] ; - auto_strings(write, dst, "/run") ; - - if (chmod(write, 0755) < 0) - log_dieusys(LOG_EXIT_SYS, "chmod", write) ; - } - - if (!build) { - - char *shebang = "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb -P\n" ; - size_t shebanglen = strlen(shebang) ; - - char run[shebanglen + strlen(S6_BINPREFIX) + strlen(logrunner) + strlen(pback) + strlen(timestamp) + strlen(pmax) + strlen(dstlog) + 45 + 1] ; - - auto_strings(run, \ - shebang, \ - "fdmove -c 2 1\n") ; - - if (!owner) - auto_strings(run + FAKELEN, S6_BINPREFIX "s6-setuidgid ", logrunner, "\n") ; - - auto_strings(run + FAKELEN, "s6-log ") ; - - if (SS_LOGGER_NOTIFY) - auto_strings(run + FAKELEN, "-d3 ") ; - - auto_strings(run + FAKELEN, "n", pback, " ") ; - - if (res->logger.timestamp < TIME_NONE) - auto_strings(run + FAKELEN, timestamp, " ") ; - - auto_strings(run + FAKELEN, "s", pmax, " ", dstlog, "\n") ; + char write[strlen(dst) + 10] ; - if (!file_write_unsafe(dst, "run.user", run, FAKELEN)) - log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ; + /** run script */ + if (!file_write_unsafe(dst, "run", res->sa.s + res->logger.execute.run.run, strlen(res->sa.s + res->logger.execute.run.run))) + log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ; - /** notification fd */ - if (SS_LOGGER_NOTIFY) - write_uint(dst, SS_NOTIFICATION, 3) ; + auto_strings(write, dst, "/run") ; - } else { + if (chmod(write, 0755) < 0) + log_dieusys(LOG_EXIT_SYS, "chmod", write) ; - if (!file_write_unsafe(dst, "run.user", res->sa.s + res->logger.execute.run.run_user, strlen(res->sa.s + res->logger.execute.run.run_user))) - log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ; - } + /** run.user script */ + if (!file_write_unsafe(dst, "run.user", res->sa.s + res->logger.execute.run.run_user, strlen(res->sa.s + res->logger.execute.run.run_user))) + log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ; - char write[strlen(dst) + 10] ; auto_strings(write, dst, "/run.user") ; if (chmod(write, 0755) < 0) diff --git a/src/lib66/write/write_oneshot.c b/src/lib66/write/write_oneshot.c index 673f975e6256857a8039256661a71854da0b0571..071f50adb2c9ef6d28c35eaa3ac860dd8d4bad9d 100644 --- a/src/lib66/write/write_oneshot.c +++ b/src/lib66/write/write_oneshot.c @@ -25,17 +25,17 @@ void write_oneshot(resolve_service_t *res, char const *dst) write_common(res, dst) ; /** run file */ - write_execute_scripts(res, &res->execute.run, "up", dst) ; + write_execute_scripts("up", res->sa.s + res->execute.run.run, dst) ; /** finish file */ if (res->execute.finish.run_user) - write_execute_scripts(res, &res->execute.finish, "down", dst) ; + write_execute_scripts("down", res->sa.s + res->execute.finish.run, dst) ; /** run.user file */ - write_execute_scripts_user(res, &res->execute.run, "up.user", dst) ; + write_execute_scripts("up.user", res->sa.s + res->execute.run.run_user, dst) ; /** finish.user file */ if (res->execute.finish.run_user) - write_execute_scripts_user(res, &res->execute.finish, "down.user", dst) ; + write_execute_scripts("down.user", res->sa.s + res->execute.finish.run_user, dst) ; } diff --git a/src/lib66/write/write_service.c b/src/lib66/write/write_service.c index d9d4c8da2f80c6044878430991e163f931ded1c9..1de04001ec76dcbb024df505e43effb3edef2180 100644 --- a/src/lib66/write/write_service.c +++ b/src/lib66/write/write_service.c @@ -47,14 +47,33 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force) { resolve_service_t fres = RESOLVE_SERVICE_ZERO ; resolve_wrapper_t_ref wres = resolve_set_struct(DATA_SERVICE, &fres) ; + ss_state_t ste = STATE_ZERO ; + if (resolve_check(workdir, name)) { + if (!resolve_read(wres, workdir, name)) + log_dieu(LOG_EXIT_SYS, "read resolve file of: ", name) ; - if (resolve_read_g(wres, workdir, name)) - if (fres.type != type && fres.live.disen) - log_die(LOG_EXIT_SYS, "Detection of incompatible type format for: ", name, " -- current: ", get_key_by_enum(ENUM_TYPE, type), " previous: ", get_key_by_enum(ENUM_TYPE, fres.type)) ; + if (state_check(fres.sa.s + fres.path.home, name)) { + if (!state_read(&ste, fres.sa.s + fres.path.home, name)) + log_dieu(LOG_EXIT_SYS, "read state file of: ", name) ; + + if (fres.type != type && FLAGS_ISSET(ste.isenabled, STATE_FLAGS_TRUE)) + log_die(LOG_EXIT_SYS, "Detection of incompatible type format for: ", name, " -- current: ", get_key_by_enum(ENUM_TYPE, type), " previous: ", get_key_by_enum(ENUM_TYPE, fres.type)) ; + } + } resolve_free(wres) ; } - + /** + * + * + * please pass through a temporary or backup + * + * just need to switch the /run/66/state/0/<service> symlink + * with atomic_symlink + * + * + * + * */ char wname[workdirlen + SS_SVC_LEN + 1 + namelen + 1] ; auto_strings(wname, workdir, SS_SVC, "/", name) ; @@ -80,6 +99,10 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force) switch(type) { + case TYPE_MODULE: + case TYPE_BUNDLE: + break ; + case TYPE_CLASSIC: write_classic(res, wname, force) ;