Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

/var/lib/dpkg/lock is file that holding a lock when "A package manager is working". But how this system works? I have /var/lib/dpkg/lock everytime when I have Linux working. When I use one of package manager for dpkg I have it without any change. So I can't see it in action.

share|improve this question

1 Answer 1

I don't know for certain, but this is most likely implemented via flock(). The flock() system call creates an advisory lock on a file. If another application tries to attain a lock on the file, the kernel will block until the original lock is gone, or return EWOULDBLOCK if the LOCK_NB option is given. This locking mechanism would allow the lock file to be used without deleting and re-creating it.

Update: Checked the source and verified that it is advisory locking, but it doesn't use flock() directly. fcntl is used:

enquiry.c:

        if (modstatdb_is_locked())
          puts(_(
"Another process has locked the database for writing, and might currently be\n"
"modifying it, some of the following problems might just be due to that.\n"));
        head_running = true;
      }

dbmodify.c:

modstatdb_is_locked(void)
{
  int lockfd;
  bool locked;

  if (dblockfd == -1) {
    lockfd = open(lockfile, O_RDONLY);
    if (lockfd == -1)
      ohshite(_("unable to open lock file %s for testing"), lockfile);
  } else {
    lockfd = dblockfd;
  }

  locked = file_is_locked(lockfd, lockfile);

  /* We only close the file if there was no lock open, otherwise we would
   * release the existing lock on close. */
  if (dblockfd == -1)
    close(lockfd);

  return locked;
}

file.c:

file_is_locked(int lockfd, const char *filename)
{
    struct flock fl;

    file_lock_setup(&fl, F_WRLCK);

    if (fcntl(lockfd, F_GETLK, &fl) == -1)
        ohshit(_("unable to check file '%s' lock status"), filename);

    if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
        return true;
    else
        return false;
}

dpkg.h:

#define LOCKFILE          "lock"

From the fcntl manpage:

   Advisory locking
       F_GETLK,  F_SETLK  and  F_SETLKW  are  used to acquire, release, and test for the existence of record locks (also known as file-segment or file-region locks).  The third
       argument, lock, is a pointer to a structure that has at least the following fields (in unspecified order).
share|improve this answer
    
I tried this but it seems to not be the case. I manually grabbed an exclusive lock on the file, but dpkg still operated normally. –  Patrick Apr 8 '13 at 16:13
    
@Patrick I can see in the source code that it does in fact call flock() on the lock file in dbmodify.c. –  jordanm Apr 8 '13 at 16:18
    
@patrick lockfile is "lock", not the database. –  jordanm Apr 8 '13 at 16:27
    
then why does flock -x /var/lib/dpkg/lock dpkg -r somepackage work? –  Patrick Apr 8 '13 at 16:30
    
@Patrick a lock doesn't stick around after the caller process exits. –  jordanm Apr 8 '13 at 16:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.