Mailinglist Archive: zypp-commit (606 mails)

< Previous Next >
[zypp-commit] r9416 - in /trunk/libzypp: tests/zypp/base/InterProcessMutex2_test.cc tests/zypp/base/InterProcessMutex_test.cc zypp/base/InterProcessMutex.cc zypp/base/InterProcessMutex.h
  • From: dmacvicar@xxxxxxxxxxxxxxxx
  • Date: Mon, 07 Apr 2008 10:18:01 -0000
  • Message-id: <20080407101802.1153D26E82@xxxxxxxxxxxxxxxx>
Author: dmacvicar
Date: Mon Apr 7 12:18:01 2008
New Revision: 9416

URL: http://svn.opensuse.org/viewcvs/zypp?rev=9416&view=rev
Log:
- move the initialization to a option class.
- allow the base path to be overriden.
by default it uses the same /var/run/zypp-name.lock if root
and /var/tmp/zypp-username/zypp-name.lock if user.

- now test pass as user too.

Modified:
trunk/libzypp/tests/zypp/base/InterProcessMutex2_test.cc
trunk/libzypp/tests/zypp/base/InterProcessMutex_test.cc
trunk/libzypp/zypp/base/InterProcessMutex.cc
trunk/libzypp/zypp/base/InterProcessMutex.h

