Commit 8f982b61 authored by Eric Vidal's avatar Eric Vidal 💬

add -F -o feature at 66-enable

parent 151fb345
......@@ -139,8 +139,12 @@ struct sv_alltype_s
uint32_t hiercopy[24] ; //dir/file to copy
int signal ;//down-signal file
unsigned int pipeline ; //pos in deps
stralloc saenv ;
uint32_t srconf ; //path of the environment file
stralloc saenv ;
/* path of the environment file, this is only concern the write
* process, the read process could be different if conf/sysadmin/service
* exist */
uint32_t srconf ;
} ;
#define SV_EXEC_ZERO \
......@@ -201,7 +205,8 @@ struct sv_alltype_s
{ 0 } , \
0 , \
0 , \
STRALLOC_ZERO \
STRALLOC_ZERO , \
0 \
}
extern sv_alltype const sv_alltype_zero ;
......@@ -298,9 +303,9 @@ extern void section_free(section_t *sec) ;
extern void freed_parser(void) ;
/** enable phase */
extern int parse_service_get_list(stralloc *result, stralloc *list) ;
extern int parse_service_before(ssexec_t *info, stralloc *parsed_list, char const *sv,unsigned int *nbsv, stralloc *sasv,unsigned int force) ;
extern int parse_service_before(ssexec_t *info, stralloc *parsed_list, char const *sv,unsigned int *nbsv, stralloc *sasv,unsigned int force,unsigned int exist) ;
extern int parse_service_deps(ssexec_t *info,stralloc *parsed_list, sv_alltype *sv_before, char const *sv,unsigned int *nbsv,stralloc *sasv,unsigned int force) ;
extern int parse_add_service(stralloc *parsed_list,sv_alltype *sv_before,char const *service,unsigned int *nbsv) ;
extern int parse_add_service(stralloc *parsed_list,sv_alltype *sv_before,char const *service,unsigned int *nbsv,uid_t owner) ;
/** utilities */
extern int parse_line(stralloc *src,size_t *pos) ;
extern int parse_quote(stralloc *src,size_t *pos) ;
......@@ -325,17 +330,17 @@ extern int clean_value(stralloc *sa) ;
extern void parse_err(int ierr,int idsec,int idkey) ;
extern int add_pipe(sv_alltype *sv, stralloc *sa) ;
/** write */
extern int write_services(ssexec_t *info,sv_alltype *sv, char const *workdir, unsigned int force) ;
extern int write_classic(sv_alltype *sv, char const *dst, unsigned int force) ;
extern int write_longrun(sv_alltype *sv,char const *dst, unsigned int force) ;
extern int write_oneshot(sv_alltype *sv,char const *dst, unsigned int force) ;
extern int write_bundle(sv_alltype *sv, char const *dst, unsigned int force) ;
extern int write_common(sv_alltype *sv, char const *dst) ;
extern int write_services(ssexec_t *info,sv_alltype *sv, char const *workdir, unsigned int force,unsigned int conf) ;
extern int write_classic(sv_alltype *sv, char const *dst, unsigned int force, unsigned int conf) ;
extern int write_longrun(sv_alltype *sv,char const *dst, unsigned int force, unsigned int conf) ;
extern int write_oneshot(sv_alltype *sv,char const *dst, unsigned int conf) ;
extern int write_bundle(sv_alltype *sv, char const *dst) ;
extern int write_common(sv_alltype *sv, char const *dst,unsigned int conf) ;
extern int write_exec(sv_alltype *sv, sv_exec *exec,char const *name,char const *dst,int mode) ;
extern int write_uint(char const *dst, char const *name, uint32_t ui) ;
extern int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *dst, int mode, unsigned int force) ;
extern int write_consprod(sv_alltype *sv,char const *prodname,char const *consname,char const *proddst,char const *consdst) ;
extern int write_dependencies(unsigned int nga,unsigned int idga,char const *dst,char const *filename, genalloc *ga, unsigned int force) ;
extern int write_dependencies(unsigned int nga,unsigned int idga,char const *dst,char const *filename, genalloc *ga) ;
extern int write_env(char const *name,stralloc *sa,char const *dst) ;
#endif
......@@ -15,6 +15,7 @@
#include <66/parser.h>
#include <string.h>
//#include <stdio.h>
#include <oblibs/string.h>
#include <oblibs/types.h>
......@@ -27,6 +28,7 @@
#include <66/resolve.h>
#include <66/utils.h>
#include <66/constants.h>
#include <66/environ.h>
int parse_service_get_list(stralloc *result, stralloc *list)
{
......@@ -46,23 +48,37 @@ int parse_service_get_list(stralloc *result, stralloc *list)
return 1 ;
}
int parse_add_service(stralloc *parsed_list,sv_alltype *sv_before,char const *service,unsigned int *nbsv)
int parse_add_service(stralloc *parsed_list,sv_alltype *sv_before,char const *service,unsigned int *nbsv,uid_t owner)
{
stralloc conf = STRALLOC_ZERO ;
size_t svlen = strlen(service) ;
char svsrc[svlen + 1] ;
if (!dirname(svsrc,service)) return 0 ;
size_t srclen = strlen(svsrc) ;
// keep source of the frontend file
sv_before->src = keep.len ;
if (!stralloc_catb(&keep,svsrc,srclen + 1)) retstralloc(0,"parse_add_service") ;
if (!stralloc_catb(parsed_list,service,svlen + 1)) retstralloc(0,"parse_add_service") ;
if (!genalloc_append(sv_alltype,&gasv,sv_before)) retstralloc(0,"parse_add_service") ;
if (!stralloc_catb(&keep,svsrc,srclen + 1)) return 0 ;
// keep source of the configuration file
if (sv_before->opts[2])
{
if (!env_resolve_conf(&conf,owner)) return 0 ;
sv_before->srconf = keep.len ;
if (!stralloc_catb(&keep,conf.s,conf.len + 1)) return 0 ;
}
// keep service on current list
if (!stralloc_catb(parsed_list,service,svlen + 1)) return 0 ;
if (!genalloc_append(sv_alltype,&gasv,sv_before)) return 0 ;
(*nbsv)++ ;
stralloc_free(&conf) ;
return 1 ;
}
int parse_service_deps(ssexec_t *info,stralloc *parsed_list, sv_alltype *sv_before, char const *sv,unsigned int *nbsv,stralloc *sasv,unsigned int force)
{
unsigned int exist = 0 ;
char *dname = 0 ;
stralloc newsv = STRALLOC_ZERO ;
if (sv_before->cname.nga)
......@@ -82,7 +98,7 @@ int parse_service_deps(ssexec_t *info,stralloc *parsed_list, sv_alltype *sv_befo
stralloc_free(&newsv) ;
return 0 ;
}
if (!parse_service_before(info,parsed_list,newsv.s,nbsv,sasv,force))
if (!parse_service_before(info,parsed_list,newsv.s,nbsv,sasv,force,exist))
{
stralloc_free(&newsv) ;
return 0 ;
......@@ -94,7 +110,7 @@ int parse_service_deps(ssexec_t *info,stralloc *parsed_list, sv_alltype *sv_befo
return 1 ;
}
int parse_service_before(ssexec_t *info,stralloc *parsed_list, char const *sv,unsigned int *nbsv, stralloc *sasv,unsigned int force)
int parse_service_before(ssexec_t *info,stralloc *parsed_list, char const *sv,unsigned int *nbsv, stralloc *sasv,unsigned int force,unsigned int exist)
{
int r = 0 , insta ;
......@@ -186,9 +202,9 @@ int parse_service_before(ssexec_t *info,stralloc *parsed_list, char const *sv,un
if (!parser(&sv_before,sasv,newsv.s)) goto err ;
if (!parse_add_service(parsed_list,&sv_before,svpath,nbsv)) goto err ;
if (!parse_add_service(parsed_list,&sv_before,svpath,nbsv,info->owner)) goto err ;
if (sv_before.cname.itype > CLASSIC)
if ((sv_before.cname.itype > CLASSIC && force > 1) || !exist)
if (!parse_service_deps(info,parsed_list,&sv_before,sv,nbsv,sasv,force)) goto err ;
freed:
......
......@@ -19,6 +19,7 @@
#include <oblibs/obgetopt.h>
#include <oblibs/error2.h>
#include <oblibs/stralist.h>
#include <oblibs/string.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
......@@ -34,7 +35,11 @@
#include <66/resolve.h>
#include <66/ssexec.h>
/** force == 1, only rewrite the service
* force == 2, rewrite the service and it dependencies*/
static unsigned int FORCE = 0 ;
/** rewrite configuration file */
static unsigned int CONF = 0 ;
static void cleanup(char const *dst)
{
......@@ -50,21 +55,46 @@ static void check_identifier(char const *name)
static void start_parser(stralloc *list,ssexec_t *info, unsigned int *nbsv)
{
unsigned int exist = 0 ;
stralloc sares = STRALLOC_ZERO ;
stralloc sasv = STRALLOC_ZERO ;
stralloc tmp = STRALLOC_ZERO ;
ss_resolve_t res = RESOLVE_ZERO ;
if (!parse_service_get_list(&tmp,list)) strerr_diefu1x(111,"get services list") ;
if (!stralloc_copy(list,&tmp)) strerr_diefu1sys(111,"copy stralloc") ;
tmp.len = 0 ;
size_t i = 0, len = list->len ;
if (!ss_resolve_pointo(&sares,info,SS_NOTYPE,SS_RESOLVE_SRC)) strerr_diefu1sys(111,"set revolve pointer to source") ;
for (;i < len; i += strlen(list->s + i) + 1)
{
char *svname = list->s+i ;
if (!parse_service_before(info,&tmp,svname,nbsv,&sasv,FORCE))
exist = 0 ;
sares.len = 0 ;
char *name = list->s+i ;
size_t namelen = strlen(name) ;
char svname[namelen + 1] ;
if (!basename(svname,name)) strerr_diefu2sys(111,"get basename of: ", name) ;
if (ss_resolve_check(sares.s,svname))
{
if (!ss_resolve_read(&res,sares.s,svname)) strerr_diefu2sys(111,"read resolve file of: ",svname) ;
if (res.disen)
{
exist = 1 ;
if (!FORCE)
{
VERBO1 strerr_warnw3x("Ignoring: ",svname," service: already enabled") ;
continue ;
}
}
}
if (!parse_service_before(info,&tmp,name,nbsv,&sasv,FORCE,exist))
strerr_diefu3x(111,"parse service file: ",svname,": or its dependencies") ;
}
stralloc_free(&sares) ;
stralloc_free(&sasv) ;
stralloc_free(&tmp) ;
ss_resolve_free(&res) ;
}
......@@ -72,6 +102,7 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
{
// be sure that the global var are set correctly
FORCE = 0 ;
CONF = 0 ;
int r ;
unsigned int nbsv, nlongrun, nclassic, start ;
......@@ -79,11 +110,8 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
stralloc home = STRALLOC_ZERO ;
stralloc workdir = STRALLOC_ZERO ;
stralloc sasrc = STRALLOC_ZERO ;
stralloc sares = STRALLOC_ZERO ;
genalloc tostart = GENALLOC_ZERO ; // type stralist
ss_resolve_t res = RESOLVE_ZERO ;
r = nbsv = nclassic = nlongrun = start = 0 ;
{
......@@ -91,12 +119,16 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
for (;;)
{
int opt = getopt_args(argc,argv, ">fS", &l) ;
int opt = getopt_args(argc,argv, ">ofFS", &l) ;
if (opt == -1) break ;
if (opt == -2) strerr_dief1x(110,"options must be set first") ;
switch (opt)
{
case 'f' : FORCE = 1 ; break ;
case 'f' : if (FORCE) exitusage(usage_enable) ;
FORCE = 1 ; break ;
case 'F' : if (FORCE) exitusage(usage_enable) ;
FORCE = 2 ; break ;
case 'o' : CONF = 1 ; break ;
case 'S' : start = 1 ; break ;
default : exitusage(usage_enable) ;
}
......@@ -106,25 +138,12 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
if (argc < 1) exitusage(usage_enable) ;
if (!ss_resolve_pointo(&sares,info,SS_NOTYPE,SS_RESOLVE_SRC)) strerr_diefu1sys(111,"set revolve pointer to source") ;
for(;*argv;argv++)
{
check_identifier(*argv) ;
if (ss_resolve_check(sares.s,*argv))
{
if (!ss_resolve_read(&res,sares.s,*argv)) strerr_diefu2sys(111,"read resolve file of: ",*argv) ;
if (res.disen && !FORCE)
{
VERBO1 strerr_warnw3x("Ignoring: ",*argv," service: already enabled") ;
continue ;
}
}
if (!ss_resolve_src_path(&sasrc,*argv,info)) strerr_diefu2x(111,"resolve source path of: ",*argv) ;
}
if (!sasrc.len) goto freed ;
start_parser(&sasrc,info,&nbsv) ;
if (!tree_copy(&workdir,info->tree.s,info->treename.s)) strerr_diefu1sys(111,"create tmp working directory") ;
......@@ -133,7 +152,7 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
{
sv_alltype_ref sv = &genalloc_s(sv_alltype,&gasv)[i] ;
char *name = keep.s + sv->cname.name ;
r = write_services(info,sv, workdir.s,FORCE) ;
r = write_services(info,sv, workdir.s,FORCE,CONF) ;
if (!r)
{
cleanup(workdir.s) ;
......@@ -213,15 +232,12 @@ int ssexec_enable(int argc, char const *const *argv,char const *const *envp,ssex
cleanup(workdir.s) ;
freed:
/** parser allocation*/
freed_parser() ;
/** inner allocation */
stralloc_free(&home) ;
stralloc_free(&workdir) ;
stralloc_free(&sasrc) ;
ss_resolve_free(&res) ;
stralloc_free(&sares) ;
for (unsigned int i = 0 ; i < genalloc_len(stralist,&tostart); i++)
VERBO1 strerr_warni2x("Enabled successfully: ", gaistr(&tostart,i)) ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment