From d239d98edbffdcbf0b81c933e7767258309a9c28 Mon Sep 17 00:00:00 2001 From: zolliker Date: Thu, 16 Oct 2008 14:33:25 +0000 Subject: [PATCH] - changed select to uselect - some fixes in tecs.c --- tecs/coc_client.c | 2 +- tecs/coc_server.c | 7 ++++--- tecs/coc_util.c | 5 +++-- tecs/make_gen | 6 +++--- tecs/myc_err.c | 1 + tecs/myc_str.c | 1 + tecs/sys_select.c | 7 ++++--- tecs/tecs.c | 6 +++--- tecs/tecs_serial.c | 5 ++++- tecs/term.c | 2 +- tecs/uselect.c | 18 ++++++++++++++++++ tecs/uselect.h | 7 +++++++ 12 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 tecs/uselect.c create mode 100644 tecs/uselect.h diff --git a/tecs/coc_client.c b/tecs/coc_client.c index ddf8a80..5e94b40 100644 --- a/tecs/coc_client.c +++ b/tecs/coc_client.c @@ -172,7 +172,7 @@ int CocCmdWithRetry(CocConn *conn) { } iret=CocTryCmd(conn); if (iret>=0) break; - close(conn->fd); + if (conn->fd > 0) close(conn->fd); conn->fd=-1; if (iret==-1) goto OnError; /* severe error */ ErrShort(ErrMessage); diff --git a/tecs/coc_server.c b/tecs/coc_server.c index f4d4160..362e1b0 100644 --- a/tecs/coc_server.c +++ b/tecs/coc_server.c @@ -16,6 +16,7 @@ #include "coc_server.h" #include "myc_str.h" #include "myc_time.h" +#include "uselect.h" #define COC_NUL COC_SEP @@ -480,7 +481,7 @@ void CocShowHandlers(char *buf, int buf_len) { #ifdef __VMS typedef size_t socklen_type; #else - typedef int socklen_type; + typedef socklen_t socklen_type; #endif int CocHandle1Request(int tmo_msec, int fd) { @@ -496,10 +497,10 @@ int CocHandle1Request(int tmo_msec, int fd) { rmask=mask; if (fd>0) FD_SET(fd, &rmask); + if (fd>=maxfd) maxfd=fd+1; tmo.tv_sec=tmo_msec / 1000; tmo.tv_usec=(tmo_msec % 1000)*1000+1; - if (fd>=maxfd) maxfd=fd+1; - ERR_SI(i=select(maxfd,&rmask,NULL,NULL,&tmo)); + ERR_I(i=uselect(maxfd,&rmask,NULL,NULL,&tmo)); if (fd>0 && FD_ISSET(fd, &rmask)) return(1); /* event on fd */ if (i==0) return(0); /* timeout */ diff --git a/tecs/coc_util.c b/tecs/coc_util.c index 422eff6..be855b7 100644 --- a/tecs/coc_util.c +++ b/tecs/coc_util.c @@ -8,6 +8,7 @@ #include "myc_err.h" #include "myc_str.h" #include "coc_util.h" +#include "uselect.h" /*-------------------------------------------------------------------------*/ /* CreateSocketAddress stolen from Tcl. Thanks to John Ousterhout */ @@ -52,7 +53,7 @@ void CocDelay(int msec) { tmo.tv_sec=msec / 1000; tmo.tv_usec=(msec % 1000)*1000+1; - select(1,NULL,NULL,NULL,&tmo); + uselect(1,NULL,NULL,NULL,&tmo); } /*-------------------------------------------------------------------------*/ @@ -79,7 +80,7 @@ int CocRecv(int fd, StrBuf *buf, int timeout, int *flag) { FD_ZERO(&mask); FD_SET(fd, &mask); - ERR_SI(i=select(fd+1,&mask,NULL,NULL,&tmo)); + ERR_SI(i=uselect(fd+1,&mask,NULL,NULL,&tmo)); if (flag!=NULL) { *flag=0; if (i==0) return(0); diff --git a/tecs/make_gen b/tecs/make_gen index 08399ef..4c28d37 100644 --- a/tecs/make_gen +++ b/tecs/make_gen @@ -10,10 +10,10 @@ LIBR_OBJ =coc_util.o myc_err.o myc_str.o myc_buf.o myc_time.o SERV_OBJ =tecs.o coc_server.o tecs_lsc.o tecs_serial.o coc_logfile.o \ - tecs_data.o tecs_logger.o $(LIBR_OBJ) + tecs_data.o tecs_logger.o uselect.o $(LIBR_OBJ) CLI_OBJ =tecs_cli.o coc_client.o $(LIBR_OBJ) TCLI_OBJ =sys_getenv.o sys_env.o myc_tmp.o sys_cmdpar.o \ - sys_date.o sys_wait.o sys_lun.o sys_rdline.o \ + sys_date.o sys_wait.o sys_lun.o sys_rdline.o uselect.o \ sys_get_key.o sys_unix.o sys_open$(SYS_OPEN).o \ $(CLI_OBJ) TECLI_OBJ =tecs_client.o tecs_plot.o str.o instr_hosts.o \ @@ -71,7 +71,7 @@ tecs_serial.o: tecs_serial.c keep_running: keep_running.c $(CC) $(CFLAGS) -o $@ $Q $(FORTIFYOBJ) -six: six.c term.o sys_select.o libtecsl.a instr_hosts.o +six: six.c term.o sys_select.o libtecsl.a instr_hosts.o uselect.o $(CC) $(CFLAGS) -o $@ $Q $(FORTIFYOBJ) rexstart: rstart.c myc_str.o myc_err.o instr_hosts.o diff --git a/tecs/myc_err.c b/tecs/myc_err.c index c92112e..0eb7bcc 100644 --- a/tecs/myc_err.c +++ b/tecs/myc_err.c @@ -21,6 +21,7 @@ void *outarg; void ErrTxt(char *text, int systemError) { if (systemError) { + stack_empty = 0; sp=0; ErrCode=errno; ErrMessage=strerror(errno); } if (stack_empty && sp>0) { diff --git a/tecs/myc_str.c b/tecs/myc_str.c index 63c98e4..06c4be4 100644 --- a/tecs/myc_str.c +++ b/tecs/myc_str.c @@ -241,6 +241,7 @@ int str_ncpy(char *dst, const char *src, int maxdest) { } int str_ncat(char *dst, const char *src, int maxdest) { + dst[maxdest-1]='\0'; strncat(dst, src, maxdest-strlen(dst)-1); if (dst[maxdest-1]!='\0') { dst[maxdest-1]='\0'; diff --git a/tecs/sys_select.c b/tecs/sys_select.c index dae71b9..4f85571 100644 --- a/tecs/sys_select.c +++ b/tecs/sys_select.c @@ -2,6 +2,7 @@ #include #include #include +#include "uselect.h" static int lastFd=-1; static struct termios atts; @@ -53,7 +54,7 @@ int sys_select_or_key(fd_set *mask, int msecTmo, int *key) { sys_keys_on(); rmask=*mask; - iret=select(FD_SETSIZE, &rmask, NULL, NULL, &tmo0); + iret=uselect(FD_SETSIZE, &rmask, NULL, NULL, &tmo0); if (iret<0) { FD_ZERO(&rmask); perror("error in select"); @@ -72,9 +73,9 @@ int sys_select_or_key(fd_set *mask, int msecTmo, int *key) { } tmo.tv_sec=msecTmo / 1000; tmo.tv_usec=(msecTmo%1000)*1000; - iret=select(FD_SETSIZE, &rmask, NULL, NULL, &tmo); + iret=uselect(FD_SETSIZE, &rmask, NULL, NULL, &tmo); } else { - iret=select(FD_SETSIZE, &rmask, NULL, NULL, NULL); + iret=uselect(FD_SETSIZE, &rmask, NULL, NULL, NULL); } if (iret<0) { FD_ZERO(&rmask); diff --git a/tecs/tecs.c b/tecs/tecs.c index e1d5219..e7fbb6d 100644 --- a/tecs/tecs.c +++ b/tecs/tecs.c @@ -633,6 +633,7 @@ again: loop=1; initMaxPower=1; resist=10; + nScan=0; } else { InitSensor(&sensC); InitSensor(&sensD); @@ -1831,7 +1832,7 @@ int Settings(void) { if (k>4) k=kk; i++; } - k = kk + nScan + 1; + /* k = kk + nScan + 1; */ if (k > 4) k = 5; } maxfld=k-1; @@ -2337,7 +2338,7 @@ int PeriodicTask(void) { ERR_P(LscCmd(ser, "ALMRST")); } } else if (loop == 1) { - if (!relay0) logfileOut(LOG_MAIN, "Relay is on!\n"); + if (relay && !relay0) logfileOut(LOG_MAIN, "Relay is on!\n"); alarmStatus[0]='\0'; } @@ -2630,7 +2631,6 @@ int DeviceHdl(int mode, void *base, int fd) { t=strchr(device, '/'); if (t==NULL) { if (0==strcmp(device, "0") || 0==strcasecmp(device, "auto")) { - nScan=0; ERR_I(ConfigByCode(0)); ERR_I(ConfigByCode(1)); } else { diff --git a/tecs/tecs_serial.c b/tecs/tecs_serial.c index b189fc9..bb70891 100644 --- a/tecs/tecs_serial.c +++ b/tecs/tecs_serial.c @@ -14,6 +14,7 @@ #include "coc_util.h" #include "myc_str.h" #include "myc_mem.h" +#include "uselect.h" #define ASYNSRV_TYPE 1 #define TERMSRV_TYPE 2 @@ -50,9 +51,11 @@ int SerWait(int tmo_msec, int fd) { FD_ZERO(&mask); FD_SET(fd, &mask); + tmo.tv_sec=tmo_msec / 1000; tmo.tv_usec=(tmo_msec % 1000) % 1000 +1; - ERR_SI(i=select(fd+1,&mask,NULL,NULL,&tmo)); + ERR_SI(i=uselect(fd+1,&mask,NULL,NULL,&tmo)); + if (i==0) return(0); /* timeout */ return(1); OnError: return(-1); diff --git a/tecs/term.c b/tecs/term.c index 84c49bb..40dbc40 100644 --- a/tecs/term.c +++ b/tecs/term.c @@ -67,7 +67,7 @@ int term_wait_socket(int socket, int msecTmo) { FD_SET(STDIN_FILENO, &mask); tmo.tv_sec=msecTmo / 1000; tmo.tv_usec=(msecTmo % 1000) * 1000; - i=select(FD_SETSIZE, &mask, NULL, NULL, &tmo); + i=uselect(FD_SETSIZE, &mask, NULL, NULL, &tmo); if (FD_ISSET(STDIN_FILENO, &mask)) { return 0; } diff --git a/tecs/uselect.c b/tecs/uselect.c new file mode 100644 index 0000000..4060270 --- /dev/null +++ b/tecs/uselect.c @@ -0,0 +1,18 @@ +#include +#include +#include "uselect.h" + +/* an uninterruptable version of select. M.Z. Oct 2008 */ + +int uselect(int nfds, + fd_set *readfds, fd_set *writefds, fd_set *exceptfds, + struct timeval *timeout) { + + sigset_t sigmask; + struct timespec tmo; + + sigfillset(&sigmask); + tmo.tv_sec = timeout->tv_sec; + tmo.tv_nsec = timeout->tv_usec * 1000; + return pselect(nfds, readfds, writefds, exceptfds, &tmo, &sigmask); +} diff --git a/tecs/uselect.h b/tecs/uselect.h new file mode 100644 index 0000000..4d848d9 --- /dev/null +++ b/tecs/uselect.h @@ -0,0 +1,7 @@ +#include + +/* an uninterruptable version of select. M.Z. Oct 2008 */ + +int uselect(int nfds, + fd_set *readfds, fd_set *writefds, fd_set *exceptfds, + struct timeval *timeout);