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

split state function

parent 6d1badda
No related branches found
No related tags found
No related merge requests found
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
/*
* 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 ;
}
}
/*
* 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 ;
}
/*
* 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 ;
}
}
/*
* 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) ;
}
/*
* 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 ;
}
/*
* 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) ;
}
/*
* 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 ;
}
}
/*
* 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 ;
}
/*
* 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 ;
}
/*
* 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>
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