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;
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
bool next()
Advance to next line.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
static const Pathname & defaultLocation()
Simple lineparser: Traverse each line in a file.
options to alter the mutex behavor
Options(ConsumerType ptype, const std::string &pname="zypp", int ptimeout=-1)
Options for a mutex of type ptype with a given name and timeout.
~InterProcessMutex()
Destructor, gives up the lock on the named resource.
bool write(const Pathname &path_r, const std::string &key_r, const std::string &val_r, const std::string &newcomment_r)
Add or change a value in sysconfig file path_r.
int unlink(const Pathname &path)
Like 'unlink'.
bool isProcessRunning(pid_t pid_r)
ConsumerType
Processes can be of two types Reader or Writer.
#define _(MSG)
Return translated text.
std::string numstring(char n, int w=0)
std::string form(const char *format,...)
Printf style construction of std::string.
Base class for Exception.
virtual ~ZYppLockedException()
InterProcessMutex(const Options &poptions)
Creates a mutex with a name and a timeout.
Assert close called on open filedescriptor.
std::string strerror(int errno_r)
Return string describing the error_r code.
Pathname lockFilePath() const