diff --git a/src/lib66/state/deps-lib/deps b/src/lib66/state/deps-lib/deps index ced15d0db94e50f4220cb2c281b998b1edee9135..d897d6f57fbc594525d11fdf5b4f2ee8b4789934 100644 --- a/src/lib66/state/deps-lib/deps +++ b/src/lib66/state/deps-lib/deps @@ -1,7 +1,11 @@ -state.o --ls6rc --ls6 +state_check.o +state_check_flags.o +state_pack.o +state_read.o +state_rmfile.o +state_setflag.o +state_unpack.o +state_write.o -loblibs --lexecline -lskarnet diff --git a/src/lib66/state/state.c b/src/lib66/state/state.c deleted file mode 100644 index 2d89384da362bdda87e04ed9b40f5d64bf334420..0000000000000000000000000000000000000000 --- a/src/lib66/state/state.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * state.c - * - * Copyright (c) 2018-2021 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 <sys/stat.h> -#include <stdlib.h>//realpath -#include <stdio.h> - -#include <oblibs/types.h> -#include <oblibs/log.h> - -#include <skalibs/posixplz.h> -#include <skalibs/uint32.h> -#include <skalibs/uint64.h> -#include <skalibs/djbunix.h> - -#include <66/state.h> - -void state_rmfile(char const *src,char const *name) -{ - log_flow() ; - - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - - unlink_void(tmp) ; -} - -void state_pack(char *pack, ss_state_t *sta) -{ - log_flow() ; - - uint32_pack_big(pack,sta->reload) ; - uint32_pack_big(pack+4,sta->init) ; - uint32_pack_big(pack+8,sta->unsupervise) ; - uint32_pack_big(pack+12,sta->state) ; - uint64_pack_big(pack+16,sta->pid) ; -} - -void state_unpack(char *pack,ss_state_t *sta) -{ - log_flow() ; - - uint32_t reload ; - uint32_t init ; - uint32_t unsupervise ; - uint32_t state ; - uint64_t pid ; - - uint32_unpack_big(pack,&reload) ; - sta->reload = reload ; - uint32_unpack_big(pack+4,&init) ; - sta->init = init ; - uint32_unpack_big(pack+8,&unsupervise) ; - sta->unsupervise = unsupervise ; - uint32_unpack_big(pack+12,&state) ; - sta->state = state ; - uint64_unpack_big(pack+16,&pid) ; - sta->pid = pid ; -} - -int state_write(ss_state_t *sta, char const *dst, char const *name) -{ - log_flow() ; - - char pack[SS_STATE_SIZE] ; - size_t dstlen = strlen(dst) ; - size_t namelen = strlen(name) ; - - char tmp[dstlen + 1 + namelen + 1] ; - memcpy(tmp,dst,dstlen) ; - tmp[dstlen] = '/' ; - memcpy(tmp + dstlen + 1, name, namelen) ; - tmp[dstlen + 1 + namelen] = 0 ; - - state_pack(pack,sta) ; - if (!openwritenclose_unsafe(tmp,pack,SS_STATE_SIZE)) return 0 ; - - return 1 ; -} - -int state_read(ss_state_t *sta, char const *src, char const *name) -{ - log_flow() ; - - char pack[SS_STATE_SIZE] ; - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - - if (openreadnclose(tmp, pack, SS_STATE_SIZE) < SS_STATE_SIZE) return 0 ; - state_unpack(pack, sta) ; - - return 1 ; -} - -int state_check(char const *src, char const *name) -{ - log_flow() ; - - int r ; - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - r = scan_mode(tmp,S_IFREG) ; - if (r <= 0) return 0 ; - return 1 ; -} - -void state_setflag(ss_state_t *sta,int flags,int flags_val) -{ - log_flow() ; - - switch (flags) - { - case SS_FLAGS_RELOAD: sta->reload = flags_val ; break ; - case SS_FLAGS_INIT: sta->init = flags_val ; break ; - case SS_FLAGS_UNSUPERVISE: sta->unsupervise = flags_val ; break ; - case SS_FLAGS_STATE: sta->state = flags_val ; break ; - case SS_FLAGS_PID: sta->pid = (uint32_t)flags_val ; break ; - default: return ; - } -} - -int state_check_flags(char const *src, char const *name,int flags) -{ - log_flow() ; - - /** unitialized at all, all flags == 0. - * Return -1 to make a distinction between - * file absent and flag == 0. */ - if (!state_check(src,name)) - return -1 ; - - ss_state_t sta = STATE_ZERO ; - - if (!state_read(&sta,src,name)) - // should not happen - return -1 ; - - switch (flags) - { - case SS_FLAGS_RELOAD: return sta.reload ; - case SS_FLAGS_INIT: return sta.init ; - case SS_FLAGS_UNSUPERVISE: return sta.unsupervise ; - case SS_FLAGS_STATE: return sta.state ; - case SS_FLAGS_PID: return sta.pid ; - default: - // should never happen - return -1 ; - } - -} diff --git a/src/lib66/state/state_check.c b/src/lib66/state/state_check.c new file mode 100644 index 0000000000000000000000000000000000000000..8f2cd210a6835ace1677523a2b41bf1c727ff385 --- /dev/null +++ b/src/lib66/state/state_check.c @@ -0,0 +1,38 @@ +/* + * state_check.c + * + * Copyright (c) 2018-2021 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 <sys/stat.h> + +#include <oblibs/log.h> +#include <oblibs/types.h> + +#include <66/state.h> + +int state_check(char const *src, char const *name) +{ + log_flow() ; + + int r ; + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + r = scan_mode(tmp,S_IFREG) ; + if (r <= 0) return 0 ; + return 1 ; +} diff --git a/src/lib66/state/state_check_flags.c b/src/lib66/state/state_check_flags.c new file mode 100644 index 0000000000000000000000000000000000000000..1daf63912ea31677c31f18ebfc8ad00bcc5d4124 --- /dev/null +++ b/src/lib66/state/state_check_flags.c @@ -0,0 +1,47 @@ +/* + * state_check_flags.c + * + * Copyright (c) 2018-2021 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 <oblibs/log.h> + +#include <66/state.h> + +int state_check_flags(char const *src, char const *name,int flags) +{ + log_flow() ; + + /** unitialized at all, all flags == 0. + * Return -1 to make a distinction between + * file absent and flag == 0. */ + if (!state_check(src,name)) + return -1 ; + + ss_state_t sta = STATE_ZERO ; + + if (!state_read(&sta,src,name)) + // should not happen + return -1 ; + + switch (flags) + { + case SS_FLAGS_RELOAD: return sta.reload ; + case SS_FLAGS_INIT: return sta.init ; + case SS_FLAGS_UNSUPERVISE: return sta.unsupervise ; + case SS_FLAGS_STATE: return sta.state ; + case SS_FLAGS_PID: return sta.pid ; + default: + // should never happen + return -1 ; + } + +} diff --git a/src/lib66/state/state_pack.c b/src/lib66/state/state_pack.c new file mode 100644 index 0000000000000000000000000000000000000000..6479adfc5de739e413e7ced9347a9993f5cb137a --- /dev/null +++ b/src/lib66/state/state_pack.c @@ -0,0 +1,30 @@ +/* + * state_pack.c + * + * Copyright (c) 2018-2021 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 <oblibs/log.h> + +#include <skalibs/uint32.h> + +#include <66/state.h> + +void state_pack(char *pack, ss_state_t *sta) +{ + log_flow() ; + + uint32_pack_big(pack,sta->reload) ; + uint32_pack_big(pack+4,sta->init) ; + uint32_pack_big(pack+8,sta->unsupervise) ; + uint32_pack_big(pack+12,sta->state) ; + uint64_pack_big(pack+16,sta->pid) ; +} diff --git a/src/lib66/state/state_read.c b/src/lib66/state/state_read.c new file mode 100644 index 0000000000000000000000000000000000000000..b2bb622ad30b3edb6747492fc917c41855ef3c52 --- /dev/null +++ b/src/lib66/state/state_read.c @@ -0,0 +1,41 @@ +/* + * state_read.c + * + * Copyright (c) 2018-2021 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 <oblibs/log.h> + +#include <skalibs/djbunix.h> + +#include <66/state.h> + +int state_read(ss_state_t *sta, char const *src, char const *name) +{ + log_flow() ; + + char pack[SS_STATE_SIZE] ; + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + + if (openreadnclose(tmp, pack, SS_STATE_SIZE) < SS_STATE_SIZE) return 0 ; + state_unpack(pack, sta) ; + + return 1 ; +} diff --git a/src/lib66/state/state_rmfile.c b/src/lib66/state/state_rmfile.c new file mode 100644 index 0000000000000000000000000000000000000000..84f860b9d9f3db7ef3d42ce19cfbdea472e81b57 --- /dev/null +++ b/src/lib66/state/state_rmfile.c @@ -0,0 +1,37 @@ +/* + * state_rmfile.c + * + * Copyright (c) 2018-2021 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 <oblibs/log.h> + +#include <skalibs/posixplz.h> + +#include <66/state.h> + +void state_rmfile(char const *src,char const *name) +{ + log_flow() ; + + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + + unlink_void(tmp) ; +} diff --git a/src/lib66/state/state_setflag.c b/src/lib66/state/state_setflag.c new file mode 100644 index 0000000000000000000000000000000000000000..946668ce5948d0fd260a7a2322984f1561b61c9a --- /dev/null +++ b/src/lib66/state/state_setflag.c @@ -0,0 +1,34 @@ +/* + * state_setflag.c + * + * Copyright (c) 2018-2021 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 <stdint.h> + +#include <oblibs/log.h> + +#include <66/state.h> + +void state_setflag(ss_state_t *sta,int flags,int flags_val) +{ + log_flow() ; + + switch (flags) + { + case SS_FLAGS_RELOAD: sta->reload = flags_val ; break ; + case SS_FLAGS_INIT: sta->init = flags_val ; break ; + case SS_FLAGS_UNSUPERVISE: sta->unsupervise = flags_val ; break ; + case SS_FLAGS_STATE: sta->state = flags_val ; break ; + case SS_FLAGS_PID: sta->pid = (uint32_t)flags_val ; break ; + default: return ; + } +} diff --git a/src/lib66/state/state_unpack.c b/src/lib66/state/state_unpack.c new file mode 100644 index 0000000000000000000000000000000000000000..3d385f8c206992ea2e50792afff234546eb9957c --- /dev/null +++ b/src/lib66/state/state_unpack.c @@ -0,0 +1,43 @@ +/* + * state_unpack.c + * + * Copyright (c) 2018-2021 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 <stdint.h> + +#include <oblibs/log.h> + +#include <skalibs/uint32.h> + +#include <66/state.h> + +void state_unpack(char *pack,ss_state_t *sta) +{ + log_flow() ; + + uint32_t reload ; + uint32_t init ; + uint32_t unsupervise ; + uint32_t state ; + uint64_t pid ; + + uint32_unpack_big(pack,&reload) ; + sta->reload = reload ; + uint32_unpack_big(pack+4,&init) ; + sta->init = init ; + uint32_unpack_big(pack+8,&unsupervise) ; + sta->unsupervise = unsupervise ; + uint32_unpack_big(pack+12,&state) ; + sta->state = state ; + uint64_unpack_big(pack+16,&pid) ; + sta->pid = pid ; +} diff --git a/src/lib66/state/state_write.c b/src/lib66/state/state_write.c new file mode 100644 index 0000000000000000000000000000000000000000..c5a5de615ea8686084a828383d48cd91ff196ece --- /dev/null +++ b/src/lib66/state/state_write.c @@ -0,0 +1,41 @@ +/* + * state_write.c + * + * Copyright (c) 2018-2021 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 <oblibs/log.h> + +#include <skalibs/djbunix.h> + +#include <66/state.h> + +int state_write(ss_state_t *sta, char const *dst, char const *name) +{ + log_flow() ; + + char pack[SS_STATE_SIZE] ; + size_t dstlen = strlen(dst) ; + size_t namelen = strlen(name) ; + + char tmp[dstlen + 1 + namelen + 1] ; + memcpy(tmp,dst,dstlen) ; + tmp[dstlen] = '/' ; + memcpy(tmp + dstlen + 1, name, namelen) ; + tmp[dstlen + 1 + namelen] = 0 ; + + state_pack(pack,sta) ; + if (!openwritenclose_unsafe(tmp,pack,SS_STATE_SIZE)) return 0 ; + + return 1 ; +} diff --git a/src/lib66/state/template.c b/src/lib66/state/template.c new file mode 100644 index 0000000000000000000000000000000000000000..b1130bedb7862441667de227469e8332bc7bc4e5 --- /dev/null +++ b/src/lib66/state/template.c @@ -0,0 +1,19 @@ +/* + * service.c + * + * Copyright (c) 2018-2021 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 <66/state.h>