Logging (--log) implemented.
authorshort <>
Thu, 3 Jun 1999 11:46:41 +0000 (11:46 +0000)
committershort <>
Thu, 3 Jun 1999 11:46:41 +0000 (11:46 +0000)
mdsms.1.in
mdsms.c
setup.h

index 8e829f9..f0924c6 100644 (file)
@@ -3,6 +3,9 @@
 .\~ $Id$
 .\~
 .\~ $Log$
 .\~ $Id$
 .\~
 .\~ $Log$
+.\~ Revision 1.3  1999/06/03 11:46:41  short
+.\~ Logging (--log) implemented.
+.\~
 .\~ Revision 1.2  1999/06/03 10:38:52  short
 .\~ Implemented remaining communication timeouts and maximum retry count.
 .\~
 .\~ Revision 1.2  1999/06/03 10:38:52  short
 .\~ Implemented remaining communication timeouts and maximum retry count.
 .\~
@@ -17,6 +20,7 @@ mdsms \- send SMSes through MobilDock
 .B mdsms
 .RB [ "-c <cfgfile>" ]
 .RB [ "-d <device>" ]
 .B mdsms
 .RB [ "-c <cfgfile>" ]
 .RB [ "-d <device>" ]
+.RB [ "-L <file>" ]
 .RB [ "-l <lockfile>" ]
 .RB [ "-s <smsc #>" ]
 .RB [ "-m <#>" ]
 .RB [ "-l <lockfile>" ]
 .RB [ "-s <smsc #>" ]
 .RB [ "-m <#>" ]
@@ -44,6 +48,13 @@ subsequently with
 .BR -l | --lockfile
 option, see below.
 .TP
 .BR -l | --lockfile
 option, see below.
 .TP
+.BR -L | --log\  < file >
+Log all important messages to this file preceded by timestamp, machine hostname
+etc., similar to output of
+.BR syslogd (8).
+If set to empty string (which is default - ~\fB        DEF_LOGNAME     \fP~), nothing
+is logged anywhere.
+.TP
 .BR -l | --lockfile\  < lockfile >
 Prior to accessing serial device specified above the lockfile should be
 acquired for correct concurrent processes behaviour. Although this name
 .BR -l | --lockfile\  < lockfile >
 Prior to accessing serial device specified above the lockfile should be
 acquired for correct concurrent processes behaviour. Although this name