Modified: trunk/libzypp/tests/zypp/base/InterProcessMutex2_test.cc
URL:
http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/InterProcessMutex2_test.cc?rev=9416&r1=9415&r2=9416&view=diff
==============================================================================
--- trunk/libzypp/tests/zypp/base/InterProcessMutex2_test.cc (original)
+++ trunk/libzypp/tests/zypp/base/InterProcessMutex2_test.cc Mon Apr 7
12:18:01 2008
@@ -46,13 +46,13 @@
MIL << "child, PID: " << getpid() << endl;
// child
sleep(3);
- BOOST_REQUIRE_THROW( InterProcessMutex(
InterProcessMutex::Reader,"testcase", 0), ZYppLockedException);
+ BOOST_REQUIRE_THROW( InterProcessMutex(
InterProcessMutex::Options(InterProcessMutex::Reader,"testcase", 0)),
ZYppLockedException);
//InterProcessMutex mutex2("testcase");
}
else
{
MIL << "parent: " << getpid() << endl;
- InterProcessMutex mutex( InterProcessMutex::Writer, "testcase");
+ InterProcessMutex mutex(
InterProcessMutex::Options(InterProcessMutex::Writer, "testcase"));
// parent
sleep(6);
wait(NULL);

Modified: trunk/libzypp/tests/zypp/base/InterProcessMutex_test.cc
URL:
http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/InterProcessMutex_test.cc?rev=9416&r1=9415&r2=9416&view=diff
==============================================================================
--- trunk/libzypp/tests/zypp/base/InterProcessMutex_test.cc (original)
+++ trunk/libzypp/tests/zypp/base/InterProcessMutex_test.cc Mon Apr 7 12:18:01
2008
@@ -44,12 +44,12 @@
{
MIL << "child, PID: " << getpid() << endl;
sleep(3);
- InterProcessMutex mutex2(InterProcessMutex::Reader,"testcase");
+ InterProcessMutex
mutex2(InterProcessMutex::Options(InterProcessMutex::Reader,"testcase"));
}
else
{
MIL << "parent: " << getpid() << endl;
- InterProcessMutex mutex(InterProcessMutex::Writer,"testcase");
+ InterProcessMutex
mutex(InterProcessMutex::Options(InterProcessMutex::Writer,"testcase"));
// parent
sleep(6);
}

Modified: trunk/libzypp/zypp/base/InterProcessMutex.cc
URL:
http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/InterProcessMutex.cc?rev=9416&r1=9415&r2=9416&view=diff
==============================================================================
--- trunk/libzypp/zypp/base/InterProcessMutex.cc (original)
+++ trunk/libzypp/zypp/base/InterProcessMutex.cc Mon Apr 7 12:18:01 2008
@@ -12,11 +12,11 @@
#include "zypp/base/InterProcessMutex.h"
#include "zypp/base/String.h"

+#include "zypp/TmpPath.h"
#include "zypp/Pathname.h"
#include "zypp/PathInfo.h"

-#define ZYPP_LOCK_FILE "/var/run/zypp.pid"
-#define LMIL MIL << "LOCK [" << _name << "] "
+#define LMIL MIL << "LOCK [" << _options.name << "] "

using namespace std;

@@ -35,16 +35,26 @@

ZYppLockedException::~ZYppLockedException() throw()
{}
-
+

-InterProcessMutex::InterProcessMutex( ConsumerType ctype,
- const std::string &name,
- int timeout )
- : _name(name)
- , _timeout(timeout)
- , _type(ctype)
+InterProcessMutex::Options::Options( ConsumerType ptype,
+ const std::string &pname,
+ int ptimeout )
+ : name(pname)
+ , timeout(ptimeout)
+ , type(ptype)
{
+ if ( geteuid() == 0 )
+ base = "/var/run";
+ else
+ base = filesystem::TmpPath::defaultLocation() + ( string("zypp-") +
getenv("USER") );
+}
+

+
+InterProcessMutex::InterProcessMutex( const Options &poptions )
+ : _options(poptions)
+{
// get the current pid
pid_t curr_pid = getpid();
Pathname lock_file = lockFilePath();
@@ -75,7 +85,7 @@
// GETLK tells you the conflicting lock as if the lock you pass
// would have been set. So set the lock type depending on wether
// we are a writer or a reader.
- lock.l_type = ( ( _type == Writer ) ? F_WRLCK : F_RDLCK );
+ lock.l_type = ( ( _options.type == Writer ) ? F_WRLCK : F_RDLCK );


// get lock information
@@ -123,11 +133,11 @@
// abort if we have slept more or equal than the timeout, but
// not for the case where timeout is negative which means no
// timeout and therefore we never abort.
- if ( (totalslept >= _timeout) && (_timeout >= 0 ) )
+ if ( (totalslept >= _options.timeout) && (_options.timeout >=
0 ) )
{
ZYPP_THROW(ZYppLockedException(

_("This action is being run by another
program already."),
- _name, lock.l_pid));
+ _options.name, lock.l_pid));
}

// if not, let sleep one second and count it
@@ -136,7 +146,7 @@
++totalslept;
continue;
}
- else if ( ( lock.l_type == F_UNLCK ) && ( _type == Reader ) )
+ else if ( ( lock.l_type == F_UNLCK ) && ( _options.type == Reader
) )
{
// either there is no lock or a reader has it so we just
// acquire a reader lock.
@@ -195,7 +205,7 @@
break;
}
}
- else if ( ( lock.l_type == F_UNLCK ) && ( _type == Writer ) )
+ else if ( ( lock.l_type == F_UNLCK ) && ( _options.type == Writer
) )
{
LMIL << "stale lock found" << endl;
// Nobody conflicts with a writer lock so nobody is actually
@@ -234,7 +244,7 @@
// try to lock it exclusively
// if it fails, someone won us, so we just go for another try
// or just abort
- LMIL << "no lock found, taking ownership of it as a " << ( (_type
== Reader ) ? "reader" : "writer" ) << endl;
+ LMIL << "no lock found, taking ownership of it as a " << (
(_options.type == Reader ) ? "reader" : "writer" ) << endl;
struct flock lock;
memset(&lock, 0, sizeof(struct flock));
lock.l_whence = SEEK_SET;
@@ -251,7 +261,7 @@
// by now the pid is written and the file locked.
// If we are a reader, just downgrade the lock to
// read shared lock.
- if ( _type == Reader )
+ if ( _options.type == Reader )
{
lock.l_type = F_RDLCK;

@@ -273,10 +283,10 @@
{
Pathname lock_file = lockFilePath();
LMIL << "dropping "
- << ( (_type == Reader ) ? "reader" : "writer" )
+ << ( (_options.type == Reader ) ? "reader" : "writer" )
<< " lock on " << lock_file << endl;

- switch ( _type )
+ switch ( _options.type )
{
case Reader:

@@ -297,7 +307,8 @@

Pathname InterProcessMutex::lockFilePath() const
{
- return Pathname("/var/run/zypp-" + _name + ".pid");
+ filesystem::assert_dir(_options.base);
+ return _options.base + ("zypp-" + _options.name + ".lock");
}

bool InterProcessMutex::isProcessRunning(pid_t pid_r)

Modified: trunk/libzypp/zypp/base/InterProcessMutex.h
URL:
http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/InterProcessMutex.h?rev=9416&r1=9415&r2=9416&view=diff
==============================================================================
--- trunk/libzypp/zypp/base/InterProcessMutex.h (original)
+++ trunk/libzypp/zypp/base/InterProcessMutex.h Mon Apr 7 12:18:01 2008
@@ -5,6 +5,7 @@
#include <string>
#include "zypp/base/Fd.h"
#include "zypp/base/Exception.h"
+#include "zypp/base/NonCopyable.h"
#include "zypp/Pathname.h"

namespace zypp
@@ -26,7 +27,6 @@
std::string _name;
};

-
/**
*
* Inter process scoped lock implementation
@@ -40,7 +40,7 @@
* currently a writer.
*
*/
-class InterProcessMutex
+class InterProcessMutex : private base::NonCopyable
{
public:
/**
@@ -54,6 +54,43 @@
};

/**
+ * options to alter the mutex behavor
+ */
+ class Options
+ {
+ public:
+ /**
+ * Options for a mutex of type \ref ptype
+ * with a given name and timeout.
+ * Default is name "zypp" and no timeout
+ * (wait till resource is free)
+ *
+ * The mutex type, Writer or Reader must be
+ * given explitly.
+ *
+ * The mutex will be handled using a lock file
+ * located on default library path if the
+ * library is running as root, and in users home
+ * directory if not.
+ *
+ */
+ Options( ConsumerType ptype,
+ const std::string &pname = "zypp",
+ int ptimeout = -1 );
+
+ /**
+ * set the path where the lockfile is
+ * created.
+ */
+ void setPath( const Pathname &base );
+
+ std::string name;
+ int timeout;
+ ConsumerType type;
+ Pathname base;
+ };
+
+ /**
* Creates a mutex with a name and a timeout.
*
* default timeout is -1 which means no timeout
@@ -68,9 +105,7 @@
* the timeout is reached.
*
*/
- InterProcessMutex( ConsumerType ctype,
- const std::string &name = "zypp",
- int timeout = -1 );
+ InterProcessMutex( const Options &poptions );

/**
* Destructor, gives up the lock on the named
@@ -83,9 +118,7 @@
Pathname lockFilePath() const;
private:
shared_ptr<Fd> _fd;
- std::string _name;
- int _timeout;
- ConsumerType _type;
+ Options _options;
};



--
To unsubscribe, e-mail: zypp-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: zypp-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages