Deleted Added
full compact
main.c (59104) main.c (66898)
1/*
2 * User Process PPP
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
1/*
2 * User Process PPP
3 *
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $FreeBSD: head/usr.sbin/ppp/main.c 59104 2000-04-09 18:27:08Z brian $
20 * $FreeBSD: head/usr.sbin/ppp/main.c 66898 2000-10-09 21:18:23Z brian $
21 *
22 * TODO:
23 */
24
25#include <sys/param.h>
26#include <netinet/in.h>
27#include <netinet/in_systm.h>
28#include <netinet/ip.h>

--- 247 unchanged lines hidden (view full) ---

276}
277
278
279int
280main(int argc, char **argv)
281{
282 char *name;
283 const char *lastlabel;
21 *
22 * TODO:
23 */
24
25#include <sys/param.h>
26#include <netinet/in.h>
27#include <netinet/in_systm.h>
28#include <netinet/ip.h>

--- 247 unchanged lines hidden (view full) ---

276}
277
278
279int
280main(int argc, char **argv)
281{
282 char *name;
283 const char *lastlabel;
284 int nfds, label, arg;
284 int label, arg;
285 struct bundle *bundle;
286 struct prompt *prompt;
287 struct switches sw;
288
285 struct bundle *bundle;
286 struct prompt *prompt;
287 struct switches sw;
288
289 nfds = getdtablesize();
290 if (nfds >= FD_SETSIZE)
291 /*
292 * If we've got loads of file descriptors, make sure they're all
293 * closed. If they aren't, we may end up with a seg fault when our
294 * `fd_set's get too big when select()ing !
295 */
296 while (--nfds > 2)
297 close(nfds);
298
299 name = strrchr(argv[0], '/');
300 log_Open(name ? name + 1 : argv[0]);
301
302#ifndef NONAT
303 PacketAliasInit();
304#endif
305 label = ProcessArgs(argc, argv, &sw);
306

--- 189 unchanged lines hidden (view full) ---

496 AbortProgram(EX_NORMAL);
497
498 return EX_NORMAL;
499}
500
501static void
502DoLoop(struct bundle *bundle)
503{
289 name = strrchr(argv[0], '/');
290 log_Open(name ? name + 1 : argv[0]);
291
292#ifndef NONAT
293 PacketAliasInit();
294#endif
295 label = ProcessArgs(argc, argv, &sw);
296

--- 189 unchanged lines hidden (view full) ---

486 AbortProgram(EX_NORMAL);
487
488 return EX_NORMAL;
489}
490
491static void
492DoLoop(struct bundle *bundle)
493{
504 fd_set rfds, wfds, efds;
494 fd_set *rfds, *wfds, *efds;
505 int i, nfds, nothing_done;
506 struct probe probe;
507
508 probe_Init(&probe);
509
495 int i, nfds, nothing_done;
496 struct probe probe;
497
498 probe_Init(&probe);
499
500 if ((rfds = mkfdset()) == NULL) {
501 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
502 return;
503 }
504
505 if ((wfds = mkfdset()) == NULL) {
506 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
507 free(rfds);
508 return;
509 }
510
511 if ((efds = mkfdset()) == NULL) {
512 log_Printf(LogERROR, "DoLoop: Cannot create fd_set\n");
513 free(rfds);
514 free(wfds);
515 return;
516 }
517
510 for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
511 nfds = 0;
518 for (; !bundle_IsDead(bundle); bundle_CleanDatalinks(bundle)) {
519 nfds = 0;
512 FD_ZERO(&rfds);
513 FD_ZERO(&wfds);
514 FD_ZERO(&efds);
520 zerofdset(rfds);
521 zerofdset(wfds);
522 zerofdset(efds);
515
516 /* All our datalinks, the tun device and the MP socket */
523
524 /* All our datalinks, the tun device and the MP socket */
517 descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
525 descriptor_UpdateSet(&bundle->desc, rfds, wfds, efds, &nfds);
518
519 /* All our prompts and the diagnostic socket */
526
527 /* All our prompts and the diagnostic socket */
520 descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
528 descriptor_UpdateSet(&server.desc, rfds, NULL, NULL, &nfds);
521
522 bundle_CleanDatalinks(bundle);
523 if (bundle_IsDead(bundle))
524 /* Don't select - we'll be here forever */
525 break;
526
527 /*
528 * It's possible that we've had a signal since we last checked. If
529 * we don't check again before calling select(), we may end up stuck
530 * after having missed the event.... sig_Handle() tries to be as
531 * quick as possible if nothing is likely to have happened.
532 * This is only really likely if we block in open(... O_NONBLOCK)
533 * which will happen with a misconfigured device.
534 */
535 if (sig_Handle())
536 continue;
537
529
530 bundle_CleanDatalinks(bundle);
531 if (bundle_IsDead(bundle))
532 /* Don't select - we'll be here forever */
533 break;
534
535 /*
536 * It's possible that we've had a signal since we last checked. If
537 * we don't check again before calling select(), we may end up stuck
538 * after having missed the event.... sig_Handle() tries to be as
539 * quick as possible if nothing is likely to have happened.
540 * This is only really likely if we block in open(... O_NONBLOCK)
541 * which will happen with a misconfigured device.
542 */
543 if (sig_Handle())
544 continue;
545
538 i = select(nfds, &rfds, &wfds, &efds, NULL);
546 i = select(nfds, rfds, wfds, efds, NULL);
539
540 if (i < 0 && errno != EINTR) {
541 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
542 if (log_IsKept(LogTIMER)) {
543 struct timeval t;
544
545 for (i = 0; i <= nfds; i++) {
547
548 if (i < 0 && errno != EINTR) {
549 log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
550 if (log_IsKept(LogTIMER)) {
551 struct timeval t;
552
553 for (i = 0; i <= nfds; i++) {
546 if (FD_ISSET(i, &rfds)) {
554 if (FD_ISSET(i, rfds)) {
547 log_Printf(LogTIMER, "Read set contains %d\n", i);
555 log_Printf(LogTIMER, "Read set contains %d\n", i);
548 FD_CLR(i, &rfds);
556 FD_CLR(i, rfds);
549 t.tv_sec = t.tv_usec = 0;
557 t.tv_sec = t.tv_usec = 0;
550 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
558 if (select(nfds, rfds, wfds, efds, &t) != -1) {
551 log_Printf(LogTIMER, "The culprit !\n");
552 break;
553 }
554 }
559 log_Printf(LogTIMER, "The culprit !\n");
560 break;
561 }
562 }
555 if (FD_ISSET(i, &wfds)) {
563 if (FD_ISSET(i, wfds)) {
556 log_Printf(LogTIMER, "Write set contains %d\n", i);
564 log_Printf(LogTIMER, "Write set contains %d\n", i);
557 FD_CLR(i, &wfds);
565 FD_CLR(i, wfds);
558 t.tv_sec = t.tv_usec = 0;
566 t.tv_sec = t.tv_usec = 0;
559 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
567 if (select(nfds, rfds, wfds, efds, &t) != -1) {
560 log_Printf(LogTIMER, "The culprit !\n");
561 break;
562 }
563 }
568 log_Printf(LogTIMER, "The culprit !\n");
569 break;
570 }
571 }
564 if (FD_ISSET(i, &efds)) {
572 if (FD_ISSET(i, efds)) {
565 log_Printf(LogTIMER, "Error set contains %d\n", i);
573 log_Printf(LogTIMER, "Error set contains %d\n", i);
566 FD_CLR(i, &efds);
574 FD_CLR(i, efds);
567 t.tv_sec = t.tv_usec = 0;
575 t.tv_sec = t.tv_usec = 0;
568 if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
576 if (select(nfds, rfds, wfds, efds, &t) != -1) {
569 log_Printf(LogTIMER, "The culprit !\n");
570 break;
571 }
572 }
573 }
574 }
575 break;
576 }
577
578 log_Printf(LogTIMER, "Select returns %d\n", i);
579
580 sig_Handle();
581
582 if (i <= 0)
583 continue;
584
585 for (i = 0; i <= nfds; i++)
577 log_Printf(LogTIMER, "The culprit !\n");
578 break;
579 }
580 }
581 }
582 }
583 break;
584 }
585
586 log_Printf(LogTIMER, "Select returns %d\n", i);
587
588 sig_Handle();
589
590 if (i <= 0)
591 continue;
592
593 for (i = 0; i <= nfds; i++)
586 if (FD_ISSET(i, &efds)) {
594 if (FD_ISSET(i, efds)) {
587 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
588 /* We deal gracefully with link descriptor exceptions */
589 if (!bundle_Exception(bundle, i)) {
590 log_Printf(LogERROR, "Exception cannot be handled !\n");
591 break;
592 }
593 }
594
595 if (i <= nfds)
596 break;
597
598 nothing_done = 1;
599
595 log_Printf(LogPHASE, "Exception detected on descriptor %d\n", i);
596 /* We deal gracefully with link descriptor exceptions */
597 if (!bundle_Exception(bundle, i)) {
598 log_Printf(LogERROR, "Exception cannot be handled !\n");
599 break;
600 }
601 }
602
603 if (i <= nfds)
604 break;
605
606 nothing_done = 1;
607
600 if (descriptor_IsSet(&server.desc, &rfds)) {
601 descriptor_Read(&server.desc, bundle, &rfds);
608 if (descriptor_IsSet(&server.desc, rfds)) {
609 descriptor_Read(&server.desc, bundle, rfds);
602 nothing_done = 0;
603 }
604
610 nothing_done = 0;
611 }
612
605 if (descriptor_IsSet(&bundle->desc, &rfds)) {
606 descriptor_Read(&bundle->desc, bundle, &rfds);
613 if (descriptor_IsSet(&bundle->desc, rfds)) {
614 descriptor_Read(&bundle->desc, bundle, rfds);
607 nothing_done = 0;
608 }
609
615 nothing_done = 0;
616 }
617
610 if (descriptor_IsSet(&bundle->desc, &wfds))
611 if (!descriptor_Write(&bundle->desc, bundle, &wfds) && nothing_done) {
618 if (descriptor_IsSet(&bundle->desc, wfds))
619 if (!descriptor_Write(&bundle->desc, bundle, wfds) && nothing_done) {
612 /*
613 * This is disasterous. The OS has told us that something is
614 * writable, and all our write()s have failed. Rather than
615 * going back immediately to do our UpdateSet()s and select(),
616 * we sleep for a bit to avoid gobbling up all cpu time.
617 */
618 struct timeval t;
619
620 t.tv_sec = 0;
621 t.tv_usec = 100000;
622 select(0, NULL, NULL, NULL, &t);
623 }
624 }
625
626 log_Printf(LogDEBUG, "DoLoop done.\n");
627}
620 /*
621 * This is disasterous. The OS has told us that something is
622 * writable, and all our write()s have failed. Rather than
623 * going back immediately to do our UpdateSet()s and select(),
624 * we sleep for a bit to avoid gobbling up all cpu time.
625 */
626 struct timeval t;
627
628 t.tv_sec = 0;
629 t.tv_usec = 100000;
630 select(0, NULL, NULL, NULL, &t);
631 }
632 }
633
634 log_Printf(LogDEBUG, "DoLoop done.\n");
635}