Skip to content
Snippets Groups Projects
Commit a1a162c9 authored by Eric Vidal's avatar Eric Vidal :speech_balloon:
Browse files

provide ssexec_all API

parent 35d0a4cf
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ src/include/66/resolve.h: src/include/66/parser.h src/include/66/ssexec.h
src/include/66/svc.h: src/include/66/resolve.h src/include/66/ssexec.h
src/include/66/tree.h: src/include/66/ssexec.h
src/include/66/utils.h: src/include/66/resolve.h src/include/66/ssexec.h
src/66/66-all.o src/66/66-all.lo: src/66/66-all.c src/include/66/config.h src/include/66/constants.h src/include/66/tree.h src/include/66/utils.h
src/66/66-all.o src/66/66-all.lo: src/66/66-all.c src/include/66/ssexec.h
src/66/66-boot.o src/66/66-boot.lo: src/66/66-boot.c src/include/66/config.h src/include/66/constants.h
src/66/66-dbctl.o src/66/66-dbctl.lo: src/66/66-dbctl.c src/include/66/ssexec.h
src/66/66-disable.o src/66/66-disable.lo: src/66/66-disable.c src/include/66/ssexec.h
......@@ -67,6 +67,7 @@ src/lib66/ss_resolve.o src/lib66/ss_resolve.lo: src/lib66/ss_resolve.c src/inclu
src/lib66/ss_resolve_graph.o src/lib66/ss_resolve_graph.lo: src/lib66/ss_resolve_graph.c src/include/66/constants.h src/include/66/resolve.h src/include/66/utils.h
src/lib66/ss_state.o src/lib66/ss_state.lo: src/lib66/ss_state.c src/include/66/state.h
src/lib66/ss_utils.o src/lib66/ss_utils.lo: src/lib66/ss_utils.c src/include/66/config.h src/include/66/constants.h src/include/66/parser.h src/include/66/resolve.h src/include/66/utils.h
src/lib66/ssexec_all.o src/lib66/ssexec_all.lo: src/lib66/ssexec_all.c src/include/66/constants.h src/include/66/ssexec.h src/include/66/tree.h src/include/66/utils.h
src/lib66/ssexec_dbctl.o src/lib66/ssexec_dbctl.lo: src/lib66/ssexec_dbctl.c src/include/66/constants.h src/include/66/db.h src/include/66/enum.h src/include/66/resolve.h src/include/66/ssexec.h src/include/66/state.h src/include/66/utils.h
src/lib66/ssexec_disable.o src/lib66/ssexec_disable.lo: src/lib66/ssexec_disable.c src/include/66/constants.h src/include/66/db.h src/include/66/resolve.h src/include/66/state.h src/include/66/svc.h src/include/66/tree.h src/include/66/utils.h
src/lib66/ssexec_enable.o src/lib66/ssexec_enable.lo: src/lib66/ssexec_enable.c src/include/66/constants.h src/include/66/db.h src/include/66/environ.h src/include/66/parser.h src/include/66/resolve.h src/include/66/ssexec.h src/include/66/svc.h src/include/66/tree.h src/include/66/utils.h
......@@ -92,7 +93,7 @@ src/lib66/tree_sethome.o src/lib66/tree_sethome.lo: src/lib66/tree_sethome.c src
src/lib66/tree_setname.o src/lib66/tree_setname.lo: src/lib66/tree_setname.c src/include/66/tree.h
src/lib66/tree_switch_current.o src/lib66/tree_switch_current.lo: src/lib66/tree_switch_current.c src/include/66/config.h src/include/66/constants.h src/include/66/tree.h src/include/66/utils.h
66-all: EXTRA_LIBS := -ls6 -loblibs -lskarnet ${SPAWN_LIB}
66-all: EXTRA_LIBS := -ls6rc -ls6 -loblibs -lskarnet
66-all: src/66/66-all.o ${LIB66}
66-boot: EXTRA_LIBS := -ls6 -loblibs -lskarnet ${SPAWN_LIB}
66-boot: src/66/66-boot.o ${LIB66}
......@@ -145,9 +146,9 @@ src/lib66/tree_switch_current.o src/lib66/tree_switch_current.lo: src/lib66/tree
execl-envfile: EXTRA_LIBS := -loblibs -lexecline -lskarnet
execl-envfile: src/extra-tools/execl-envfile.o ${LIBEXECLINE}
ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
lib66.a.xyzzy: src/lib66/backup_cmd_switcher.o src/lib66/backup_make_new.o src/lib66/backup_realpath_sym.o src/lib66/db_compile.o src/lib66/db_find_compiled_state.o src/lib66/db_ok.o src/lib66/db_switch_to.o src/lib66/db_update.o src/lib66/hpr_shutdown.o src/lib66/hpr_wall.o src/lib66/parser.o src/lib66/parser_enabled.o src/lib66/parser_module.o src/lib66/parser_utils.o src/lib66/parser_write.o src/lib66/rc_init.o src/lib66/rc_manage.o src/lib66/rc_send.o src/lib66/rc_unsupervise.o src/lib66/ss_environ.o src/lib66/ss_get_enum.o src/lib66/ss_info_utils.o src/lib66/ss_instance.o src/lib66/ss_resolve.o src/lib66/ss_resolve_graph.o src/lib66/ss_state.o src/lib66/ss_utils.o src/lib66/ssexec_dbctl.o src/lib66/ssexec_enable.o src/lib66/ssexec_env.o src/lib66/ssexec_disable.o src/lib66/ssexec_free.o src/lib66/ssexec_help.o src/lib66/ssexec_init.o src/lib66/ssexec_main.o src/lib66/ssexec_start.o src/lib66/ssexec_stop.o src/lib66/ssexec_svctl.o src/lib66/svc_init.o src/lib66/svc_init_pipe.o src/lib66/svc_send.o src/lib66/svc_switch_to.o src/lib66/svc_unsupervise.o src/lib66/tree_cmd_state.o src/lib66/tree_copy.o src/lib66/tree_copy_tmp.o src/lib66/tree_find_current.o src/lib66/tree_get_permissions.o src/lib66/tree_sethome.o src/lib66/tree_setname.o src/lib66/tree_switch_current.o
lib66.a.xyzzy: src/lib66/backup_cmd_switcher.o src/lib66/backup_make_new.o src/lib66/backup_realpath_sym.o src/lib66/db_compile.o src/lib66/db_find_compiled_state.o src/lib66/db_ok.o src/lib66/db_switch_to.o src/lib66/db_update.o src/lib66/hpr_shutdown.o src/lib66/hpr_wall.o src/lib66/parser.o src/lib66/parser_enabled.o src/lib66/parser_module.o src/lib66/parser_utils.o src/lib66/parser_write.o src/lib66/rc_init.o src/lib66/rc_manage.o src/lib66/rc_send.o src/lib66/rc_unsupervise.o src/lib66/ss_environ.o src/lib66/ss_get_enum.o src/lib66/ss_info_utils.o src/lib66/ss_instance.o src/lib66/ss_resolve.o src/lib66/ss_resolve_graph.o src/lib66/ss_state.o src/lib66/ss_utils.o src/lib66/ssexec_all.o src/lib66/ssexec_dbctl.o src/lib66/ssexec_enable.o src/lib66/ssexec_env.o src/lib66/ssexec_disable.o src/lib66/ssexec_free.o src/lib66/ssexec_help.o src/lib66/ssexec_init.o src/lib66/ssexec_main.o src/lib66/ssexec_start.o src/lib66/ssexec_stop.o src/lib66/ssexec_svctl.o src/lib66/svc_init.o src/lib66/svc_init_pipe.o src/lib66/svc_send.o src/lib66/svc_switch_to.o src/lib66/svc_unsupervise.o src/lib66/tree_cmd_state.o src/lib66/tree_copy.o src/lib66/tree_copy_tmp.o src/lib66/tree_find_current.o src/lib66/tree_get_permissions.o src/lib66/tree_sethome.o src/lib66/tree_setname.o src/lib66/tree_switch_current.o
else
lib66.a.xyzzy: src/lib66/backup_cmd_switcher.lo src/lib66/backup_make_new.lo src/lib66/backup_realpath_sym.lo src/lib66/db_compile.lo src/lib66/db_find_compiled_state.lo src/lib66/db_ok.lo src/lib66/db_switch_to.lo src/lib66/db_update.lo src/lib66/hpr_shutdown.lo src/lib66/hpr_wall.lo src/lib66/parser.lo src/lib66/parser_enabled.lo src/lib66/parser_module.lo src/lib66/parser_utils.lo src/lib66/parser_write.lo src/lib66/rc_init.lo src/lib66/rc_manage.lo src/lib66/rc_send.lo src/lib66/rc_unsupervise.lo src/lib66/ss_environ.lo src/lib66/ss_get_enum.lo src/lib66/ss_info_utils.lo src/lib66/ss_instance.lo src/lib66/ss_resolve.lo src/lib66/ss_resolve_graph.lo src/lib66/ss_state.lo src/lib66/ss_utils.lo src/lib66/ssexec_dbctl.lo src/lib66/ssexec_enable.lo src/lib66/ssexec_env.lo src/lib66/ssexec_disable.lo src/lib66/ssexec_free.lo src/lib66/ssexec_help.lo src/lib66/ssexec_init.lo src/lib66/ssexec_main.lo src/lib66/ssexec_start.lo src/lib66/ssexec_stop.lo src/lib66/ssexec_svctl.lo src/lib66/svc_init.lo src/lib66/svc_init_pipe.lo src/lib66/svc_send.lo src/lib66/svc_switch_to.lo src/lib66/svc_unsupervise.lo src/lib66/tree_cmd_state.lo src/lib66/tree_copy.lo src/lib66/tree_copy_tmp.lo src/lib66/tree_find_current.lo src/lib66/tree_get_permissions.lo src/lib66/tree_sethome.lo src/lib66/tree_setname.lo src/lib66/tree_switch_current.lo
lib66.a.xyzzy: src/lib66/backup_cmd_switcher.lo src/lib66/backup_make_new.lo src/lib66/backup_realpath_sym.lo src/lib66/db_compile.lo src/lib66/db_find_compiled_state.lo src/lib66/db_ok.lo src/lib66/db_switch_to.lo src/lib66/db_update.lo src/lib66/hpr_shutdown.lo src/lib66/hpr_wall.lo src/lib66/parser.lo src/lib66/parser_enabled.lo src/lib66/parser_module.lo src/lib66/parser_utils.lo src/lib66/parser_write.lo src/lib66/rc_init.lo src/lib66/rc_manage.lo src/lib66/rc_send.lo src/lib66/rc_unsupervise.lo src/lib66/ss_environ.lo src/lib66/ss_get_enum.lo src/lib66/ss_info_utils.lo src/lib66/ss_instance.lo src/lib66/ss_resolve.lo src/lib66/ss_resolve_graph.lo src/lib66/ss_state.lo src/lib66/ss_utils.lo src/lib66/ssexec_all.lo src/lib66/ssexec_dbctl.lo src/lib66/ssexec_enable.lo src/lib66/ssexec_env.lo src/lib66/ssexec_disable.lo src/lib66/ssexec_free.lo src/lib66/ssexec_help.lo src/lib66/ssexec_init.lo src/lib66/ssexec_main.lo src/lib66/ssexec_start.lo src/lib66/ssexec_stop.lo src/lib66/ssexec_svctl.lo src/lib66/svc_init.lo src/lib66/svc_init_pipe.lo src/lib66/svc_send.lo src/lib66/svc_switch_to.lo src/lib66/svc_unsupervise.lo src/lib66/tree_cmd_state.lo src/lib66/tree_copy.lo src/lib66/tree_copy_tmp.lo src/lib66/tree_find_current.lo src/lib66/tree_get_permissions.lo src/lib66/tree_sethome.lo src/lib66/tree_setname.lo src/lib66/tree_switch_current.lo
endif
lib66.so.xyzzy: EXTRA_LIBS := -ls6rc -ls6 -loblibs -lexecline -lskarnet
lib66.so.xyzzy: src/lib66/backup_cmd_switcher.lo src/lib66/backup_make_new.lo src/lib66/backup_realpath_sym.lo src/lib66/db_compile.lo src/lib66/db_find_compiled_state.lo src/lib66/db_ok.lo src/lib66/db_switch_to.lo src/lib66/db_update.lo src/lib66/hpr_shutdown.lo src/lib66/hpr_wall.lo src/lib66/parser.lo src/lib66/parser_enabled.lo src/lib66/parser_module.lo src/lib66/parser_utils.lo src/lib66/parser_write.lo src/lib66/rc_init.lo src/lib66/rc_manage.lo src/lib66/rc_send.lo src/lib66/rc_unsupervise.lo src/lib66/ss_environ.lo src/lib66/ss_get_enum.lo src/lib66/ss_info_utils.lo src/lib66/ss_instance.lo src/lib66/ss_resolve.lo src/lib66/ss_resolve_graph.lo src/lib66/ss_state.lo src/lib66/ss_utils.lo src/lib66/ssexec_dbctl.lo src/lib66/ssexec_enable.lo src/lib66/ssexec_env.lo src/lib66/ssexec_disable.lo src/lib66/ssexec_free.lo src/lib66/ssexec_help.lo src/lib66/ssexec_init.lo src/lib66/ssexec_main.lo src/lib66/ssexec_start.lo src/lib66/ssexec_stop.lo src/lib66/ssexec_svctl.lo src/lib66/svc_init.lo src/lib66/svc_init_pipe.lo src/lib66/svc_send.lo src/lib66/svc_switch_to.lo src/lib66/svc_unsupervise.lo src/lib66/tree_cmd_state.lo src/lib66/tree_copy.lo src/lib66/tree_copy_tmp.lo src/lib66/tree_find_current.lo src/lib66/tree_get_permissions.lo src/lib66/tree_sethome.lo src/lib66/tree_setname.lo src/lib66/tree_switch_current.lo
lib66.so.xyzzy: src/lib66/backup_cmd_switcher.lo src/lib66/backup_make_new.lo src/lib66/backup_realpath_sym.lo src/lib66/db_compile.lo src/lib66/db_find_compiled_state.lo src/lib66/db_ok.lo src/lib66/db_switch_to.lo src/lib66/db_update.lo src/lib66/hpr_shutdown.lo src/lib66/hpr_wall.lo src/lib66/parser.lo src/lib66/parser_enabled.lo src/lib66/parser_module.lo src/lib66/parser_utils.lo src/lib66/parser_write.lo src/lib66/rc_init.lo src/lib66/rc_manage.lo src/lib66/rc_send.lo src/lib66/rc_unsupervise.lo src/lib66/ss_environ.lo src/lib66/ss_get_enum.lo src/lib66/ss_info_utils.lo src/lib66/ss_instance.lo src/lib66/ss_resolve.lo src/lib66/ss_resolve_graph.lo src/lib66/ss_state.lo src/lib66/ss_utils.lo src/lib66/ssexec_all.lo src/lib66/ssexec_dbctl.lo src/lib66/ssexec_enable.lo src/lib66/ssexec_env.lo src/lib66/ssexec_disable.lo src/lib66/ssexec_free.lo src/lib66/ssexec_help.lo src/lib66/ssexec_init.lo src/lib66/ssexec_main.lo src/lib66/ssexec_start.lo src/lib66/ssexec_stop.lo src/lib66/ssexec_svctl.lo src/lib66/svc_init.lo src/lib66/svc_init_pipe.lo src/lib66/svc_send.lo src/lib66/svc_switch_to.lo src/lib66/svc_unsupervise.lo src/lib66/tree_cmd_state.lo src/lib66/tree_copy.lo src/lib66/tree_copy_tmp.lo src/lib66/tree_find_current.lo src/lib66/tree_get_permissions.lo src/lib66/tree_sethome.lo src/lib66/tree_setname.lo src/lib66/tree_switch_current.lo
......@@ -12,346 +12,26 @@
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <oblibs/log.h>
#include <oblibs/obgetopt.h>
#include <oblibs/types.h>
#include <oblibs/string.h>
#include <oblibs/files.h>
#include <oblibs/sastr.h>
#include <skalibs/buffer.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <66/constants.h>
#include <66/config.h>
#include <66/utils.h>
#include <66/tree.h>
static unsigned int DEADLINE = 0 ;
unsigned int trc = 0 ;
#define USAGE "66-all [ -h ] [ -z ] [ -v verbosity ] [ -f ] [ -T timeout ] [ -l live ] [ -t tree ] up/down"
static inline void info_help (void)
{
static char const *help =
"66-all <options> up/down\n"
"\n"
"options :\n"
" -h: print this help\n"
" -z: use color\n"
" -v: increase/decrease verbosity\n"
" -T: timeout\n"
" -l: live directory\n"
" -t: tree to use\n"
" -f: fork the process\n"
;
if (buffer_putsflush(buffer_1, help) < 0)
log_dieusys(LOG_EXIT_SYS, "write to stdout") ;
}
int doit(char const *tree,char const *treename,char const *live, unsigned int what,uid_t owner, char const *const *envp)
{
int r, wstat ;
pid_t pid ;
stralloc dirlist = STRALLOC_ZERO ;
char ownerstr[UID_FMT] ;
size_t ownerlen = uid_fmt(ownerstr,owner) ;
ownerstr[ownerlen] = 0 ;
size_t livelen = strlen(live) - 1 ;
size_t treenamelen = strlen(treename) ;
char src[livelen + SS_STATE_LEN + 1 + ownerlen + 1 + treenamelen + 1 + SS_LIVETREE_INIT_LEN + 1] ;
memcpy(src,live,livelen) ;
memcpy(src + livelen, SS_STATE,SS_STATE_LEN) ;
src[livelen + SS_STATE_LEN] = '/' ;
memcpy(src + livelen + SS_STATE_LEN + 1,ownerstr,ownerlen) ;
src[livelen + SS_STATE_LEN + 1 + ownerlen] = '/' ;
memcpy(src + livelen + SS_STATE_LEN + 1 + ownerlen + 1,treename,treenamelen) ;
src[livelen + SS_STATE_LEN + 1 + ownerlen + 1 + treenamelen] = '/' ;
memcpy(src+ livelen + SS_STATE_LEN + 1 + ownerlen + 1 + treenamelen + 1,SS_LIVETREE_INIT,SS_LIVETREE_INIT_LEN) ;
src[livelen + SS_STATE_LEN + 1 + ownerlen + 1 + treenamelen + 1 + SS_LIVETREE_INIT_LEN] = 0 ;
r = scan_mode(src,S_IFREG) ;
if (r == -1) log_die(LOG_EXIT_SYS,src," conflicted format") ;
if (!r){ log_warn("uninitialized tree:", treename) ; goto freed ; }
src[livelen + SS_STATE_LEN + 1 + ownerlen + 1 + treenamelen] = 0 ;
if (!sastr_dir_get(&dirlist,src,"init",S_IFREG))
log_dieusys(LOG_EXIT_SYS,"get contents of directory: ",src) ;
if (dirlist.len)
{
size_t i = 0, len = sastr_len(&dirlist) ;
char const *newargv[10 + len + 1] ;
unsigned int m = 0 ;
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, VERBOSITY)] = 0 ;
char tt[UINT32_FMT] ;
tt[uint32_fmt(tt,DEADLINE)] = 0 ;
if (what)
newargv[m++] = SS_BINPREFIX "66-start" ;
else
newargv[m++] = SS_BINPREFIX "66-stop" ;
newargv[m++] = "-v" ;
newargv[m++] = fmt ;
newargv[m++] = "-T" ;
newargv[m++] = tt ;
newargv[m++] = "-l" ;
newargv[m++] = live ;
newargv[m++] = "-t" ;
newargv[m++] = treename ;
len = dirlist.len ;
for (;i < len; i += strlen(dirlist.s + i) + 1)
newargv[m++] = dirlist.s+i ;
newargv[m++] = 0 ;
pid = child_spawn0(newargv[0],newargv,envp) ;
if (waitpid_nointr(pid,&wstat, 0) < 0)
{
log_warnusys("wait for ",newargv[0]) ;
goto err ;
}
if (wstat) goto err ;
}
else log_info("Empty tree: ",treename," -- nothing to do") ;
freed:
stralloc_free(&dirlist) ;
return 1 ;
err:
stralloc_free(&dirlist) ;
return 0 ;
}
static void all_redir_fd(void)
{
int fd ;
while((fd = open("/dev/tty",O_RDWR|O_NOCTTY)) >= 0)
{
if (fd >= 3) break ;
}
dup2 (fd,0) ;
dup2 (fd,1) ;
dup2 (fd,2) ;
fd_close(fd) ;
if (setsid() < 0) log_dieusys(LOG_EXIT_SYS,"setsid") ;
if ((chdir("/")) < 0) log_dieusys(LOG_EXIT_SYS,"chdir") ;
ioctl(0,TIOCSCTTY,1) ;
umask(022) ;
}
#include <66/ssexec.h>
int main(int argc, char const *const *argv,char const *const *envp)
{
int r, what, wstat, shut = 0, fd ;
pid_t pid ;
uid_t owner ;
size_t statesize, statelen, pos = 0 ;
char const *treename = NULL ;
log_color = &log_color_disable ;
stralloc base = STRALLOC_ZERO ;
stralloc scandir = STRALLOC_ZERO ;
stralloc livetree = STRALLOC_ZERO ;
stralloc tree = STRALLOC_ZERO ;
stralloc live = STRALLOC_ZERO ;
stralloc contents = STRALLOC_ZERO ;
what = 1 ;
PROG = "66-all" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = getopt_args(argc,argv, ">hv:l:T:t:fz", &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 'v' : if (!uint0_scan(l.arg, &VERBOSITY)) log_usage(USAGE) ; break ;
case 'l' : if (!stralloc_cats(&live,l.arg)) log_die_nomem("stralloc") ;
if (!stralloc_0(&live)) log_die_nomem("stralloc") ;
break ;
case 'T' : if (!uint0_scan(l.arg, &DEADLINE)) log_usage(USAGE) ; break ;
case 't' : treename = l.arg ; break ;
case 'f' : shut = 1 ; break ;
case 'z' : log_color = !isatty(1) ? &log_color_disable : &log_color_enable ; break ;
default : log_usage(USAGE) ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc != 1) log_usage(USAGE) ;
if (*argv[0] == 'u') what = 1 ;
else if (*argv[0] == 'd') what = 0 ;
else log_usage(USAGE) ;
owner = MYUID ;
if (!set_ownersysdir(&base,owner)) log_dieusys(LOG_EXIT_SYS, "set owner directory") ;
r = set_livedir(&live) ;
if (!r) log_die_nomem("stralloc") ;
if (r < 0 ) log_die(LOG_EXIT_SYS,"live: ",live.s," must be an absolute path") ;
if (!stralloc_copy(&scandir,&live))log_die_nomem("stralloc") ;
r = set_livescan(&scandir,owner) ;
if (!r) log_die_nomem("stralloc") ;
if (r < 0 ) log_die(LOG_EXIT_SYS,"scandir: ",scandir.s," must be an absolute path") ;
if ((scandir_ok(scandir.s)) <= 0) log_die(LOG_EXIT_SYS,"scandir: ", scandir.s," is not running") ;
if (!stralloc_copy(&livetree,&live)) log_die_nomem("stralloc") ;
r = set_livetree(&livetree,owner) ;
if (!r) log_die_nomem("stralloc") ;
if (r < 0 ) log_die(LOG_EXIT_SYS,"livetree: ",livetree.s," must be an absolute path") ;
char ste[base.len + SS_SYSTEM_LEN + SS_STATE_LEN + 1] ;
memcpy(ste,base.s,base.len) ;
memcpy(ste + base.len,SS_SYSTEM,SS_SYSTEM_LEN) ;
memcpy(ste + base.len + SS_SYSTEM_LEN, SS_STATE ,SS_STATE_LEN) ;
statelen = base.len + SS_SYSTEM_LEN + SS_STATE_LEN ;
ste[statelen] = 0 ;
r = scan_mode(ste,S_IFREG) ;
if (r < 0) log_die(LOG_EXIT_SYS,"conflict format for: ",ste) ;
if (!r) log_dieusys(LOG_EXIT_SYS,"find: ",ste) ;
/** only one tree?*/
if (treename)
{
if (!stralloc_cats(&contents,treename))log_dieu(LOG_EXIT_SYS,"add: ", treename," as tree to start") ;
if (!stralloc_0(&contents)) log_die_nomem("stralloc") ;
}
else
{
statesize = file_get_size(ste) ;
r = openreadfileclose(ste,&contents,statesize) ;
if(!r) log_dieusys(LOG_EXIT_SYS,"open: ", ste) ;
/** ensure that we have an empty line at the end of the string*/
if (!stralloc_cats(&contents,"\n")) log_die_nomem("stralloc") ;
if (!stralloc_0(&contents)) log_die_nomem("stralloc") ;
if (!sastr_clean_element(&contents))
{
log_info("nothing to do") ;
goto end ;
}
}
if (shut)
{
pid_t dpid ;
int wstat = 0 ;
dpid = fork() ;
if (dpid < 0) log_dieusys(LOG_EXIT_SYS,"fork") ;
else if (dpid > 0)
{
if (waitpid_nointr(dpid,&wstat, 0) < 0)
log_dieusys(LOG_EXIT_SYS,"wait for child") ;
if (wstat)
log_die(LOG_EXIT_SYS,"child fail") ;
goto end ;
}
else all_redir_fd() ;
}
/** Down process? reverse in that case to respect tree start order*/
if (!what)
if (!sastr_reverse(&contents)) log_dieu(LOG_EXIT_SYS,"reserve tree order") ;
for (;pos < contents.len; pos += strlen(contents.s + pos) + 1)
{
tree.len = 0 ;
char *treename = contents.s + pos ;
if(!stralloc_cats(&tree,treename)) log_die_nomem("stralloc") ;
if(!stralloc_0(&tree)) log_die_nomem("stralloc") ;
r = tree_sethome(&tree,base.s,owner) ;
if (r < 0 || !r) log_dieusys(LOG_EXIT_SYS,"find tree: ", tree.s) ;
if (!tree_get_permissions(tree.s,owner))
log_die(LOG_EXIT_USER,"You're not allowed to use the tree: ",tree.s) ;
if (what)
{
char const *newargv[9] ;
unsigned int m = 0 ;
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, VERBOSITY)] = 0 ;
newargv[m++] = SS_BINPREFIX "66-init" ;
newargv[m++] = "-v" ;
newargv[m++] = fmt ;
newargv[m++] = "-l" ;
newargv[m++] = live.s ;
newargv[m++] = "-t" ;
newargv[m++] = treename ;
newargv[m++] = "b" ;
newargv[m++] = 0 ;
ssexec_t info = SSEXEC_ZERO ;
pid = child_spawn0(newargv[0],newargv,envp) ;
if (waitpid_nointr(pid,&wstat, 0) < 0)
log_dieusys(LOG_EXIT_SYS,"wait for ",newargv[0]) ;
info.prog = PROG ;
info.help = help_all ;
info.usage = usage_all ;
if (wstat)
log_dieu(LOG_EXIT_SYS,"initiate services of tree: ",treename) ;
/** 66-all supports to not define a default tree to start/stop,
* but ssexec_main do not support it. The tree_sethome() function
* will complain if a current tree is not define.
* So, ask to not set it. */
log_trace("reload scandir: ",scandir.s) ;
if (scandir_send_signal(scandir.s,"an") <= 0)
log_dieusys(LOG_EXIT_SYS,"reload scandir: ",scandir.s) ;
}
if (!doit(tree.s,treename,live.s,what,owner,envp)) log_dieu(LOG_EXIT_SYS,(what) ? "start" : "stop" , " services of tree: ",treename) ;
}
end:
if (shut)
{
while((fd = open("/dev/tty",O_RDWR|O_NOCTTY)) >= 0)
{
if (fd >= 3) break ;
}
dup2 (fd,0) ;
dup2 (fd,1) ;
dup2 (fd,2) ;
fd_close(fd) ;
}
stralloc_free(&base) ;
stralloc_free(&live) ;
stralloc_free(&tree) ;
stralloc_free(&livetree) ;
stralloc_free(&scandir) ;
stralloc_free(&contents) ;
info.skip_opt_tree = 1 ;
return 0 ;
return ssexec_main(argc,argv,envp,&ssexec_all,&info) ;
}
${LIB66}
-ls6rc
-ls6
-loblibs
-lskarnet
${SPAWN_LIB}
......@@ -39,6 +39,8 @@ struct ssexec_s
uint8_t opt_tree ;
uint8_t opt_timeout ;
uint8_t opt_color ;
// skip option definition 0->no,1-yes
uint8_t skip_opt_tree ; // tree,treename, treeallow and the permissions for the tree will be not set
} ;
#define SSEXEC_ZERO { .base = STRALLOC_ZERO , \
......@@ -57,9 +59,10 @@ struct ssexec_s
.opt_live = 0 , \
.opt_tree = 0 , \
.opt_timeout = 0 , \
.opt_color = 0 }
.opt_color = 0 , \
.skip_opt_tree = 0 }
typedef int ssexec_func_t(int, char const *const *argv, char const *const *envp, ssexec_t *info) ;
typedef int ssexec_func_t(int argc, char const *const *argv, char const *const *envp, ssexec_t *info) ;
typedef ssexec_func_t *ssexec_func_t_ref ;
extern void ssexec_free(ssexec_t *info) ;
......@@ -74,6 +77,7 @@ extern ssexec_func_t ssexec_stop ;
extern ssexec_func_t ssexec_svctl ;
extern ssexec_func_t ssexec_dbctl ;
extern ssexec_func_t ssexec_env ;
extern ssexec_func_t ssexec_all ;
extern char const *usage_enable ;
extern char const *help_enable ;
......@@ -91,6 +95,8 @@ extern char const *usage_init ;
extern char const *help_init ;
extern char const *usage_env ;
extern char const *help_env ;
extern char const *usage_all ;
extern char const *help_all ;
extern int ssexec_main(int argc, char const *const *argv, char const *const *envp,ssexec_func_t *func,ssexec_t *info) ;
......
......@@ -25,6 +25,7 @@ ss_resolve.o
ss_resolve_graph.o
ss_state.o
ss_utils.o
ssexec_all.o
ssexec_dbctl.o
ssexec_enable.o
ssexec_env.o
......
/*
* ssexec_all.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 <sys/stat.h>//S_IFREG,umask
#include <sys/types.h>//pid_t
#include <fcntl.h>//O_RDWR
#include <unistd.h>//dup2,setsid,chdir,fork
#include <sys/ioctl.h>
#include <stdint.h>//uint8_t
#include <stddef.h>//size_t
#include <oblibs/string.h>
#include <oblibs/types.h>
#include <oblibs/log.h>
#include <oblibs/sastr.h>
#include <oblibs/obgetopt.h>
#include <oblibs/files.h>
#include <skalibs/types.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <66/ssexec.h>
#include <66/constants.h>
#include <66/tree.h>
#include <66/utils.h>//scandir_ok
int all_doit(ssexec_t *info, unsigned int what, char const *const *envp)
{
int r ;
stralloc salist = STRALLOC_ZERO ;
char ownerstr[UID_FMT] ;
size_t ownerlen = uid_fmt(ownerstr,info->owner) ;
ownerstr[ownerlen] = 0 ;
char src[info->live.len + SS_STATE_LEN + 1 + ownerlen + 1 + info->treename.len + 1 + SS_LIVETREE_INIT_LEN + 1] ;
auto_strings(src,info->live.s,SS_STATE,"/",ownerstr,"/",info->treename.s,SS_LIVETREE_INIT) ;
r = scan_mode(src,S_IFREG) ;
if (r == -1) log_die(LOG_EXIT_SYS,src," conflicted format") ;
if (!r) {
log_warn ("uninitialized tree: ", info->treename.s) ;
goto freed ;
}
src[info->live.len + SS_STATE_LEN + 1 + ownerlen + 1 + info->treename.len] = 0 ;
if (!sastr_dir_get(&salist,src,"init",S_IFREG))
log_dieusys(LOG_EXIT_SYS,"get contents of directory: ",src) ;
if (salist.len)
{
size_t pos = 0, len = sastr_len(&salist) ;
int nargc = 2 + len + 1 ;
char const *newargv[nargc] ;
unsigned int m = 0 ;
newargv[m++] = "fake_name" ;
FOREACH_SASTR(&salist,pos) {
newargv[m++] = salist.s + pos ;
}
newargv[m++] = 0 ;
if (what) {
if (ssexec_start(nargc,newargv,envp,info))
goto err ;
} else {
if (ssexec_stop(nargc,newargv,envp,info))
goto err ;
}
}
else
log_info("Empty tree: ",info->treename.s," -- nothing to do") ;
freed:
stralloc_free(&salist) ;
return 1 ;
err:
stralloc_free(&salist) ;
return 0 ;
}
static void all_redir_fd(void)
{
int fd ;
while((fd = open("/dev/tty",O_RDWR|O_NOCTTY)) >= 0)
{
if (fd >= 3) break ;
}
dup2 (fd,0) ;
dup2 (fd,1) ;
dup2 (fd,2) ;
fd_close(fd) ;
if (setsid() < 0)
log_dieusys(LOG_EXIT_SYS,"setsid") ;
if ((chdir("/")) < 0)
log_dieusys(LOG_EXIT_SYS,"chdir") ;
ioctl(0,TIOCSCTTY,1) ;
umask(022) ;
}
int ssexec_all(int argc, char const *const *argv,char const *const *envp,ssexec_t *info)
{
int r, what = 1, shut = 0, fd ;
uint8_t unsupervise = 0 ;
size_t statesize, pos = 0 ;
stralloc contents = STRALLOC_ZERO ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = getopt_args(argc,argv, ">fU", &l) ;
if (opt == -1) break ;
if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ;
switch (opt)
{
case 'f' : shut = 1 ; break ;
case 'U' : unsupervise = 1 ;
break ;
default : log_usage(usage_all) ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc != 1) log_usage(usage_all) ;
if (*argv[0] == 'u')
what = 1 ;
else if (*argv[0] == 'd')
what = 0 ;
else
log_usage(usage_all) ;
if ((scandir_ok(info->scandir.s)) <= 0) log_die(LOG_EXIT_SYS,"scandir: ", info->scandir.s," is not running") ;
char ste[info->base.len + SS_SYSTEM_LEN + SS_STATE_LEN + 1] ;
auto_strings(info->base.s,SS_SYSTEM,SS_STATE) ;
r = scan_mode(ste,S_IFREG) ;
if (r < 0) log_die(LOG_EXIT_SYS,"conflict format for: ",ste) ;
if (!r) log_dieusys(LOG_EXIT_SYS,"find: ",ste) ;
/** only one tree?*/
if (info->treename.len)
{
if (!stralloc_cats(&contents,info->treename.s))log_dieu(LOG_EXIT_SYS,"add: ", info->treename.s," as tree to start") ;
if (!stralloc_0(&contents)) log_die_nomem("stralloc") ;
}
else
{
statesize = file_get_size(ste) ;
r = openreadfileclose(ste,&contents,statesize) ;
if(!r) log_dieusys(LOG_EXIT_SYS,"open: ", ste) ;
/** ensure that we have an empty line at the end of the string*/
if (!stralloc_cats(&contents,"\n") ||
!stralloc_0(&contents)) log_die_nomem("stralloc") ;
if (!sastr_clean_element(&contents)) {
log_info("nothing to do") ;
goto end ;
}
}
if (shut)
{
pid_t dpid ;
int wstat = 0 ;
dpid = fork() ;
if (dpid < 0) log_dieusys(LOG_EXIT_SYS,"fork") ;
else if (dpid > 0)
{
if (waitpid_nointr(dpid,&wstat, 0) < 0)
log_dieusys(LOG_EXIT_SYS,"wait for child") ;
if (wstat)
log_die(LOG_EXIT_SYS,"child fail") ;
goto end ;
}
else all_redir_fd() ;
}
/** Down process? reverse in that case to respect tree start order*/
if (!what)
if (!sastr_reverse(&contents)) log_dieu(LOG_EXIT_SYS,"reserve tree order") ;
FOREACH_SASTR(&contents,pos) {
info->treename.len = 0 ;
if (!auto_stra(&info->treename,contents.s + pos))
log_die_nomem("stralloc") ;
info->tree.len = 0 ;
if (!auto_stra(&info->tree,contents.s + pos))
log_die_nomem("stralloc") ;
r = tree_sethome(&info->tree,info->base.s,info->owner) ;
if (r < 0 || !r) log_dieusys(LOG_EXIT_SYS,"find tree: ", info->treename.s) ;
if (!tree_get_permissions(info->tree.s,info->owner))
log_die(LOG_EXIT_USER,"You're not allowed to use the tree: ",info->tree.s) ;
if (what)
{
int nargc = 3 ;
char const *newargv[nargc] ;
unsigned int m = 0 ;
newargv[m++] = "fake_name" ;
newargv[m++] = "b" ;
newargv[m++] = 0 ;
if (ssexec_init(nargc,newargv,envp,info))
log_dieu(LOG_EXIT_SYS,"initiate services of tree: ",info->treename.s) ;
log_trace("reload scandir: ",info->scandir.s) ;
if (scandir_send_signal(info->scandir.s,"an") <= 0)
log_dieusys(LOG_EXIT_SYS,"reload scandir: ",info->scandir.s) ;
}
if (!all_doit(info,what,envp))
log_dieu(LOG_EXIT_SYS,(what) ? "start" : "stop" , " services of tree: ",info->treename.s) ;
}
end:
if (shut)
{
while((fd = open("/dev/tty",O_RDWR|O_NOCTTY)) >= 0)
{
if (fd >= 3) break ;
}
dup2 (fd,0) ;
dup2 (fd,1) ;
dup2 (fd,2) ;
fd_close(fd) ;
}
stralloc_free(&contents) ;
return 0 ;
}
......@@ -151,3 +151,19 @@ char const *help_env =
" -i: import configuration files from src version to dst version\n"
" -e: edit the file with EDITOR\n"
;
char const *usage_all = "66-all [ -h ] [ -z ] [ -v verbosity ] [ -T timeout ] [ -l live ] [ -t tree ] [ -f ] [ -U ] up/down" ;
char const *help_all =
"66-all <options> up/down\n"
"\n"
"options :\n"
" -h: print this help\n"
" -z: use color\n"
" -v: increase/decrease verbosity\n"
" -T: timeout\n"
" -l: live directory\n"
" -t: tree to use\n"
" -f: fork the process\n"
" -U: unsupervise the tree\n"
;
......@@ -32,16 +32,19 @@ void set_ssinfo(ssexec_t *info)
if (!set_ownersysdir(&info->base,info->owner)) log_dieusys(LOG_EXIT_SYS, "set owner directory") ;
r = tree_sethome(&info->tree,info->base.s,info->owner) ;
if (r < 0) log_dieu(LOG_EXIT_USER,"find the current tree. You must use -t options") ;
if (!r) log_dieusys(LOG_EXIT_SYS,"find tree: ", info->tree.s) ;
if (info->skip_opt_tree) {
r = tree_setname(&info->treename,info->tree.s) ;
if (r < 0) log_dieu(LOG_EXIT_SYS,"set the tree name") ;
r = tree_sethome(&info->tree,info->base.s,info->owner) ;
if (r < 0) log_dieu(LOG_EXIT_USER,"find the current tree. You must use -t options") ;
if (!r) log_dieusys(LOG_EXIT_SYS,"find tree: ", info->tree.s) ;
if (!tree_get_permissions(info->tree.s,info->owner))
log_die(LOG_EXIT_USER,"You're not allowed to use the tree: ",info->tree.s) ;
else info->treeallow = 1 ;
r = tree_setname(&info->treename,info->tree.s) ;
if (r < 0) log_dieu(LOG_EXIT_SYS,"set the tree name") ;
if (!tree_get_permissions(info->tree.s,info->owner))
log_die(LOG_EXIT_USER,"You're not allowed to use the tree: ",info->tree.s) ;
else info->treeallow = 1 ;
}
r = set_livedir(&info->live) ;
if (!r) log_die_nomem("stralloc") ;
......@@ -95,6 +98,7 @@ int ssexec_main(int argc, char const *const *argv,char const *const *envp,ssexec
case 't' : if(!stralloc_cats(&info->tree,l.arg)) log_die_nomem("stralloc") ;
if(!stralloc_0(&info->tree)) log_die_nomem("stralloc") ;
info->opt_tree = 1 ;
info->skip_opt_tree = 0 ;
break ;
case 'T' : if (!uint0_scan(l.arg, &info->timeout)) log_usage(info->usage) ;
info->opt_timeout = 1 ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment