add permissive and prohibited signals

This commit is contained in:
Edward Emelianov
2026-03-22 22:30:55 +03:00
parent 6c36b0acc9
commit ddce8437ce
4 changed files with 44 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ LDFLAGS += -lusefull_macros
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu23
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
TARGFILE := $(OBJDIR)/TARGET

View File

@@ -58,8 +58,18 @@ static sl_option_t cmdlnopts[] = {
end_option
};
// SIGUSR1 - FORBID observations
// SIGUSR2 - allow
void signals(int sig){
if(sig){
if(sig == SIGUSR1){
forbid_observations(1);
return;
}else if(sig == SIGUSR2){
forbid_observations(0);
return;
}
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
LOGERR("Exit with status %d", sig);
@@ -93,6 +103,8 @@ int main(int argc, char **argv){
signal(SIGQUIT, signals);
signal(SIGTSTP, SIG_IGN);
signal(SIGHUP, signals);
signal(SIGUSR1, signals);
signal(SIGUSR2, signals);
runserver(G.isunix, G.node, G.maxclients);
LOGMSG("Ended");
DBG("Close");

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdatomic.h>
#include <inttypes.h>
#include <pthread.h>
#include <string.h>
@@ -44,7 +45,10 @@ typedef struct{
static dome_t Dome = {0};
static sl_sock_t *locksock = NULL;
// external signal "deny/allow"
static atomic_bool ForbidObservations = 0; // ==1 if all is forbidden -> close dome and not allow to open
static sl_sock_t *locksock = NULL; // local server socket
static sl_ringbuffer_t *rb = NULL; // incoming serial data
void stopserver(){
@@ -84,8 +88,11 @@ static sl_sock_hresult_e dtimeh(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U
return RESULT_SILENCE;
}
#define CHKALLOWED() do{if(ForbidObservations || Dome.errcode){pthread_mutex_unlock(&Dome.mutex); return RESULT_FAIL;}}while(0)
static sl_sock_hresult_e openh(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
pthread_mutex_lock(&Dome.mutex);
CHKALLOWED();
Dome.cmd = CMD_OPEN;
pthread_mutex_unlock(&Dome.mutex);
return RESULT_OK;
@@ -100,6 +107,7 @@ static sl_sock_hresult_e closeh(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item
static sl_sock_hresult_e stoph(_U_ sl_sock_t *client, _U_ sl_sock_hitem_t *item, _U_ const char *req){
pthread_mutex_lock(&Dome.mutex);
CHKALLOWED();
Dome.cmd = CMD_STOP;
pthread_mutex_unlock(&Dome.mutex);
return RESULT_OK;
@@ -138,6 +146,7 @@ static sl_sock_hresult_e statush(sl_sock_t *client, _U_ sl_sock_hitem_t *item, _
snprintf(buf+l, 255-l, "\n");
sl_sock_sendstrmessage(client, buf);
}
if(ForbidObservations) sl_sock_sendstrmessage(client, "FORBIDDEN\n");
return RESULT_SILENCE;
}
@@ -303,6 +312,7 @@ static int poll_device(){
}
void runserver(int isunix, const char *node, int maxclients){
int forbidden = 0;
if(locksock) sl_sock_delete(&locksock);
if(rb) sl_RB_delete(&rb);
rb = sl_RB_new(BUFSIZ);
@@ -323,6 +333,14 @@ void runserver(int isunix, const char *node, int maxclients){
sl_sock_defmsghandler(locksock, defhandler);
double tgot = 0.;
while(locksock && locksock->connected){
if(ForbidObservations){
if(!forbidden){
if(0 == write_cmd(TXT_CLOSEDOME)) forbidden = 1;
pthread_mutex_lock(&Dome.mutex);
Dome.cmd = CMD_NONE;
pthread_mutex_unlock(&Dome.mutex);
}
}else forbidden = 0;
usleep(1000);
if(!locksock->rthread){
WARNX("Server handlers thread is dead");
@@ -355,3 +373,14 @@ void runserver(int isunix, const char *node, int maxclients){
}
stopserver();
}
void forbid_observations(int forbid){
if(forbid){
ForbidObservations = true;
LOGWARN("Got forbidden signal");
}else{
ForbidObservations = false;
LOGWARN("Got allowed signal");
}
DBG("Change ForbidObservations=%d", forbid);
}

View File

@@ -27,3 +27,4 @@
void runserver(int isunix, const char *node, int maxclients);
void stopserver();
void forbid_observations(int forbid);