diff -up lockdev-1.0.3/src/lockdev.c.pidexists lockdev-1.0.3/src/lockdev.c --- lockdev-1.0.3/src/lockdev.c.pidexists 2009-12-05 15:56:46.000000000 +0100 +++ lockdev-1.0.3/src/lockdev.c 2009-12-05 16:05:46.000000000 +0100 @@ -148,7 +148,7 @@ static inline int _dl_lock_semaphore ( v static inline int _dl_block_semaphore ( void); static pid_t _dl_check_lock ( const char * lockname); static char * _dl_check_devname ( const char * devname); - +static inline int _dl_pid_exists ( pid_t pid ); #define SEMAPHORE "LOCKDEV" #define close_n_return( v) return( _dl_unlock_semaphore( v)) @@ -394,7 +394,7 @@ _dl_check_lock(const char *lockname) /* checks content's format */ if ( j == 1 ) { /* checks process existence */ - if ( ( kill( pid_read, 0) == 0 ) || (errno == EPERM) ) { + if ( _dl_pid_exists( pid_read) || (errno == EPERM) ) { _debug( 2, "_dl_check_lock() locked by %d\n", (int)pid_read); return pid_read; } @@ -443,7 +443,7 @@ _dl_check_lock(const char *lockname) return -1; } fscanf( fd, "%d", &pid2); - if ( pid2 && (pid2 != pid_read) && ( kill( pid2, 0) == 0 )) { + if ( pid2 && (pid2 != pid_read) && _dl_pid_exists(pid2) ) { /* lock file was changed! let us quickly * put it back again */ @@ -502,6 +502,17 @@ _dl_check_devname (const char *devname) return strcpy(m, p); } +/* for internal use */ +/* correctly check if a process with a given pid exists */ +static inline int +_dl_pid_exists(pid_t pid) +{ + if ( !kill( pid, 0)) + return 1; + if ( errno == ESRCH) + return 0; + return 1; +} /* exported by the interface file lockdev.h */ /* ZERO means that the device wasn't locked, but could have been locked later */