mirror of
https://github.com/eddyem/small_tel.git
synced 2026-03-24 10:40:56 +03:00
add permissive and prohibited signals
This commit is contained in:
@@ -5,7 +5,7 @@ LDFLAGS += -lusefull_macros
|
|||||||
SRCS := $(wildcard *.c)
|
SRCS := $(wildcard *.c)
|
||||||
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
|
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
|
||||||
OBJDIR := mk
|
OBJDIR := mk
|
||||||
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
|
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu23
|
||||||
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
|
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
|
||||||
DEPS := $(OBJS:.o=.d)
|
DEPS := $(OBJS:.o=.d)
|
||||||
TARGFILE := $(OBJDIR)/TARGET
|
TARGFILE := $(OBJDIR)/TARGET
|
||||||
|
|||||||
@@ -58,8 +58,18 @@ static sl_option_t cmdlnopts[] = {
|
|||||||
end_option
|
end_option
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// SIGUSR1 - FORBID observations
|
||||||
|
// SIGUSR2 - allow
|
||||||
void signals(int sig){
|
void signals(int sig){
|
||||||
if(sig){
|
if(sig){
|
||||||
|
if(sig == SIGUSR1){
|
||||||
|
forbid_observations(1);
|
||||||
|
return;
|
||||||
|
}else if(sig == SIGUSR2){
|
||||||
|
forbid_observations(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
signal(sig, SIG_IGN);
|
signal(sig, SIG_IGN);
|
||||||
DBG("Get signal %d, quit.\n", sig);
|
DBG("Get signal %d, quit.\n", sig);
|
||||||
LOGERR("Exit with status %d", sig);
|
LOGERR("Exit with status %d", sig);
|
||||||
@@ -93,6 +103,8 @@ int main(int argc, char **argv){
|
|||||||
signal(SIGQUIT, signals);
|
signal(SIGQUIT, signals);
|
||||||
signal(SIGTSTP, SIG_IGN);
|
signal(SIGTSTP, SIG_IGN);
|
||||||
signal(SIGHUP, signals);
|
signal(SIGHUP, signals);
|
||||||
|
signal(SIGUSR1, signals);
|
||||||
|
signal(SIGUSR2, signals);
|
||||||
runserver(G.isunix, G.node, G.maxclients);
|
runserver(G.isunix, G.node, G.maxclients);
|
||||||
LOGMSG("Ended");
|
LOGMSG("Ended");
|
||||||
DBG("Close");
|
DBG("Close");
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -44,7 +45,10 @@ typedef struct{
|
|||||||
|
|
||||||
static dome_t Dome = {0};
|
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
|
static sl_ringbuffer_t *rb = NULL; // incoming serial data
|
||||||
|
|
||||||
void stopserver(){
|
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;
|
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){
|
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);
|
pthread_mutex_lock(&Dome.mutex);
|
||||||
|
CHKALLOWED();
|
||||||
Dome.cmd = CMD_OPEN;
|
Dome.cmd = CMD_OPEN;
|
||||||
pthread_mutex_unlock(&Dome.mutex);
|
pthread_mutex_unlock(&Dome.mutex);
|
||||||
return RESULT_OK;
|
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){
|
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);
|
pthread_mutex_lock(&Dome.mutex);
|
||||||
|
CHKALLOWED();
|
||||||
Dome.cmd = CMD_STOP;
|
Dome.cmd = CMD_STOP;
|
||||||
pthread_mutex_unlock(&Dome.mutex);
|
pthread_mutex_unlock(&Dome.mutex);
|
||||||
return RESULT_OK;
|
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");
|
snprintf(buf+l, 255-l, "\n");
|
||||||
sl_sock_sendstrmessage(client, buf);
|
sl_sock_sendstrmessage(client, buf);
|
||||||
}
|
}
|
||||||
|
if(ForbidObservations) sl_sock_sendstrmessage(client, "FORBIDDEN\n");
|
||||||
return RESULT_SILENCE;
|
return RESULT_SILENCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,6 +312,7 @@ static int poll_device(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void runserver(int isunix, const char *node, int maxclients){
|
void runserver(int isunix, const char *node, int maxclients){
|
||||||
|
int forbidden = 0;
|
||||||
if(locksock) sl_sock_delete(&locksock);
|
if(locksock) sl_sock_delete(&locksock);
|
||||||
if(rb) sl_RB_delete(&rb);
|
if(rb) sl_RB_delete(&rb);
|
||||||
rb = sl_RB_new(BUFSIZ);
|
rb = sl_RB_new(BUFSIZ);
|
||||||
@@ -323,6 +333,14 @@ void runserver(int isunix, const char *node, int maxclients){
|
|||||||
sl_sock_defmsghandler(locksock, defhandler);
|
sl_sock_defmsghandler(locksock, defhandler);
|
||||||
double tgot = 0.;
|
double tgot = 0.;
|
||||||
while(locksock && locksock->connected){
|
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);
|
usleep(1000);
|
||||||
if(!locksock->rthread){
|
if(!locksock->rthread){
|
||||||
WARNX("Server handlers thread is dead");
|
WARNX("Server handlers thread is dead");
|
||||||
@@ -355,3 +373,14 @@ void runserver(int isunix, const char *node, int maxclients){
|
|||||||
}
|
}
|
||||||
stopserver();
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,3 +27,4 @@
|
|||||||
|
|
||||||
void runserver(int isunix, const char *node, int maxclients);
|
void runserver(int isunix, const char *node, int maxclients);
|
||||||
void stopserver();
|
void stopserver();
|
||||||
|
void forbid_observations(int forbid);
|
||||||
|
|||||||
Reference in New Issue
Block a user