From: Hallvard Furuseth Date: Mon, 17 Sep 2012 13:42:15 +0000 (+0200) Subject: ITS#7364 Always sem_unlink() in mdb_env_open(). X-Git-Tag: OPENLDAP_REL_ENG_2_4_33~17^2~5 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=c760e536ec1b9fed01b4d8e8d884533dceec397d;p=openldap ITS#7364 Always sem_unlink() in mdb_env_open(). 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. --- diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index a593cd9314..9546d01f47 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -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;