Diffstat (limited to 'noncore/settings/networksettings/ppp/modem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/modem.cpp | 85 |
1 files changed, 71 insertions, 14 deletions
diff --git a/noncore/settings/networksettings/ppp/modem.cpp b/noncore/settings/networksettings/ppp/modem.cpp index d23fee4..3dbc8c3 100644 --- a/noncore/settings/networksettings/ppp/modem.cpp +++ b/noncore/settings/networksettings/ppp/modem.cpp | |||
@@ -48,17 +48,14 @@ | |||
48 | #endif | 48 | #endif |
49 | 49 | ||
50 | #define strlcpy strcpy | 50 | #define strlcpy strcpy |
51 | #include "auth.h" | 51 | #include "auth.h" |
52 | #include "modem.h" | 52 | #include "modem.h" |
53 | #include "pppdata.h" | 53 | #include "pppdata.h" |
54 | //#include <klocale.h> | ||
55 | #define i18n QObject::tr | ||
56 | #define qError qDebug | 54 | #define qError qDebug |
57 | //#include <kdebug.h> | 55 | |
58 | //#include <config.h> | ||
59 | 56 | ||
60 | #define MY_ASSERT(x) if (!(x)) { \ | 57 | #define MY_ASSERT(x) if (!(x)) { \ |
61 | qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \ | 58 | qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \ |
62 | exit(1); } | 59 | exit(1); } |
63 | 60 | ||
64 | 61 | ||
@@ -96,13 +93,13 @@ const char* pppdPath() { | |||
96 | Modem::Modem( PPPData* pd ) | 93 | Modem::Modem( PPPData* pd ) |
97 | { | 94 | { |
98 | _pppdata = pd; | 95 | _pppdata = pd; |
99 | modemfd = -1; | 96 | modemfd = -1; |
100 | _pppdExitStatus = -1; | 97 | _pppdExitStatus = -1; |
101 | pppdPid = -1; | 98 | pppdPid = -1; |
102 | sn = 0L; | 99 | sn = m_modemDebug = 0L; |
103 | data_mode = false; | 100 | data_mode = false; |
104 | modem_is_locked = false; | 101 | modem_is_locked = false; |
105 | lockfile[0] = '\0'; | 102 | lockfile[0] = '\0'; |
106 | device = "/dev/modem"; | 103 | device = "/dev/modem"; |
107 | } | 104 | } |
108 | 105 | ||
@@ -166,28 +163,28 @@ bool Modem::opentty() { | |||
166 | 163 | ||
167 | //begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { | 164 | //begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { |
168 | close(modemfd); | 165 | close(modemfd); |
169 | device = _pppdata->modemDevice(); | 166 | device = _pppdata->modemDevice(); |
170 | if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) { | 167 | if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) { |
171 | qDebug("error opening modem device !"); | 168 | qDebug("error opening modem device !"); |
172 | errmsg = i18n("Unable to open modem."); | 169 | errmsg = QObject::tr("Unable to open modem."); |
173 | return false; | 170 | return false; |
174 | } | 171 | } |
175 | //bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { | 172 | //bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { |
176 | //} | 173 | //} |
177 | 174 | ||
178 | #if 0 | 175 | #if 0 |
179 | if(_pppdata->UseCDLine()) { | 176 | if(_pppdata->UseCDLine()) { |
180 | if(ioctl(modemfd, TIOCMGET, &flags) == -1) { | 177 | if(ioctl(modemfd, TIOCMGET, &flags) == -1) { |
181 | errmsg = i18n("Unable to detect state of CD line."); | 178 | errmsg = QObject::tr("Unable to detect state of CD line."); |
182 | ::close(modemfd); | 179 | ::close(modemfd); |
183 | modemfd = -1; | 180 | modemfd = -1; |
184 | return false; | 181 | return false; |
185 | } | 182 | } |
186 | if ((flags&TIOCM_CD) == 0) { | 183 | if ((flags&TIOCM_CD) == 0) { |
187 | errmsg = i18n("The modem is not ready."); | 184 | errmsg = QObject::tr("The modem is not ready."); |
188 | ::close(modemfd); | 185 | ::close(modemfd); |
189 | modemfd = -1; | 186 | modemfd = -1; |
190 | return false; | 187 | return false; |
191 | } | 188 | } |
192 | } | 189 | } |
193 | #endif | 190 | #endif |
@@ -197,13 +194,13 @@ bool Modem::opentty() { | |||
197 | 194 | ||
198 | if(tcgetattr(modemfd, &tty) < 0){ | 195 | if(tcgetattr(modemfd, &tty) < 0){ |
199 | // this helps in some cases | 196 | // this helps in some cases |
200 | tcsendbreak(modemfd, 0); | 197 | tcsendbreak(modemfd, 0); |
201 | sleep(1); | 198 | sleep(1); |
202 | if(tcgetattr(modemfd, &tty) < 0){ | 199 | if(tcgetattr(modemfd, &tty) < 0){ |
203 | errmsg = i18n("The modem is busy."); | 200 | errmsg = QObject::tr("The modem is busy."); |
204 | ::close(modemfd); | 201 | ::close(modemfd); |
205 | modemfd = -1; | 202 | modemfd = -1; |
206 | return false; | 203 | return false; |
207 | } | 204 | } |
208 | } | 205 | } |
209 | 206 | ||
@@ -242,31 +239,31 @@ bool Modem::opentty() { | |||
242 | cfsetospeed(&tty, modemspeed()); | 239 | cfsetospeed(&tty, modemspeed()); |
243 | cfsetispeed(&tty, modemspeed()); | 240 | cfsetispeed(&tty, modemspeed()); |
244 | 241 | ||
245 | tcdrain(modemfd); | 242 | tcdrain(modemfd); |
246 | 243 | ||
247 | if(tcsetattr(modemfd, TCSANOW, &tty) < 0){ | 244 | if(tcsetattr(modemfd, TCSANOW, &tty) < 0){ |
248 | errmsg = i18n("The modem is busy."); | 245 | errmsg = QObject::tr("The modem is busy."); |
249 | ::close(modemfd); | 246 | ::close(modemfd); |
250 | modemfd=-1; | 247 | modemfd=-1; |
251 | return false; | 248 | return false; |
252 | } | 249 | } |
253 | 250 | ||
254 | errmsg = i18n("Modem Ready."); | 251 | errmsg = QObject::tr("Modem Ready."); |
255 | return true; | 252 | return true; |
256 | } | 253 | } |
257 | 254 | ||
258 | 255 | ||
259 | bool Modem::closetty() { | 256 | bool Modem::closetty() { |
260 | if(modemfd >=0 ) { | 257 | if(modemfd >=0 ) { |
261 | stop(); | 258 | stop(); |
262 | /* discard data not read or transmitted */ | 259 | /* discard data not read or transmitted */ |
263 | tcflush(modemfd, TCIOFLUSH); | 260 | tcflush(modemfd, TCIOFLUSH); |
264 | 261 | ||
265 | if(tcsetattr(modemfd, TCSANOW, &initial_tty) < 0){ | 262 | if(tcsetattr(modemfd, TCSANOW, &initial_tty) < 0){ |
266 | errmsg = i18n("Can't restore tty settings: tcsetattr()\n"); | 263 | errmsg = QObject::tr("Can't restore tty settings: tcsetattr()\n"); |
267 | ::close(modemfd); | 264 | ::close(modemfd); |
268 | modemfd = -1; | 265 | modemfd = -1; |
269 | return false; | 266 | return false; |
270 | } | 267 | } |
271 | ::close(modemfd); | 268 | ::close(modemfd); |
272 | modemfd = -1; | 269 | modemfd = -1; |
@@ -410,13 +407,13 @@ bool Modem::hangup() { | |||
410 | signal(SIGALRM, SIG_IGN); | 407 | signal(SIGALRM, SIG_IGN); |
411 | } else { | 408 | } else { |
412 | // we reach this point if the alarm handler got called | 409 | // we reach this point if the alarm handler got called |
413 | closetty(); | 410 | closetty(); |
414 | close(modemfd); | 411 | close(modemfd); |
415 | modemfd = -1; | 412 | modemfd = -1; |
416 | errmsg = i18n("The modem does not respond."); | 413 | errmsg = QObject::tr("The modem does not respond."); |
417 | return false; | 414 | return false; |
418 | } | 415 | } |
419 | 416 | ||
420 | #ifndef __svr4__ // drops DTR but doesn't set it afterwards again. not good for init. | 417 | #ifndef __svr4__ // drops DTR but doesn't set it afterwards again. not good for init. |
421 | tcgetattr(modemfd, &temptty); | 418 | tcgetattr(modemfd, &temptty); |
422 | cfsetospeed(&temptty, B0); | 419 | cfsetospeed(&temptty, B0); |
@@ -552,13 +549,13 @@ QString Modem::parseModemSpeed(const QString &s) { | |||
552 | } | 549 | } |
553 | } | 550 | } |
554 | } | 551 | } |
555 | } | 552 | } |
556 | 553 | ||
557 | if(rx == -1 && tx == -1) | 554 | if(rx == -1 && tx == -1) |
558 | result = i18n("Unknown speed"); | 555 | result = QObject::tr("Unknown speed"); |
559 | else if(tx == -1) | 556 | else if(tx == -1) |
560 | result.setNum(rx); | 557 | result.setNum(rx); |
561 | else if(rx == -1) // should not happen | 558 | else if(rx == -1) // should not happen |
562 | result.setNum(tx); | 559 | result.setNum(tx); |
563 | else | 560 | else |
564 | result.sprintf("%d/%d", rx, tx); | 561 | result.sprintf("%d/%d", rx, tx); |
@@ -874,16 +871,20 @@ bool Modem::execpppd(const char *arguments) { | |||
874 | 871 | ||
875 | if(modemfd<0) | 872 | if(modemfd<0) |
876 | return false; | 873 | return false; |
877 | 874 | ||
878 | _pppdExitStatus = -1; | 875 | _pppdExitStatus = -1; |
879 | 876 | ||
877 | (void)::pipe( m_pppdLOG ); | ||
878 | |||
880 | switch(pppdPid = fork()) | 879 | switch(pppdPid = fork()) |
881 | { | 880 | { |
882 | case -1: | 881 | case -1: |
883 | fprintf(stderr,"In parent: fork() failed\n"); | 882 | fprintf(stderr,"In parent: fork() failed\n"); |
883 | ::close( m_pppdLOG[0] ); | ||
884 | ::close( m_pppdLOG[1] ); | ||
884 | return false; | 885 | return false; |
885 | break; | 886 | break; |
886 | 887 | ||
887 | case 0: | 888 | case 0: |
888 | // let's parse the arguments the user supplied into UNIX suitable form | 889 | // let's parse the arguments the user supplied into UNIX suitable form |
889 | // that is a list of pointers each pointing to exactly one word | 890 | // that is a list of pointers each pointing to exactly one word |
@@ -899,15 +900,19 @@ bool Modem::execpppd(const char *arguments) { | |||
899 | if(ioctl(modemfd, TIOCSPGRP, &pgrpid)<0) | 900 | if(ioctl(modemfd, TIOCSPGRP, &pgrpid)<0) |
900 | fprintf(stderr, "ioctl() failed.\n"); | 901 | fprintf(stderr, "ioctl() failed.\n"); |
901 | #endif | 902 | #endif |
902 | if(tcsetpgrp(modemfd, pgrpid)<0) | 903 | if(tcsetpgrp(modemfd, pgrpid)<0) |
903 | fprintf(stderr, "tcsetpgrp() failed.\n"); | 904 | fprintf(stderr, "tcsetpgrp() failed.\n"); |
904 | 905 | ||
906 | ::close( m_pppdLOG[0] ); | ||
907 | ::setenv( "LANG", "C", 1 ); // overwrite | ||
908 | dup2(m_pppdLOG[1], 11 ); // for logfd 11 | ||
905 | dup2(modemfd, 0); | 909 | dup2(modemfd, 0); |
906 | dup2(modemfd, 1); | 910 | dup2(modemfd, 1); |
907 | 911 | ||
912 | |||
908 | switch (checkForInterface()) { | 913 | switch (checkForInterface()) { |
909 | case 1: | 914 | case 1: |
910 | fprintf(stderr, "Cannot determine if kernel supports ppp.\n"); | 915 | fprintf(stderr, "Cannot determine if kernel supports ppp.\n"); |
911 | break; | 916 | break; |
912 | case -1: | 917 | case -1: |
913 | fprintf(stderr, "Kernel does not support ppp, oops.\n"); | 918 | fprintf(stderr, "Kernel does not support ppp, oops.\n"); |
@@ -921,21 +926,41 @@ bool Modem::execpppd(const char *arguments) { | |||
921 | _exit(0); | 926 | _exit(0); |
922 | break; | 927 | break; |
923 | 928 | ||
924 | default: | 929 | default: |
925 | qDebug("In parent: pppd pid %d\n",pppdPid); | 930 | qDebug("In parent: pppd pid %d\n",pppdPid); |
926 | close(modemfd); | 931 | close(modemfd); |
932 | |||
933 | ::close( m_pppdLOG[1] ); | ||
934 | // set it to nonblocking io | ||
935 | int flag = ::fcntl( m_pppdLOG[0], F_GETFL ); | ||
936 | |||
937 | if ( !(flag & O_NONBLOCK) ) { | ||
938 | qDebug("Setting nonblocking io"); | ||
939 | flag |= O_NONBLOCK; | ||
940 | ::fcntl(m_pppdLOG[0], F_SETFL, flag ); | ||
941 | } | ||
942 | |||
943 | delete m_modemDebug; | ||
944 | m_modemDebug = new QSocketNotifier(m_pppdLOG[0], QSocketNotifier::Read, this ); | ||
945 | connect(m_modemDebug, SIGNAL(activated(int) ), | ||
946 | this, SLOT(slotModemDebug(int) ) ); | ||
947 | |||
927 | modemfd = -1; | 948 | modemfd = -1; |
949 | m_pppdDev = QString::fromLatin1("ppp0"); | ||
928 | return true; | 950 | return true; |
929 | break; | 951 | break; |
930 | } | 952 | } |
931 | } | 953 | } |
932 | 954 | ||
933 | 955 | ||
934 | bool Modem::killpppd() { | 956 | bool Modem::killpppd() { |
957 | qDebug("In killpppd and pid is %d", pppdPid ); | ||
935 | if(pppdPid > 0) { | 958 | if(pppdPid > 0) { |
959 | delete m_modemDebug; | ||
960 | m_modemDebug = 0; | ||
936 | qDebug("In killpppd(): Sending SIGTERM to %d\n", pppdPid); | 961 | qDebug("In killpppd(): Sending SIGTERM to %d\n", pppdPid); |
937 | if(kill(pppdPid, SIGTERM) < 0) { | 962 | if(kill(pppdPid, SIGTERM) < 0) { |
938 | qDebug("Error terminating %d. Sending SIGKILL\n", pppdPid); | 963 | qDebug("Error terminating %d. Sending SIGKILL\n", pppdPid); |
939 | if(kill(pppdPid, SIGKILL) < 0) { | 964 | if(kill(pppdPid, SIGKILL) < 0) { |
940 | qDebug("Error killing %d\n", pppdPid); | 965 | qDebug("Error killing %d\n", pppdPid); |
941 | return false; | 966 | return false; |
@@ -1018,6 +1043,38 @@ int Modem::openResolv(int flags) | |||
1018 | 1043 | ||
1019 | bool Modem::setHostname(const QString & name) | 1044 | bool Modem::setHostname(const QString & name) |
1020 | { | 1045 | { |
1021 | return sethostname(name, name.length()) == 0; | 1046 | return sethostname(name, name.length()) == 0; |
1022 | } | 1047 | } |
1023 | 1048 | ||
1049 | QString Modem::pppDevice()const { | ||
1050 | return m_pppdDev; | ||
1051 | } | ||
1052 | void Modem::setPPPDevice( const QString& dev ) { | ||
1053 | m_pppdDev = dev; | ||
1054 | } | ||
1055 | pid_t Modem::pppPID()const { | ||
1056 | return pppdPid; | ||
1057 | } | ||
1058 | void Modem::setPPPDPid( pid_t pid ) { | ||
1059 | qDebug("Modem setting pid"); | ||
1060 | _pppdExitStatus = -1; | ||
1061 | pppdPid = pid; | ||
1062 | modemfd = -1; | ||
1063 | } | ||
1064 | void Modem::slotModemDebug(int fd) { | ||
1065 | char buf[2049]; | ||
1066 | int len; | ||
1067 | |||
1068 | // read in pppd data look for Using interface | ||
1069 | // then read the interface | ||
1070 | // we limit to 10 device now 0-9 | ||
1071 | if((len = ::read(fd, buf, 2048)) > 0) { | ||
1072 | buf[len+1] = '\0'; | ||
1073 | char *found; | ||
1074 | if ( (found = ::strstr(buf, "Using interface ") ) ) { | ||
1075 | found += 16; | ||
1076 | m_pppdDev = QString::fromLatin1(found, 5 ); | ||
1077 | m_pppdDev = m_pppdDev.simplifyWhiteSpace(); | ||
1078 | } | ||
1079 | } | ||
1080 | } | ||