diff --git a/mdsms.c b/mdsms.c
index af7fe28..48eb8f5 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -5,6 +5,9 @@ static char rcsid[] ATTR_UNUSED = "$Id$";
 
 /*
  * $Log$
 
 /*
  * $Log$
+ * Revision 1.3  1999/06/03 11:46:41  short
+ * Logging (--log) implemented.
+ *
  * Revision 1.2  1999/06/03 10:38:52  short
  * Implemented remaining communication timeouts and maximum retry count.
  *
  * Revision 1.2  1999/06/03 10:38:52  short
  * Implemented remaining communication timeouts and maximum retry count.
  *
@@ -31,6 +34,7 @@ static char rcsid[] ATTR_UNUSED = "$Id$";
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
+#include <time.h>
 
 #ifdef HAVE_GETOPT_LONG
 #include <getopt.h>
 
 #ifdef HAVE_GETOPT_LONG
 #include <getopt.h>
@@ -72,7 +76,7 @@ static int verbose
 static char *pname;
 static int dis_cleanup=0,devfd=-1;
 
 static char *pname;
 static int dis_cleanup=0,devfd=-1;
 
-static char *phone,*body,*device,*lockfile,*smsc,*maxretry,*readtime,*chartime,*cmdtime;
+static char *phone,*body,*device,*logname,*lockfile,*smsc,*maxretry,*readtime,*chartime,*cmdtime;
 static int readbody;
 static long maxretryn=DEF_MAXRETRY,readtimen=DEF_READTIME,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
 static size_t bodylen;
 static int readbody;
 static long maxretryn=DEF_MAXRETRY,readtimen=DEF_READTIME,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
 static size_t bodylen;
@@ -82,6 +86,47 @@ static char lockreal[512],locked;
 
 static struct termios restios;
 static char restios_yes;
 
 static struct termios restios;
 static char restios_yes;
+static FILE *logf;
+
+static void vlogmsg(
+#ifndef PRINTF_WORKS_PM
+               char outerr,
+#endif
+               const char *fmt,va_list ap)
+{
+time_t stamp;
+char *ctm,*s;
+pid_t mypid=-1;
+char host[LINE_MAX];
+
+       if (!logf) return;
+       if (mypid==-1) {
+               mypid=getpid();
+               if (gethostname(host,sizeof(host))) strcpy(host,"<ERROR>");
+               }
+       time(&stamp);
+       ctm=ctime(&stamp);
+       if ((s=strchr(ctm,'\n'))) *s='\0';
+       fprintf(logf,"%s %s %s[%d]: ",ctm,host,pname,mypid);
+       vfprintf(logf,fmt,ap);
+#ifndef PRINTF_WORKS_PM
+       if (outerr) fputs(strerror(errno),logf);
+#endif
+       fputc('\n',logf);
+       fflush(logf);
+}
+
+static void logmsg(const char *fmt,...)
+{
+va_list ap;
+       va_start(ap,fmt);
+       vlogmsg(
+#ifndef PRINTF_WORKS_PM
+               0,
+#endif
+               fmt,ap);
+       va_end(ap);
+}
 
 static void error(const char *fmt,...)
 {
 
 static void error(const char *fmt,...)
 {
@@ -110,6 +155,13 @@ size_t fmtl;
                nfmt
 #endif
                ,ap);
                nfmt
 #endif
                ,ap);
+       if (fatal=='!') vlogmsg(
+#ifdef PRINTF_WORKS_PM
+               fmt
+#else
+               pm,nfmt
+#endif
+               ,ap);
        va_end(ap);
 
 #ifndef PRINTF_WORKS_PM
        va_end(ap);
 
 #ifndef PRINTF_WORKS_PM
@@ -156,6 +208,7 @@ static void usage(void)
 %s\
 \n\
 Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
 %s\
 \n\
 Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
+             [-L|--log <file>]\n\
              [-l|--lockfile <lock>] [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
              [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
              [-f|--file] [-v|--verbose] [-h|--help] [-V|--version]\n\
              [-l|--lockfile <lock>] [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
              [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
              [-f|--file] [-v|--verbose] [-h|--help] [-V|--version]\n\
@@ -164,6 +217,7 @@ Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
  -c, --config\tRead this additional config file\n\
 \t\t(def. \"" CONFIG_MAIN "\" and \"$HOME" CONFIG_HOME "\")\n\
  -d, --device\tMobilDock on this serial device (def. \"" DEF_DEVICE "\")\n\
  -c, --config\tRead this additional config file\n\
 \t\t(def. \"" CONFIG_MAIN "\" and \"$HOME" CONFIG_HOME "\")\n\
  -d, --device\tMobilDock on this serial device (def. \"" DEF_DEVICE "\")\n\
+ -L, --log\tLog all important messages to this file (def. \"" DEF_LOGNAME "\")\n\
  -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
 \t\t(def. \"%s\")\n\
  -s, --smsc\tUse this SMS Center number (def. query from Siemens A1)\n\
  -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
 \t\t(def. \"%s\")\n\
  -s, --smsc\tUse this SMS Center number (def. query from Siemens A1)\n\
@@ -182,6 +236,7 @@ Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
 static const struct option longopts[]={
 {"config"  ,1,0,'c'},
 {"device"  ,1,0,'d'},
 static const struct option longopts[]={
 {"config"  ,1,0,'c'},
 {"device"  ,1,0,'d'},
+{"log"     ,1,0,'l'},
 {"lockfile",1,0,'l'},
 {"smsc"    ,1,0,'s'},
 {"maxretry",1,0,'m'},
 {"lockfile",1,0,'l'},
 {"smsc"    ,1,0,'s'},
 {"maxretry",1,0,'m'},
@@ -276,6 +331,7 @@ static struct {
        unsigned stamp;
        } optset[]={
                { 'd',&device   },
        unsigned stamp;
        } optset[]={
                { 'd',&device   },
+               { 'L',&logname  },
                { 'l',&lockfile },
                { 's',&smsc     },
                { 'm',&maxretry },
                { 'l',&lockfile },
                { 's',&smsc     },
                { 'm',&maxretry },
@@ -292,7 +348,7 @@ int i;
 
        seq++;
        optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
 
        seq++;
        optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
-       while ((optc=getopt_long(argp,args,"c:d:l:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
+       while ((optc=getopt_long(argp,args,"c:d:L:l:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
                case 'c':
                        if (cfgstacki>=NELEM(cfgstack)) {
                                error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
                case 'c':
                        if (cfgstacki>=NELEM(cfgstack)) {
                                error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
@@ -300,7 +356,7 @@ int i;
                                }
                        chk(cfgstack[cfgstacki++]=strdup(optarg));
                        break;
                                }
                        chk(cfgstack[cfgstacki++]=strdup(optarg));
                        break;
-               case 'd': case 'l': case 's': case 'm': case 'r': case 't': case 'T':
+               case 'd': case 'L': case 'l': case 's': case 'm': case 'r': case 't': case 'T':
                        for (i=0;i<NELEM(optset);i++)
                                if (optset[i].c==optc) {
                                        if (optset[i].stamp && optset[i].stamp!=seq) {
                        for (i=0;i<NELEM(optset);i++)
                                if (optset[i].c==optc) {
                                        if (optset[i].stamp && optset[i].stamp!=seq) {
@@ -369,7 +425,7 @@ static const struct {
                {&device,"device for communication"},
 #endif
        };
                {&device,"device for communication"},
 #endif
        };
-static char **emptycheck[]={&smsc,&body};
+static char **emptycheck[]={&logname,&smsc,&body};
 
 static void lockclose(int fd)
 {
 
 static void lockclose(int fd)
 {
@@ -429,9 +485,11 @@ remove:
        lockclose(fd);
 }
 
        lockclose(fd);
 }
 
+static char wasalarm=0;
 static void sigalarm(int signo)
 {
        signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
 static void sigalarm(int signo)
 {
        signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
+       wasalarm=1;
        if (verbose>=1) error("Timed out");
 }
 
        if (verbose>=1) error("Timed out");
 }
 
@@ -467,12 +525,11 @@ void *p;
        assert(catchdatal<=catchdatasiz);
 }
 
        assert(catchdatal<=catchdatasiz);
 }
 
+static int retrycnt=0;
 static void retrying(void)
 {
 static void retrying(void)
 {
-static int cnt=0;
-
-       if (++cnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
-       if (verbose>=2) error(".Retrying phase, %d out of %d..",cnt,maxretryn);
+       if (++retrycnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
+       if (verbose>=2) error(".Retrying phase, %d out of %d..",retrycnt,maxretryn);
 }
 
 static char *devcmd(const char *term,const char *catch,const char *send,...)
 }
 
 static char *devcmd(const char *term,const char *catch,const char *send,...)
@@ -527,6 +584,7 @@ err:
        fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
        bufl=0;
        record=NULL;
        fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
        bufl=0;
        record=NULL;
+       wasalarm=0;
        alarm(readtimen);
        for (;;) {
                blocking(0);
        alarm(readtimen);
        for (;;) {
                blocking(0);
@@ -538,7 +596,8 @@ err:
                        got=read(devfd,buf+bufl,1);
                        }
                if (got<=0) {
                        got=read(devfd,buf+bufl,1);
                        }
                if (got<=0) {
-                       error("Could read device data (ret=%d): %m",got);
+                       if (wasalarm) error("!Maximum response timeout (%ds) exceeded",readtimen);
+                       error("Couldn't read device data (ret=%d): %m",got);
                        goto err;
                        }
                s=buf+bufl;
                        goto err;
                        }
                s=buf+bufl;
@@ -730,7 +789,7 @@ int i;
 unsigned fatal=0;
 struct termios tios;
 
 unsigned fatal=0;
 struct termios tios;
 
-       pname=*argv;
+       if ((s=strrchr((pname=*argv),'/'))) pname=s+1;
        atexit(cleanup);
        signal(SIGTERM,(RETSIGTYPE (*)(int))cleanup);
        signal(SIGQUIT,(RETSIGTYPE (*)(int))cleanup);
        atexit(cleanup);
        signal(SIGTERM,(RETSIGTYPE (*)(int))cleanup);
        signal(SIGQUIT,(RETSIGTYPE (*)(int))cleanup);
@@ -759,6 +818,7 @@ char *buf=malloc(l+50);
                        free(*emptycheck[i]);
                             *emptycheck[i]=NULL;
                        }
                        free(*emptycheck[i]);
                             *emptycheck[i]=NULL;
                        }
+       if (!logname) logname=DEF_LOGNAME;
        if (!lockfile) lockfile=DEF_LOCKFILE;
        if (!device) device=DEF_DEVICE;
        if (body && readbody) {
        if (!lockfile) lockfile=DEF_LOCKFILE;
        if (!device) device=DEF_DEVICE;
        if (body && readbody) {
@@ -808,11 +868,22 @@ size_t l=strlen(device);
                        }
                error("!Invalid format-character '%c' in lockfile format-string, only \"%%s\" allowed",*s);
                }
                        }
                error("!Invalid format-character '%c' in lockfile format-string, only \"%%s\" allowed",*s);
                }
+       
+       if (*logname) {
+               if (!(logf=fopen(logname,"a")))
+                       error("!Error opening log \"%s\" for append: %m",logname);
+               logmsg("Starting up: " PACKAGE " " VERSION);
+               }
        genpdu();
                
        if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
        genpdu();
                
        if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
+time_t start,end;
                if (verbose>=1) error(".Locking device \"%s\" by \"%s\"..",device,lockreal);
                if (verbose>=1) error(".Locking device \"%s\" by \"%s\"..",device,lockreal);
+               time(&start);
                lockdevice();
                lockdevice();
+               time(&end);
+               if ((end-=start)>LOCKREPORT)
+                       logmsg("Device lock succeeded after %d seconds",end);
                }
        if (verbose>=1) error(".Opening device \"%s\"..",device);
        if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
                }
        if (verbose>=1) error(".Opening device \"%s\"..",device);
        if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
@@ -849,5 +920,6 @@ size_t l=strlen(device);
        if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);
        devcmd(NULL,NULL,"\r\nAT");
 
        if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);
        devcmd(NULL,NULL,"\r\nAT");
 
+       logmsg("SMS sent (after %d retries), message reference: %s",retrycnt,s);
        return(EXIT_SUCCESS);
 }
        return(EXIT_SUCCESS);
 }
diff --git a/setup.h b/setup.h
index 14d6e2c..aecda29 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -2,6 +2,9 @@
  * $Id$
  *
  * $Log$
  * $Id$
  *
  * $Log$
+ * Revision 1.4  1999/06/03 11:46:41  short
+ * Logging (--log) implemented.
+ *
  * Revision 1.3  1999/06/03 10:52:32  short
  * Commented all the entries in setup.h.
  *
  * Revision 1.3  1999/06/03 10:52:32  short
  * Commented all the entries in setup.h.
  *
@@ -17,6 +20,7 @@
 #define CONFIG_HOME    "/." PACKAGE "rc" /* user-local cfg file,
                                             $HOME is prepended automatically */
 #define DEF_DEVICE "/dev/mobildock" /* --device default */
 #define CONFIG_HOME    "/." PACKAGE "rc" /* user-local cfg file,
                                             $HOME is prepended automatically */
 #define DEF_DEVICE "/dev/mobildock" /* --device default */
