]> git.sur5r.net Git - openldap/commitdiff
ITS#7364 Always sem_unlink() in mdb_env_open().
authorHallvard Furuseth <hallvard@openldap.org>
Mon, 17 Sep 2012 13:42:15 +0000 (15:42 +0200)
committerHallvard Furuseth <hallvard@openldap.org>
Mon, 17 Sep 2012 13:42:15 +0000 (15:42 +0200)
Drop the sem_unlink() error checks, which could prevent the 2nd
unlink.  Instead use O_EXCL in sem_open().

This makes "open+close the database" the API for trying to clean
away the old semaphores, if they were left behind by a previous run.

libraries/libmdb/mdb.c

index a593cd931475cbefe30135f23fe97fa0a6ac0e37..9546d01f4738ef6a4f11c72f8180130da315941f 100644 (file)
@@ -3006,12 +3006,12 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                val.mv_size = sizeof(idbuf);
                mdb_hash_hex(&val, hexbuf);
                sprintf(env->me_txns->mti_rmname, "Global\\MDBr%s", hexbuf);
+               sprintf(env->me_txns->mti_wmname, "Global\\MDBw%s", hexbuf);
                env->me_rmutex = CreateMutex(&mdb_all_sa, FALSE, env->me_txns->mti_rmname);
                if (!env->me_rmutex) {
                        rc = ErrCode();
                        goto fail;
                }
-               sprintf(env->me_txns->mti_wmname, "Global\\MDBw%s", hexbuf);
                env->me_wmutex = CreateMutex(&mdb_all_sa, FALSE, env->me_txns->mti_wmname);
                if (!env->me_wmutex) {
                        rc = ErrCode();
@@ -3033,23 +3033,20 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                val.mv_size = sizeof(idbuf);
                mdb_hash_hex(&val, hexbuf);
                sprintf(env->me_txns->mti_rmname, "/MDBr%s", hexbuf);
-               if (sem_unlink(env->me_txns->mti_rmname)) {
-                       rc = ErrCode();
-                       if (rc != ENOENT && rc != EINVAL)
-                               goto fail;
-               }
-               env->me_rmutex = sem_open(env->me_txns->mti_rmname, O_CREAT, mode, 1);
+               sprintf(env->me_txns->mti_wmname, "/MDBw%s", hexbuf);
+               /* Clean up after a previous run, if needed:  Try to
+                * remove both semaphores before doing anything else.
+                */
+               sem_unlink(env->me_txns->mti_rmname);
+               sem_unlink(env->me_txns->mti_wmname);
+               env->me_rmutex = sem_open(env->me_txns->mti_rmname,
+                       O_CREAT|O_EXCL, mode, 1);
                if (env->me_rmutex == SEM_FAILED) {
                        rc = ErrCode();
                        goto fail;
                }
-               sprintf(env->me_txns->mti_wmname, "/MDBw%s", hexbuf);
-               if (sem_unlink(env->me_txns->mti_wmname)) {
-                       rc = ErrCode();
-                       if (rc != ENOENT && rc != EINVAL)
-                               goto fail;
-               }
-               env->me_wmutex = sem_open(env->me_txns->mti_wmname, O_CREAT, mode, 1);
+               env->me_wmutex = sem_open(env->me_txns->mti_wmname,
+                       O_CREAT|O_EXCL, mode, 1);
                if (env->me_wmutex == SEM_FAILED) {
                        rc = ErrCode();
                        goto fail;