summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/system.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/system.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings2/system.cpp108
1 files changed, 85 insertions, 23 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/system.cpp b/noncore/settings/networksettings2/networksettings2/system.cpp
index 2133d34..a579396 100644
--- a/noncore/settings/networksettings2/networksettings2/system.cpp
+++ b/noncore/settings/networksettings2/networksettings2/system.cpp
@@ -8,6 +8,7 @@
8#include <sys/ioctl.h> 8#include <sys/ioctl.h>
9#include <sys/socket.h> 9#include <sys/socket.h>
10#include <stdlib.h> 10#include <stdlib.h>
11#include <stdio.h>
11#include <fcntl.h> 12#include <fcntl.h>
12#include <errno.h> 13#include <errno.h>
13#include <unistd.h> 14#include <unistd.h>
@@ -17,6 +18,7 @@
17#include <qstringlist.h> 18#include <qstringlist.h>
18#include <qfile.h> 19#include <qfile.h>
19#include <qtextstream.h> 20#include <qtextstream.h>
21#include <qapplication.h>
20 22
21#include "resources.h" 23#include "resources.h"
22#include "system.h" 24#include "system.h"
@@ -35,7 +37,7 @@ static char Dig2Hex[] = {
35// get LOW nibble of byte 37// get LOW nibble of byte
36#define LN(x) Dig2Hex[((x)&0x0f)] 38#define LN(x) Dig2Hex[((x)&0x0f)]
37 39
38System::System( void ) : ProbedInterfaces() { 40System::System( void ) : QObject(), ProbedInterfaces() {
39 probeInterfaces(); 41 probeInterfaces();
40} 42}
41 43
@@ -47,7 +49,7 @@ System::~System( void ) {
47int System::runAsRoot( const QString & S ) { 49int System::runAsRoot( const QString & S ) {
48 QString MyS = S; 50 QString MyS = S;
49 char * usr = getenv("USER"); 51 char * usr = getenv("USER");
50 int rv; 52 char ch;
51 53
52 if( S.isEmpty() ) { 54 if( S.isEmpty() ) {
53 // loophole to start shell 55 // loophole to start shell
@@ -58,22 +60,47 @@ int System::runAsRoot( const QString & S ) {
58 MyS.prepend( "sudo " ); 60 MyS.prepend( "sudo " );
59 } 61 }
60 62
61 fprintf( stderr, "Executing %s\n", MyS.latin1() ); 63 Log(("Executing %s\n", MyS.latin1() ));
62 64
63 rv = system( MyS.latin1() ) ; 65 emit lineFromCommand( tr("Command : ") + MyS );
64 switch( rv ) { 66 emit lineFromCommand( "---------------" );
65 case -1 : 67 Log(( "Command : %s\n", MyS.latin1() ) );
66 // cannot fork 68 MyS += " 2>&1 ";
67 return 1; 69 OutputOfCmd = popen( MyS.latin1(), "r" ) ;
68 case 127 : 70 if( ! OutputOfCmd ) {
69 // cannot start shell 71 // cannot fork
70 return 2; 72 return 1;
71 default : 73 }
72 if( WEXITSTATUS(rv) != 0 ) { 74
73 // error in command 75 // read all data
74 return 3; 76 QString Line = "";
77 while( 1 ) {
78 if( fread( &ch, 1, 1, OutputOfCmd ) < 1 )
79 // eof
80 break;
81 if( ch == '\n' || ch == '\r' ) {
82 if( ! Line.isEmpty() ) {
83 Log(( "read cmd output : **%s**\n", Line.latin1() ) );
84 emit lineFromCommand( Line );
85 Line = "";
86 qApp->processEvents();
75 } 87 }
88 } else {
89 Line += ch;
90 }
91 }
92
93 if( ! Line.isEmpty() ) {
94 emit lineFromCommand( Line );
95 Log(( "read cmd output : **%s**\n", Line.latin1() ) );
96 }
97 Log(( "End of command\n", Line.latin1() ) );
98
99 if( pclose( OutputOfCmd ) < 0 ) {
100 // error in command
101 return 3;
76 } 102 }
103
77 // all is fine 104 // all is fine
78 return 0; 105 return 0;
79} 106}
@@ -198,7 +225,7 @@ void System::probeInterfaces( void ) {
198 225
199 if ( ! ( IFI = ProbedInterfaces.find( NicName ) ) ) { 226 if ( ! ( IFI = ProbedInterfaces.find( NicName ) ) ) {
200 // new nic 227 // new nic
201 fprintf( stderr, "NEWNIC %s\n", NicName.latin1()); 228 Log(("NEWNIC %s\n", NicName.latin1()));
202 IFI = new InterfaceInfo; 229 IFI = new InterfaceInfo;
203 IFI->Name = line.left(loc); 230 IFI->Name = line.left(loc);
204 IFI->NetNode = 0; 231 IFI->NetNode = 0;
@@ -225,8 +252,8 @@ void System::probeInterfaces( void ) {
225 IFI->MACAddress = ""; 252 IFI->MACAddress = "";
226 253
227 if( ioctl(sockfd, SIOCGIFHWADDR, &ifrs) >= 0 ) { 254 if( ioctl(sockfd, SIOCGIFHWADDR, &ifrs) >= 0 ) {
228 fprintf( stderr, "%s = %d\n", IFI->Name.latin1(), 255 Log(("%s = %d\n", IFI->Name.latin1(),
229 ifrs.ifr_hwaddr.sa_family ); 256 ifrs.ifr_hwaddr.sa_family ));
230 257
231 IFI->CardType = ifrs.ifr_hwaddr.sa_family; 258 IFI->CardType = ifrs.ifr_hwaddr.sa_family;
232 switch( ifrs.ifr_hwaddr.sa_family ) { 259 switch( ifrs.ifr_hwaddr.sa_family ) {
@@ -293,7 +320,7 @@ void System::probeInterfaces( void ) {
293 } 320 }
294 } 321 }
295 } else // else already probed before -> just update 322 } else // else already probed before -> just update
296 fprintf( stderr, "OLDNIC %s\n", NicName.latin1()); 323 Log(("OLDNIC %s\n", NicName.latin1()));
297 324
298 // get dynamic info 325 // get dynamic info
299 if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) { 326 if( ioctl(sockfd, SIOCGIFFLAGS, &ifrs) >= 0 ) {
@@ -323,7 +350,7 @@ void System::probeInterfaces( void ) {
323 } else { 350 } else {
324 IFI->Netmask = ""; 351 IFI->Netmask = "";
325 } 352 }
326 fprintf( stderr, "NIC %s UP %d\n", NicName.latin1(), IFI->IsUp ); 353 Log(("NIC %s UP %d\n", NicName.latin1(), IFI->IsUp ));
327 } 354 }
328} 355}
329 356
@@ -332,7 +359,7 @@ void System::execAsUser( QString & Cmd, char * argv[] ) {
332 359
333 if( CU.UserName.isEmpty() ) { 360 if( CU.UserName.isEmpty() ) {
334 // if we come here, the exec was not successfull 361 // if we come here, the exec was not successfull
335 fprintf( stderr, "User not known \n" ); 362 Log(("User not known \n" ));
336 return; 363 return;
337 } 364 }
338 365
@@ -351,5 +378,40 @@ void System::execAsUser( QString & Cmd, char * argv[] ) {
351 execve( Cmd.latin1(), argv, envp ); 378 execve( Cmd.latin1(), argv, envp );
352 379
353 // if we come here, the exec was not successfull 380 // if we come here, the exec was not successfull
354 fprintf( stderr, "Could not exec : %d\n", errno ); 381 Log(("Could not exec : %d\n", errno ));
382}
383
384#include <stdarg.h>
385static FILE * logf = 0;
386
387void VLog( char * Format, ... ) {
388 va_list l;
389
390 va_start(l, Format );
391
392 if( logf == (FILE *)0 ) {
393 // logf = fopen( "/tmp/ns2log", "a" );
394 logf = stderr;
395 if( ! logf ) {
396 fprintf( stderr, "Cannot open logfile /tmp/ns2log %d\n",
397 errno );
398 logf = (FILE *)1;
399 } else {
400 fprintf( logf, "____ OPEN LOGFILE ____\n");
401 }
402 }
403
404 if( (long)logf > 1 ) {
405 vfprintf( logf, Format, l );
406 }
407 va_end( l );
408
409}
410
411void LogClose( void ) {
412 if( (long)logf > 1 ) {
413 fprintf( logf, "____ CLOSE LOGFILE ____\n");
414 fclose( logf );
415 logf = 0;
416 }
355} 417}