fixed for libusefull_macros 0.3.2

This commit is contained in:
Edward Emelianov 2025-04-16 15:11:27 +03:00
parent 46557ee23d
commit aa9165b323
5 changed files with 27 additions and 35 deletions

View File

@ -25,14 +25,12 @@
// default PID filename:
#define DEFAULT_PIDFILE "/tmp/usbsock.pid"
#define DEFAULT_PORT "1234"
#define DEFAULT_SOCKPATH "\0canbus"
static int help;
static glob_pars G = {
.pidfile = DEFAULT_PIDFILE,
.speed = 9600,
// .port = DEFAULT_PORT,
.path = DEFAULT_SOCKPATH,
.logfile = NULL // don't save logs
};
@ -42,15 +40,14 @@ glob_pars *GP = &G;
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
static sl_option_t cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"devpath", NEED_ARG, NULL, 'd', arg_string, APTR(&G.devpath), _("serial device path")},
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("serial device speed (default: 9600)")},
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs (default: none)")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
{"client", NO_ARGS, NULL, 'c', arg_int, APTR(&G.client), _("run as client")},
{"sockpath",NEED_ARG, NULL, 'f', arg_string, APTR(&G.path), _("socket path (start from \\0 for no files)")},
// {"port", NEED_ARG, NULL, 'P', arg_string, APTR(&G.port), _("port to connect (default: " DEFAULT_PORT ")")},
{"sockpath",NEED_ARG, NULL, 'f', arg_string, APTR(&G.path), _("socket path (start from \\0 for no files) or port")},
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), _("increase log verbose level (default: LOG_WARN) and messages (default: none)")},
end_option
};
@ -68,12 +65,12 @@ void parse_args(int argc, char **argv){
char helpstring[1024], *hptr = helpstring;
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
// format of help: "Usage: progname [args]\n"
change_helpstring(helpstring);
sl_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
sl_parseargs(&argc, &argv, cmdlnopts);
for(i = 0; i < argc; i++)
printf("Ignore parameter\t%s\n", argv[i]);
if(help) showhelp(-1, cmdlnopts);
if(help) sl_showhelp(-1, cmdlnopts);
}
/**

View File

@ -27,7 +27,7 @@ typedef struct{
char *devpath; // path to serial device
char *pidfile; // name of PID file
char *logfile; // logging to this file
char *path; // path to socket file (UNIX-socket) or number of port (local INET)
char *path; // path to socket file
int speed; // connection speed
int verbose; // verbose level: for messages & logging
int client; // ==1 if application runs in client mode

View File

@ -26,7 +26,7 @@
#include "cmdlnopts.h"
#include "sersock.h"
static TTY_descr *dev = NULL;
static sl_tty_t *dev = NULL;
static pid_t childpid = 0;
int server = 1;
@ -38,8 +38,8 @@ void signals(int sig){
}
// master process
DBG("Master process");
if(!server) restore_console();
else if(dev) close_tty(&dev);
if(!server) sl_restore_con();
else if(dev) sl_tty_close(&dev);
if(sig){
DBG("Exit with signal %d", sig);
signal(sig, SIG_IGN);
@ -53,7 +53,8 @@ void signals(int sig){
}
int main(int argc, char **argv){
initial_setup();
char *self = strdup(argv[0]);
sl_init();
parse_args(argc, argv);
if(GP->logfile){
int lvl = LOGLEVEL_WARN + GP->verbose;
@ -61,20 +62,14 @@ int main(int argc, char **argv){
if(lvl > LOGLEVEL_ANY) lvl = LOGLEVEL_ANY;
verbose(1, "Log file %s @ level %d\n", GP->logfile, lvl);
OPENLOG(GP->logfile, lvl, 1);
if(!globlog) WARNX("Can't create log file");
if(!sl_globlog) WARNX("Can't create log file");
}
if(GP->client) server = 0;
else if(!GP->devpath){
LOGERR("You should point serial device path");
ERRX("You should point serial device path");
}
/* int port = atoi(GP->port);
if(port < 1024 || port > 65535){
LOGERR("Wrong port value: %d", port);
WARNX("Wrong port value: %d", port);
return 1;
}*/
if(server) check4running(NULL, GP->pidfile);
if(server) sl_check4running(self, GP->pidfile);
// signal reactions:
signal(SIGTERM, signals); // kill (-15) - quit
signal(SIGHUP, SIG_IGN); // hup - ignore
@ -84,17 +79,17 @@ int main(int argc, char **argv){
LOGMSG("Started");
#ifndef EBUG
if(server){
unsigned int pause = 1;
unsigned int pause = 5;
while(1){
childpid = fork();
if(childpid){ // master
double t0 = dtime();
double t0 = sl_dtime();
LOGMSG("Created child with pid %d", childpid);
wait(NULL);
LOGWARN("Child %d died", childpid);
if(dtime() - t0 < 1.) ++pause;
if(sl_dtime() - t0 < 1.) pause += 5;
else pause = 1;
if(pause > 30) pause = 30;
if(pause > 60) pause = 60;
sleep(pause); // wait a little before respawn
}else{ // slave
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies

View File

@ -27,7 +27,7 @@
#include "sersock.h"
// work with single client, return FALSE if disconnected
static int handle_socket(int sock, TTY_descr *d){
static int handle_socket(int sock, sl_tty_t *d){
char buff[BUFLEN];
ssize_t rd = read(sock, buff, BUFLEN-1);;
DBG("Got %zd bytes", rd);
@ -37,7 +37,7 @@ static int handle_socket(int sock, TTY_descr *d){
}
// add trailing zero to be on the safe side
buff[rd] = 0;
DBG("GOT: %s", buff);
DBG("GOT: _%s_", buff);
ssize_t blen = strlen(buff);
if(blen != write(d->comfd, buff, blen)){
LOGWARN("write()");
@ -85,7 +85,7 @@ static int canberead(int fd){
* @param len (o) - amount of butes read (-1 if disconnected)
* @return pointer to data buffer or NULL if none
*/
static char *getserdata(TTY_descr *D, int *len){
static char *getserdata(sl_tty_t *D, int *len){
static char serbuf[BUFLEN], *ptr = serbuf;
static size_t blen = BUFLEN - 1;
if(!D || D->comfd < 0) return NULL;
@ -149,7 +149,7 @@ static char *getserdata(TTY_descr *D, int *len){
return D->buf;
}
static void server_(int sock, TTY_descr *d){
static void server_(int sock, sl_tty_t *d){
if(listen(sock, MAXCLIENTS) == -1){
WARN("listen");
LOGWARN("listen");
@ -260,7 +260,7 @@ static char *mygetline(){
}
static void client_(int sock){
setup_con(); // convert console mode into non-canon
sl_setup_con(); // convert console mode into non-canon
int Bufsiz = BUFLEN;
char *recvBuff = MALLOC(char, Bufsiz);
while(1){
@ -293,10 +293,10 @@ static void client_(int sock){
* @param speed - connection speed
* @return pointer to device structure if all OK
*/
static TTY_descr *openserialdev(char *path, int speed){
TTY_descr *d = new_tty(path, speed, BUFLEN);
static sl_tty_t *openserialdev(char *path, int speed){
sl_tty_t *d = sl_tty_new(path, speed, BUFLEN);
DBG("Device created");
if(!d || !(tty_open(d, 1))){
if(!d || !(sl_tty_open(d, 1))){
WARN("Can't open device %s", path);
LOGWARN("Can't open device %s", path);
return NULL;
@ -305,7 +305,7 @@ static TTY_descr *openserialdev(char *path, int speed){
return d;
}
int start_socket(int server, char *path, TTY_descr **dev){
int start_socket(int server, char *path, sl_tty_t **dev){
char apath[128];
DBG("path: %s", path);
char *eptr;

View File

@ -26,6 +26,6 @@
#include <usefull_macros.h>
int start_socket(int server, char *path, TTY_descr **dev);
int start_socket(int server, char *path, sl_tty_t **dev);
#endif // SERSOCK_H__