aboutsummaryrefslogtreecommitdiff
#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)
{
    /* Get rid of the lock file.  Have to be careful not to introduce
       races.  Write a (meaningless) token to the file to indicate to
       other processes waiting on this lock that the lock is stale
       (deleted). */
    unlink(path.c_str());
    writeFull(fd, "d");
    /* Note that the result of unlink() is ignored; removing the lock
       file is an optimisation, not a necessity. */
}


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; /* entire file */

    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;
}


/* This enables us to check whether are not already holding a lock on
   a file ourselves.  POSIX locks (fcntl) suck in this respect: if we
   close a descriptor, the previous lock will be closed as well.  And
   there is no way to query whether we already have a lock (F_GETLK
   only works on locks held by other processes). */
static StringSet lockedPaths; /* !!! not thread-safe */


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());
    
    /* Note that `fds' is built incrementally so that the destructor
       will only release those locks that we have already acquired. */

    /* Sort the paths.  This assures that locks are always acquired in
       the same order, thus preventing deadlocks. */
    Paths paths(_paths.begin(), _paths.end());
    paths.sort();
    
    /* Acquire the lock for each path. */
    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) {

            /* Open/create the lock file. */
	    fd = openLockFile(lockPath, true);

            /* Acquire an exclusive lock. */
            if (!lockFile(fd, ltWrite, false)) {
                if (wait) {
                    if (waitMsg != "") printMsg(lvlError, waitMsg);
                    lockFile(fd, ltWrite, true);
                } else {
                    /* Failed to lock this path; release all other
                       locks. */
                    unlock();
                    return false;
                }
            }

            debug(format("lock acquired on `%1%'") % lockPath);

            /* Check that the lock file hasn't become stale (i.e.,
               hasn't been unlinked). */
            struct stat st;
            if (fstat(fd, &st) == -1)
                throw SysError(format("statting lock file `%1%'") % lockPath);
            if (st.st_size != 0)
                /* This lock file has been unlinked, so we're holding
                   a lock on a deleted file.  This means that other
                   processes may create and acquire a lock on
                   `lockPath', and proceed.  So we must retry. */
                debug(format("open lock file `%1%' has become stale") % lockPath);
            else
                break;
        }

        /* Use borrow so that the descriptor isn't closed. */
        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();
}

 
}
71999logplainabout -rw-r--r--android.scm51004logplainabout -rw-r--r--animation.scm19902logplainabout -rw-r--r--anthy.scm2879logplainabout -rw-r--r--antivirus.scm7311logplainabout -rw-r--r--apl.scm3278logplainabout -rw-r--r--apr.scm4258logplainabout -rw-r--r--arcan.scm11139logplainabout -rw-r--r--aspell.scm20760logplainabout -rw-r--r--assembly.scm21458logplainabout -rw-r--r--astronomy.scm64457logplainabout -rw-r--r--attr.scm3492logplainabout -rw-r--r--audio.scm219322logplainabout -rw-r--r--augeas.scm2617logplainabout -rw-r--r--authentication.scm6796logplainabout -rw-r--r--autogen.scm3406logplainabout -rw-r--r--autotools.scm27444logplainabout d---------aux-files336logplain -rw-r--r--avahi.scm5706logplainabout -rw-r--r--avr-xyz.scm3853logplainabout -rw-r--r--avr.scm7769logplainabout -rw-r--r--axoloti.scm29334logplainabout -rw-r--r--backup.scm59026logplainabout -rw-r--r--barrier.scm2761logplainabout -rw-r--r--base.scm62956logplainabout -rw-r--r--bash.scm19472logplainabout -rw-r--r--batik.scm8758logplainabout -rw-r--r--bdw-gc.scm5914logplainabout -rw-r--r--benchmark.scm25154logplainabout -rw-r--r--bioconductor.scm558146logplainabout -rw-r--r--bioinformatics.scm670004logplainabout -rw-r--r--bison.scm3264logplainabout -rw-r--r--bittorrent.scm24785logplainabout -rw-r--r--boost.scm20568logplainabout -rw-r--r--bootloaders.scm59303logplainabout -rw-r--r--bootstrap.scm45634logplainabout -rw-r--r--browser-extensions.scm4613logplainabout -rw-r--r--build-tools.scm23625logplainabout -rw-r--r--busybox.scm7282logplainabout -rw-r--r--c.scm44681logplainabout -rw-r--r--calcurse.scm3469logplainabout -rw-r--r--calendar.scm18319logplainabout -rw-r--r--ccache.scm3110logplainabout -rw-r--r--cdrom.scm42487logplainabout -rw-r--r--cedille.scm5388logplainabout -rw-r--r--certs.scm13054logplainabout -rw-r--r--check.scm132646logplainabout -rw-r--r--chemistry.scm27126logplainabout -rw-r--r--chez.scm52065logplainabout -rw-r--r--chicken.scm10435logplainabout -rw-r--r--chromium.scm44364logplainabout -rw-r--r--ci.scm13025logplainabout -rw-r--r--cinnamon.scm2923logplainabout -rw-r--r--clojure.scm23753logplainabout -rw-r--r--cluster.scm11186logplainabout -rw-r--r--cmake.scm17896logplainabout -rw-r--r--cobol.scm3394logplainabout -rw-r--r--code.scm39954logplainabout -rw-r--r--commencement.scm161099logplainabout -rw-r--r--compression.scm116611logplainabout -rw-r--r--compton.scm5628logplainabout -rw-r--r--configuration-management.scm4326logplainabout -rw-r--r--conky.scm3453logplainabout -rw-r--r--connman.scm7258logplainabout -rw-r--r--containers.scm13462logplainabout -rw-r--r--convmv.scm2300logplainabout -rw-r--r--cook.scm4101logplainabout -rw-r--r--coq.scm26898logplainabout -rw-r--r--cpio.scm2300logplainabout -rw-r--r--cpp.scm75960logplainabout -rw-r--r--cppi.scm1775logplainabout -rw-r--r--cran.scm1170866logplainabout -rw-r--r--crates-graphics.scm109795logplainabout -rw-r--r--crates-gtk.scm63246logplainabout -rw-r--r--crates-io.scm2500277logplainabout -rw-r--r--cross-base.scm25398logplainabout -rw-r--r--crypto.scm70617logplainabout -rw-r--r--cryptsetup.scm6632logplainabout -rw-r--r--cups.scm46132logplainabout -rw-r--r--curl.scm16018logplainabout -rw-r--r--cvassistant.scm3600logplainabout -rw-r--r--cybersecurity.scm6305logplainabout -rw-r--r--cyrus-sasl.scm3439logplainabout -rw-r--r--databases.scm186091logplainabout -rw-r--r--datamash.scm6624logplainabout -rw-r--r--datastructures.scm16061logplainabout -rw-r--r--dav.scm6853logplainabout -rw-r--r--dbm.scm8182logplainabout -rw-r--r--dc.scm2177logplainabout -rw-r--r--debian.scm18531logplainabout -rw-r--r--debug.scm36014logplainabout -rw-r--r--dejagnu.scm3502logplainabout -rw-r--r--dezyne.scm5515logplainabout -rw-r--r--dhall.scm5386logplainabout -rw-r--r--dico.scm3663logplainabout -rw-r--r--dictionaries.scm17755logplainabout -rw-r--r--diffoscope.scm14524logplainabout -rw-r--r--digest.scm5298logplainabout -rw-r--r--direct-connect.scm3169logplainabout -rw-r--r--disk.scm58035logplainabout -rw-r--r--display-managers.scm18201logplainabout -rw-r--r--distributed.scm3870logplainabout -rw-r--r--django.scm52194logplainabout -rw-r--r--djvu.scm18433logplainabout -rw-r--r--dlang.scm22903logplainabout -rw-r--r--dns.scm58009logplainabout -rw-r--r--docbook.scm31538logplainabout -rw-r--r--docker.scm32774logplainabout -rw-r--r--documentation.scm16667logplainabout -rw-r--r--dunst.scm3639logplainabout -rw-r--r--dvtm.scm2302logplainabout -rw-r--r--easyrpg.scm4376logplainabout -rw-r--r--ebook.scm26943logplainabout -rw-r--r--ed.scm2372logplainabout -rw-r--r--education.scm52777logplainabout -rw-r--r--efi.scm9698logplainabout -rw-r--r--electronics.scm17679logplainabout -rw-r--r--elf.scm13141logplainabout -rw-r--r--elixir.scm5023logplainabout -rw-r--r--elm.scm28154logplainabout -rw-r--r--emacs-xyz.scm1199119logplainabout -rw-r--r--emacs.scm24195logplainabout -rw-r--r--embedded.scm73202logplainabout -rw-r--r--emulators.scm98122logplainabout -rw-r--r--enchant.scm6456logplainabout -rw-r--r--engineering.scm150494logplainabout -rw-r--r--enlightenment.scm24615logplainabout -rw-r--r--entr.scm3301logplainabout -rw-r--r--erlang.scm9406logplainabout -rw-r--r--esolangs.scm5350logplainabout -rw-r--r--fabric-management.scm9260logplainabout -rw-r--r--fcitx.scm9002logplainabout -rw-r--r--fcitx5.scm18909logplainabout -rw-r--r--figlet.scm1797logplainabout -rw-r--r--file-systems.scm72035logplainabout -rw-r--r--file.scm2852logplainabout -rw-r--r--finance.scm75113logplainabout -rw-r--r--firmware.scm27117logplainabout -rw-r--r--flashing-tools.scm21971logplainabout -rw-r--r--flex.scm3195logplainabout -rw-r--r--fltk.scm6075logplainabout -rw-r--r--fonts.scm114334logplainabout -rw-r--r--fontutils.scm70866logplainabout -rw-r--r--forth.scm2426logplainabout -rw-r--r--fpga.scm21942logplainabout -rw-r--r--freedesktop.scm101296logplainabout -rw-r--r--freeipmi.scm2494logplainabout -rw-r--r--fribidi.scm4059logplainabout -rw-r--r--ftp.scm11772logplainabout -rw-r--r--fvwm.scm3028logplainabout -rw-r--r--game-development.scm116342logplainabout -rw-r--r--games.scm514655logplainabout -rw-r--r--gawk.scm5430logplainabout -rw-r--r--gcal.scm2889logplainabout -rw-r--r--gcc.scm58535logplainabout -rw-r--r--gd.scm6528logplainabout -rw-r--r--gdb.scm7349logplainabout -rw-r--r--genealogy.scm4362logplainabout -rw-r--r--genimage.scm6158logplainabout -rw-r--r--geo.scm102454logplainabout -rw-r--r--gettext.scm13319logplainabout -rw-r--r--ghostscript.scm16930logplainabout -rw-r--r--gimp.scm27071logplainabout -rw-r--r--gkrellm.scm2437logplainabout -rw-r--r--gl.scm48690logplainabout -rw-r--r--glib.scm54256logplainabout -rw-r--r--gnome-xyz.scm51968logplainabout -rw-r--r--gnome.scm497388logplainabout -rw-r--r--gnu-doc.scm4129logplainabout -rw-r--r--gnu-pw-mgr.scm2629logplainabout -rw-r--r--gnucash.scm14556logplainabout -rw-r--r--gnunet.scm17625logplainabout -rw-r--r--gnupg.scm47706logplainabout -rw-r--r--gnustep.scm13326logplainabout -rw-r--r--gnuzilla.scm85401logplainabout -rw-r--r--gobby.scm7917logplainabout -rw-r--r--golang.scm395647logplainabout