summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/gprs/GPRSrun.cpp
blob: fd61f3a9c08dd03c78fed3bb2a5072abff708f74 (plain)
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
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <qdir.h>
#include <system.h>
#include <resources.h>
#include <netnode.h>
#include "GPRSrun.h"

State_t GPRSRun::detectState( void ) { 

      // is pppd still running ?
      // is rfcomm still active
      NetworkSetup * NC = networkSetup();
      InterfaceInfo * I = NC->assignedInterface();

      QDir D("/var/run");

      if( I ) {
        // has some pppx attached
        return ( I->IsUp ) ? IsUp : Available;
      }

      // check ppp itself and figure out interface

      odebug << "Check for ppp " << NC->name() << oendl;
      if( D.exists( QString("ppp-")+removeSpaces(NC->name())+".pid") ) {
        // get pid and check if pppd is still running
        QFile F( D.path()+"/ppp-"+removeSpaces(NC->name())+".pid");

        odebug << "PPP PID " << F.name() << oendl;
        if( F.open( IO_ReadOnly ) ) {
          QTextStream TS(&F);
          QString X = TS.readLine();
          PPPPid = X.toULong();
          int rv;

          rv = ::kill( PPPPid, 0 );
          if( rv == 0 ||
              ( rv < 0 && errno == EPERM )
            ) {
            // pppd is still up
            X = TS.readLine();
            I = NSResources->system().findInterface(X);

            odebug << "ppp running : IFace " << X << " = " << (long)I << oendl;

            if( I ) {
              NC->assignInterface( I );
              return (I->IsUp) ? IsUp : Available;
            }

            return Available;

          } else {
            // pppd is down
            PPPPid = 0;
          } 
        } // else pppd is down
      }
      NC->assignInterface( 0 );
      return Unknown;
}

QString GPRSRun::setMyState( NetworkSetup * NC, Action_t A , bool ) { 

    if( A == Up ) {
      // start ppp on deviceFile
      QStringList SL;
      SL << "pon"
         << removeSpaces( NC->name() )
         << NC->device()->deviceFile();

      if( ! NSResources->system().execAsUser( SL ) ) {
        return QString("Cannot start pppd for %1").arg(NC->name());
      }
    } else if ( A == Down ) {
      if( PPPPid == 0 ) {
        detectState();
      }
      if( PPPPid ) {
        QStringList SL;

        SL << "poff"
           << removeSpaces( NC->name() );

        if( ! NSResources->system().execAsUser( SL ) ) {
          return QString("Cannot terminate pppd for %1").arg(NC->name());
        }
        NC->assignInterface( 0 );
        odebug << "ppp stopped " << oendl;
        PPPPid = 0;
      }
    }

    return QString();
}

bool GPRSRun::handlesInterface( const QString & S ) {
    return Pat.match( S ) >= 0;
}

bool GPRSRun::handlesInterface( InterfaceInfo * I ) {
    return handlesInterface( I->Name );
}