nitlog: oops, didn't deal properly with posts in months that haven't
[wvapps.git] / wvdial / pppmon.cc
blob1c72a3754e5c44d1e487cc7562f4b0f4c66e8664
3 // not finished
4 // author: arvin@suse.de
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <wvstream.h>
14 #include "wvdialmon.h"
15 #include "wvfork.h"
16 #include "wvfdstream.h"
19 WvStream *pppd_log = NULL; // to read messages of pppd
21 WvDialMon pppd_mon;
24 int main( int argc, char ** argv )
26 int argc_ppp = 0;
27 char *argv_ppp[ argc+10 ];
29 argv_ppp[argc_ppp++] = "/usr/sbin/pppd";
31 for( int i = 1; i < argc; i++ )
32 argv_ppp[argc_ppp++] = argv[i];
35 // open a pipe to access the messages of pppd
36 int pppd_msgfd[2];
37 if( pipe( pppd_msgfd ) == -1 ) {
38 fprintf( stderr, "pipe failed: %s\n", strerror(errno) );
39 exit( EXIT_FAILURE );
42 char buffer[20];
43 sprintf( buffer, "%d", pppd_msgfd[1] );
44 argv_ppp[argc_ppp++] = "logfd";
45 argv_ppp[argc_ppp++] = buffer;
47 pppd_log = new WvFDStream( pppd_msgfd[0] );
49 pppd_mon.setconnectmsg( "Connected..." );
53 for( int i = 0; i < argc_ppp; i++ )
54 printf( "%s\n", argv_ppp[i] );
58 // fork and exec pppd
59 pid_t pid = wvfork();
61 if( pid == (pid_t) 0 ) { // we are the child
62 argv_ppp[argc_ppp] = NULL;
63 execv( argv_ppp[0], argv_ppp );
64 fprintf( stderr, "exec failed: %s\n", strerror(errno) );
65 exit( EXIT_FAILURE );
68 if( pid < (pid_t) 0 ) { // the fork failed
69 fprintf( stderr, "error: can't fork child process\n" );
70 exit( EXIT_FAILURE );
75 ppp_pipe = new WvPipe( argv_ppp[0], argv_ppp, false, false, false );
79 // install signals
83 for( ;; ) {
85 // see if pppd is still alive
90 // now watch for messages and output to stdout
92 if( pppd_log != NULL && pppd_log->isok() ) {
94 char *line;
96 do {
98 line = pppd_log->blocking_getline( 100 );
99 if( line != NULL ) {
100 char *buffer1 = pppd_mon.analyse_line( line );
101 if( buffer1 != NULL && buffer1[0] != '\0' ) {
102 char buffer2[ strlen( buffer1 ) + 10 ];
103 sprintf( buffer2, "pppd: %s", buffer1 );
104 fprintf( stdout, "%s", buffer2 );
107 } while( line != NULL );
113 exit( EXIT_SUCCESS );