diff --git a/package/deps.mak b/package/deps.mak index 6138a0e8d4073813cc8bf9bff099485cfecded36..e8c9eea766a1fc954e2b99def80eaff65b314961 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -22,7 +22,9 @@ src/66/66-enable.o src/66/66-enable.lo: src/66/66-enable.c src/include/66/ssexec src/66/66-env.o src/66/66-env.lo: src/66/66-env.c src/include/66/ssexec.h src/66/66-hpr.o src/66/66-hpr.lo: src/66/66-hpr.c src/include/66/config.h src/include/66/hpr.h src/66/66-init.o src/66/66-init.lo: src/66/66-init.c src/include/66/ssexec.h +src/66/66-inresolve.o src/66/66-inresolve.lo: src/66/66-inresolve.c src/include/66/constants.h src/include/66/info.h src/include/66/resolve.h src/include/66/utils.h src/66/66-inservice.o src/66/66-inservice.lo: src/66/66-inservice.c src/include/66/constants.h src/include/66/enum.h src/include/66/environ.h src/include/66/info.h src/include/66/resolve.h src/include/66/state.h src/include/66/tree.h src/include/66/utils.h +src/66/66-instate.o src/66/66-instate.lo: src/66/66-instate.c src/include/66/constants.h src/include/66/info.h src/include/66/resolve.h src/include/66/state.h src/include/66/utils.h src/66/66-intree.o src/66/66-intree.lo: src/66/66-intree.c src/include/66/backup.h src/include/66/constants.h src/include/66/enum.h src/include/66/info.h src/include/66/resolve.h src/include/66/tree.h src/include/66/utils.h src/66/66-parser.o src/66/66-parser.lo: src/66/66-parser.c src/include/66/constants.h src/include/66/parser.h src/include/66/utils.h src/66/66-scanctl.o src/66/66-scanctl.lo: src/66/66-scanctl.c src/include/66/utils.h @@ -106,8 +108,12 @@ src/lib66/tree_switch_current.o src/lib66/tree_switch_current.lo: src/lib66/tree 66-info: src/66/66-info.o ${LIB66} -ls6 -loblibs -lskarnet 66-init: EXTRA_LIBS := 66-init: src/66/66-init.o ${LIB66} -ls6 -loblibs -lskarnet +66-inresolve: EXTRA_LIBS := +66-inresolve: src/66/66-inresolve.o ${LIB66} -loblibs -lskarnet 66-inservice: EXTRA_LIBS := ${SPAWN_LIB} 66-inservice: src/66/66-inservice.o ${LIB66} -ls6 -loblibs -lskarnet +66-instate: EXTRA_LIBS := +66-instate: src/66/66-instate.o ${LIB66} -loblibs -lskarnet 66-intree: EXTRA_LIBS := 66-intree: src/66/66-intree.o ${LIB66} -ls6 -loblibs -lskarnet 66-parser: EXTRA_LIBS := diff --git a/package/modes b/package/modes index 6029ee2e8aa2d0dc77e1547eb2a9df790a87d4fb..9193f0f48fd30d7981e3036dff9fee9be278219c 100644 --- a/package/modes +++ b/package/modes @@ -11,7 +11,9 @@ 66-svctl 0755 66-all 0755 66-intree 0755 +66-inresolve 0755 66-inservice 0755 +66-instate 0755 66-env 0755 66-boot 0755 66-shutdown 0755 diff --git a/package/targets.mak b/package/targets.mak index 739164005e4b41177dab04e21b8965a724eacb36..73289d8bd6f9af07ac3acfcf908b800b0d23005e 100644 --- a/package/targets.mak +++ b/package/targets.mak @@ -12,7 +12,9 @@ BIN_TARGETS := \ 66-svctl \ 66-all \ 66-intree \ +66-inresolve \ 66-inservice \ +66-instate \ 66-env \ 66-boot \ 66-shutdown \ diff --git a/src/66/66-inresolve.c b/src/66/66-inresolve.c new file mode 100644 index 0000000000000000000000000000000000000000..3057b9a1e40bd171c3bf8566341e3728d6161b85 --- /dev/null +++ b/src/66/66-inresolve.c @@ -0,0 +1,288 @@ +/* + * 66-inresolve.c + * + * Copyright (c) 2018-2020 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> +#include <wchar.h> + +#include <oblibs/log.h> +#include <oblibs/sastr.h> +#include <oblibs/string.h> +#include <oblibs/obgetopt.h> +#include <oblibs/types.h> + +#include <skalibs/buffer.h> +#include <skalibs/types.h> +#include <skalibs/stralloc.h> +#include <skalibs/lolstdio.h> + +#include <66/resolve.h> +#include <66/info.h> +#include <66/utils.h> +#include <66/constants.h> + +#define MAXOPTS 28 + +static wchar_t const field_suffix[] = L" :" ; +static char fields[INFO_NKEY][INFO_FIELD_MAXLEN] = {{ 0 }} ; + +#define USAGE "66-inresolve [ -h ] [ -z ] [ -t tree ] [ -l ] service" + +static inline void info_help (void) +{ + static char const *help = +"66-inresolve <options> service \n" +"\n" +"options :\n" +" -h: print this help\n" +" -z: use color\n" +" -t: only search service at the specified tree\n" +" -l: prints information of the associated logger if exist\n" +; + + if (buffer_putsflush(buffer_1, help) < 0) + log_dieusys(LOG_EXIT_SYS, "write to stdout") ; +} + +static void info_display_string(char const *field,char const *str) +{ + info_display_field_name(field) ; + + if (!*str) + { + if (!bprintf(buffer_1,"%s%s",log_color->warning,"None")) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + } + else + { + if (!buffer_puts(buffer_1,str)) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + } + if (buffer_putsflush(buffer_1,"\n") == -1) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; +} + +static void info_display_int(char const *field,unsigned int id) +{ + char *str = 0 ; + char ival[UINT_FMT] ; + ival[uint_fmt(ival,id)] = 0 ; + str = ival ; + + info_display_string(field,str) ; +} + +int main(int argc, char const *const *argv) +{ + size_t newlen = 0, pos = 0 ; + int found = 0 ; + uint8_t logger = 0 ; + uid_t owner ; + char ownerstr[UID_FMT] ; + ss_resolve_t res = RESOLVE_ZERO ; + ss_resolve_t lres = RESOLVE_ZERO ; + stralloc satree = STRALLOC_ZERO ; + stralloc src = STRALLOC_ZERO ; + stralloc tmp = STRALLOC_ZERO ; + char const *tname = 0 ; + char const *svname = 0 ; + + log_color = &log_color_disable ; + + char buf[MAXOPTS][INFO_FIELD_MAXLEN] = { + "Name", + "Description" , + "Version", + "Logger", + "Logreal", + "Logassoc", + "Dstlog", + "Deps", + "Optsdeps", + "Extdeps", + "Contents" , + "Src" , + "Srconf", + "Live", + "Runat", + "Tree", + "Treename", + "State", + "Exec_run" , + "Exec_finish" , + "Type" , + "Ndeps" , + "Noptsdeps" , + "Nextdeps" , + "Ncontents" , + "Down" , + "Disen", + "Real logger name" } ; + + PROG = "66-inresolve" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + + for (;;) + { + int opt = getopt_args(argc,argv, ">hzlt:", &l) ; + if (opt == -1) break ; + if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ; + switch (opt) + { + case 'h' : info_help(); return 0 ; + case 'z' : log_color = !isatty(1) ? &log_color_disable : &log_color_enable ; break ; + case 't' : tname = l.arg ; break ; + case 'l' : logger = 1 ; break ; + default : log_usage(USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (!argc) log_usage(USAGE) ; + svname = *argv ; + + owner = getuid() ; + size_t ownerlen = uid_fmt(ownerstr,owner) ; + ownerstr[ownerlen] = 0 ; + + if (!set_ownersysdir(&src,owner)) log_dieusys(LOG_EXIT_SYS, "set owner directory") ; + if (!auto_stra(&src,SS_SYSTEM)) log_die_nomem("stralloc") ; + + if (!scan_mode(src.s,S_IFDIR)) + { + log_info("no tree exist yet") ; + goto freed ; + } + + if (!auto_stra(&src,"/")) log_die_nomem("stralloc") ; + newlen = src.len ; + + if (!tname) + { + if (!stralloc_0(&src) || + !stralloc_copy(&tmp,&src)) log_die_nomem("stralloc") ; + + if (!sastr_dir_get(&satree, src.s,SS_BACKUP+1, S_IFDIR)) + log_dieu(LOG_EXIT_SYS,"get tree from directory: ",src.s) ; + + if (satree.len) + { + for(pos = 0 ; pos < satree.len ; pos += strlen(satree.s + pos) + 1) + { + tmp.len = newlen ; + char *name = satree.s + pos ; + + if (!auto_stra(&tmp,name,SS_SVDIRS)) log_die_nomem("stralloc") ; + if (ss_resolve_check(tmp.s,svname)) + { + if (!found) + if (!stralloc_copy(&src,&tmp)) log_die_nomem("stralloc") ; + found++ ; + } + } + } + else + { + log_info("no tree exist yet") ; + goto freed ; + } + } + else + { + if (!auto_stra(&src,tname,SS_SVDIRS)) log_die_nomem("stralloc") ; + if (ss_resolve_check(src.s,svname)) found++; + } + + if (!found) + { + log_die(LOG_EXIT_SYS,"unknown service: ",svname) ; + + } + else if (found > 1) + { + log_die(LOG_EXIT_SYS,svname," is set on different tree -- please use -t options") ; + } + + info_field_align(buf,fields,field_suffix,MAXOPTS) ; + + if (!ss_resolve_read(&res,src.s,svname)) log_dieusys(111,"read resolve file") ; + + info_display_string(fields[0],res.sa.s + res.name) ; + info_display_string(fields[1],res.sa.s + res.description) ; + info_display_string(fields[2],res.sa.s + res.version) ; + info_display_string(fields[3],res.sa.s + res.logger) ; + info_display_string(fields[4],res.sa.s + res.logreal) ; + info_display_string(fields[5],res.sa.s + res.logassoc) ; + info_display_string(fields[6],res.sa.s + res.dstlog) ; + info_display_string(fields[7],res.sa.s + res.deps) ; + info_display_string(fields[8],res.sa.s + res.optsdeps) ; + info_display_string(fields[9],res.sa.s + res.extdeps) ; + info_display_string(fields[10],res.sa.s + res.contents) ; + info_display_string(fields[11],res.sa.s + res.src) ; + info_display_string(fields[12],res.sa.s + res.srconf) ; + info_display_string(fields[13],res.sa.s + res.live) ; + info_display_string(fields[14],res.sa.s + res.runat) ; + info_display_string(fields[15],res.sa.s + res.tree) ; + info_display_string(fields[16],res.sa.s + res.treename) ; + info_display_string(fields[17],res.sa.s + res.state) ; + info_display_string(fields[18],res.sa.s + res.exec_run) ; + info_display_string(fields[19],res.sa.s + res.exec_finish) ; + info_display_int(fields[20],res.type) ; + info_display_int(fields[21],res.ndeps) ; + info_display_int(fields[22],res.noptsdeps) ; + info_display_int(fields[23],res.nextdeps) ; + info_display_int(fields[24],res.ncontents) ; + info_display_int(fields[25],res.down) ; + info_display_int(fields[26],res.disen) ; + + if (res.logger && logger) + { + if (!ss_resolve_read(&lres,src.s,res.sa.s + res.logger)) log_dieusys(111,"read resolve file of: ",res.sa.s + res.logger) ; + + if (buffer_putsflush(buffer_1,"\n") == -1) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + + info_display_string(fields[27],res.sa.s + res.logreal) ; + info_display_string(fields[0],lres.sa.s + lres.name) ; + info_display_string(fields[1],lres.sa.s + lres.description) ; + info_display_string(fields[2],lres.sa.s + lres.version) ; + info_display_string(fields[3],lres.sa.s + lres.logger) ; + info_display_string(fields[4],lres.sa.s + lres.logreal) ; + info_display_string(fields[5],lres.sa.s + lres.logassoc) ; + info_display_string(fields[6],lres.sa.s + lres.dstlog) ; + info_display_string(fields[7],lres.sa.s + lres.deps) ; + info_display_string(fields[11],lres.sa.s + lres.src) ; + info_display_string(fields[12],lres.sa.s + lres.srconf) ; + info_display_string(fields[13],lres.sa.s + lres.live) ; + info_display_string(fields[14],lres.sa.s + lres.runat) ; + info_display_string(fields[15],lres.sa.s + lres.tree) ; + info_display_string(fields[16],lres.sa.s + lres.treename) ; + info_display_string(fields[17],lres.sa.s + lres.state) ; + info_display_string(fields[18],lres.sa.s + lres.exec_run) ; + info_display_string(fields[19],lres.sa.s + lres.exec_finish) ; + info_display_int(fields[20],lres.type) ; + info_display_int(fields[21],lres.ndeps) ; + info_display_int(fields[25],lres.down) ; + info_display_int(fields[26],lres.disen) ; + } + + freed: + ss_resolve_free(&res) ; + ss_resolve_free(&lres) ; + stralloc_free(&satree) ; + stralloc_free(&src) ; + stralloc_free(&tmp) ; + return 0 ; +} diff --git a/src/66/66-instate.c b/src/66/66-instate.c new file mode 100644 index 0000000000000000000000000000000000000000..005bdae04e1e24af8e9628c7dfbf5e75ef1f61fc --- /dev/null +++ b/src/66/66-instate.c @@ -0,0 +1,238 @@ +/* + * 66-state.c + * + * Copyright (c) 2018-2020 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> +#include <wchar.h> + +#include <oblibs/log.h> +#include <oblibs/sastr.h> +#include <oblibs/string.h> +#include <oblibs/obgetopt.h> +#include <oblibs/types.h> + +#include <skalibs/buffer.h> +#include <skalibs/types.h> +#include <skalibs/stralloc.h> +#include <skalibs/lolstdio.h> + +#include <66/resolve.h> +#include <66/info.h> +#include <66/utils.h> +#include <66/constants.h> +#include <66/state.h> + +#define MAXOPTS 7 + +static wchar_t const field_suffix[] = L" :" ; +static char fields[INFO_NKEY][INFO_FIELD_MAXLEN] = {{ 0 }} ; + +#define USAGE "66-instate [ -h ] [ -z ] [ -t tree ] [ -l ] service" + +static inline void info_help (void) +{ + static char const *help = +"66-instate <options> service \n" +"\n" +"options :\n" +" -h: print this help\n" +" -z: use color\n" +" -t: only search service at the specified tree\n" +" -l: prints information of the associated logger if exist\n" +; + + if (buffer_putsflush(buffer_1, help) < 0) + log_dieusys(LOG_EXIT_SYS, "write to stdout") ; +} + +static void info_display_string(char const *field,char const *str) +{ + info_display_field_name(field) ; + + if (!*str) + { + if (!bprintf(buffer_1,"%s%s",log_color->warning,"None")) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + } + else + { + if (!buffer_puts(buffer_1,str)) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + } + if (buffer_putsflush(buffer_1,"\n") == -1) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; +} + +static void info_display_int(char const *field,unsigned int id) +{ + char *str = 0 ; + char ival[UINT_FMT] ; + ival[uint_fmt(ival,id)] = 0 ; + str = ival ; + + info_display_string(field,str) ; +} + +int main(int argc, char const *const *argv) +{ + size_t newlen = 0, pos = 0 ; + int found = 0 ; + uint8_t logger = 0 ; + uid_t owner ; + char ownerstr[UID_FMT] ; + ss_resolve_t res = RESOLVE_ZERO ; + stralloc satree = STRALLOC_ZERO ; + stralloc src = STRALLOC_ZERO ; + stralloc tmp = STRALLOC_ZERO ; + ss_state_t sta = STATE_ZERO ; + char const *tname = 0 ; + char const *svname = 0 ; + char const *ste = 0 ; + + log_color = &log_color_disable ; + + char buf[MAXOPTS][INFO_FIELD_MAXLEN] = { + "Reload", + "Init", + "Unsupervise", + "State", + "Pid", + "Name" , + "Real logger name" } ; + + PROG = "66-instate" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + + for (;;) + { + int opt = getopt_args(argc,argv, ">hzlt:", &l) ; + if (opt == -1) break ; + if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ; + switch (opt) + { + case 'h' : info_help(); return 0 ; + case 'z' : log_color = !isatty(1) ? &log_color_disable : &log_color_enable ; break ; + case 't' : tname = l.arg ; break ; + case 'l' : logger = 1 ; break ; + default : log_usage(USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (!argc) log_usage(USAGE) ; + svname = *argv ; + + owner = getuid() ; + size_t ownerlen = uid_fmt(ownerstr,owner) ; + ownerstr[ownerlen] = 0 ; + + if (!set_ownersysdir(&src,owner)) log_dieusys(LOG_EXIT_SYS, "set owner directory") ; + if (!auto_stra(&src,SS_SYSTEM)) log_die_nomem("stralloc") ; + + if (!scan_mode(src.s,S_IFDIR)) + { + log_info("no tree exist yet") ; + goto freed ; + } + + if (!auto_stra(&src,"/")) log_die_nomem("stralloc") ; + newlen = src.len ; + + if (!tname) + { + if (!stralloc_0(&src) || + !stralloc_copy(&tmp,&src)) log_die_nomem("stralloc") ; + + if (!sastr_dir_get(&satree, src.s,SS_BACKUP+1, S_IFDIR)) + log_dieu(LOG_EXIT_SYS,"get tree from directory: ",src.s) ; + + if (satree.len) + { + for(pos = 0 ; pos < satree.len ; pos += strlen(satree.s + pos) + 1) + { + tmp.len = newlen ; + char *name = satree.s + pos ; + + if (!auto_stra(&tmp,name,SS_SVDIRS)) log_die_nomem("stralloc") ; + if (ss_resolve_check(tmp.s,svname)) + { + if (!found) + if (!stralloc_copy(&src,&tmp)) log_die_nomem("stralloc") ; + found++ ; + } + } + } + else + { + log_info("no tree exist yet") ; + goto freed ; + } + } + else + { + if (!auto_stra(&src,tname,SS_SVDIRS)) log_die_nomem("stralloc") ; + if (ss_resolve_check(src.s,svname)) found++; + } + + if (!found) + { + log_die(LOG_EXIT_SYS,"unknown service: ",svname) ; + + } + else if (found > 1) + { + log_die(LOG_EXIT_SYS,svname," is set on different tree -- please use -t options") ; + } + if (!ss_resolve_read(&res,src.s,svname)) log_dieusys(111,"read resolve file of: ",src.s,"/.resolve/",svname) ; + + info_field_align(buf,fields,field_suffix,MAXOPTS) ; + + ste = res.sa.s + res.state ; + + if (!ss_state_check(ste,svname)) log_dieusys(111,"unitialized: ",svname) ; + if (!ss_state_read(&sta,ste,svname)) log_dieusys(111,"read state file of: ",ste,"/",svname) ; + + info_display_string(fields[5],svname) ; + info_display_int(fields[0],sta.reload) ; + info_display_int(fields[1],sta.init) ; + info_display_int(fields[2],sta.unsupervise) ; + info_display_int(fields[3],sta.state) ; + info_display_int(fields[4],sta.pid) ; + + if (res.logger && logger) + { + svname = res.sa.s + res.logger ; + if (!ss_state_check(ste,svname)) log_dieusys(111,"unitialized: ",svname) ; + if (!ss_state_read(&sta,ste,svname)) log_dieusys(111,"read state file of: ",ste,"/",svname) ; + + if (buffer_putsflush(buffer_1,"\n") == -1) + log_dieusys(LOG_EXIT_SYS,"write to stdout") ; + + info_display_string(fields[6],res.sa.s + res.logreal) ; + info_display_int(fields[0],sta.reload) ; + info_display_int(fields[1],sta.init) ; + info_display_int(fields[2],sta.unsupervise) ; + info_display_int(fields[3],sta.state) ; + info_display_int(fields[4],sta.pid) ; + + } + + freed: + ss_resolve_free(&res) ; + stralloc_free(&satree) ; + stralloc_free(&src) ; + stralloc_free(&tmp) ; + return 0 ; +} diff --git a/src/66/deps-exe/66-inresolve b/src/66/deps-exe/66-inresolve new file mode 100644 index 0000000000000000000000000000000000000000..9c58365fcec451152a7b10b51a36af3a3612a3fa --- /dev/null +++ b/src/66/deps-exe/66-inresolve @@ -0,0 +1,3 @@ +${LIB66} +-loblibs +-lskarnet diff --git a/src/66/deps-exe/66-instate b/src/66/deps-exe/66-instate new file mode 100644 index 0000000000000000000000000000000000000000..9c58365fcec451152a7b10b51a36af3a3612a3fa --- /dev/null +++ b/src/66/deps-exe/66-instate @@ -0,0 +1,3 @@ +${LIB66} +-loblibs +-lskarnet