]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
Apparently the IBM patent on LZW is still pending.
[ppp.git] / pppd / main.c
index 285f219061f3a2ff53d269916b4400cdeb4d1a07..65a8877c6f276da8a96f0a96200aef54ecfd1daa 100644 (file)
@@ -66,7 +66,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: main.c,v 1.145 2004/11/12 10:30:51 paulus Exp $"
+#define RCSID  "$Id: main.c,v 1.148 2004/11/13 12:05:48 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -172,6 +172,7 @@ int devfd = -1;                     /* fd of underlying device */
 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;
@@ -470,6 +471,8 @@ main(argc, argv)
 
     setup_signals();
 
+    create_linkpidfile(getpid());
+
     waiting = 0;
 
     /*
@@ -479,14 +482,8 @@ main(argc, argv)
        /*
         * Open the loopback channel and set it up to be the ppp interface.
         */
-#ifdef USE_TDB
-       tdb_writelock(pppdb);
-#endif
        fd_loop = open_ppp_loopback();
        set_ifunit(1);
-#ifdef USE_TDB
-       tdb_writeunlock(pppdb);
-#endif
        /*
         * Configure the interface and mark it up, etc.
         */
@@ -515,13 +512,13 @@ main(argc, argv)
            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;
 
            /*
@@ -540,9 +537,10 @@ main(argc, argv)
        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();
            }
@@ -554,7 +552,7 @@ main(argc, argv)
            }
        }
 
-       if (!persist || (maxfail > 0 && unsuccess >= maxfail))
+       if (!persist || asked_to_quit || (maxfail > 0 && unsuccess >= maxfail))
            break;
 
        if (demand)
@@ -628,6 +626,7 @@ handle_events()
     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;
@@ -1921,6 +1920,41 @@ script_unsetenv(var)
 #endif
 }
 
+/*
+ * Any arbitrary string used as a key for locking the database.
+ * It doesn't matter what it is as long as all pppds use the same string.
+ */
+#define PPPD_LOCK_KEY  "pppd lock"
+
+/*
+ * lock_db - get an exclusive lock on the TDB database.
+ * Used to ensure atomicity of various lookup/modify operations.
+ */
+void lock_db()
+{
+#ifdef USE_TDB
+       TDB_DATA key;
+
+       key.dptr = PPPD_LOCK_KEY;
+       key.dsize = strlen(key.dptr);
+       tdb_chainlock(pppdb, key);
+#endif
+}
+
+/*
+ * unlock_db - remove the exclusive lock obtained by lock_db.
+ */
+void unlock_db()
+{
+#ifdef USE_TDB
+       TDB_DATA key;
+
+       key.dptr = PPPD_LOCK_KEY;
+       key.dsize = strlen(key.dptr);
+       tdb_chainunlock(pppdb, key);
+#endif
+}
+
 #ifdef USE_TDB
 /*
  * update_db_entry - update our entry in the database.