1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#ident "$Id: do_stat.c,v 4.3 2014/02/02 12:50:48 gert Exp $ Copyright (c) Gert Doering"
/* do_stat.c
*
* This module handles grabbing the modem call statistics and logging it
* (basically, it's a stripped down version of do_chat() with different
* logging capabilities, working only on a line-by-line basis)
*/
#include <stdio.h>
#include "syslibs.h"
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#ifndef sunos4
#include <sys/ioctl.h>
#endif
#ifndef EINTR
#include <errno.h>
#endif
#include "mgetty.h"
#include "policy.h"
#include "tio.h"
static boolean has_timeout;
static RETSIGTYPE timeout(SIG_HDLR_ARGS)
{
has_timeout = TRUE;
}
void get_statistics _P3( (fd, expect_send, tgt_file),
int fd, char * expect_send[], char * tgt_file )
{
char line[MAXLINE];
int r;
FILE * fp = NULL; /* target file */
if ( tgt_file != NULL ) /* to file, not to lprintf() */
{
fp = fopen( tgt_file, "a" );
if ( fp == NULL )
lprintf( L_ERROR, "do_stat: can't open %s", tgt_file );
else /* open ok, log time */
{
time_t now = time(NULL);
char *snow = ctime( &now );
if ( snow )
fprintf( fp, "--- %.*s ---\n", (int) strlen(snow)-1, snow);
}
}
while ( *expect_send != NULL )
{
/* handle "expect" part */
r=0;
if ( **expect_send != 0 ) /* !empty string */
{
lprintf( L_MESG, "do_stat: expect '%s'", *expect_send );
/* 10 seconds timeout should be sufficient here */
signal( SIGALRM, timeout );
alarm(10);
while( 1 )
{
if ( read( fd, &line[r], 1 ) != 1 )
{
alarm(0);
if ( has_timeout )
lprintf( L_WARN, "do_stat: timeout" );
else
lprintf( L_ERROR, "do_stat: error reading data" );
if ( fp != NULL ) fclose( fp );
return;
}
if ( line[r] == '\r' || line[r] == '\n' ) /* line full */
{
line[r] = 0; /* terminate */
if ( r != 0 ) /* log... */
{
if ( fp == NULL ) /* to logfile */
lprintf( L_MESG, "*** %s", line );
else /* to stat.f. */
fprintf( fp, "%s\n", line );
}
if ( strncmp( line, *expect_send,
strlen( *expect_send ) ) == 0 )
{
lputs( L_MESG, " ** found **" ); break;
}
r=0;
}
else if ( r<sizeof(line)-1) r++;
} /* expect loop */
alarm(0);
} /* if (!empty) */
expect_send++;
if ( *expect_send == NULL ) break;
/* handle "send" part */
do_chat_send( fd, *expect_send );
expect_send++;
}
if ( fp != NULL ) fclose( fp );
}
|