Diffstat (limited to 'noncore/settings/networksettings/ppp/modem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/modem.cpp | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/noncore/settings/networksettings/ppp/modem.cpp b/noncore/settings/networksettings/ppp/modem.cpp index 3dbc8c3..f3f2639 100644 --- a/noncore/settings/networksettings/ppp/modem.cpp +++ b/noncore/settings/networksettings/ppp/modem.cpp | |||
@@ -34,49 +34,49 @@ | |||
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 | #define qError qDebug | 54 | #define qError qDebug |
55 | 55 | ||
56 | 56 | ||
57 | #define MY_ASSERT(x) if (!(x)) { \ | 57 | #define MY_ASSERT(x) if (!(x)) { \ |
58 | qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \ | 58 | ofatal << "ASSERT: \"" << #x << "\" in " << __FILE__ << " (" << __LINE__ << ")\n" << oendl; \ |
59 | exit(1); } | 59 | exit(1); } |
60 | 60 | ||
61 | 61 | ||
62 | static sigjmp_buf jmp_buffer; | 62 | static sigjmp_buf jmp_buffer; |
63 | 63 | ||
64 | //Modem *Modem::modem = 0; | 64 | //Modem *Modem::modem = 0; |
65 | 65 | ||
66 | 66 | ||
67 | const char* pppdPath() { | 67 | const char* pppdPath() { |
68 | // wasting a few bytes | 68 | // wasting a few bytes |
69 | static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; | 69 | static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; |
70 | static char *pppdPath = 0L; | 70 | static char *pppdPath = 0L; |
71 | char *p; | 71 | char *p; |
72 | 72 | ||
73 | if(pppdPath == 0L) { | 73 | if(pppdPath == 0L) { |
74 | const char *c = PPPDSEARCHPATH; | 74 | const char *c = PPPDSEARCHPATH; |
75 | while(*c != '\0') { | 75 | while(*c != '\0') { |
76 | while(*c == ':') | 76 | while(*c == ':') |
77 | c++; | 77 | c++; |
78 | p = buffer; | 78 | p = buffer; |
79 | while(*c != '\0' && *c != ':') | 79 | while(*c != '\0' && *c != ':') |
80 | *p++ = *c++; | 80 | *p++ = *c++; |
81 | *p = '\0'; | 81 | *p = '\0'; |
82 | strcat(p, "/"); | 82 | strcat(p, "/"); |
@@ -144,49 +144,49 @@ speed_t Modem::modemspeed() { | |||
144 | case 2304: | 144 | case 2304: |
145 | return B230400; | 145 | return B230400; |
146 | break; | 146 | break; |
147 | #endif | 147 | #endif |
148 | 148 | ||
149 | #ifdef B460800 | 149 | #ifdef B460800 |
150 | case 4608: | 150 | case 4608: |
151 | return B460800; | 151 | return B460800; |
152 | break; | 152 | break; |
153 | #endif | 153 | #endif |
154 | 154 | ||
155 | default: | 155 | default: |
156 | return B38400; | 156 | return B38400; |
157 | break; | 157 | break; |
158 | } | 158 | } |
159 | } | 159 | } |
160 | 160 | ||
161 | bool Modem::opentty() { | 161 | bool Modem::opentty() { |
162 | // int flags; | 162 | // int flags; |
163 | 163 | ||
164 | //begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { | 164 | //begin if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { |
165 | close(modemfd); | 165 | close(modemfd); |
166 | device = _pppdata->modemDevice(); | 166 | device = _pppdata->modemDevice(); |
167 | if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) { | 167 | if ((modemfd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) == -1) { |
168 | qDebug("error opening modem device !"); | 168 | odebug << "error opening modem device !" << oendl; |
169 | errmsg = QObject::tr("Unable to open modem."); | 169 | errmsg = QObject::tr("Unable to open modem."); |
170 | return false; | 170 | return false; |
171 | } | 171 | } |
172 | //bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { | 172 | //bend if((modemfd = Requester::rq->openModem(gpppdata.modemDevice()))<0) { |
173 | //} | 173 | //} |
174 | 174 | ||
175 | #if 0 | 175 | #if 0 |
176 | if(_pppdata->UseCDLine()) { | 176 | if(_pppdata->UseCDLine()) { |
177 | if(ioctl(modemfd, TIOCMGET, &flags) == -1) { | 177 | if(ioctl(modemfd, TIOCMGET, &flags) == -1) { |
178 | errmsg = QObject::tr("Unable to detect state of CD line."); | 178 | errmsg = QObject::tr("Unable to detect state of CD line."); |
179 | ::close(modemfd); | 179 | ::close(modemfd); |
180 | modemfd = -1; | 180 | modemfd = -1; |
181 | return false; | 181 | return false; |
182 | } | 182 | } |
183 | if ((flags&TIOCM_CD) == 0) { | 183 | if ((flags&TIOCM_CD) == 0) { |
184 | errmsg = QObject::tr("The modem is not ready."); | 184 | errmsg = QObject::tr("The modem is not ready."); |
185 | ::close(modemfd); | 185 | ::close(modemfd); |
186 | modemfd = -1; | 186 | modemfd = -1; |
187 | return false; | 187 | return false; |
188 | } | 188 | } |
189 | } | 189 | } |
190 | #endif | 190 | #endif |
191 | 191 | ||
192 | tcdrain (modemfd); | 192 | tcdrain (modemfd); |
@@ -285,108 +285,108 @@ void Modem::readtty(int) { | |||
285 | c = buffer[i] & 0x7F; | 285 | c = buffer[i] & 0x7F; |
286 | emit charWaiting(c); | 286 | emit charWaiting(c); |
287 | } | 287 | } |
288 | } | 288 | } |
289 | } | 289 | } |
290 | 290 | ||
291 | 291 | ||
292 | void Modem::notify(const QObject *receiver, const char *member) { | 292 | void Modem::notify(const QObject *receiver, const char *member) { |
293 | connect(this, SIGNAL(charWaiting(unsigned char)), receiver, member); | 293 | connect(this, SIGNAL(charWaiting(unsigned char)), receiver, member); |
294 | startNotifier(); | 294 | startNotifier(); |
295 | } | 295 | } |
296 | 296 | ||
297 | 297 | ||
298 | void Modem::stop() { | 298 | void Modem::stop() { |
299 | disconnect(SIGNAL(charWaiting(unsigned char))); | 299 | disconnect(SIGNAL(charWaiting(unsigned char))); |
300 | stopNotifier(); | 300 | stopNotifier(); |
301 | } | 301 | } |
302 | 302 | ||
303 | 303 | ||
304 | void Modem::startNotifier() { | 304 | void Modem::startNotifier() { |
305 | if(modemfd >= 0) { | 305 | if(modemfd >= 0) { |
306 | if(sn == 0) { | 306 | if(sn == 0) { |
307 | sn = new QSocketNotifier(modemfd, QSocketNotifier::Read, this); | 307 | sn = new QSocketNotifier(modemfd, QSocketNotifier::Read, this); |
308 | connect(sn, SIGNAL(activated(int)), SLOT(readtty(int))); | 308 | connect(sn, SIGNAL(activated(int)), SLOT(readtty(int))); |
309 | qDebug("QSocketNotifier started!"); | 309 | odebug << "QSocketNotifier started!" << oendl; |
310 | } else { | 310 | } else { |
311 | qDebug("QSocketNotifier re-enabled!"); | 311 | odebug << "QSocketNotifier re-enabled!" << oendl; |
312 | sn->setEnabled(true); | 312 | sn->setEnabled(true); |
313 | } | 313 | } |
314 | } | 314 | } |
315 | } | 315 | } |
316 | 316 | ||
317 | 317 | ||
318 | void Modem::stopNotifier() { | 318 | void Modem::stopNotifier() { |
319 | if(sn != 0) { | 319 | if(sn != 0) { |
320 | sn->setEnabled(false); | 320 | sn->setEnabled(false); |
321 | disconnect(sn); | 321 | disconnect(sn); |
322 | delete sn; | 322 | delete sn; |
323 | sn = 0; | 323 | sn = 0; |
324 | qDebug( "QSocketNotifier stopped!" ); | 324 | odebug << "QSocketNotifier stopped!" << oendl; |
325 | } | 325 | } |
326 | } | 326 | } |
327 | 327 | ||
328 | 328 | ||
329 | void Modem::flush() { | 329 | void Modem::flush() { |
330 | char c; | 330 | char c; |
331 | while(read(modemfd, &c, 1) == 1); | 331 | while(read(modemfd, &c, 1) == 1); |
332 | } | 332 | } |
333 | 333 | ||
334 | 334 | ||
335 | bool Modem::writeChar(unsigned char c) { | 335 | bool Modem::writeChar(unsigned char c) { |
336 | int s; | 336 | int s; |
337 | do { | 337 | do { |
338 | s = write(modemfd, &c, 1); | 338 | s = write(modemfd, &c, 1); |
339 | if (s < 0) { | 339 | if (s < 0) { |
340 | qError( "write() in Modem::writeChar failed" ); | 340 | oerr << "write() in Modem::writeChar failed" << oendl; |
341 | return false; | 341 | return false; |
342 | } | 342 | } |
343 | } while(s == 0); | 343 | } while(s == 0); |
344 | 344 | ||
345 | return true; | 345 | return true; |
346 | } | 346 | } |
347 | 347 | ||
348 | 348 | ||
349 | bool Modem::writeLine(const char *buf) { | 349 | bool Modem::writeLine(const char *buf) { |
350 | int len = strlen(buf); | 350 | int len = strlen(buf); |
351 | char *b = new char[len+2]; | 351 | char *b = new char[len+2]; |
352 | memcpy(b, buf, len); | 352 | memcpy(b, buf, len); |
353 | // different modems seem to need different line terminations | 353 | // different modems seem to need different line terminations |
354 | QString term = _pppdata->enter(); | 354 | QString term = _pppdata->enter(); |
355 | if(term == "LF") | 355 | if(term == "LF") |
356 | b[len++]='\n'; | 356 | b[len++]='\n'; |
357 | else if(term == "CR") | 357 | else if(term == "CR") |
358 | b[len++]='\r'; | 358 | b[len++]='\r'; |
359 | else if(term == "CR/LF") { | 359 | else if(term == "CR/LF") { |
360 | b[len++]='\r'; | 360 | b[len++]='\r'; |
361 | b[len++]='\n'; | 361 | b[len++]='\n'; |
362 | } | 362 | } |
363 | int l = len; | 363 | int l = len; |
364 | while(l) { | 364 | while(l) { |
365 | int wr = write(modemfd, &b[len-l], l); | 365 | int wr = write(modemfd, &b[len-l], l); |
366 | if(wr < 0) { | 366 | if(wr < 0) { |
367 | // TODO do something meaningful with the error code (or ignore it | 367 | // TODO do something meaningful with the error code (or ignore it |
368 | qError( "write() in Modem::writeLine failed" ); | 368 | oerr << "write() in Modem::writeLine failed" << oendl; |
369 | delete[] b; | 369 | delete[] b; |
370 | return false; | 370 | return false; |
371 | } | 371 | } |
372 | l -= wr; | 372 | l -= wr; |
373 | } | 373 | } |
374 | delete[] b; | 374 | delete[] b; |
375 | return true; | 375 | return true; |
376 | } | 376 | } |
377 | 377 | ||
378 | 378 | ||
379 | bool Modem::hangup() { | 379 | bool Modem::hangup() { |
380 | // this should really get the modem to hang up and go into command mode | 380 | // this should really get the modem to hang up and go into command mode |
381 | // If anyone sees a fault in the following please let me know, since | 381 | // If anyone sees a fault in the following please let me know, since |
382 | // this is probably the most imporant snippet of code in the whole of | 382 | // this is probably the most imporant snippet of code in the whole of |
383 | // kppp. If people complain about kppp being stuck, this piece of code | 383 | // kppp. If people complain about kppp being stuck, this piece of code |
384 | // is most likely the reason. | 384 | // is most likely the reason. |
385 | struct termios temptty; | 385 | struct termios temptty; |
386 | 386 | ||
387 | if(modemfd >= 0) { | 387 | if(modemfd >= 0) { |
388 | 388 | ||
389 | // is this Escape & HangupStr stuff really necessary ? (Harri) | 389 | // is this Escape & HangupStr stuff really necessary ? (Harri) |
390 | 390 | ||
391 | if (data_mode) escape_to_command_mode(); | 391 | if (data_mode) escape_to_command_mode(); |
392 | 392 | ||
@@ -453,49 +453,49 @@ void Modem::escape_to_command_mode() { | |||
453 | 453 | ||
454 | // +3 because quiet time must be greater than guard time. | 454 | // +3 because quiet time must be greater than guard time. |
455 | usleep((_pppdata->modemEscapeGuardTime()+3)*20000); | 455 | usleep((_pppdata->modemEscapeGuardTime()+3)*20000); |
456 | QCString tmp = _pppdata->modemEscapeStr().local8Bit(); | 456 | QCString tmp = _pppdata->modemEscapeStr().local8Bit(); |
457 | write(modemfd, tmp.data(), tmp.length()); | 457 | write(modemfd, tmp.data(), tmp.length()); |
458 | tcflush(modemfd, TCIOFLUSH); | 458 | tcflush(modemfd, TCIOFLUSH); |
459 | usleep((_pppdata->modemEscapeGuardTime()+3)*20000); | 459 | usleep((_pppdata->modemEscapeGuardTime()+3)*20000); |
460 | 460 | ||
461 | data_mode = false; | 461 | data_mode = false; |
462 | } | 462 | } |
463 | 463 | ||
464 | 464 | ||
465 | const QString Modem::modemMessage() { | 465 | const QString Modem::modemMessage() { |
466 | return errmsg; | 466 | return errmsg; |
467 | } | 467 | } |
468 | 468 | ||
469 | 469 | ||
470 | QString Modem::parseModemSpeed(const QString &s) { | 470 | QString Modem::parseModemSpeed(const QString &s) { |
471 | // this is a small (and bad) parser for modem speeds | 471 | // this is a small (and bad) parser for modem speeds |
472 | int rx = -1; | 472 | int rx = -1; |
473 | int tx = -1; | 473 | int tx = -1; |
474 | int i; | 474 | int i; |
475 | QString result; | 475 | QString result; |
476 | 476 | ||
477 | qDebug( "Modem reported result string: %s", s.latin1()); | 477 | odebug << "Modem reported result string: " << s.latin1() << "" << oendl; |
478 | 478 | ||
479 | const int RXMAX = 7; | 479 | const int RXMAX = 7; |
480 | const int TXMAX = 2; | 480 | const int TXMAX = 2; |
481 | QRegExp rrx[RXMAX] = { | 481 | QRegExp rrx[RXMAX] = { |
482 | QRegExp("[0-9]+[:/ ]RX", false), | 482 | QRegExp("[0-9]+[:/ ]RX", false), |
483 | QRegExp("[0-9]+RX", false), | 483 | QRegExp("[0-9]+RX", false), |
484 | QRegExp("[/: -][0-9]+[/: ]", false), | 484 | QRegExp("[/: -][0-9]+[/: ]", false), |
485 | QRegExp("[/: -][0-9]+$", false), | 485 | QRegExp("[/: -][0-9]+$", false), |
486 | QRegExp("CARRIER [^0-9]*[0-9]+", false), | 486 | QRegExp("CARRIER [^0-9]*[0-9]+", false), |
487 | QRegExp("CONNECT [^0-9]*[0-9]+", false), | 487 | QRegExp("CONNECT [^0-9]*[0-9]+", false), |
488 | QRegExp("[0-9]+") // panic mode | 488 | QRegExp("[0-9]+") // panic mode |
489 | }; | 489 | }; |
490 | 490 | ||
491 | QRegExp trx[TXMAX] = { | 491 | QRegExp trx[TXMAX] = { |
492 | QRegExp("[0-9]+[:/ ]TX", false), | 492 | QRegExp("[0-9]+[:/ ]TX", false), |
493 | QRegExp("[0-9]+TX", false) | 493 | QRegExp("[0-9]+TX", false) |
494 | }; | 494 | }; |
495 | 495 | ||
496 | for(i = 0; i < RXMAX; i++) { | 496 | for(i = 0; i < RXMAX; i++) { |
497 | int len, idx, result; | 497 | int len, idx, result; |
498 | if((idx = rrx[i].match(s,0,&len)) > -1) { | 498 | if((idx = rrx[i].match(s,0,&len)) > -1) { |
499 | // if((idx = rrx[i].search(s)) > -1) { | 499 | // if((idx = rrx[i].search(s)) > -1) { |
500 | // len = rrx[i].matchedLength(); | 500 | // len = rrx[i].matchedLength(); |
501 | 501 | ||
@@ -539,155 +539,155 @@ QString Modem::parseModemSpeed(const QString &s) { | |||
539 | // be converted to an int. | 539 | // be converted to an int. |
540 | // | 540 | // |
541 | if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) { | 541 | if((idx = rrx[RXMAX-1].match(sub,0,&len)) > -1) { |
542 | // if((idx = rrx[RXMAX-1].search(sub)) > -1) { | 542 | // if((idx = rrx[RXMAX-1].search(sub)) > -1) { |
543 | // len = rrx[RXMAX-1].matchedLength(); | 543 | // len = rrx[RXMAX-1].matchedLength(); |
544 | sub = sub.mid(idx, len); | 544 | sub = sub.mid(idx, len); |
545 | result = sub.toInt(); | 545 | result = sub.toInt(); |
546 | if(result > 0) { | 546 | if(result > 0) { |
547 | tx = result; | 547 | tx = result; |
548 | break; | 548 | break; |
549 | } | 549 | } |
550 | } | 550 | } |
551 | } | 551 | } |
552 | } | 552 | } |
553 | 553 | ||
554 | if(rx == -1 && tx == -1) | 554 | if(rx == -1 && tx == -1) |
555 | result = QObject::tr("Unknown speed"); | 555 | result = QObject::tr("Unknown speed"); |
556 | else if(tx == -1) | 556 | else if(tx == -1) |
557 | result.setNum(rx); | 557 | result.setNum(rx); |
558 | else if(rx == -1) // should not happen | 558 | else if(rx == -1) // should not happen |
559 | result.setNum(tx); | 559 | result.setNum(tx); |
560 | else | 560 | else |
561 | result.sprintf("%d/%d", rx, tx); | 561 | result.sprintf("%d/%d", rx, tx); |
562 | 562 | ||
563 | qDebug( "The parsed result is: %s", result.latin1()); | 563 | odebug << "The parsed result is: " << result.latin1() << "" << oendl; |
564 | 564 | ||
565 | return result; | 565 | return result; |
566 | } | 566 | } |
567 | 567 | ||
568 | 568 | ||
569 | // 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 |
570 | // a lock file can't be created ( permission problem ) | 570 | // a lock file can't be created ( permission problem ) |
571 | int Modem::lockdevice() { | 571 | int Modem::lockdevice() { |
572 | int fd; | 572 | int fd; |
573 | char newlock[80]=""; // safe | 573 | char newlock[80]=""; // safe |
574 | 574 | ||
575 | if(!_pppdata->modemLockFile()) { | 575 | if(!_pppdata->modemLockFile()) { |
576 | qDebug("The user doesn't want a lockfile."); | 576 | odebug << "The user doesn't want a lockfile." << oendl; |
577 | return 0; | 577 | return 0; |
578 | } | 578 | } |
579 | 579 | ||
580 | if (modem_is_locked) | 580 | if (modem_is_locked) |
581 | return 1; | 581 | return 1; |
582 | 582 | ||
583 | QString lockfile = LOCK_DIR"/LCK.."; | 583 | QString lockfile = LOCK_DIR"/LCK.."; |
584 | lockfile += _pppdata->modemDevice().mid(5); // append everything after /dev/ | 584 | lockfile += _pppdata->modemDevice().mid(5); // append everything after /dev/ |
585 | 585 | ||
586 | if(access(QFile::encodeName(lockfile), F_OK) == 0) { | 586 | if(access(QFile::encodeName(lockfile), F_OK) == 0) { |
587 | // if ((fd = Requester::rq-> | 587 | // if ((fd = Requester::rq-> |
588 | if ((fd = openLockfile(QFile::encodeName(lockfile), O_RDONLY)) >= 0) { | 588 | if ((fd = openLockfile(QFile::encodeName(lockfile), O_RDONLY)) >= 0) { |
589 | // Mario: it's not necessary to read more than lets say 32 bytes. If | 589 | // Mario: it's not necessary to read more than lets say 32 bytes. If |
590 | // file has more than 32 bytes, skip the rest | 590 | // file has more than 32 bytes, skip the rest |
591 | char oldlock[33]; // safe | 591 | char oldlock[33]; // safe |
592 | int sz = read(fd, &oldlock, 32); | 592 | int sz = read(fd, &oldlock, 32); |
593 | close (fd); | 593 | close (fd); |
594 | if (sz <= 0) | 594 | if (sz <= 0) |
595 | return 1; | 595 | return 1; |
596 | oldlock[sz] = '\0'; | 596 | oldlock[sz] = '\0'; |
597 | 597 | ||
598 | qDebug( "Device is locked by: %s", oldlock); | 598 | odebug << "Device is locked by: " << oldlock << "" << oendl; |
599 | 599 | ||
600 | int oldpid; | 600 | int oldpid; |
601 | int match = sscanf(oldlock, "%d", &oldpid); | 601 | int match = sscanf(oldlock, "%d", &oldpid); |
602 | 602 | ||
603 | // found a pid in lockfile ? | 603 | // found a pid in lockfile ? |
604 | if (match < 1 || oldpid <= 0) | 604 | if (match < 1 || oldpid <= 0) |
605 | return 1; | 605 | return 1; |
606 | 606 | ||
607 | // check if process exists | 607 | // check if process exists |
608 | if (kill((pid_t)oldpid, 0) == 0 || errno != ESRCH) | 608 | if (kill((pid_t)oldpid, 0) == 0 || errno != ESRCH) |
609 | return 1; | 609 | return 1; |
610 | 610 | ||
611 | qDebug( "lockfile is stale" ); | 611 | odebug << "lockfile is stale" << oendl; |
612 | } | 612 | } |
613 | } | 613 | } |
614 | 614 | ||
615 | fd = openLockfile(_pppdata->modemDevice(),O_WRONLY|O_TRUNC|O_CREAT); | 615 | fd = openLockfile(_pppdata->modemDevice(),O_WRONLY|O_TRUNC|O_CREAT); |
616 | if(fd >= 0) { | 616 | if(fd >= 0) { |
617 | sprintf(newlock,"%010d\n", getpid()); | 617 | sprintf(newlock,"%010d\n", getpid()); |
618 | qDebug("Locking Device: %s", newlock); | 618 | odebug << "Locking Device: " << newlock << "" << oendl; |
619 | 619 | ||
620 | write(fd, newlock, strlen(newlock)); | 620 | write(fd, newlock, strlen(newlock)); |
621 | close(fd); | 621 | close(fd); |
622 | modem_is_locked=true; | 622 | modem_is_locked=true; |
623 | 623 | ||
624 | return 0; | 624 | return 0; |
625 | } | 625 | } |
626 | 626 | ||
627 | return -1; | 627 | return -1; |
628 | 628 | ||
629 | } | 629 | } |
630 | 630 | ||
631 | 631 | ||
632 | // UnLock modem device | 632 | // UnLock modem device |
633 | void Modem::unlockdevice() { | 633 | void Modem::unlockdevice() { |
634 | if (modem_is_locked) { | 634 | if (modem_is_locked) { |
635 | qDebug( "UnLocking Modem Device" ); | 635 | odebug << "UnLocking Modem Device" << oendl; |
636 | close(modemfd); | 636 | close(modemfd); |
637 | modemfd = -1; | 637 | modemfd = -1; |
638 | unlink(lockfile); | 638 | unlink(lockfile); |
639 | lockfile[0] = '\0'; | 639 | lockfile[0] = '\0'; |
640 | modem_is_locked=false; | 640 | modem_is_locked=false; |
641 | } | 641 | } |
642 | } | 642 | } |
643 | 643 | ||
644 | int Modem::openLockfile( QString lockfile, int flags) | 644 | int Modem::openLockfile( QString lockfile, int flags) |
645 | { | 645 | { |
646 | int fd; | 646 | int fd; |
647 | int mode; | 647 | int mode; |
648 | flags = O_RDONLY; | 648 | flags = O_RDONLY; |
649 | if(flags == O_WRONLY|O_TRUNC|O_CREAT) | 649 | if(flags == O_WRONLY|O_TRUNC|O_CREAT) |
650 | mode = 0644; | 650 | mode = 0644; |
651 | else | 651 | else |
652 | mode = 0; | 652 | mode = 0; |
653 | 653 | ||
654 | lockfile = LOCK_DIR; | 654 | lockfile = LOCK_DIR; |
655 | lockfile += "/LCK.."; | 655 | lockfile += "/LCK.."; |
656 | lockfile += device.right( device.length() - device.findRev("/") -1 ); | 656 | lockfile += device.right( device.length() - device.findRev("/") -1 ); |
657 | qDebug("lockfile >%s<",lockfile.latin1()); | 657 | odebug << "lockfile >" << lockfile.latin1() << "<" << oendl; |
658 | // TODO: | 658 | // TODO: |
659 | // struct stat st; | 659 | // struct stat st; |
660 | // if(stat(lockfile.data(), &st) == -1) { | 660 | // if(stat(lockfile.data(), &st) == -1) { |
661 | // if(errno == EBADF) | 661 | // if(errno == EBADF) |
662 | // return -1; | 662 | // return -1; |
663 | // } else { | 663 | // } else { |
664 | // // make sure that this is a regular file | 664 | // // make sure that this is a regular file |
665 | // if(!S_ISREG(st.st_mode)) | 665 | // if(!S_ISREG(st.st_mode)) |
666 | // return -1; | 666 | // return -1; |
667 | // } | 667 | // } |
668 | if ((fd = open(lockfile, flags, mode)) == -1) { | 668 | if ((fd = open(lockfile, flags, mode)) == -1) { |
669 | qDebug("error opening lockfile!"); | 669 | odebug << "error opening lockfile!" << oendl; |
670 | lockfile = QString::null; | 670 | lockfile = QString::null; |
671 | fd = open(DEVNULL, O_RDONLY); | 671 | fd = open(DEVNULL, O_RDONLY); |
672 | } else | 672 | } else |
673 | fchown(fd, 0, 0); | 673 | fchown(fd, 0, 0); |
674 | return fd; | 674 | return fd; |
675 | } | 675 | } |
676 | 676 | ||
677 | 677 | ||
678 | 678 | ||
679 | void alarm_handler(int) { | 679 | void alarm_handler(int) { |
680 | // fprintf(stderr, "alarm_handler(): Received SIGALRM\n"); | 680 | // fprintf(stderr, "alarm_handler(): Received SIGALRM\n"); |
681 | 681 | ||
682 | // jump | 682 | // jump |
683 | siglongjmp(jmp_buffer, 1); | 683 | siglongjmp(jmp_buffer, 1); |
684 | } | 684 | } |
685 | 685 | ||
686 | 686 | ||
687 | const char* Modem::authFile(Auth method, int version) { | 687 | const char* Modem::authFile(Auth method, int version) { |
688 | switch(method|version) { | 688 | switch(method|version) { |
689 | case PAP|Original: | 689 | case PAP|Original: |
690 | return PAP_AUTH_FILE; | 690 | return PAP_AUTH_FILE; |
691 | break; | 691 | break; |
692 | case PAP|New: | 692 | case PAP|New: |
693 | return PAP_AUTH_FILE".new"; | 693 | return PAP_AUTH_FILE".new"; |
@@ -906,84 +906,84 @@ bool Modem::execpppd(const char *arguments) { | |||
906 | ::close( m_pppdLOG[0] ); | 906 | ::close( m_pppdLOG[0] ); |
907 | ::setenv( "LANG", "C", 1 ); // overwrite | 907 | ::setenv( "LANG", "C", 1 ); // overwrite |
908 | dup2(m_pppdLOG[1], 11 ); // for logfd 11 | 908 | dup2(m_pppdLOG[1], 11 ); // for logfd 11 |
909 | dup2(modemfd, 0); | 909 | dup2(modemfd, 0); |
910 | dup2(modemfd, 1); | 910 | dup2(modemfd, 1); |
911 | 911 | ||
912 | 912 | ||
913 | switch (checkForInterface()) { | 913 | switch (checkForInterface()) { |
914 | case 1: | 914 | case 1: |
915 | fprintf(stderr, "Cannot determine if kernel supports ppp.\n"); | 915 | fprintf(stderr, "Cannot determine if kernel supports ppp.\n"); |
916 | break; | 916 | break; |
917 | case -1: | 917 | case -1: |
918 | fprintf(stderr, "Kernel does not support ppp, oops.\n"); | 918 | fprintf(stderr, "Kernel does not support ppp, oops.\n"); |
919 | break; | 919 | break; |
920 | case 0: | 920 | case 0: |
921 | fprintf(stderr, "Kernel supports ppp alright.\n"); | 921 | fprintf(stderr, "Kernel supports ppp alright.\n"); |
922 | break; | 922 | break; |
923 | } | 923 | } |
924 | 924 | ||
925 | execve(pppdPath(), args, 0L); | 925 | execve(pppdPath(), args, 0L); |
926 | _exit(0); | 926 | _exit(0); |
927 | break; | 927 | break; |
928 | 928 | ||
929 | default: | 929 | default: |
930 | qDebug("In parent: pppd pid %d\n",pppdPid); | 930 | odebug << "In parent: pppd pid " << pppdPid << "\n" << oendl; |
931 | close(modemfd); | 931 | close(modemfd); |
932 | 932 | ||
933 | ::close( m_pppdLOG[1] ); | 933 | ::close( m_pppdLOG[1] ); |
934 | // set it to nonblocking io | 934 | // set it to nonblocking io |
935 | int flag = ::fcntl( m_pppdLOG[0], F_GETFL ); | 935 | int flag = ::fcntl( m_pppdLOG[0], F_GETFL ); |
936 | 936 | ||
937 | if ( !(flag & O_NONBLOCK) ) { | 937 | if ( !(flag & O_NONBLOCK) ) { |
938 | qDebug("Setting nonblocking io"); | 938 | odebug << "Setting nonblocking io" << oendl; |
939 | flag |= O_NONBLOCK; | 939 | flag |= O_NONBLOCK; |
940 | ::fcntl(m_pppdLOG[0], F_SETFL, flag ); | 940 | ::fcntl(m_pppdLOG[0], F_SETFL, flag ); |
941 | } | 941 | } |
942 | 942 | ||
943 | delete m_modemDebug; | 943 | delete m_modemDebug; |
944 | m_modemDebug = new QSocketNotifier(m_pppdLOG[0], QSocketNotifier::Read, this ); | 944 | m_modemDebug = new QSocketNotifier(m_pppdLOG[0], QSocketNotifier::Read, this ); |
945 | connect(m_modemDebug, SIGNAL(activated(int) ), | 945 | connect(m_modemDebug, SIGNAL(activated(int) ), |
946 | this, SLOT(slotModemDebug(int) ) ); | 946 | this, SLOT(slotModemDebug(int) ) ); |
947 | 947 | ||
948 | modemfd = -1; | 948 | modemfd = -1; |
949 | m_pppdDev = QString::fromLatin1("ppp0"); | 949 | m_pppdDev = QString::fromLatin1("ppp0"); |
950 | return true; | 950 | return true; |
951 | break; | 951 | break; |
952 | } | 952 | } |
953 | } | 953 | } |
954 | 954 | ||
955 | 955 | ||
956 | bool Modem::killpppd() { | 956 | bool Modem::killpppd() { |
957 | qDebug("In killpppd and pid is %d", pppdPid ); | 957 | odebug << "In killpppd and pid is " << pppdPid << "" << oendl; |
958 | if(pppdPid > 0) { | 958 | if(pppdPid > 0) { |
959 | delete m_modemDebug; | 959 | delete m_modemDebug; |
960 | m_modemDebug = 0; | 960 | m_modemDebug = 0; |
961 | qDebug("In killpppd(): Sending SIGTERM to %d\n", pppdPid); | 961 | odebug << "In killpppd(): Sending SIGTERM to " << pppdPid << "\n" << oendl; |
962 | if(kill(pppdPid, SIGTERM) < 0) { | 962 | if(kill(pppdPid, SIGTERM) < 0) { |
963 | qDebug("Error terminating %d. Sending SIGKILL\n", pppdPid); | 963 | odebug << "Error terminating " << pppdPid << ". Sending SIGKILL\n" << oendl; |
964 | if(kill(pppdPid, SIGKILL) < 0) { | 964 | if(kill(pppdPid, SIGKILL) < 0) { |
965 | qDebug("Error killing %d\n", pppdPid); | 965 | odebug << "Error killing " << pppdPid << "\n" << oendl; |
966 | return false; | 966 | return false; |
967 | } | 967 | } |
968 | } | 968 | } |
969 | } | 969 | } |
970 | return true; | 970 | return true; |
971 | } | 971 | } |
972 | 972 | ||
973 | 973 | ||
974 | void Modem::parseargs(char* buf, char** args) { | 974 | void Modem::parseargs(char* buf, char** args) { |
975 | int nargs = 0; | 975 | int nargs = 0; |
976 | int quotes; | 976 | int quotes; |
977 | 977 | ||
978 | while(nargs < MaxArgs-1 && *buf != '\0') { | 978 | while(nargs < MaxArgs-1 && *buf != '\0') { |
979 | 979 | ||
980 | quotes = 0; | 980 | quotes = 0; |
981 | 981 | ||
982 | // Strip whitespace. Use nulls, so that the previous argument is | 982 | // Strip whitespace. Use nulls, so that the previous argument is |
983 | // terminated automatically. | 983 | // terminated automatically. |
984 | 984 | ||
985 | while ((*buf == ' ' ) || (*buf == '\t' ) || (*buf == '\n' ) ) | 985 | while ((*buf == ' ' ) || (*buf == '\t' ) || (*buf == '\n' ) ) |
986 | *buf++ = '\0'; | 986 | *buf++ = '\0'; |
987 | 987 | ||
988 | // detect begin of quoted argument | 988 | // detect begin of quoted argument |
989 | if (*buf == '"' || *buf == '\'') { | 989 | if (*buf == '"' || *buf == '\'') { |
@@ -1014,67 +1014,67 @@ void Modem::parseargs(char* buf, char** args) { | |||
1014 | bool Modem::execPPPDaemon(const QString & arguments) | 1014 | bool Modem::execPPPDaemon(const QString & arguments) |
1015 | { | 1015 | { |
1016 | if(execpppd(arguments)) { | 1016 | if(execpppd(arguments)) { |
1017 | _pppdata->setpppdRunning(true); | 1017 | _pppdata->setpppdRunning(true); |
1018 | return true; | 1018 | return true; |
1019 | } else | 1019 | } else |
1020 | return false; | 1020 | return false; |
1021 | } | 1021 | } |
1022 | 1022 | ||
1023 | void Modem::killPPPDaemon() | 1023 | void Modem::killPPPDaemon() |
1024 | { | 1024 | { |
1025 | _pppdata->setpppdRunning(false); | 1025 | _pppdata->setpppdRunning(false); |
1026 | killpppd(); | 1026 | killpppd(); |
1027 | } | 1027 | } |
1028 | 1028 | ||
1029 | int Modem::pppdExitStatus() | 1029 | int Modem::pppdExitStatus() |
1030 | { | 1030 | { |
1031 | return _pppdExitStatus; | 1031 | return _pppdExitStatus; |
1032 | } | 1032 | } |
1033 | 1033 | ||
1034 | int Modem::openResolv(int flags) | 1034 | int Modem::openResolv(int flags) |
1035 | { | 1035 | { |
1036 | int fd; | 1036 | int fd; |
1037 | if ((fd = open(_PATH_RESCONF, flags)) == -1) { | 1037 | if ((fd = open(_PATH_RESCONF, flags)) == -1) { |
1038 | qDebug("error opening resolv.conf!"); | 1038 | odebug << "error opening resolv.conf!" << oendl; |
1039 | fd = open(DEVNULL, O_RDONLY); | 1039 | fd = open(DEVNULL, O_RDONLY); |
1040 | } | 1040 | } |
1041 | return fd; | 1041 | return fd; |
1042 | } | 1042 | } |
1043 | 1043 | ||
1044 | bool Modem::setHostname(const QString & name) | 1044 | bool Modem::setHostname(const QString & name) |
1045 | { | 1045 | { |
1046 | return sethostname(name, name.length()) == 0; | 1046 | return sethostname(name, name.length()) == 0; |
1047 | } | 1047 | } |
1048 | 1048 | ||
1049 | QString Modem::pppDevice()const { | 1049 | QString Modem::pppDevice()const { |
1050 | return m_pppdDev; | 1050 | return m_pppdDev; |
1051 | } | 1051 | } |
1052 | void Modem::setPPPDevice( const QString& dev ) { | 1052 | void Modem::setPPPDevice( const QString& dev ) { |
1053 | m_pppdDev = dev; | 1053 | m_pppdDev = dev; |
1054 | } | 1054 | } |
1055 | pid_t Modem::pppPID()const { | 1055 | pid_t Modem::pppPID()const { |
1056 | return pppdPid; | 1056 | return pppdPid; |
1057 | } | 1057 | } |
1058 | void Modem::setPPPDPid( pid_t pid ) { | 1058 | void Modem::setPPPDPid( pid_t pid ) { |
1059 | qDebug("Modem setting pid"); | 1059 | odebug << "Modem setting pid" << oendl; |
1060 | _pppdExitStatus = -1; | 1060 | _pppdExitStatus = -1; |
1061 | pppdPid = pid; | 1061 | pppdPid = pid; |
1062 | modemfd = -1; | 1062 | modemfd = -1; |
1063 | } | 1063 | } |
1064 | void Modem::slotModemDebug(int fd) { | 1064 | void Modem::slotModemDebug(int fd) { |
1065 | char buf[2049]; | 1065 | char buf[2049]; |
1066 | int len; | 1066 | int len; |
1067 | 1067 | ||
1068 | // read in pppd data look for Using interface | 1068 | // read in pppd data look for Using interface |
1069 | // then read the interface | 1069 | // then read the interface |
1070 | // we limit to 10 device now 0-9 | 1070 | // we limit to 10 device now 0-9 |
1071 | if((len = ::read(fd, buf, 2048)) > 0) { | 1071 | if((len = ::read(fd, buf, 2048)) > 0) { |
1072 | buf[len+1] = '\0'; | 1072 | buf[len+1] = '\0'; |
1073 | char *found; | 1073 | char *found; |
1074 | if ( (found = ::strstr(buf, "Using interface ") ) ) { | 1074 | if ( (found = ::strstr(buf, "Using interface ") ) ) { |
1075 | found += 16; | 1075 | found += 16; |
1076 | m_pppdDev = QString::fromLatin1(found, 5 ); | 1076 | m_pppdDev = QString::fromLatin1(found, 5 ); |
1077 | m_pppdDev = m_pppdDev.simplifyWhiteSpace(); | 1077 | m_pppdDev = m_pppdDev.simplifyWhiteSpace(); |
1078 | } | 1078 | } |
1079 | } | 1079 | } |
1080 | } | 1080 | } |