4

I am curious of how /var/cache/apt/archives/lock used in apt-get. I found out that it is a regular file and I think it is used by calling flock on it every time apt-get needs to make some changes to /var/cache/apt/archives.

1
2

Short answer is that apt is using fcntl to control lock files.


Depend on what you are doing different locks will be created by apt, let's consider sudo apt update which apt-pkg/update.cc is responsible of, and runs this condition before actually updating our sources lists:

if (Fetcher.GetLock(_config->FindDir("Dir::State::Lists")) == false)

Note that _config->FindDir("Dir::State::Lists") will return: /lists, you can find this out by running:

$ apt-config dump | grep lists
Dir::State::lists "lists/";

So GetLock() will creates a lock file in /var/lib/apt/lists/, and if I run an other sudo apt update process I'll get:

Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock ...
E: Unable to lock directory /var/lib/apt/lists/

GetLock() itself lives here in apt-pkg/contrib/fileutl.cc:

Let's have a look at its comment:

// GetLock - Gets a lock file                       /*{{{*/
// ---------------------------------------------------------------------
/* This will create an empty file of the given name and lock it. Once this
   is done all other calls to GetLock in any other process will fail with
   -1. The return result is the fd of the file, the call should call
   close at some time. */

So next time when I run apt update this function instead of creating the lock file will returns -1 and our condition fails and we receive an error.

If we look into the codes of GetLock we can see that it's using fcntl to provide its functionalities.

Basically fileutl.cc is a file utility containing two main function, which defines GetLock as "dpkg compatible lock file manipulation" method.

fcntl itself provides a bunch of system calls to manipulate file descriptors. One of the functionalities which is being provided by fcntl is "POSIX record locks, also known as process-associated locks":

man 2 fcntl

And I'm sure you already know the idea behind using lock files, but to make sure everyone who comes here knows about it:

What is the purpose of lock file?

6
  • So /var/cache/apt/archives/lock are not used?
    – JiaHao Xu
    Sep 21 '18 at 13:30
  • @JiaHaoXu Yes, it's being used... but apt does not uses flock to manage the lock process. it has it's own functions which are using fcntl.
    – Ravexina
    Sep 21 '18 at 13:34
  • @ Ravexina I see... And can you explain how it is being used?
    – JiaHao Xu
    Sep 21 '18 at 13:48
  • @JiaHaoXu I think I already mentioned all necessary parts, you have to read the sources to find out what exactly happening here. What are you looking for? so we can help you exactly with that.
    – Ravexina
    Sep 21 '18 at 13:54
  • Sorry I didn't read that article.
    – JiaHao Xu
    Sep 21 '18 at 14:03
1

Apt and dpkg use lockfiles (in /var/lib/, not /var/cache) to ensure that package management actions and the package database are correctly in sync.

This means that if you install or remove the a package (like the hello package), the package database will be accurate, and your package manager will properly show the correct status of the package.

There are several possible solutions to ensure that package management actions and the package database are in sync. Lockfiles are the solution that the apt developers have chosen...mostly because it is a solution that is simple, easy to troubleshoot, and easy to understand.

3
  • He is asking how it's being done behind the scene I guess, he already should know the why...
    – Ravexina
    Sep 21 '18 at 12:47
  • So what does /var/cache/apt/archives/lock actually do?
    – JiaHao Xu
    Sep 21 '18 at 13:30
  • @Ravexina seems a lot like an XY Problem question, so maybe knows the why...but maybe not.
    – user535733
    Sep 21 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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