19 #define LMIL MIL << "LOCK [" << _options.name << "] "
28 ZYppLockedException::ZYppLockedException(
const std::string & msg_r,
29 const std::string &name,
32 , _locker_pid (locker_pid)
41 const std::string &pname,
59 pid_t curr_pid = getpid();
71 _fd.reset(
new Fd( lock_file, O_RDWR | O_CREAT | O_EXCL, 0666) );
75 if ( !
_fd || !
_fd->isOpen() )
80 _fd.reset(
new Fd( lock_file, O_RDWR ) );
86 memset(&lock, 0,
sizeof(
struct flock));
87 lock.l_whence = SEEK_SET;
96 if (fcntl(
_fd->fd(), F_GETLK, &lock) < 0)
102 MIL << lock_file <<
" : ";
103 switch ( lock.l_type )
106 MIL <<
" Write-Lock conflicts" << endl;
109 MIL <<
" Read-Lock conflicts" << endl;
112 MIL <<
" No lock conflicts" << endl;
132 if ( lock.l_type != F_UNLCK )
135 LMIL <<
"pid " << lock.l_pid <<
" is running and has a lock that conflicts with us." << std::endl;
143 _(
"This action is being run by another program already."),
148 LMIL <<
"waiting 1 second..." << endl;
159 lock.l_type = F_WRLCK;
161 if (fcntl(
_fd->fd(), F_GETLK, &lock) < 0)
166 if ( lock.l_type == F_UNLCK )
168 LMIL <<
"no previous readers, unlinking lock file and retrying." << endl;
176 memset(&lock, 0,
sizeof(
struct flock));
177 lock.l_type = F_WRLCK;
178 lock.l_whence = SEEK_SET;
179 lock.l_pid = getpid();
181 if (fcntl(
_fd->fd(), F_SETLK, &lock) < 0)
188 else if ( lock.l_type == F_RDLCK )
191 LMIL <<
"previous readers on lock file. taking lock as a reader." << std::endl;
192 memset(&lock, 0,
sizeof(
struct flock));
193 lock.l_type = F_RDLCK;
194 lock.l_whence = SEEK_SET;
195 lock.l_pid = getpid();
197 if (fcntl(
_fd->fd(), F_SETLK, &lock) < 0)
207 ERR <<
"impossible condition" << endl;
214 LMIL <<
"stale lock found" << endl;
222 memset(&lock, 0,
sizeof(
struct flock));
223 lock.l_type = F_WRLCK;
224 lock.l_whence = SEEK_SET;
225 lock.l_pid = getpid();
227 if (fcntl(
_fd->fd(), F_SETLK, &lock) < 0)
237 LMIL <<
"undefined case!" << endl;
251 LMIL <<
"no lock found, taking ownership of it as a " << ( (
_options.
type ==
Reader ) ?
"reader" :
"writer" ) << endl;
253 memset(&lock, 0,
sizeof(
struct flock));
254 lock.l_whence = SEEK_SET;
255 lock.l_type = F_WRLCK;
256 lock.l_pid = getpid();
258 if (fcntl(
_fd->fd(), F_SETLK, &lock) < 0)
262 sprintf( buffer,
"%d\n", curr_pid);
263 write(
_fd->fd(), buffer, strlen(buffer));
270 lock.l_type = F_RDLCK;
272 if (fcntl(
_fd->fd(), F_SETLK, &lock) < 0)
280 LMIL <<
"Lock intialized" << endl;
291 <<
" lock on " << lock_file << endl;
323 PathInfo status( procdir/
"status" );
324 XXX <<
"Checking " << status << endl;
325 bool still_running = status.isExist();
329 Pathname p( procdir/
"exe" );
332 p = procdir/
"cmdline";
334 std::ifstream infile( p.c_str() );
341 return still_running;