mirror of
https://github.com/eddyem/bta-meteo-logger.git
synced 2025-12-06 02:35:19 +03:00
check bug @x86_64
This commit is contained in:
parent
80154b79e8
commit
288cfcb2b9
9
C/bta_pos/Makefile
Normal file
9
C/bta_pos/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
CC = gcc
|
||||
LDFLAGS = -lcrypt
|
||||
|
||||
all : bta_pos
|
||||
|
||||
bta_pos : bta_pos.c bta_shdata.c
|
||||
$(CC) bta_pos.c bta_shdata.c $(LDFLAGS) -o bta_pos
|
||||
clean:
|
||||
/bin/rm -f *.o *~
|
||||
@ -1,6 +1,6 @@
|
||||
#include "bta_shdata.h"
|
||||
|
||||
inline double sec2deg(double angle){
|
||||
static inline double sec2deg(double angle){
|
||||
return angle / 3600;
|
||||
}
|
||||
|
||||
|
||||
356
C/bta_pos/bta_shdata.c
Normal file
356
C/bta_pos/bta_shdata.c
Normal file
@ -0,0 +1,356 @@
|
||||
// (C) V.S. Shergin, SAO RAS
|
||||
#include <err.h>
|
||||
#include "bta_shdata.h"
|
||||
|
||||
#pragma pack(push, 4)
|
||||
// Main command channel (level 5)
|
||||
struct CMD_Queue mcmd = {{"Mcmd"}, 0200,0,-1,0};
|
||||
// Operator command channel (level 4)
|
||||
struct CMD_Queue ocmd = {{"Ocmd"}, 0200,0,-1,0};
|
||||
// User command channel (level 2/3)
|
||||
struct CMD_Queue ucmd = {{"Ucmd"}, 0200,0,-1,0};
|
||||
|
||||
#define MSGLEN (80)
|
||||
static char msg[MSGLEN];
|
||||
#define WARN(...) warn(__VA_ARGS__)
|
||||
#define PERR(...) do{snprintf(msg, MSGLEN, __VA_ARGS__); perror(msg);} while(0)
|
||||
#ifdef EBUG
|
||||
#define FNAME() fprintf(stderr, "\n%s (%s, line %d)\n", __func__, __FILE__, __LINE__)
|
||||
#define DBG(...) do{fprintf(stderr, "%s (%s, line %d): ", __func__, __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fprintf(stderr, "\n");} while(0)
|
||||
#else
|
||||
#define FNAME() do{}while(0)
|
||||
#define DBG(...) do{}while(0)
|
||||
#endif //EBUG
|
||||
|
||||
|
||||
#ifndef BTA_MODULE
|
||||
volatile struct BTA_Data *sdt;
|
||||
volatile struct BTA_Local *sdtl;
|
||||
|
||||
volatile struct SHM_Block sdat = {
|
||||
{"Sdat"},
|
||||
sizeof(struct BTA_Data),
|
||||
2048,0444,
|
||||
SHM_RDONLY,
|
||||
bta_data_init,
|
||||
bta_data_check,
|
||||
bta_data_close,
|
||||
ClientSide,-1,NULL
|
||||
};
|
||||
|
||||
int snd_id = -1; // client sender ID
|
||||
int cmd_src_pid = 0; // next command source PID
|
||||
uint32_t cmd_src_ip = 0;// next command source IP
|
||||
|
||||
/**
|
||||
* Init data
|
||||
*/
|
||||
void bta_data_init() {
|
||||
sdt = (struct BTA_Data *)sdat.addr;
|
||||
sdtl = (struct BTA_Local *)(sdat.addr+sizeof(struct BTA_Data));
|
||||
if(sdat.side == ClientSide) {
|
||||
if(sdt->magic != sdat.key.code) {
|
||||
WARN("Wrong shared data (maybe server turned off)");
|
||||
}
|
||||
if(sdt->version == 0) {
|
||||
WARN("Null shared data version (maybe server turned off)");
|
||||
}
|
||||
else if(sdt->version != BTA_Data_Ver) {
|
||||
WARN("Wrong shared data version: I'am - %d, but server - %d ...",
|
||||
BTA_Data_Ver, sdt->version );
|
||||
}
|
||||
if(sdt->size != sdat.size) {
|
||||
if(sdt->size > sdat.size) {
|
||||
WARN("Wrong shared area size: I needs - %d, but server - %d ...",
|
||||
sdat.size, sdt->size );
|
||||
} else {
|
||||
WARN("Attention! Too little shared data structure!");
|
||||
WARN("I needs - %d, but server gives only %d ...",
|
||||
sdat.size, sdt->size );
|
||||
WARN("May be server's version too old!?");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* ServerSide */
|
||||
if(sdt->magic == sdat.key.code &&
|
||||
sdt->version == BTA_Data_Ver &&
|
||||
sdt->size == sdat.size)
|
||||
return;
|
||||
memset(sdat.addr, 0, sdat.maxsize);
|
||||
sdt->magic = sdat.key.code;
|
||||
sdt->version = BTA_Data_Ver;
|
||||
sdt->size = sdat.size;
|
||||
Tel_Hardware = Hard_On;
|
||||
Pos_Corr = PC_On;
|
||||
TrkOk_Mode = UseDiffVel | UseDiffAZ ;
|
||||
inp_B = 591.;
|
||||
Pressure = 595.;
|
||||
PEP_code_A = 0x002aaa;
|
||||
PEP_code_Z = 0x002aaa;
|
||||
PEP_code_P = 0x002aaa;
|
||||
PEP_code_F = 0x002aaa;
|
||||
PEP_code_D = 0x002aaa;
|
||||
DomeSEW_N = 1;
|
||||
}
|
||||
|
||||
int bta_data_check() {
|
||||
return( (sdt->magic == sdat.key.code) && (sdt->version == BTA_Data_Ver) );
|
||||
}
|
||||
|
||||
void bta_data_close() {
|
||||
if(sdat.side == ServerSide) {
|
||||
sdt->magic = 0;
|
||||
sdt->version = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate shared memory segment
|
||||
*/
|
||||
int get_shm_block(volatile struct SHM_Block *sb, int server) {
|
||||
int getsize = (server)? sb->maxsize : sb->size;
|
||||
// first try to find existing one
|
||||
sb->id = shmget(sb->key.code, getsize, sb->mode);
|
||||
if(sb->id < 0 && errno == ENOENT && server){
|
||||
// if no - try to create a new one
|
||||
int cresize = sb->maxsize;
|
||||
if(sb->size > cresize){
|
||||
WARN("Wrong shm maxsize(%d) < realsize(%d)",sb->maxsize,sb->size);
|
||||
cresize = sb->size;
|
||||
}
|
||||
sb->id = shmget(sb->key.code, cresize, IPC_CREAT|IPC_EXCL|sb->mode);
|
||||
}
|
||||
if(sb->id < 0){
|
||||
if(server)
|
||||
PERR("Can't create shared memory segment '%s'",sb->key.name);
|
||||
else
|
||||
PERR("Can't find shared segment '%s' (maybe no server process) ",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
// attach it to our memory space
|
||||
sb->addr = (unsigned char *) shmat(sb->id, NULL, sb->atflag);
|
||||
if((long)sb->addr == -1){
|
||||
PERR("Can't attach shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
if(server && (shmctl(sb->id, SHM_LOCK, NULL) < 0)){
|
||||
PERR("Can't prevents swapping of shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
DBG("Create & attach shared memory segment '%s' %dbytes", sb->key.name, sb->size);
|
||||
sb->side = server;
|
||||
if(sb->init != NULL)
|
||||
sb->init();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int close_shm_block(volatile struct SHM_Block *sb){
|
||||
int ret;
|
||||
if(sb->close != NULL)
|
||||
sb->close();
|
||||
if(sb->side == ServerSide) {
|
||||
// ret = shmctl(sb->id, SHM_UNLOCK, NULL);
|
||||
ret = shmctl(sb->id, IPC_RMID, NULL);
|
||||
}
|
||||
ret = shmdt (sb->addr);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create|Find command queue
|
||||
*/
|
||||
void get_cmd_queue(struct CMD_Queue *cq, int server){
|
||||
if (!server && cq->id >= 0) { //if already in use set current
|
||||
snd_id = cq->id;
|
||||
return;
|
||||
}
|
||||
// first try to find existing one
|
||||
cq->id = msgget(cq->key.code, cq->mode);
|
||||
// if no - try to create a new one
|
||||
if(cq->id<0 && errno == ENOENT && server)
|
||||
cq->id = msgget(cq->key.code, IPC_CREAT|IPC_EXCL|cq->mode);
|
||||
if(cq->id<0){
|
||||
if(server)
|
||||
PERR("Can't create comand queue '%s'",cq->key.name);
|
||||
else
|
||||
PERR("Can't find comand queue '%s' (maybe no server process) ",cq->key.name);
|
||||
return;
|
||||
}
|
||||
cq->side = server;
|
||||
if(server){
|
||||
char buf[120]; /* выбросить все команды из очереди */
|
||||
while(msgrcv(cq->id, (struct msgbuf *)buf, 112, 0, IPC_NOWAIT) > 0);
|
||||
}else
|
||||
snd_id = cq->id;
|
||||
cq->acckey = 0;
|
||||
}
|
||||
|
||||
#endif // BTA_MODULE
|
||||
|
||||
|
||||
int check_shm_block(volatile struct SHM_Block *sb) {
|
||||
if(sb->check)
|
||||
return(sb->check());
|
||||
else return(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set access key in current channel
|
||||
*/
|
||||
void set_acckey(uint32_t newkey){
|
||||
if(snd_id < 0) return;
|
||||
if(ucmd.id == snd_id) ucmd.acckey = newkey;
|
||||
else if(ocmd.id == snd_id) ocmd.acckey = newkey;
|
||||
else if(mcmd.id == snd_id) mcmd.acckey = newkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup source data for one following command if default values
|
||||
* (IP == 0 - local, PID = current) not suits
|
||||
*/
|
||||
void set_cmd_src(uint32_t ip, int pid) {
|
||||
cmd_src_pid = pid;
|
||||
cmd_src_ip = ip;
|
||||
}
|
||||
|
||||
#pragma pack(push, 4)
|
||||
/**
|
||||
* Send client commands to server
|
||||
*/
|
||||
void send_cmd(int cmd_code, char *buf, int size) {
|
||||
struct my_msgbuf mbuf;
|
||||
if(snd_id < 0) return;
|
||||
if(size > 100) size = 100;
|
||||
if(cmd_code > 0)
|
||||
mbuf.mtype = cmd_code;
|
||||
else
|
||||
return;
|
||||
if(ucmd.id == snd_id) mbuf.acckey = ucmd.acckey;
|
||||
else if(ocmd.id == snd_id) mbuf.acckey = ocmd.acckey;
|
||||
else if(mcmd.id == snd_id) mbuf.acckey = mcmd.acckey;
|
||||
|
||||
mbuf.src_pid = cmd_src_pid ? cmd_src_pid : getpid();
|
||||
mbuf.src_ip = cmd_src_ip;
|
||||
cmd_src_pid = cmd_src_ip = 0;
|
||||
|
||||
if(size > 0)
|
||||
memcpy(mbuf.mtext, buf, size);
|
||||
else {
|
||||
mbuf.mtext[0] = 0;
|
||||
size = 1;
|
||||
}
|
||||
msgsnd(snd_id, (struct msgbuf *)&mbuf, size+12, IPC_NOWAIT);
|
||||
}
|
||||
|
||||
void send_cmd_noarg(int cmd_code) {
|
||||
send_cmd(cmd_code, NULL, 0);
|
||||
}
|
||||
void send_cmd_str(int cmd_code, char *arg) {
|
||||
send_cmd(cmd_code, arg, strlen(arg)+1);
|
||||
}
|
||||
void send_cmd_i1(int cmd_code, int32_t arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i2(int cmd_code, int32_t arg1, int32_t arg2) {
|
||||
int32_t ibuf[2];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)ibuf, 2*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i3(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3) {
|
||||
int32_t ibuf[3];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
send_cmd(cmd_code, (char *)ibuf, 3*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i4(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4) {
|
||||
int32_t ibuf[4];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
ibuf[3] = arg4;
|
||||
send_cmd(cmd_code, (char *)ibuf, 4*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_d1(int32_t cmd_code, double arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(double));
|
||||
}
|
||||
void send_cmd_d2(int cmd_code, double arg1, double arg2) {
|
||||
double dbuf[2];
|
||||
dbuf[0] = arg1;
|
||||
dbuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)dbuf, 2*sizeof(double));
|
||||
}
|
||||
void send_cmd_i1d1(int cmd_code, int32_t arg1, double arg2) {
|
||||
struct {
|
||||
int32_t ival;
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival = arg1;
|
||||
buf.dval = arg2;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i2d1(int cmd_code, int32_t arg1, int32_t arg2, double arg3) {
|
||||
struct {
|
||||
int32_t ival[2];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.dval = arg3;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i3d1(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, double arg4) {
|
||||
struct {
|
||||
int32_t ival[3];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.ival[2] = arg3;
|
||||
buf.dval = arg4;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
|
||||
void encode_lev_passwd(char *passwd, int nlev, uint32_t *keylev, uint32_t *codlev){
|
||||
char salt[4];
|
||||
char *encr;
|
||||
union {
|
||||
uint32_t ui;
|
||||
char c[4];
|
||||
} key, cod;
|
||||
sprintf(salt,"L%1d",nlev);
|
||||
encr = (char *)crypt(passwd, salt);
|
||||
cod.c[0] = encr[2];
|
||||
key.c[0] = encr[3];
|
||||
cod.c[1] = encr[4];
|
||||
key.c[1] = encr[5];
|
||||
cod.c[2] = encr[6];
|
||||
key.c[2] = encr[7];
|
||||
cod.c[3] = encr[8];
|
||||
key.c[3] = encr[9];
|
||||
*keylev = key.ui;
|
||||
*codlev = cod.ui;
|
||||
}
|
||||
|
||||
int find_lev_passwd(char *passwd, uint32_t *keylev, uint32_t *codlev){
|
||||
int nlev;
|
||||
for(nlev = 5; nlev > 0; --nlev){
|
||||
encode_lev_passwd(passwd, nlev, keylev, codlev);
|
||||
if(*codlev == code_Lev(nlev)) break;
|
||||
}
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
int check_lev_passwd(char *passwd){
|
||||
uint32_t keylev,codlev;
|
||||
int nlev;
|
||||
nlev = find_lev_passwd(passwd, &keylev, &codlev);
|
||||
if(nlev > 0) set_acckey(keylev);
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
#pragma pack(pop)
|
||||
File diff suppressed because it is too large
Load Diff
1158
C/bta_shdata.h
1158
C/bta_shdata.h
File diff suppressed because it is too large
Load Diff
1141
C/bta_shdata.h.old
1141
C/bta_shdata.h.old
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -141,7 +141,7 @@ void find_starting_pos(){ //
|
||||
cache_fd = open("./cache", O_RDONLY);
|
||||
}
|
||||
if(cache_fd < 0){
|
||||
WARN(_L(_s_Cant_open_cache_));
|
||||
WARN("%s", _L(_s_Cant_open_cache_));
|
||||
exit(1);
|
||||
}
|
||||
while(read(cache_fd, &cache, sizeof(cache)) > 0){
|
||||
@ -348,7 +348,7 @@ int main(int argc, char **argv){
|
||||
out_fd = open("./out", O_RDONLY);
|
||||
}
|
||||
if(out_fd < 0){
|
||||
WARN(_L(_s_Cant_open_data_));
|
||||
WARN("%s", _L(_s_Cant_open_data_));
|
||||
return(1);
|
||||
}
|
||||
if(lookout) print_dates();
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
CC = gcc
|
||||
LDFLAGS = -lcrypt
|
||||
CPPFLAGS =
|
||||
all : daemon tempmon tempmon_cli clean
|
||||
daemon : daemon.o
|
||||
$(CC) $(LDFLAGS) daemon.c $(CPPFLAGS) -o daemon
|
||||
tempmon : client.o
|
||||
$(CC) client.c $(CPPFLAGS) -o tempmon
|
||||
tempmon_cli : client_cli.o
|
||||
$(CC) client_cli.c $(CPPFLAGS) -o tempmon_cli
|
||||
LDFLAGS = -lcrypt -lm
|
||||
all : daemon tempmon tempmon_cli
|
||||
daemon : daemon.c bta_shdata.c
|
||||
$(CC) $(LDFLAGS) daemon.c bta_shdata.c $(CFLAGS) -o daemon
|
||||
tempmon : client.c
|
||||
$(CC) client.c $(CFLAGS) -o tempmon
|
||||
tempmon_cli : client_cli.c
|
||||
$(CC) client_cli.c $(CFLAGS) -o tempmon_cli
|
||||
|
||||
clean:
|
||||
/bin/rm -f *.o *~
|
||||
|
||||
|
||||
356
C/daemon/bta_shdata.c
Normal file
356
C/daemon/bta_shdata.c
Normal file
@ -0,0 +1,356 @@
|
||||
// (C) V.S. Shergin, SAO RAS
|
||||
#include <err.h>
|
||||
#include "bta_shdata.h"
|
||||
|
||||
#pragma pack(push, 4)
|
||||
// Main command channel (level 5)
|
||||
struct CMD_Queue mcmd = {{"Mcmd"}, 0200,0,-1,0};
|
||||
// Operator command channel (level 4)
|
||||
struct CMD_Queue ocmd = {{"Ocmd"}, 0200,0,-1,0};
|
||||
// User command channel (level 2/3)
|
||||
struct CMD_Queue ucmd = {{"Ucmd"}, 0200,0,-1,0};
|
||||
|
||||
#define MSGLEN (80)
|
||||
static char msg[MSGLEN];
|
||||
#define WARN(...) warn(__VA_ARGS__)
|
||||
#define PERR(...) do{snprintf(msg, MSGLEN, __VA_ARGS__); perror(msg);} while(0)
|
||||
#ifdef EBUG
|
||||
#define FNAME() fprintf(stderr, "\n%s (%s, line %d)\n", __func__, __FILE__, __LINE__)
|
||||
#define DBG(...) do{fprintf(stderr, "%s (%s, line %d): ", __func__, __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fprintf(stderr, "\n");} while(0)
|
||||
#else
|
||||
#define FNAME() do{}while(0)
|
||||
#define DBG(...) do{}while(0)
|
||||
#endif //EBUG
|
||||
|
||||
|
||||
#ifndef BTA_MODULE
|
||||
volatile struct BTA_Data *sdt;
|
||||
volatile struct BTA_Local *sdtl;
|
||||
|
||||
volatile struct SHM_Block sdat = {
|
||||
{"Sdat"},
|
||||
sizeof(struct BTA_Data),
|
||||
2048,0444,
|
||||
SHM_RDONLY,
|
||||
bta_data_init,
|
||||
bta_data_check,
|
||||
bta_data_close,
|
||||
ClientSide,-1,NULL
|
||||
};
|
||||
|
||||
int snd_id = -1; // client sender ID
|
||||
int cmd_src_pid = 0; // next command source PID
|
||||
uint32_t cmd_src_ip = 0;// next command source IP
|
||||
|
||||
/**
|
||||
* Init data
|
||||
*/
|
||||
void bta_data_init() {
|
||||
sdt = (struct BTA_Data *)sdat.addr;
|
||||
sdtl = (struct BTA_Local *)(sdat.addr+sizeof(struct BTA_Data));
|
||||
if(sdat.side == ClientSide) {
|
||||
if(sdt->magic != sdat.key.code) {
|
||||
WARN("Wrong shared data (maybe server turned off)");
|
||||
}
|
||||
if(sdt->version == 0) {
|
||||
WARN("Null shared data version (maybe server turned off)");
|
||||
}
|
||||
else if(sdt->version != BTA_Data_Ver) {
|
||||
WARN("Wrong shared data version: I'am - %d, but server - %d ...",
|
||||
BTA_Data_Ver, sdt->version );
|
||||
}
|
||||
if(sdt->size != sdat.size) {
|
||||
if(sdt->size > sdat.size) {
|
||||
WARN("Wrong shared area size: I needs - %d, but server - %d ...",
|
||||
sdat.size, sdt->size );
|
||||
} else {
|
||||
WARN("Attention! Too little shared data structure!");
|
||||
WARN("I needs - %d, but server gives only %d ...",
|
||||
sdat.size, sdt->size );
|
||||
WARN("May be server's version too old!?");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* ServerSide */
|
||||
if(sdt->magic == sdat.key.code &&
|
||||
sdt->version == BTA_Data_Ver &&
|
||||
sdt->size == sdat.size)
|
||||
return;
|
||||
memset(sdat.addr, 0, sdat.maxsize);
|
||||
sdt->magic = sdat.key.code;
|
||||
sdt->version = BTA_Data_Ver;
|
||||
sdt->size = sdat.size;
|
||||
Tel_Hardware = Hard_On;
|
||||
Pos_Corr = PC_On;
|
||||
TrkOk_Mode = UseDiffVel | UseDiffAZ ;
|
||||
inp_B = 591.;
|
||||
Pressure = 595.;
|
||||
PEP_code_A = 0x002aaa;
|
||||
PEP_code_Z = 0x002aaa;
|
||||
PEP_code_P = 0x002aaa;
|
||||
PEP_code_F = 0x002aaa;
|
||||
PEP_code_D = 0x002aaa;
|
||||
DomeSEW_N = 1;
|
||||
}
|
||||
|
||||
int bta_data_check() {
|
||||
return( (sdt->magic == sdat.key.code) && (sdt->version == BTA_Data_Ver) );
|
||||
}
|
||||
|
||||
void bta_data_close() {
|
||||
if(sdat.side == ServerSide) {
|
||||
sdt->magic = 0;
|
||||
sdt->version = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate shared memory segment
|
||||
*/
|
||||
int get_shm_block(volatile struct SHM_Block *sb, int server) {
|
||||
int getsize = (server)? sb->maxsize : sb->size;
|
||||
// first try to find existing one
|
||||
sb->id = shmget(sb->key.code, getsize, sb->mode);
|
||||
if(sb->id < 0 && errno == ENOENT && server){
|
||||
// if no - try to create a new one
|
||||
int cresize = sb->maxsize;
|
||||
if(sb->size > cresize){
|
||||
WARN("Wrong shm maxsize(%d) < realsize(%d)",sb->maxsize,sb->size);
|
||||
cresize = sb->size;
|
||||
}
|
||||
sb->id = shmget(sb->key.code, cresize, IPC_CREAT|IPC_EXCL|sb->mode);
|
||||
}
|
||||
if(sb->id < 0){
|
||||
if(server)
|
||||
PERR("Can't create shared memory segment '%s'",sb->key.name);
|
||||
else
|
||||
PERR("Can't find shared segment '%s' (maybe no server process) ",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
// attach it to our memory space
|
||||
sb->addr = (unsigned char *) shmat(sb->id, NULL, sb->atflag);
|
||||
if((long)sb->addr == -1){
|
||||
PERR("Can't attach shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
if(server && (shmctl(sb->id, SHM_LOCK, NULL) < 0)){
|
||||
PERR("Can't prevents swapping of shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
DBG("Create & attach shared memory segment '%s' %dbytes", sb->key.name, sb->size);
|
||||
sb->side = server;
|
||||
if(sb->init != NULL)
|
||||
sb->init();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int close_shm_block(volatile struct SHM_Block *sb){
|
||||
int ret;
|
||||
if(sb->close != NULL)
|
||||
sb->close();
|
||||
if(sb->side == ServerSide) {
|
||||
// ret = shmctl(sb->id, SHM_UNLOCK, NULL);
|
||||
ret = shmctl(sb->id, IPC_RMID, NULL);
|
||||
}
|
||||
ret = shmdt (sb->addr);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create|Find command queue
|
||||
*/
|
||||
void get_cmd_queue(struct CMD_Queue *cq, int server){
|
||||
if (!server && cq->id >= 0) { //if already in use set current
|
||||
snd_id = cq->id;
|
||||
return;
|
||||
}
|
||||
// first try to find existing one
|
||||
cq->id = msgget(cq->key.code, cq->mode);
|
||||
// if no - try to create a new one
|
||||
if(cq->id<0 && errno == ENOENT && server)
|
||||
cq->id = msgget(cq->key.code, IPC_CREAT|IPC_EXCL|cq->mode);
|
||||
if(cq->id<0){
|
||||
if(server)
|
||||
PERR("Can't create comand queue '%s'",cq->key.name);
|
||||
else
|
||||
PERR("Can't find comand queue '%s' (maybe no server process) ",cq->key.name);
|
||||
return;
|
||||
}
|
||||
cq->side = server;
|
||||
if(server){
|
||||
char buf[120]; /* выбросить все команды из очереди */
|
||||
while(msgrcv(cq->id, (struct msgbuf *)buf, 112, 0, IPC_NOWAIT) > 0);
|
||||
}else
|
||||
snd_id = cq->id;
|
||||
cq->acckey = 0;
|
||||
}
|
||||
|
||||
#endif // BTA_MODULE
|
||||
|
||||
|
||||
int check_shm_block(volatile struct SHM_Block *sb) {
|
||||
if(sb->check)
|
||||
return(sb->check());
|
||||
else return(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set access key in current channel
|
||||
*/
|
||||
void set_acckey(uint32_t newkey){
|
||||
if(snd_id < 0) return;
|
||||
if(ucmd.id == snd_id) ucmd.acckey = newkey;
|
||||
else if(ocmd.id == snd_id) ocmd.acckey = newkey;
|
||||
else if(mcmd.id == snd_id) mcmd.acckey = newkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup source data for one following command if default values
|
||||
* (IP == 0 - local, PID = current) not suits
|
||||
*/
|
||||
void set_cmd_src(uint32_t ip, int pid) {
|
||||
cmd_src_pid = pid;
|
||||
cmd_src_ip = ip;
|
||||
}
|
||||
|
||||
#pragma pack(push, 4)
|
||||
/**
|
||||
* Send client commands to server
|
||||
*/
|
||||
void send_cmd(int cmd_code, char *buf, int size) {
|
||||
struct my_msgbuf mbuf;
|
||||
if(snd_id < 0) return;
|
||||
if(size > 100) size = 100;
|
||||
if(cmd_code > 0)
|
||||
mbuf.mtype = cmd_code;
|
||||
else
|
||||
return;
|
||||
if(ucmd.id == snd_id) mbuf.acckey = ucmd.acckey;
|
||||
else if(ocmd.id == snd_id) mbuf.acckey = ocmd.acckey;
|
||||
else if(mcmd.id == snd_id) mbuf.acckey = mcmd.acckey;
|
||||
|
||||
mbuf.src_pid = cmd_src_pid ? cmd_src_pid : getpid();
|
||||
mbuf.src_ip = cmd_src_ip;
|
||||
cmd_src_pid = cmd_src_ip = 0;
|
||||
|
||||
if(size > 0)
|
||||
memcpy(mbuf.mtext, buf, size);
|
||||
else {
|
||||
mbuf.mtext[0] = 0;
|
||||
size = 1;
|
||||
}
|
||||
msgsnd(snd_id, (struct msgbuf *)&mbuf, size+12, IPC_NOWAIT);
|
||||
}
|
||||
|
||||
void send_cmd_noarg(int cmd_code) {
|
||||
send_cmd(cmd_code, NULL, 0);
|
||||
}
|
||||
void send_cmd_str(int cmd_code, char *arg) {
|
||||
send_cmd(cmd_code, arg, strlen(arg)+1);
|
||||
}
|
||||
void send_cmd_i1(int cmd_code, int32_t arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i2(int cmd_code, int32_t arg1, int32_t arg2) {
|
||||
int32_t ibuf[2];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)ibuf, 2*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i3(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3) {
|
||||
int32_t ibuf[3];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
send_cmd(cmd_code, (char *)ibuf, 3*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i4(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4) {
|
||||
int32_t ibuf[4];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
ibuf[3] = arg4;
|
||||
send_cmd(cmd_code, (char *)ibuf, 4*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_d1(int32_t cmd_code, double arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(double));
|
||||
}
|
||||
void send_cmd_d2(int cmd_code, double arg1, double arg2) {
|
||||
double dbuf[2];
|
||||
dbuf[0] = arg1;
|
||||
dbuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)dbuf, 2*sizeof(double));
|
||||
}
|
||||
void send_cmd_i1d1(int cmd_code, int32_t arg1, double arg2) {
|
||||
struct {
|
||||
int32_t ival;
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival = arg1;
|
||||
buf.dval = arg2;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i2d1(int cmd_code, int32_t arg1, int32_t arg2, double arg3) {
|
||||
struct {
|
||||
int32_t ival[2];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.dval = arg3;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i3d1(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, double arg4) {
|
||||
struct {
|
||||
int32_t ival[3];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.ival[2] = arg3;
|
||||
buf.dval = arg4;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
|
||||
void encode_lev_passwd(char *passwd, int nlev, uint32_t *keylev, uint32_t *codlev){
|
||||
char salt[4];
|
||||
char *encr;
|
||||
union {
|
||||
uint32_t ui;
|
||||
char c[4];
|
||||
} key, cod;
|
||||
sprintf(salt,"L%1d",nlev);
|
||||
encr = (char *)crypt(passwd, salt);
|
||||
cod.c[0] = encr[2];
|
||||
key.c[0] = encr[3];
|
||||
cod.c[1] = encr[4];
|
||||
key.c[1] = encr[5];
|
||||
cod.c[2] = encr[6];
|
||||
key.c[2] = encr[7];
|
||||
cod.c[3] = encr[8];
|
||||
key.c[3] = encr[9];
|
||||
*keylev = key.ui;
|
||||
*codlev = cod.ui;
|
||||
}
|
||||
|
||||
int find_lev_passwd(char *passwd, uint32_t *keylev, uint32_t *codlev){
|
||||
int nlev;
|
||||
for(nlev = 5; nlev > 0; --nlev){
|
||||
encode_lev_passwd(passwd, nlev, keylev, codlev);
|
||||
if(*codlev == code_Lev(nlev)) break;
|
||||
}
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
int check_lev_passwd(char *passwd){
|
||||
uint32_t keylev,codlev;
|
||||
int nlev;
|
||||
nlev = find_lev_passwd(passwd, &keylev, &codlev);
|
||||
if(nlev > 0) set_acckey(keylev);
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
#pragma pack(pop)
|
||||
File diff suppressed because it is too large
Load Diff
@ -42,7 +42,7 @@ time_t aver_interval = 1;
|
||||
int out_fd;
|
||||
void sendSVG(const int image_type, unsigned char stat_mask, int im_height);
|
||||
|
||||
inline void minmax(float *min, float *max, float param){
|
||||
static inline void minmax(float *min, float *max, float param){
|
||||
if(param > *max) *max = param;
|
||||
else if(param < *min) *min = param;
|
||||
}
|
||||
@ -56,7 +56,7 @@ void print_curvals(unsigned char stat_mask){ // stat_mask -
|
||||
max_wind, min_wind, avr_wind, // ÓËÏÒÏÓÔØ ×ÅÔÒÁ
|
||||
max_pres, min_pres, avr_pres, // ÄÁ×ÌÅÎÉÅ
|
||||
max_hmd, min_hmd, avr_hmd; // ×ÌÁÖÎÏÓÔØ
|
||||
printf(Content_type);
|
||||
printf("%s", Content_type);
|
||||
while(read(out_fd, &data, sizeof(data)) == sizeof(data)){
|
||||
if(data.seconds < t_start) continue;
|
||||
if(data.seconds > t_end) break;
|
||||
@ -104,7 +104,7 @@ void print_curvals(unsigned char stat_mask){ // stat_mask -
|
||||
avr_otemp/d_len, avr_itemp/d_len, avr_mtemp/d_len,
|
||||
avr_wind/d_len, avr_pres/d_len, avr_hmd/d_len,
|
||||
_L(_s_Monlen_), d_len);
|
||||
printf("<a href=\"%s?Save=1&Tstart=%d&Tend=%d&Stat=%d&Aver=%d\"'>%s</a>\n",
|
||||
printf("<a href=\"%s?Save=1&Tstart=%zd&Tend=%zd&Stat=%d&Aver=%zd\"'>%s</a>\n",
|
||||
SCRIPT_PATH, t_start, t_end, stat_mask, aver_interval, _L(_s_Save_file_));
|
||||
}
|
||||
|
||||
@ -205,7 +205,7 @@ void fill_forms(const char* alert_message, const char S_flag){
|
||||
unsigned char mask;
|
||||
int im_ht = 600;
|
||||
if(S_flag == 0){
|
||||
printf(Content_type);
|
||||
printf("%s", Content_type);
|
||||
if(alert_message) printf("<div><h1 align=center>%s</h1></div>\n", alert_message);
|
||||
return;
|
||||
}
|
||||
@ -376,7 +376,7 @@ void find_starting_pos(){ //
|
||||
int cache_fd;
|
||||
Cache cache;
|
||||
if((cache_fd = open(CACHE_FILE, O_RDONLY)) < 0){
|
||||
printf(Content_type);
|
||||
printf("%s", Content_type);
|
||||
printf("<h1>%s</h1>", _L(_s_Cant_open_cache_));
|
||||
exit(1);
|
||||
}
|
||||
@ -430,7 +430,7 @@ void show_extremums(int ch){
|
||||
struct tm ltime;
|
||||
time_t t1[6], t2[6], t_first=0, t_last=0;
|
||||
|
||||
printf(Content_type);
|
||||
printf("%s", Content_type);
|
||||
if(get_qs_param(qs, "Stat", tmp, 16))
|
||||
stat_mask = atoi(tmp);
|
||||
if(stat_mask == 0) stat_mask = 0xFF;
|
||||
@ -588,7 +588,7 @@ void show_extremums(int ch){
|
||||
if(ch == 0){
|
||||
printf("<h2>%s</h2>\n", _L(_s_Mode_Times_));
|
||||
inline void fmtprnt(char **str, time_t tm){
|
||||
printf("%s: %d %s (%.2f %s)<br>\n", _L(str), tm, _L(_s_seconds_),
|
||||
printf("%s: %zd %s (%.2f %s)<br>\n", _L(str), tm, _L(_s_seconds_),
|
||||
((float)tm)/86400., _L(_s_days_));
|
||||
}
|
||||
fmtprnt(_s_Stopped_, t1[0]);
|
||||
@ -615,14 +615,14 @@ int main(){
|
||||
if(get_qs_param(qs, "Visor", tmp, 128)){ // ÏÔËÒÙ×ÁÅÍ/ÚÁËÒÙ×ÁÅÍ ÚÁÂÒÁÌÏ
|
||||
int shmid, param = atoi(tmp);
|
||||
shmid = shmget(SHM_KEY, sizeof(int), IPC_CREAT | 0666);
|
||||
printf(Content_type);
|
||||
if(shmid < 0) printf(_L(_s_noVisor_));
|
||||
printf("%s", Content_type);
|
||||
if(shmid < 0) printf("%s", _L(_s_noVisor_));
|
||||
else{
|
||||
Visor = (int*) shmat(shmid, NULL, 0);
|
||||
if(Visor){
|
||||
if(param > 0) *Visor = !(*Visor); // param >0 - ÓÍÅÎÁ ÓÏÓÔÏÑÎÉÑ
|
||||
}
|
||||
else printf(_L(_s_noVisor_));
|
||||
else printf("%s", _L(_s_noVisor_));
|
||||
}
|
||||
printf("%s:<br><font color='%s'>%s</font><p style='margin-top: 25px;'>%s:<br>", _L(_s_CurVstat_),
|
||||
*Visor?"green":"red", *Visor?_L(_s_Vopen_):_L(_s_Vclose_), _L(_s_ChVstat_));
|
||||
|
||||
@ -115,7 +115,7 @@ void find_starting_pos(){ //
|
||||
cache_fd = open("./cache", O_RDONLY);
|
||||
}
|
||||
if(cache_fd < 0){
|
||||
WARN(_L(_s_Cant_open_cache_));
|
||||
WARN("%s", _L(_s_Cant_open_cache_));
|
||||
exit(1);
|
||||
}
|
||||
while(read(cache_fd, &cache, sizeof(cache)) > 0){
|
||||
@ -316,7 +316,7 @@ int main(int argc, char **argv){
|
||||
out_fd = open("./out", O_RDONLY);
|
||||
}
|
||||
if(out_fd < 0){
|
||||
WARN(_L(_s_Cant_open_data_));
|
||||
WARN("%s", _L(_s_Cant_open_data_));
|
||||
return(1);
|
||||
}
|
||||
if(lookout) print_dates();
|
||||
|
||||
BIN
C/daemon/daemon
BIN
C/daemon/daemon
Binary file not shown.
@ -93,7 +93,7 @@ void check4running(){
|
||||
fclose(pidfile);
|
||||
}
|
||||
|
||||
inline void LOG(char* thetime, char* thetext){
|
||||
static inline void LOG(char* thetime, char* thetext){
|
||||
fprintf(stderr, "%s\t%s\n", thetime, thetext);
|
||||
fprintf(F_log, "%s\t%s\n", thetime, thetext);
|
||||
}
|
||||
@ -184,7 +184,7 @@ int get_data(monit_d *data){//
|
||||
*p++ = Pressure;
|
||||
*h++ = val_Hmd;
|
||||
if(++nn == MAX_DATA_LEN) break;
|
||||
usleep(100000); // ÏÐÒÁÛÉ×ÁÅÍ 10 ÒÁÚ × ÓÅËÕÎÄÕ
|
||||
usleep(1000000); // ÏÐÒÁÛÉ×ÁÅÍ 1 ÒÁÚ × ÓÅËÕÎÄÕ
|
||||
// !!! ÎÁ ÃÉÆÒÅ 10 ÚÁ×ÑÚÁÎÏ ×ÙÞÉÓÌÅÎÉÅ ÏÂßÅÍÁ ÐÁÍÑÔÉ ÄÌÑ calloc, ÅÓÌÉ ÎÁÄÏ ÂÕÄÅÔ
|
||||
// ÉÚÍÅÎÉÔØ ×ÅÌÉÞÉÎÕ ÐÁÕÚÙ, ÎÅÏÂÈÏÄÉÍÏ É ÓËÏÒÒÅËÔÉÒÏ×ÁÔØ MAX_DATA_LEN × main
|
||||
}
|
||||
@ -280,7 +280,7 @@ int main(int argc, char** argv){
|
||||
get_shm_block( &sdat, ClientSide);
|
||||
shmid = shmget(SHM_KEY, sizeof(int), IPC_CREAT | 0666);
|
||||
Visor = (int*) shmat(shmid, NULL, 0);
|
||||
MAX_DATA_LEN = ((int)(TIMEINTERVAL)) * 10 + 11; // 11 = ÐÏÇÒÅÛÎÏÓÔØ ÏËÒÕÇÌÅÎÉÑ + 1 ÐÒÏ ÚÁÐÁÓ
|
||||
MAX_DATA_LEN = ((int)(TIMEINTERVAL)) + 2;
|
||||
t_out = (data_type*)calloc(MAX_DATA_LEN, sizeof(data_type));
|
||||
t_in = (data_type*)calloc(MAX_DATA_LEN, sizeof(data_type));
|
||||
t_mir = (data_type*)calloc(MAX_DATA_LEN, sizeof(data_type));
|
||||
|
||||
@ -15,13 +15,14 @@
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef __CLIENT_C__
|
||||
#include "bta_shdata.h"
|
||||
#endif
|
||||
|
||||
#define WARN(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
|
||||
|
||||
#define TIMEINTERVAL 9.99 // ÉÎÔÅÒ×ÁÌ ÕÓÒÅÄÎÅÎÉÑ ÄÁÎÎÙÈ (× ÓÅËÕÎÄÁÈ)
|
||||
#define TIMEINTERVAL 599.999 // ÉÎÔÅÒ×ÁÌ ÕÓÒÅÄÎÅÎÉÑ ÄÁÎÎÙÈ (× ÓÅËÕÎÄÁÈ)
|
||||
#define LOG_FILE "/home/eddy/_data/.temp_daemon" // ÆÁÊÌ Ó ÌÏÇÁÍÉ
|
||||
#define CACHE_FILE "/home/eddy/_data/.cache" // ÉÎÄÅËÓÎÙÊ ÆÁÊÌ
|
||||
#define OUT_FILE "/home/eddy/_data/.out" // ÓÁÍÁ ÉÎÆÏÒÍÁÃÉÑ
|
||||
|
||||
BIN
C/daemon/tempmon
BIN
C/daemon/tempmon
Binary file not shown.
356
C/test/bta_shdata.c
Normal file
356
C/test/bta_shdata.c
Normal file
@ -0,0 +1,356 @@
|
||||
// (C) V.S. Shergin, SAO RAS
|
||||
#include <err.h>
|
||||
#include "bta_shdata.h"
|
||||
|
||||
#pragma pack(push, 4)
|
||||
// Main command channel (level 5)
|
||||
struct CMD_Queue mcmd = {{"Mcmd"}, 0200,0,-1,0};
|
||||
// Operator command channel (level 4)
|
||||
struct CMD_Queue ocmd = {{"Ocmd"}, 0200,0,-1,0};
|
||||
// User command channel (level 2/3)
|
||||
struct CMD_Queue ucmd = {{"Ucmd"}, 0200,0,-1,0};
|
||||
|
||||
#define MSGLEN (80)
|
||||
static char msg[MSGLEN];
|
||||
#define WARN(...) warn(__VA_ARGS__)
|
||||
#define PERR(...) do{snprintf(msg, MSGLEN, __VA_ARGS__); perror(msg);} while(0)
|
||||
#ifdef EBUG
|
||||
#define FNAME() fprintf(stderr, "\n%s (%s, line %d)\n", __func__, __FILE__, __LINE__)
|
||||
#define DBG(...) do{fprintf(stderr, "%s (%s, line %d): ", __func__, __FILE__, __LINE__); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fprintf(stderr, "\n");} while(0)
|
||||
#else
|
||||
#define FNAME() do{}while(0)
|
||||
#define DBG(...) do{}while(0)
|
||||
#endif //EBUG
|
||||
|
||||
|
||||
#ifndef BTA_MODULE
|
||||
volatile struct BTA_Data *sdt;
|
||||
volatile struct BTA_Local *sdtl;
|
||||
|
||||
volatile struct SHM_Block sdat = {
|
||||
{"Sdat"},
|
||||
sizeof(struct BTA_Data),
|
||||
2048,0444,
|
||||
SHM_RDONLY,
|
||||
bta_data_init,
|
||||
bta_data_check,
|
||||
bta_data_close,
|
||||
ClientSide,-1,NULL
|
||||
};
|
||||
|
||||
int snd_id = -1; // client sender ID
|
||||
int cmd_src_pid = 0; // next command source PID
|
||||
uint32_t cmd_src_ip = 0;// next command source IP
|
||||
|
||||
/**
|
||||
* Init data
|
||||
*/
|
||||
void bta_data_init() {
|
||||
sdt = (struct BTA_Data *)sdat.addr;
|
||||
sdtl = (struct BTA_Local *)(sdat.addr+sizeof(struct BTA_Data));
|
||||
if(sdat.side == ClientSide) {
|
||||
if(sdt->magic != sdat.key.code) {
|
||||
WARN("Wrong shared data (maybe server turned off)");
|
||||
}
|
||||
if(sdt->version == 0) {
|
||||
WARN("Null shared data version (maybe server turned off)");
|
||||
}
|
||||
else if(sdt->version != BTA_Data_Ver) {
|
||||
WARN("Wrong shared data version: I'am - %d, but server - %d ...",
|
||||
BTA_Data_Ver, sdt->version );
|
||||
}
|
||||
if(sdt->size != sdat.size) {
|
||||
if(sdt->size > sdat.size) {
|
||||
WARN("Wrong shared area size: I needs - %d, but server - %d ...",
|
||||
sdat.size, sdt->size );
|
||||
} else {
|
||||
WARN("Attention! Too little shared data structure!");
|
||||
WARN("I needs - %d, but server gives only %d ...",
|
||||
sdat.size, sdt->size );
|
||||
WARN("May be server's version too old!?");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* ServerSide */
|
||||
if(sdt->magic == sdat.key.code &&
|
||||
sdt->version == BTA_Data_Ver &&
|
||||
sdt->size == sdat.size)
|
||||
return;
|
||||
memset(sdat.addr, 0, sdat.maxsize);
|
||||
sdt->magic = sdat.key.code;
|
||||
sdt->version = BTA_Data_Ver;
|
||||
sdt->size = sdat.size;
|
||||
Tel_Hardware = Hard_On;
|
||||
Pos_Corr = PC_On;
|
||||
TrkOk_Mode = UseDiffVel | UseDiffAZ ;
|
||||
inp_B = 591.;
|
||||
Pressure = 595.;
|
||||
PEP_code_A = 0x002aaa;
|
||||
PEP_code_Z = 0x002aaa;
|
||||
PEP_code_P = 0x002aaa;
|
||||
PEP_code_F = 0x002aaa;
|
||||
PEP_code_D = 0x002aaa;
|
||||
DomeSEW_N = 1;
|
||||
}
|
||||
|
||||
int bta_data_check() {
|
||||
return( (sdt->magic == sdat.key.code) && (sdt->version == BTA_Data_Ver) );
|
||||
}
|
||||
|
||||
void bta_data_close() {
|
||||
if(sdat.side == ServerSide) {
|
||||
sdt->magic = 0;
|
||||
sdt->version = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate shared memory segment
|
||||
*/
|
||||
int get_shm_block(volatile struct SHM_Block *sb, int server) {
|
||||
int getsize = (server)? sb->maxsize : sb->size;
|
||||
// first try to find existing one
|
||||
sb->id = shmget(sb->key.code, getsize, sb->mode);
|
||||
if(sb->id < 0 && errno == ENOENT && server){
|
||||
// if no - try to create a new one
|
||||
int cresize = sb->maxsize;
|
||||
if(sb->size > cresize){
|
||||
WARN("Wrong shm maxsize(%d) < realsize(%d)",sb->maxsize,sb->size);
|
||||
cresize = sb->size;
|
||||
}
|
||||
sb->id = shmget(sb->key.code, cresize, IPC_CREAT|IPC_EXCL|sb->mode);
|
||||
}
|
||||
if(sb->id < 0){
|
||||
if(server)
|
||||
PERR("Can't create shared memory segment '%s'",sb->key.name);
|
||||
else
|
||||
PERR("Can't find shared segment '%s' (maybe no server process) ",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
// attach it to our memory space
|
||||
sb->addr = (unsigned char *) shmat(sb->id, NULL, sb->atflag);
|
||||
if((long)sb->addr == -1){
|
||||
PERR("Can't attach shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
if(server && (shmctl(sb->id, SHM_LOCK, NULL) < 0)){
|
||||
PERR("Can't prevents swapping of shared memory segment '%s'",sb->key.name);
|
||||
return 0;
|
||||
}
|
||||
DBG("Create & attach shared memory segment '%s' %dbytes", sb->key.name, sb->size);
|
||||
sb->side = server;
|
||||
if(sb->init != NULL)
|
||||
sb->init();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int close_shm_block(volatile struct SHM_Block *sb){
|
||||
int ret;
|
||||
if(sb->close != NULL)
|
||||
sb->close();
|
||||
if(sb->side == ServerSide) {
|
||||
// ret = shmctl(sb->id, SHM_UNLOCK, NULL);
|
||||
ret = shmctl(sb->id, IPC_RMID, NULL);
|
||||
}
|
||||
ret = shmdt (sb->addr);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create|Find command queue
|
||||
*/
|
||||
void get_cmd_queue(struct CMD_Queue *cq, int server){
|
||||
if (!server && cq->id >= 0) { //if already in use set current
|
||||
snd_id = cq->id;
|
||||
return;
|
||||
}
|
||||
// first try to find existing one
|
||||
cq->id = msgget(cq->key.code, cq->mode);
|
||||
// if no - try to create a new one
|
||||
if(cq->id<0 && errno == ENOENT && server)
|
||||
cq->id = msgget(cq->key.code, IPC_CREAT|IPC_EXCL|cq->mode);
|
||||
if(cq->id<0){
|
||||
if(server)
|
||||
PERR("Can't create comand queue '%s'",cq->key.name);
|
||||
else
|
||||
PERR("Can't find comand queue '%s' (maybe no server process) ",cq->key.name);
|
||||
return;
|
||||
}
|
||||
cq->side = server;
|
||||
if(server){
|
||||
char buf[120]; /* выбросить все команды из очереди */
|
||||
while(msgrcv(cq->id, (struct msgbuf *)buf, 112, 0, IPC_NOWAIT) > 0);
|
||||
}else
|
||||
snd_id = cq->id;
|
||||
cq->acckey = 0;
|
||||
}
|
||||
|
||||
#endif // BTA_MODULE
|
||||
|
||||
|
||||
int check_shm_block(volatile struct SHM_Block *sb) {
|
||||
if(sb->check)
|
||||
return(sb->check());
|
||||
else return(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set access key in current channel
|
||||
*/
|
||||
void set_acckey(uint32_t newkey){
|
||||
if(snd_id < 0) return;
|
||||
if(ucmd.id == snd_id) ucmd.acckey = newkey;
|
||||
else if(ocmd.id == snd_id) ocmd.acckey = newkey;
|
||||
else if(mcmd.id == snd_id) mcmd.acckey = newkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup source data for one following command if default values
|
||||
* (IP == 0 - local, PID = current) not suits
|
||||
*/
|
||||
void set_cmd_src(uint32_t ip, int pid) {
|
||||
cmd_src_pid = pid;
|
||||
cmd_src_ip = ip;
|
||||
}
|
||||
|
||||
#pragma pack(push, 4)
|
||||
/**
|
||||
* Send client commands to server
|
||||
*/
|
||||
void send_cmd(int cmd_code, char *buf, int size) {
|
||||
struct my_msgbuf mbuf;
|
||||
if(snd_id < 0) return;
|
||||
if(size > 100) size = 100;
|
||||
if(cmd_code > 0)
|
||||
mbuf.mtype = cmd_code;
|
||||
else
|
||||
return;
|
||||
if(ucmd.id == snd_id) mbuf.acckey = ucmd.acckey;
|
||||
else if(ocmd.id == snd_id) mbuf.acckey = ocmd.acckey;
|
||||
else if(mcmd.id == snd_id) mbuf.acckey = mcmd.acckey;
|
||||
|
||||
mbuf.src_pid = cmd_src_pid ? cmd_src_pid : getpid();
|
||||
mbuf.src_ip = cmd_src_ip;
|
||||
cmd_src_pid = cmd_src_ip = 0;
|
||||
|
||||
if(size > 0)
|
||||
memcpy(mbuf.mtext, buf, size);
|
||||
else {
|
||||
mbuf.mtext[0] = 0;
|
||||
size = 1;
|
||||
}
|
||||
msgsnd(snd_id, (struct msgbuf *)&mbuf, size+12, IPC_NOWAIT);
|
||||
}
|
||||
|
||||
void send_cmd_noarg(int cmd_code) {
|
||||
send_cmd(cmd_code, NULL, 0);
|
||||
}
|
||||
void send_cmd_str(int cmd_code, char *arg) {
|
||||
send_cmd(cmd_code, arg, strlen(arg)+1);
|
||||
}
|
||||
void send_cmd_i1(int cmd_code, int32_t arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i2(int cmd_code, int32_t arg1, int32_t arg2) {
|
||||
int32_t ibuf[2];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)ibuf, 2*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i3(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3) {
|
||||
int32_t ibuf[3];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
send_cmd(cmd_code, (char *)ibuf, 3*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_i4(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, int32_t arg4) {
|
||||
int32_t ibuf[4];
|
||||
ibuf[0] = arg1;
|
||||
ibuf[1] = arg2;
|
||||
ibuf[2] = arg3;
|
||||
ibuf[3] = arg4;
|
||||
send_cmd(cmd_code, (char *)ibuf, 4*sizeof(int32_t));
|
||||
}
|
||||
void send_cmd_d1(int32_t cmd_code, double arg1) {
|
||||
send_cmd(cmd_code, (char *)&arg1, sizeof(double));
|
||||
}
|
||||
void send_cmd_d2(int cmd_code, double arg1, double arg2) {
|
||||
double dbuf[2];
|
||||
dbuf[0] = arg1;
|
||||
dbuf[1] = arg2;
|
||||
send_cmd(cmd_code, (char *)dbuf, 2*sizeof(double));
|
||||
}
|
||||
void send_cmd_i1d1(int cmd_code, int32_t arg1, double arg2) {
|
||||
struct {
|
||||
int32_t ival;
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival = arg1;
|
||||
buf.dval = arg2;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i2d1(int cmd_code, int32_t arg1, int32_t arg2, double arg3) {
|
||||
struct {
|
||||
int32_t ival[2];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.dval = arg3;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
void send_cmd_i3d1(int cmd_code, int32_t arg1, int32_t arg2, int32_t arg3, double arg4) {
|
||||
struct {
|
||||
int32_t ival[3];
|
||||
double dval;
|
||||
} buf;
|
||||
buf.ival[0] = arg1;
|
||||
buf.ival[1] = arg2;
|
||||
buf.ival[2] = arg3;
|
||||
buf.dval = arg4;
|
||||
send_cmd(cmd_code, (char *)&buf, sizeof(buf));
|
||||
}
|
||||
|
||||
void encode_lev_passwd(char *passwd, int nlev, uint32_t *keylev, uint32_t *codlev){
|
||||
char salt[4];
|
||||
char *encr;
|
||||
union {
|
||||
uint32_t ui;
|
||||
char c[4];
|
||||
} key, cod;
|
||||
sprintf(salt,"L%1d",nlev);
|
||||
encr = (char *)crypt(passwd, salt);
|
||||
cod.c[0] = encr[2];
|
||||
key.c[0] = encr[3];
|
||||
cod.c[1] = encr[4];
|
||||
key.c[1] = encr[5];
|
||||
cod.c[2] = encr[6];
|
||||
key.c[2] = encr[7];
|
||||
cod.c[3] = encr[8];
|
||||
key.c[3] = encr[9];
|
||||
*keylev = key.ui;
|
||||
*codlev = cod.ui;
|
||||
}
|
||||
|
||||
int find_lev_passwd(char *passwd, uint32_t *keylev, uint32_t *codlev){
|
||||
int nlev;
|
||||
for(nlev = 5; nlev > 0; --nlev){
|
||||
encode_lev_passwd(passwd, nlev, keylev, codlev);
|
||||
if(*codlev == code_Lev(nlev)) break;
|
||||
}
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
int check_lev_passwd(char *passwd){
|
||||
uint32_t keylev,codlev;
|
||||
int nlev;
|
||||
nlev = find_lev_passwd(passwd, &keylev, &codlev);
|
||||
if(nlev > 0) set_acckey(keylev);
|
||||
return(nlev);
|
||||
}
|
||||
|
||||
#pragma pack(pop)
|
||||
1782
C/test/bta_shdata.h
1782
C/test/bta_shdata.h
File diff suppressed because it is too large
Load Diff
0
cgi-bin/bta_print.cgi
Executable file → Normal file
0
cgi-bin/bta_print.cgi
Executable file → Normal file
Loading…
x
Reference in New Issue
Block a user