123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "pathlocks.hh"
- #include "util.hh"
- #include <cerrno>
- #include <cstdlib>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- namespace nix {
- int openLockFile(const Path & path, bool create)
- {
- AutoCloseFD fd;
- fd = open(path.c_str(), O_RDWR | (create ? O_CREAT : 0), 0600);
- if (fd == -1 && (create || errno != ENOENT))
- throw SysError(format("opening lock file `%1%'") % path);
- closeOnExec(fd);
- return fd.borrow();
- }
- void deleteLockFile(const Path & path, int fd)
- {
-
- unlink(path.c_str());
- writeFull(fd, "d");
-
- }
- bool lockFile(int fd, LockType lockType, bool wait)
- {
- struct flock lock;
- if (lockType == ltRead) lock.l_type = F_RDLCK;
- else if (lockType == ltWrite) lock.l_type = F_WRLCK;
- else if (lockType == ltNone) lock.l_type = F_UNLCK;
- else abort();
- lock.l_whence = SEEK_SET;
- lock.l_start = 0;
- lock.l_len = 0;
- if (wait) {
- while (fcntl(fd, F_SETLKW, &lock) != 0) {
- checkInterrupt();
- if (errno != EINTR)
- throw SysError(format("acquiring/releasing lock"));
- }
- } else {
- while (fcntl(fd, F_SETLK, &lock) != 0) {
- checkInterrupt();
- if (errno == EACCES || errno == EAGAIN) return false;
- if (errno != EINTR)
- throw SysError(format("acquiring/releasing lock"));
- }
- }
- return true;
- }
- static StringSet lockedPaths;
- PathLocks::PathLocks()
- : deletePaths(false)
- {
- }
- PathLocks::PathLocks(const PathSet & paths, const string & waitMsg)
- : deletePaths(false)
- {
- lockPaths(paths, waitMsg);
- }
- bool PathLocks::lockPaths(const PathSet & _paths,
- const string & waitMsg, bool wait)
- {
- assert(fds.empty());
-
-
-
- Paths paths(_paths.begin(), _paths.end());
- paths.sort();
-
-
- foreach (Paths::iterator, i, paths) {
- checkInterrupt();
- Path path = *i;
- Path lockPath = path + ".lock";
- debug(format("locking path `%1%'") % path);
- if (lockedPaths.find(lockPath) != lockedPaths.end())
- throw Error("deadlock: trying to re-acquire self-held lock");
- AutoCloseFD fd;
-
- while (1) {
-
- fd = openLockFile(lockPath, true);
-
- if (!lockFile(fd, ltWrite, false)) {
- if (wait) {
- if (waitMsg != "") printMsg(lvlError, waitMsg);
- lockFile(fd, ltWrite, true);
- } else {
-
- unlock();
- return false;
- }
- }
- debug(format("lock acquired on `%1%'") % lockPath);
-
- struct stat st;
- if (fstat(fd, &st) == -1)
- throw SysError(format("statting lock file `%1%'") % lockPath);
- if (st.st_size != 0)
-
- debug(format("open lock file `%1%' has become stale") % lockPath);
- else
- break;
- }
-
- fds.push_back(FDPair(fd.borrow(), lockPath));
- lockedPaths.insert(lockPath);
- }
- return true;
- }
- PathLocks::~PathLocks()
- {
- try {
- unlock();
- } catch (...) {
- ignoreException();
- }
- }
- void PathLocks::unlock()
- {
- foreach (list<FDPair>::iterator, i, fds) {
- if (deletePaths) deleteLockFile(i->second, i->first);
- lockedPaths.erase(i->second);
- if (close(i->first) == -1)
- printMsg(lvlError,
- format("error (ignored): cannot close lock file on `%1%'") % i->second);
- debug(format("lock released on `%1%'") % i->second);
- }
- fds.clear();
- }
- void PathLocks::setDeletion(bool deletePaths)
- {
- this->deletePaths = deletePaths;
- }
- bool pathIsLockedByMe(const Path & path)
- {
- Path lockPath = path + ".lock";
- return lockedPaths.find(lockPath) != lockedPaths.end();
- }
-
- }
|