mirror of
https://github.com/eddyem/small_tel.git
synced 2026-03-20 00:31:00 +03:00
Add Weather_chk
This commit is contained in:
@@ -30,7 +30,7 @@ glob_pars *GP;
|
||||
void signals(int signo){
|
||||
restore_console();
|
||||
restore_tty();
|
||||
putlog("exit with status %d", signo);
|
||||
LOG("exit with status %d", signo);
|
||||
exit(signo);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ int main(int argc, char **argv){
|
||||
signals(0); // never reached!
|
||||
}
|
||||
if(GP->logfile)
|
||||
openlogfile(GP->logfile);
|
||||
Cl_createlog(GP->logfile);
|
||||
#ifndef EBUG
|
||||
if(daemon(1, 0)){
|
||||
ERR("daemon()");
|
||||
@@ -57,10 +57,9 @@ int main(int argc, char **argv){
|
||||
while(1){ // guard for dead processes
|
||||
pid_t childpid = fork();
|
||||
if(childpid){
|
||||
putlog("create child with PID %d\n", childpid);
|
||||
LOG("create child with PID %d\n", childpid);
|
||||
DBG("Created child with PID %d\n", childpid);
|
||||
wait(NULL);
|
||||
putlog("child %d died\n", childpid);
|
||||
WARNX("Child %d died\n", childpid);
|
||||
sleep(1);
|
||||
}else{
|
||||
@@ -75,7 +74,6 @@ int main(int argc, char **argv){
|
||||
* INSERT CODE HERE
|
||||
* connection check & device validation
|
||||
*/
|
||||
//if(!G->terminal) signals(15); // there's not main controller connected to given terminal
|
||||
daemonize(GP->port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -25,7 +25,6 @@
|
||||
#include "term.h"
|
||||
#include <netdb.h> // addrinfo
|
||||
#include <arpa/inet.h> // inet_ntop
|
||||
#include <pthread.h>
|
||||
#include <limits.h> // INT_xxx
|
||||
#include <signal.h> // pthread_kill
|
||||
#include <unistd.h> // daemon
|
||||
@@ -73,7 +72,7 @@ static int waittoread(int sock){
|
||||
}
|
||||
|
||||
/**************** SERVER FUNCTIONS ****************/
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
/**
|
||||
* Send data over socket
|
||||
* @param sock - socket fd
|
||||
@@ -126,7 +125,7 @@ static char* stringscan(char *str, char *needle){
|
||||
}
|
||||
|
||||
static void *handle_socket(void *asock){
|
||||
//putlog("handle_socket(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
//LOG("handle_socket(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
FNAME();
|
||||
int sock = *((int*)asock);
|
||||
int webquery = 0; // whether query is web or regular
|
||||
@@ -142,13 +141,13 @@ static void *handle_socket(void *asock){
|
||||
continue;
|
||||
}
|
||||
if(!(rd = read(sock, buff, BUFLEN-1))){
|
||||
//putlog("socket closed. Exit");
|
||||
//LOG("socket closed. Exit");
|
||||
break;
|
||||
}
|
||||
//putlog("client send %zd bytes", rd);
|
||||
//LOG("client send %zd bytes", rd);
|
||||
DBG("Got %zd bytes", rd);
|
||||
if(rd < 0){ // error
|
||||
//putlog("some error occured");
|
||||
//LOG("some error occured");
|
||||
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
|
||||
break;
|
||||
}
|
||||
@@ -166,7 +165,7 @@ static void *handle_socket(void *asock){
|
||||
DBG("user send: %s\nfound=%s", buff, found);
|
||||
if(GP->echo){
|
||||
if(!send_data(sock, webquery, found)){
|
||||
putlog("can't send data, some error occured");
|
||||
LOG("can't send data, some error occured");
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&mutex);
|
||||
@@ -180,17 +179,17 @@ static void *handle_socket(void *asock){
|
||||
}
|
||||
close(sock);
|
||||
//DBG("closed");
|
||||
//putlog("socket closed, exit");
|
||||
//LOG("socket closed, exit");
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// main socket server
|
||||
static void *server(void *asock){
|
||||
putlog("server(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
LOG("server(): getpid: %d, pthread_self: %lu, tid: %lu",getpid(), pthread_self(), syscall(SYS_gettid));
|
||||
int sock = *((int*)asock);
|
||||
if(listen(sock, BACKLOG) == -1){
|
||||
putlog("listen() failed");
|
||||
LOG("listen() failed");
|
||||
WARN("listen");
|
||||
return NULL;
|
||||
}
|
||||
@@ -201,7 +200,7 @@ static void *server(void *asock){
|
||||
if(!waittoread(sock)) continue;
|
||||
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
|
||||
if(newsock <= 0){
|
||||
putlog("accept() failed");
|
||||
LOG("accept() failed");
|
||||
WARN("accept()");
|
||||
continue;
|
||||
}
|
||||
@@ -209,18 +208,18 @@ static void *server(void *asock){
|
||||
struct in_addr ipAddr = pV4Addr->sin_addr;
|
||||
char str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &ipAddr, str, INET_ADDRSTRLEN);
|
||||
//putlog("get connection from %s", str);
|
||||
//LOG("get connection from %s", str);
|
||||
DBG("Got connection from %s\n", str);
|
||||
pthread_t handler_thread;
|
||||
if(pthread_create(&handler_thread, NULL, handle_socket, (void*) &newsock)){
|
||||
putlog("server(): pthread_create() failed");
|
||||
LOG("server(): pthread_create() failed");
|
||||
WARN("pthread_create()");
|
||||
}else{
|
||||
DBG("Thread created, detouch");
|
||||
pthread_detach(handler_thread); // don't care about thread state
|
||||
}
|
||||
}
|
||||
putlog("server(): UNREACHABLE CODE REACHED!");
|
||||
LOG("server(): UNREACHABLE CODE REACHED!");
|
||||
}
|
||||
|
||||
// data gathering & socket management
|
||||
@@ -228,17 +227,17 @@ static void daemon_(int sock){
|
||||
if(sock < 0) return;
|
||||
pthread_t sock_thread;
|
||||
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
||||
putlog("daemon_(): pthread_create() failed");
|
||||
LOG("daemon_(): pthread_create() failed");
|
||||
ERR("pthread_create()");
|
||||
}
|
||||
double tgot = 0.;
|
||||
do{
|
||||
if(pthread_kill(sock_thread, 0) == ESRCH){ // died
|
||||
WARNX("Sockets thread died");
|
||||
putlog("Sockets thread died");
|
||||
LOG("Sockets thread died");
|
||||
pthread_join(sock_thread, NULL);
|
||||
if(pthread_create(&sock_thread, NULL, server, (void*) &sock)){
|
||||
putlog("daemon_(): new pthread_create() failed");
|
||||
LOG("daemon_(): new pthread_create() failed");
|
||||
ERR("pthread_create()");
|
||||
}
|
||||
}
|
||||
@@ -257,7 +256,7 @@ static void daemon_(int sock){
|
||||
*/
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}while(1);
|
||||
putlog("daemon_(): UNREACHABLE CODE REACHED!");
|
||||
LOG("daemon_(): UNREACHABLE CODE REACHED!");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -295,14 +294,14 @@ void daemonize(char *port){
|
||||
break; // if we get here, we have a successfull connection
|
||||
}
|
||||
if(p == NULL){
|
||||
putlog("failed to bind socket, exit");
|
||||
LOG("failed to bind socket, exit");
|
||||
// looped off the end of the list with no successful bind
|
||||
ERRX("failed to bind socket");
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
daemon_(sock);
|
||||
close(sock);
|
||||
putlog("socket closed, exit");
|
||||
LOG("socket closed, exit");
|
||||
signals(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -72,8 +72,8 @@ void try_connect(char *device){
|
||||
char tmpbuf[4096];
|
||||
fflush(stdout);
|
||||
tty_init(device);
|
||||
read_tty(tmpbuf, 4096); // clear rbuf
|
||||
putlog("Connected to %s", device);
|
||||
while(read_tty(tmpbuf, 4096)); // clear rbuf
|
||||
LOG("Connected to %s", device);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -387,48 +387,62 @@ int str2double(double *num, const char *str){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FILE *Flog = NULL; // log file descriptor
|
||||
char *logname = NULL;
|
||||
time_t log_open_time = 0;
|
||||
//////////// logging
|
||||
|
||||
static Cl_log log = {0};
|
||||
/**
|
||||
* Try to open log file
|
||||
* if failed show warning message
|
||||
* @brief Cl_createlog - create log file: init mutex, test file open ability
|
||||
* @param log - log structure
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
void openlogfile(char *name){
|
||||
if(!name){
|
||||
WARNX(_("Need filename"));
|
||||
return;
|
||||
int Cl_createlog(char *logname){
|
||||
if(log.logpath){
|
||||
FREE(log.logpath);
|
||||
pthread_mutex_destroy(&log.mutex);
|
||||
}
|
||||
green(_("Try to open log file %s in append mode\n"), name);
|
||||
if(!(Flog = fopen(name, "a"))){
|
||||
WARN(_("Can't open log file"));
|
||||
return;
|
||||
FILE *logfd = fopen(logname, "a");
|
||||
if(!logfd){
|
||||
WARN("Can't open log file");
|
||||
return 2;
|
||||
}
|
||||
log_open_time = time(NULL);
|
||||
logname = name;
|
||||
log.logpath = strdup(logname);
|
||||
fclose(logfd);
|
||||
if(pthread_mutex_init(&log.mutex, NULL)){
|
||||
WARN("Can't init log mutes");
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save message to log file, rotate logs every 24 hours
|
||||
* @brief Cl_putlog - put message to log file with/without timestamp
|
||||
* @param timest - ==1 to put timestamp
|
||||
* @param log - pointer to log structure
|
||||
* @param lvl - message loglevel (if lvl > loglevel, message won't be printed)
|
||||
* @param fmt - format and the rest part of message
|
||||
* @return amount of symbols saved in file
|
||||
*/
|
||||
int putlog(const char *fmt, ...){
|
||||
if(!Flog) return 0;
|
||||
time_t t_now = time(NULL);
|
||||
if(t_now - log_open_time > 86400){ // rotate log
|
||||
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
|
||||
fclose(Flog);
|
||||
char newname[PATH_MAX];
|
||||
snprintf(newname, PATH_MAX, "%s.old", logname);
|
||||
if(rename(logname, newname)) WARN("rename()");
|
||||
openlogfile(logname);
|
||||
if(!Flog) return 0;
|
||||
int Cl_putlogt(const char *fmt, ...){
|
||||
if(pthread_mutex_lock(&log.mutex)){
|
||||
WARN("Can't lock log mutex");
|
||||
return 0;
|
||||
}
|
||||
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
|
||||
int i = 0;
|
||||
FILE *logfd = fopen(log.logpath, "a");
|
||||
if(!logfd) goto rtn;
|
||||
char strtm[128];
|
||||
time_t t = time(NULL);
|
||||
struct tm *curtm = localtime(&t);
|
||||
strftime(strtm, 128, "%Y/%m/%d-%H:%M:%S", curtm);
|
||||
i = fprintf(logfd, "%s\t", strtm);
|
||||
va_list ar;
|
||||
va_start(ar, fmt);
|
||||
i = vfprintf(Flog, fmt, ar);
|
||||
i += vfprintf(logfd, fmt, ar);
|
||||
va_end(ar);
|
||||
fprintf(Flog, "\n");
|
||||
fflush(Flog);
|
||||
i += fprintf(logfd, "\n");
|
||||
fclose(logfd);
|
||||
rtn:
|
||||
pthread_mutex_unlock(&log.mutex);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#define _(String) (String)
|
||||
#define N_(String) (String)
|
||||
#endif
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <termio.h>
|
||||
@@ -76,10 +77,11 @@
|
||||
*/
|
||||
extern int globErr;
|
||||
extern void signals(int sig);
|
||||
#define ERR(...) do{globErr=errno; _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define ERRX(...) do{globErr=0; _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define WARN(...) do{globErr=errno; _WARN(__VA_ARGS__);}while(0)
|
||||
#define WARNX(...) do{globErr=0; _WARN(__VA_ARGS__);}while(0)
|
||||
#define ERR(...) do{globErr=errno; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define ERRX(...) do{globErr=0; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__); signals(9);}while(0)
|
||||
#define WARN(...) do{globErr=errno; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__);}while(0)
|
||||
#define WARNX(...) do{globErr=0; Cl_putlogt(__VA_ARGS__); _WARN(__VA_ARGS__);}while(0)
|
||||
#define LOG(...) do{Cl_putlogt(__VA_ARGS__); }while(0)
|
||||
|
||||
/*
|
||||
* print function name, debug messages
|
||||
@@ -135,6 +137,12 @@ int write_tty(char *buff, size_t length);
|
||||
|
||||
int str2double(double *num, const char *str);
|
||||
|
||||
void openlogfile(char *name);
|
||||
int putlog(const char *fmt, ...);
|
||||
typedef struct{
|
||||
char *logpath; // full path to logfile
|
||||
pthread_mutex_t mutex; // log mutex
|
||||
} Cl_log;
|
||||
|
||||
int Cl_createlog(char *logname);
|
||||
int Cl_putlogt(const char *fmt, ...);
|
||||
|
||||
#endif // __USEFULL_MACROS_H__
|
||||
|
||||
Reference in New Issue
Block a user