+#define DEF_LOGNAME "" /* --log default */
 #ifndef DEF_LOCKFILE
 #define DEF_LOCKFILE "/var/lock/LCK..%s" /* --lockfile default */
 #endif
 #ifndef DEF_LOCKFILE
 #define DEF_LOCKFILE "/var/lock/LCK..%s" /* --lockfile default */
 #endif
@@ -32,6 +36,8 @@
 #define DEVLOCK_MAXEMPTY 1 /* seconds to wait while lockfile is empty
                               to prevent races during lock creation */
 #define MAXSENDTIME 2 /* max. allowed number of seconds for sending one char */
 #define DEVLOCK_MAXEMPTY 1 /* seconds to wait while lockfile is empty
                               to prevent races during lock creation */
 #define MAXSENDTIME 2 /* max. allowed number of seconds for sending one char */
+#define LOCKREPORT 15 /* report to logfile if device lock took more than
+                         this parameter taken in seconds */
 #define ADDR_NAT 0x81 /* PDU address type for non-'+' numbers */
 #define ADDR_INT 0x91 /* PDU address type for numbers prefixed by '+' */
 #define PDU_TYPE 0x11 /* first byte of PDU, SMS submit */
 #define ADDR_NAT 0x81 /* PDU address type for non-'+' numbers */
 #define ADDR_INT 0x91 /* PDU address type for numbers prefixed by '+' */
 #define PDU_TYPE 0x11 /* first byte of PDU, SMS submit */