* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: main.c,v 1.147 2004/11/13 12:02:22 paulus Exp $"
+#define RCSID  "$Id: main.c,v 1.148 2004/11/13 12:05:48 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
 int fd_ppp = -1;               /* fd for talking PPP */
 int phase;                     /* where the link is at */
 int kill_link;
+int asked_to_quit;
 int open_ccp_flag;
 int listen_time;
 int got_sigusr2;
            add_fd(fd_loop);
            for (;;) {
                handle_events();
-               if (kill_link && !persist)
+               if (asked_to_quit)
                    break;
                if (get_loop_output())
                    break;
            }
            remove_fd(fd_loop);
-           if (kill_link && !persist)
+           if (asked_to_quit)
                break;
 
            /*
        while (phase != PHASE_DEAD) {
            handle_events();
            get_input();
-           if (kill_link) {
-               bundle_terminating = 1;
+           if (kill_link)
                lcp_close(0, "User request");
+           if (asked_to_quit) {
+               bundle_terminating = 1;
                if (phase == PHASE_MASTER)
                    mp_bundle_terminated();
            }
            }
        }
 
-       if (!persist || (maxfail > 0 && unsuccess >= maxfail))
+       if (!persist || asked_to_quit || (maxfail > 0 && unsuccess >= maxfail))
            break;
 
        if (demand)
     if (got_sigterm) {
        info("Terminating on signal %d", got_sigterm);
        kill_link = 1;
+       asked_to_quit = 1;
        persist = 0;
        status = EXIT_USER_REQUEST;
        got_sigterm = 0;
 
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: tty.c,v 1.20 2004/11/12 09:51:23 paulus Exp $"
+#define RCSID  "$Id: tty.c,v 1.21 2004/11/13 12:05:48 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
 
 extern uid_t uid;
 extern int kill_link;
+extern int asked_to_quit;
+extern int got_sigterm;
 
 /* XXX */
 extern int privopen;           /* don't lock, open device as root */
         * in order to wait for the carrier detect signal from the modem.
         */
        hungup = 0;
-       kill_link = 0;
+       got_sigterm = 0;
        connector = doing_callback? callback_script: connect_script;
        if (devnam[0] != 0) {
                for (;;) {
                                status = EXIT_INIT_FAILED;
                                goto errret;
                        }
-                       if (kill_link) {
+                       if (got_sigterm) {
                                disconnect_tty();
                                goto errret;
                        }
                                status = EXIT_CONNECT_FAILED;
                                goto errret;
                        }
-                       if (kill_link) {
+                       if (got_sigterm) {
                                disconnect_tty();
                                goto errret;
                        }
                                error("Failed to reopen %s: %m", devnam);
                                status = EXIT_OPEN_FAILED;
                        }
-                       if (!persist || errno != EINTR || hungup || kill_link)
+                       if (!persist || errno != EINTR || hungup || got_sigterm)
                                goto errret;
                }
                close(i);
                real_ttyfd = -1;
        }
        ttyfd = -1;
+       if (got_sigterm)
+               asked_to_quit = 1;
        return -1;
 }