- /*
- * If we're doing dial-on-demand, set up the interface now.
- */
- if (demand) {
- /*
- * Open the loopback channel and set it up to be the ppp interface.
- */
- open_ppp_loopback();
-
- syslog(LOG_INFO, "Using interface ppp%d", ifunit);
- (void) sprintf(ifname, "ppp%d", ifunit);
- script_setenv("IFNAME", ifname);
-
- create_pidfile(); /* write pid to file */
-
- /*
- * Configure the interface and mark it up, etc.
- */
- demand_conf();
- }
-
- for (;;) {
-
- need_holdoff = 1;
-
- if (demand) {
- /*
- * Don't do anything until we see some activity.
- */
- phase = PHASE_DORMANT;
- kill_link = 0;
- demand_unblock();
- for (;;) {
- wait_loop_output(timeleft(&timo));
- calltimeout();
- if (kill_link) {
- if (!persist)
- die(0);
- kill_link = 0;
- }
- if (get_loop_output())
- break;
- reap_kids();
- }
-
- /*
- * Now we want to bring up the link.
- */
- demand_block();
- syslog(LOG_INFO, "Starting link");
- }
-
- /*
- * Lock the device if we've been asked to.
- */
- if (lockflag && !default_device) {
- if (lock(devnam) < 0)
- goto fail;
- locked = 1;
- }
-
- /*
- * Open the serial device and set it up to be the ppp interface.
- * First we open it in non-blocking mode so we can set the
- * various termios flags appropriately. If we aren't dialling
- * out and we want to use the modem lines, we reopen it later
- * in order to wait for the carrier detect signal from the modem.
- */
- while ((ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0)) < 0) {
- if (errno != EINTR)
- syslog(LOG_ERR, "Failed to open %s: %m", devnam);
- if (!persist || errno != EINTR)
- goto fail;
- }
- if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1
- || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
- syslog(LOG_WARNING,
- "Couldn't reset non-blocking mode on device: %m");
-
- hungup = 0;
- kill_link = 0;
-
- /*
- * Do the equivalent of `mesg n' to stop broadcast messages.
- */
- if (fstat(ttyfd, &statbuf) < 0
- || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
- syslog(LOG_WARNING,
- "Couldn't restrict write permissions to %s: %m", devnam);
- } else
- tty_mode = statbuf.st_mode;
-
- /* run connection script */
- if (connector && connector[0]) {
- MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector));
-
- /*
- * Set line speed, flow control, etc.
- * On most systems we set CLOCAL for now so that we can talk
- * to the modem before carrier comes up. But this has the
- * side effect that we might miss it if CD drops before we
- * get to clear CLOCAL below. On systems where we can talk
- * successfully to the modem with CLOCAL clear and CD down,
- * we can clear CLOCAL at this point.
- */
- set_up_tty(ttyfd, 1);
-
- /* drop dtr to hang up in case modem is off hook */
- if (!default_device && modem) {
- setdtr(ttyfd, FALSE);
- sleep(1);
- setdtr(ttyfd, TRUE);
- }
-
- if (device_script(connector, ttyfd, ttyfd) < 0) {
- syslog(LOG_ERR, "Connect script failed");
- setdtr(ttyfd, FALSE);
- goto fail;
- }
-
-
- syslog(LOG_INFO, "Serial connection established.");
- sleep(1); /* give it time to set up its terminal */
- }
-
- /* set line speed, flow control, etc.; clear CLOCAL if modem option */
- set_up_tty(ttyfd, 0);
-
- /* reopen tty if necessary to wait for carrier */
- if (connector == NULL && modem) {
- while ((i = open(devnam, O_RDWR)) < 0) {
- if (errno != EINTR)
- syslog(LOG_ERR, "Failed to reopen %s: %m", devnam);
- if (!persist || errno != EINTR || hungup || kill_link)
- goto fail;
- }
- close(i);
- }
-
- /* run welcome script, if any */
- if (welcomer && welcomer[0]) {
- if (device_script(welcomer, ttyfd, ttyfd) < 0)
- syslog(LOG_WARNING, "Welcome script failed");
- }
-
- /* set up the serial device as a ppp interface */
- establish_ppp(ttyfd);
-
- if (!demand) {
-
- syslog(LOG_INFO, "Using interface ppp%d", ifunit);
- (void) sprintf(ifname, "ppp%d", ifunit);
- script_setenv("IFNAME", ifname);
-
- create_pidfile(); /* write pid to file */
- }
-
- /*
- * Start opening the connection and wait for
- * incoming events (reply, timeout, etc.).
- */
- syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam);
- lcp_lowerup(0);
- lcp_open(0); /* Start protocol */
- for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) {
- wait_input(timeleft(&timo));
- calltimeout();
- get_input();
- if (kill_link) {
- lcp_close(0, "User request");
- kill_link = 0;
- }
- if (open_ccp_flag) {
- if (phase == PHASE_NETWORK) {
- ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
- (*ccp_protent.open)(0);
- }
- open_ccp_flag = 0;
- }
- reap_kids(); /* Don't leave dead kids lying around */
- }
-
- /*
- * If we may want to bring the link up again, transfer
- * the ppp unit back to the loopback. Set the
- * real serial device back to its normal mode of operation.
- */
- clean_check();
- if (demand)
- restore_loop();
- disestablish_ppp(ttyfd);
-
- /*
- * Run disconnector script, if requested.
- * XXX we may not be able to do this if the line has hung up!
- */
- if (disconnector && !hungup) {
- set_up_tty(ttyfd, 1);
- if (device_script(disconnector, ttyfd, ttyfd) < 0) {
- syslog(LOG_WARNING, "disconnect script failed");
- } else {
- syslog(LOG_INFO, "Serial link disconnected.");
- }
- }
-
- fail:
- if (ttyfd >= 0)
- close_tty();
- if (locked) {
- unlock();
- locked = 0;
- }
-
- if (!demand) {
- if (pidfilename[0] != 0
- && unlink(pidfilename) < 0 && errno != ENOENT)
- syslog(LOG_WARNING, "unable to delete pid file: %m");
- pidfilename[0] = 0;
- }
-
- if (!persist)
- die(1);
-
- if (demand)
- demand_discard();
- if (holdoff > 0 && need_holdoff) {
- phase = PHASE_HOLDOFF;
- TIMEOUT(holdoff_end, NULL, holdoff);
- do {
- wait_time(timeleft(&timo));
- calltimeout();
- if (kill_link) {
- if (!persist)
- die(0);
- kill_link = 0;
- phase = PHASE_DORMANT; /* allow signal to end holdoff */
- }
- reap_kids();
- } while (phase == PHASE_HOLDOFF);
- }