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 | |||
@@ -30,97 +30,94 @@ | |||
30 | #include <fcntl.h> | 30 | #include <fcntl.h> |
31 | #include <signal.h> | 31 | #include <signal.h> |
32 | #include <sys/ioctl.h> | 32 | #include <sys/ioctl.h> |
33 | #include <sys/types.h> | 33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | 34 | #include <sys/stat.h> |
35 | #include <setjmp.h> | 35 | #include <setjmp.h> |
36 | #include <regex.h> | 36 | #include <regex.h> |
37 | #include <qregexp.h> | 37 | #include <qregexp.h> |
38 | #include <assert.h> | 38 | #include <assert.h> |
39 | #include <string.h> | 39 | #include <string.h> |
40 | 40 | ||
41 | #ifdef HAVE_RESOLV_H | 41 | #ifdef HAVE_RESOLV_H |
42 | # include <arpa/nameser.h> | 42 | # include <arpa/nameser.h> |
43 | # include <resolv.h> | 43 | # include <resolv.h> |
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | #ifndef _PATH_RESCONF | 46 | #ifndef _PATH_RESCONF |
47 | #define _PATH_RESCONF "/etc/resolv.conf" | 47 | #define _PATH_RESCONF "/etc/resolv.conf" |
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 | ||
65 | static sigjmp_buf jmp_buffer; | 62 | static sigjmp_buf jmp_buffer; |
66 | 63 | ||
67 | //Modem *Modem::modem = 0; | 64 | //Modem *Modem::modem = 0; |
68 | 65 | ||
69 | 66 | ||
70 | const char* pppdPath() { | 67 | const char* pppdPath() { |
71 | // wasting a few bytes | 68 | // wasting a few bytes |
72 | static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; | 69 | static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; |
73 | static char *pppdPath = 0L; | 70 | static char *pppdPath = 0L; |
74 | char *p; | 71 | char *p; |
75 | 72 | ||
76 | if(pppdPath == 0L) { | 73 | if(pppdPath == 0L) { |
77 | const char *c = PPPDSEARCHPATH; | 74 | const char *c = PPPDSEARCHPATH; |
78 | while(*c != '\0') { | 75 | while(*c != '\0') { |
79 | while(*c == ':') | 76 | while(*c == ':') |
80 | c++; | 77 | c++; |
81 | p = buffer; | 78 | p = buffer; |
82 | while(*c != '\0' && *c != ':') | 79 | while(*c != '\0' && *c != ':') |
83 | *p++ = *c++; | 80 | *p++ = *c++; |
84 | *p = '\0'; | 81 | *p = '\0'; |
85 | strcat(p, "/"); | 82 | strcat(p, "/"); |
86 | strcat(p, PPPDNAME); | 83 | strcat(p, PPPDNAME); |
87 | if(access(buffer, F_OK) == 0) | 84 | if(access(buffer, F_OK) == 0) |
88 | return (pppdPath = buffer); | 85 | return (pppdPath = buffer); |
89 | } | 86 | } |
90 | } | 87 | } |
91 | 88 | ||
92 | return pppdPath; | 89 | return pppdPath; |
93 | } | 90 | } |
94 | 91 | ||
95 | 92 | ||
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 | ||
109 | 106 | ||
110 | Modem::~Modem() | 107 | Modem::~Modem() |
111 | { | 108 | { |
112 | } | 109 | } |
113 | 110 | ||
114 | 111 | ||
115 | speed_t Modem::modemspeed() { | 112 | speed_t Modem::modemspeed() { |
116 | // convert the string modem speed int the gpppdata object to a t_speed type | 113 | // convert the string modem speed int the gpppdata object to a t_speed type |
117 | // to set the modem. The constants here should all be ifdef'd because | 114 | // to set the modem. The constants here should all be ifdef'd because |
118 | // other systems may not have them | 115 | // other systems may not have them |
119 | int i = _pppdata->speed().toInt()/100; | 116 | int i = _pppdata->speed().toInt()/100; |
120 | 117 | ||
121 | switch(i) { | 118 | switch(i) { |
122 | case 24: | 119 | case 24: |
123 | return B2400; | 120 | return B2400; |
124 | break; | 121 | break; |
125 | case 96: | 122 | case 96: |
126 | return B9600; | 123 | return B9600; |
@@ -148,143 +145,143 @@ speed_t Modem::modemspeed() { | |||
148 | return B230400; | 145 | return B230400; |
149 | break; | 146 | break; |
150 | #endif | 147 | #endif |
151 | 148 | ||
152 | #ifdef B460800 | 149 | #ifdef B460800 |
153 | case 4608: | 150 | case 4608: |
154 | return B460800; | 151 | return B460800; |
155 | break; | 152 | break; |
156 | #endif | 153 | #endif |
157 | 154 | ||
158 | default: | 155 | default: |
159 | return B38400; | 156 | return B38400; |
160 | break; | 157 | break; |
161 | } | 158 | } |
162 | } | 159 | } |
163 | 160 | ||
164 | bool Modem::opentty() { | 161 | bool Modem::opentty() { |
165 | // int flags; | 162 | // int flags; |
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 |
194 | 191 | ||
195 | tcdrain (modemfd); | 192 | tcdrain (modemfd); |
196 | tcflush (modemfd, TCIOFLUSH); | 193 | tcflush (modemfd, TCIOFLUSH); |
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 | ||
210 | memset(&initial_tty,'\0',sizeof(initial_tty)); | 207 | memset(&initial_tty,'\0',sizeof(initial_tty)); |
211 | 208 | ||
212 | initial_tty = tty; | 209 | initial_tty = tty; |
213 | 210 | ||
214 | tty.c_cc[VMIN] = 0; // nonblocking | 211 | tty.c_cc[VMIN] = 0; // nonblocking |
215 | tty.c_cc[VTIME] = 0; | 212 | tty.c_cc[VTIME] = 0; |
216 | tty.c_oflag = 0; | 213 | tty.c_oflag = 0; |
217 | tty.c_lflag = 0; | 214 | tty.c_lflag = 0; |
218 | 215 | ||
219 | tty.c_cflag &= ~(CSIZE | CSTOPB | PARENB); | 216 | tty.c_cflag &= ~(CSIZE | CSTOPB | PARENB); |
220 | tty.c_cflag |= CS8 | CREAD; | 217 | tty.c_cflag |= CS8 | CREAD; |
221 | tty.c_cflag |= CLOCAL; // ignore modem status lines | 218 | tty.c_cflag |= CLOCAL; // ignore modem status lines |
222 | tty.c_iflag = IGNBRK | IGNPAR /* | ISTRIP */ ; | 219 | tty.c_iflag = IGNBRK | IGNPAR /* | ISTRIP */ ; |
223 | tty.c_lflag &= ~ICANON; // non-canonical mode | 220 | tty.c_lflag &= ~ICANON; // non-canonical mode |
224 | tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHOKE); | 221 | tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHOKE); |
225 | 222 | ||
226 | 223 | ||
227 | if(_pppdata->flowcontrol() != "None") { | 224 | if(_pppdata->flowcontrol() != "None") { |
228 | if(_pppdata->flowcontrol() == "CRTSCTS") { | 225 | if(_pppdata->flowcontrol() == "CRTSCTS") { |
229 | tty.c_cflag |= CRTSCTS; | 226 | tty.c_cflag |= CRTSCTS; |
230 | } | 227 | } |
231 | else { | 228 | else { |
232 | tty.c_iflag |= IXON | IXOFF; | 229 | tty.c_iflag |= IXON | IXOFF; |
233 | tty.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */ | 230 | tty.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */ |
234 | tty.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */ | 231 | tty.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */ |
235 | } | 232 | } |
236 | } | 233 | } |
237 | else { | 234 | else { |
238 | tty.c_cflag &= ~CRTSCTS; | 235 | tty.c_cflag &= ~CRTSCTS; |
239 | tty.c_iflag &= ~(IXON | IXOFF); | 236 | tty.c_iflag &= ~(IXON | IXOFF); |
240 | } | 237 | } |
241 | 238 | ||
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; |
273 | } | 270 | } |
274 | 271 | ||
275 | return true; | 272 | return true; |
276 | } | 273 | } |
277 | 274 | ||
278 | 275 | ||
279 | void Modem::readtty(int) { | 276 | void Modem::readtty(int) { |
280 | char buffer[200]; | 277 | char buffer[200]; |
281 | unsigned char c; | 278 | unsigned char c; |
282 | int len; | 279 | int len; |
283 | 280 | ||
284 | // read data in chunks of up to 200 bytes | 281 | // read data in chunks of up to 200 bytes |
285 | if((len = ::read(modemfd, buffer, 200)) > 0) { | 282 | if((len = ::read(modemfd, buffer, 200)) > 0) { |
286 | // split buffer into single characters for further processing | 283 | // split buffer into single characters for further processing |
287 | for(int i = 0; i < len; i++) { | 284 | for(int i = 0; i < len; i++) { |
288 | c = buffer[i] & 0x7F; | 285 | c = buffer[i] & 0x7F; |
289 | emit charWaiting(c); | 286 | emit charWaiting(c); |
290 | } | 287 | } |
@@ -392,49 +389,49 @@ bool Modem::hangup() { | |||
392 | // is this Escape & HangupStr stuff really necessary ? (Harri) | 389 | // is this Escape & HangupStr stuff really necessary ? (Harri) |
393 | 390 | ||
394 | if (data_mode) escape_to_command_mode(); | 391 | if (data_mode) escape_to_command_mode(); |
395 | 392 | ||
396 | // Then hangup command | 393 | // Then hangup command |
397 | writeLine(_pppdata->modemHangupStr().local8Bit()); | 394 | writeLine(_pppdata->modemHangupStr().local8Bit()); |
398 | 395 | ||
399 | usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 sec | 396 | usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 sec |
400 | 397 | ||
401 | #ifndef DEBUG_WO_DIALING | 398 | #ifndef DEBUG_WO_DIALING |
402 | if (sigsetjmp(jmp_buffer, 1) == 0) { | 399 | if (sigsetjmp(jmp_buffer, 1) == 0) { |
403 | // set alarm in case tcsendbreak() hangs | 400 | // set alarm in case tcsendbreak() hangs |
404 | signal(SIGALRM, alarm_handler); | 401 | signal(SIGALRM, alarm_handler); |
405 | alarm(2); | 402 | alarm(2); |
406 | 403 | ||
407 | tcsendbreak(modemfd, 0); | 404 | tcsendbreak(modemfd, 0); |
408 | 405 | ||
409 | alarm(0); | 406 | alarm(0); |
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); |
423 | cfsetispeed(&temptty, B0); | 420 | cfsetispeed(&temptty, B0); |
424 | tcsetattr(modemfd, TCSAFLUSH, &temptty); | 421 | tcsetattr(modemfd, TCSAFLUSH, &temptty); |
425 | #else | 422 | #else |
426 | int modemstat; | 423 | int modemstat; |
427 | ioctl(modemfd, TIOCMGET, &modemstat); | 424 | ioctl(modemfd, TIOCMGET, &modemstat); |
428 | modemstat &= ~TIOCM_DTR; | 425 | modemstat &= ~TIOCM_DTR; |
429 | ioctl(modemfd, TIOCMSET, &modemstat); | 426 | ioctl(modemfd, TIOCMSET, &modemstat); |
430 | ioctl(modemfd, TIOCMGET, &modemstat); | 427 | ioctl(modemfd, TIOCMGET, &modemstat); |
431 | modemstat |= TIOCM_DTR; | 428 | modemstat |= TIOCM_DTR; |
432 | ioctl(modemfd, TIOCMSET, &modemstat); | 429 | ioctl(modemfd, TIOCMSET, &modemstat); |
433 | #endif | 430 | #endif |
434 | 431 | ||
435 | usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 secs | 432 | usleep(_pppdata->modemInitDelay() * 10000); // 0.01 - 3.0 secs |
436 | 433 | ||
437 | cfsetospeed(&temptty, modemspeed()); | 434 | cfsetospeed(&temptty, modemspeed()); |
438 | cfsetispeed(&temptty, modemspeed()); | 435 | cfsetispeed(&temptty, modemspeed()); |
439 | tcsetattr(modemfd, TCSAFLUSH, &temptty); | 436 | tcsetattr(modemfd, TCSAFLUSH, &temptty); |
440 | #endif | 437 | #endif |
@@ -534,49 +531,49 @@ QString Modem::parseModemSpeed(const QString &s) { | |||
534 | // | 531 | // |
535 | // trx[i] has been matched, idx contains the start of the match | 532 | // trx[i] has been matched, idx contains the start of the match |
536 | // and len contains how long the match is. Extract the match. | 533 | // and len contains how long the match is. Extract the match. |
537 | // | 534 | // |
538 | QString sub = s.mid(idx, len); | 535 | QString sub = s.mid(idx, len); |
539 | 536 | ||
540 | // | 537 | // |
541 | // Now extract the digits only from the match, which will then | 538 | // Now extract the digits only from the match, which will then |
542 | // be converted to an int. | 539 | // be converted to an int. |
543 | // | 540 | // |
544 | if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) { | 541 | if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) { |
545 | // if((idx = rrx[RXMAX-1].search(sub)) > -1) { | 542 | // if((idx = rrx[RXMAX-1].search(sub)) > -1) { |
546 | // len = rrx[RXMAX-1].matchedLength(); | 543 | // len = rrx[RXMAX-1].matchedLength(); |
547 | sub = sub.mid(idx, len); | 544 | sub = sub.mid(idx, len); |
548 | result = sub.toInt(); | 545 | result = sub.toInt(); |
549 | if(result > 0) { | 546 | if(result > 0) { |
550 | tx = result; | 547 | tx = result; |
551 | break; | 548 | break; |
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); |
565 | 562 | ||
566 | qDebug( "The parsed result is: %s", result.latin1()); | 563 | qDebug( "The parsed result is: %s", result.latin1()); |
567 | 564 | ||
568 | return result; | 565 | return result; |
569 | } | 566 | } |
570 | 567 | ||
571 | 568 | ||
572 | // Lock modem device. Returns 0 on success 1 if the modem is locked and -1 if | 569 | // Lock modem device. Returns 0 on success 1 if the modem is locked and -1 if |
573 | // a lock file can't be created ( permission problem ) | 570 | // a lock file can't be created ( permission problem ) |
574 | int Modem::lockdevice() { | 571 | int Modem::lockdevice() { |
575 | int fd; | 572 | int fd; |
576 | char newlock[80]=""; // safe | 573 | char newlock[80]=""; // safe |
577 | 574 | ||
578 | if(!_pppdata->modemLockFile()) { | 575 | if(!_pppdata->modemLockFile()) { |
579 | qDebug("The user doesn't want a lockfile."); | 576 | qDebug("The user doesn't want a lockfile."); |
580 | return 0; | 577 | return 0; |
581 | } | 578 | } |
582 | 579 | ||
@@ -856,104 +853,132 @@ int checkForInterface() | |||
856 | #else | 853 | #else |
857 | return -1; | 854 | return -1; |
858 | #endif | 855 | #endif |
859 | } | 856 | } |
860 | return 0; | 857 | return 0; |
861 | #else | 858 | #else |
862 | // We attempt to use the SunOS/SysVr4 method and stat /dev/ppp | 859 | // We attempt to use the SunOS/SysVr4 method and stat /dev/ppp |
863 | struct stat buf; | 860 | struct stat buf; |
864 | 861 | ||
865 | memset(&buf, 0, sizeof(buf)); | 862 | memset(&buf, 0, sizeof(buf)); |
866 | return stat("/dev/ppp", &buf); | 863 | return stat("/dev/ppp", &buf); |
867 | #endif | 864 | #endif |
868 | } | 865 | } |
869 | 866 | ||
870 | bool Modem::execpppd(const char *arguments) { | 867 | bool Modem::execpppd(const char *arguments) { |
871 | char buf[MAX_CMDLEN]; | 868 | char buf[MAX_CMDLEN]; |
872 | char *args[MaxArgs]; | 869 | char *args[MaxArgs]; |
873 | pid_t pgrpid; | 870 | pid_t pgrpid; |
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 |
890 | strlcpy(buf, arguments); | 891 | strlcpy(buf, arguments); |
891 | parseargs(buf, args); | 892 | parseargs(buf, args); |
892 | // become a session leader and let /dev/ttySx | 893 | // become a session leader and let /dev/ttySx |
893 | // be the controlling terminal. | 894 | // be the controlling terminal. |
894 | pgrpid = setsid(); | 895 | pgrpid = setsid(); |
895 | #ifdef TIOCSCTTY | 896 | #ifdef TIOCSCTTY |
896 | if(ioctl(modemfd, TIOCSCTTY, 0)<0) | 897 | if(ioctl(modemfd, TIOCSCTTY, 0)<0) |
897 | fprintf(stderr, "ioctl() failed.\n"); | 898 | fprintf(stderr, "ioctl() failed.\n"); |
898 | #elif defined (TIOCSPGRP) | 899 | #elif defined (TIOCSPGRP) |
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"); |
914 | break; | 919 | break; |
915 | case 0: | 920 | case 0: |
916 | fprintf(stderr, "Kernel supports ppp alright.\n"); | 921 | fprintf(stderr, "Kernel supports ppp alright.\n"); |
917 | break; | 922 | break; |
918 | } | 923 | } |
919 | 924 | ||
920 | execve(pppdPath(), args, 0L); | 925 | execve(pppdPath(), args, 0L); |
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; |
942 | } | 967 | } |
943 | } | 968 | } |
944 | } | 969 | } |
945 | return true; | 970 | return true; |
946 | } | 971 | } |
947 | 972 | ||
948 | 973 | ||
949 | void Modem::parseargs(char* buf, char** args) { | 974 | void Modem::parseargs(char* buf, char** args) { |
950 | int nargs = 0; | 975 | int nargs = 0; |
951 | int quotes; | 976 | int quotes; |
952 | 977 | ||
953 | while(nargs < MaxArgs-1 && *buf != '\0') { | 978 | while(nargs < MaxArgs-1 && *buf != '\0') { |
954 | 979 | ||
955 | quotes = 0; | 980 | quotes = 0; |
956 | 981 | ||
957 | // Strip whitespace. Use nulls, so that the previous argument is | 982 | // Strip whitespace. Use nulls, so that the previous argument is |
958 | // terminated automatically. | 983 | // terminated automatically. |
959 | 984 | ||
@@ -1000,24 +1025,56 @@ void Modem::killPPPDaemon() | |||
1000 | _pppdata->setpppdRunning(false); | 1025 | _pppdata->setpppdRunning(false); |
1001 | killpppd(); | 1026 | killpppd(); |
1002 | } | 1027 | } |
1003 | 1028 | ||
1004 | int Modem::pppdExitStatus() | 1029 | int Modem::pppdExitStatus() |
1005 | { | 1030 | { |
1006 | return _pppdExitStatus; | 1031 | return _pppdExitStatus; |
1007 | } | 1032 | } |
1008 | 1033 | ||
1009 | int Modem::openResolv(int flags) | 1034 | int Modem::openResolv(int flags) |
1010 | { | 1035 | { |
1011 | int fd; | 1036 | int fd; |
1012 | if ((fd = open(_PATH_RESCONF, flags)) == -1) { | 1037 | if ((fd = open(_PATH_RESCONF, flags)) == -1) { |
1013 | qDebug("error opening resolv.conf!"); | 1038 | qDebug("error opening resolv.conf!"); |
1014 | fd = open(DEVNULL, O_RDONLY); | 1039 | fd = open(DEVNULL, O_RDONLY); |
1015 | } | 1040 | } |
1016 | return fd; | 1041 | return fd; |
1017 | } | 1042 | } |
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 | } | ||