Fix mdb_reader_pid().

Treat unexpected errors as "don't know".  Invert Pidcheck return
value, so nonzero including error codes = "the process may exist".

On Windows: Catch exited but still existing processes.  Handle
undefined PROCESS_QUERY_LIMITED_INFORMATION.
On Unix: don't trust F_GETLK error to leave the input alone,
the fcntl() doc seems unclear.
vmware
Hallvard Furuseth 11 years ago
parent 636c2d2a29
commit 1878213092
  1. 58
      libraries/liblmdb/mdb.c

@ -162,6 +162,11 @@
#define GET_PAGESIZE(x) {SYSTEM_INFO si; GetSystemInfo(&si); (x) = si.dwPageSize;} #define GET_PAGESIZE(x) {SYSTEM_INFO si; GetSystemInfo(&si); (x) = si.dwPageSize;}
#define close(fd) (CloseHandle(fd) ? 0 : -1) #define close(fd) (CloseHandle(fd) ? 0 : -1)
#define munmap(ptr,len) UnmapViewOfFile(ptr) #define munmap(ptr,len) UnmapViewOfFile(ptr)
#ifndef PROCESS_QUERY_LIMITED_INFORMATION
#define MDB_PROCESS_QUERY_LIMITED_INFORMATION PROCESS_QUERY_LIMITED_INFORMATION
#else
#define MDB_PROCESS_QUERY_LIMITED_INFORMATION 0x1000
#endif
#define Z "I" #define Z "I"
#else #else
@ -2019,7 +2024,8 @@ enum Pidlock_op {
#endif #endif
/** Set or check a pid lock. Set returns 0 on success. /** Set or check a pid lock. Set returns 0 on success.
* Check returns 0 if lock exists (meaning the process is alive). * Check returns 0 if the process is certainly dead, nonzero if it may
* be alive (the lock exists or an error happened so we do not know).
* *
* On Windows Pidset is a no-op, we merely check for the existence * On Windows Pidset is a no-op, we merely check for the existence
* of the process with the given pid. On POSIX we use a single byte * of the process with the given pid. On POSIX we use a single byte
@ -2029,32 +2035,35 @@ static int
mdb_reader_pid(MDB_env *env, enum Pidlock_op op, pid_t pid) mdb_reader_pid(MDB_env *env, enum Pidlock_op op, pid_t pid)
{ {
#ifdef _WIN32 #ifdef _WIN32
int ret = 0;
HANDLE h; HANDLE h;
int ver, query; if (op == Pidcheck) {
switch(op) {
case Pidset:
break;
case Pidcheck:
h = OpenProcess(env->me_pidquery, FALSE, pid); h = OpenProcess(env->me_pidquery, FALSE, pid);
/* No documented "no such process" code, but other program use this: */
if (!h) if (!h)
return GetLastError(); return ErrCode() != ERROR_INVALID_PARAMETER;
/* A process exists until all handles to it close. Has it exited? */
ret = WaitForSingleObject(h, 0) != 0;
CloseHandle(h); CloseHandle(h);
break;
} }
return 0; return ret;
#else #else
int rc; for (;;) {
struct flock lock_info; int rc;
memset((void *)&lock_info, 0, sizeof(lock_info)); struct flock lock_info;
lock_info.l_type = F_WRLCK; memset(&lock_info, 0, sizeof(lock_info));
lock_info.l_whence = SEEK_SET; lock_info.l_type = F_WRLCK;
lock_info.l_start = pid; lock_info.l_whence = SEEK_SET;
lock_info.l_len = 1; lock_info.l_start = pid;
while ((rc = fcntl(env->me_lfd, op, &lock_info)) && lock_info.l_len = 1;
(rc = ErrCode()) == EINTR) ; if ((rc = fcntl(env->me_lfd, op, &lock_info)) == 0) {
if (op == F_GETLK && rc == 0 && lock_info.l_type == F_UNLCK) if (op == F_GETLK && lock_info.l_type != F_UNLCK)
rc = -1; rc = -1;
return rc; } else if ((rc = ErrCode()) == EINTR) {
continue;
}
return rc;
}
#endif #endif
} }
@ -3247,7 +3256,7 @@ mdb_env_open2(MDB_env *env)
/* See if we should use QueryLimited */ /* See if we should use QueryLimited */
rc = GetVersion(); rc = GetVersion();
if ((rc & 0xff) > 5) if ((rc & 0xff) > 5)
env->me_pidquery = PROCESS_QUERY_LIMITED_INFORMATION; env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION;
else else
env->me_pidquery = PROCESS_QUERY_INFORMATION; env->me_pidquery = PROCESS_QUERY_INFORMATION;
@ -8144,9 +8153,10 @@ int mdb_reader_check(MDB_env *env, int *dead)
if (mr[i].mr_pid && mr[i].mr_pid != env->me_pid) { if (mr[i].mr_pid && mr[i].mr_pid != env->me_pid) {
pid = mr[i].mr_pid; pid = mr[i].mr_pid;
if (mdb_pid_insert(pids, pid) == 0) { if (mdb_pid_insert(pids, pid) == 0) {
if (mdb_reader_pid(env, Pidcheck, pid)) { if (!mdb_reader_pid(env, Pidcheck, pid)) {
LOCK_MUTEX_R(env); LOCK_MUTEX_R(env);
if (mdb_reader_pid(env, Pidcheck, pid)) { /* Recheck, a new process may have reused pid */
if (!mdb_reader_pid(env, Pidcheck, pid)) {
for (j=i; j<rdrs; j++) for (j=i; j<rdrs; j++)
if (mr[j].mr_pid == pid) { if (mr[j].mr_pid == pid) {
mr[j].mr_pid = 0; mr[j].mr_pid = 0;

Loading…
Cancel
Save