How /var/lib/dpkg/lock works?

Clash Royale CLAN TAG#URR8PPP
/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.
dpkg lock
add a comment |
/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.
dpkg lock
add a comment |
/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.
dpkg lock
/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.
dpkg lock
dpkg lock
edited Apr 8 '13 at 23:54
Gilles
530k12810631591
530k12810631591
asked Apr 8 '13 at 15:06
YarLinuxYarLinux
4871410
4871410
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 ben"
"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).
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 callflock()on the lock file indbmodify.c.
– jordanm
Apr 8 '13 at 16:18
@patrick lockfile is "lock", not the database.
– jordanm
Apr 8 '13 at 16:27
then why doesflock -x /var/lib/dpkg/lock dpkg -r somepackagework?
– 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
|
show 3 more comments
As I see, dpkg locks the file /var/lib/dpkg/lock using lockf(3), which in turn uses fcntl(2).
$ sudo strace dpkg -r somepackage 2>&1 |
> grep F_SETLKW
fcntl64(5, F_SETLKW64, l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0) = 0
This means that you cannot lock the file from the shell using flock(1), because this calls flock(2), which in many systems doesn't interact with fnctl locks.
$ sudo strace flock -x /var/lib/dpkg/lock 2>&1 |
> grep 'flock('
flock(3, LOCK_EX)
You can however lock the file with the with-lock-ex program or this Python script, both of which acquire a compatible lock.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f71716%2fhow-var-lib-dpkg-lock-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 ben"
"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).
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 callflock()on the lock file indbmodify.c.
– jordanm
Apr 8 '13 at 16:18
@patrick lockfile is "lock", not the database.
– jordanm
Apr 8 '13 at 16:27
then why doesflock -x /var/lib/dpkg/lock dpkg -r somepackagework?
– 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
|
show 3 more comments
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 ben"
"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).
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 callflock()on the lock file indbmodify.c.
– jordanm
Apr 8 '13 at 16:18
@patrick lockfile is "lock", not the database.
– jordanm
Apr 8 '13 at 16:27
then why doesflock -x /var/lib/dpkg/lock dpkg -r somepackagework?
– 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
|
show 3 more comments
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 ben"
"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).
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 ben"
"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).
edited Apr 8 '13 at 16:28
answered Apr 8 '13 at 15:38
jordanmjordanm
30.3k28492
30.3k28492
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 callflock()on the lock file indbmodify.c.
– jordanm
Apr 8 '13 at 16:18
@patrick lockfile is "lock", not the database.
– jordanm
Apr 8 '13 at 16:27
then why doesflock -x /var/lib/dpkg/lock dpkg -r somepackagework?
– 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
|
show 3 more comments
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 callflock()on the lock file indbmodify.c.
– jordanm
Apr 8 '13 at 16:18
@patrick lockfile is "lock", not the database.
– jordanm
Apr 8 '13 at 16:27
then why doesflock -x /var/lib/dpkg/lock dpkg -r somepackagework?
– 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
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
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 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
@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
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
@Patrick a lock doesn't stick around after the caller process exits.
– jordanm
Apr 8 '13 at 16:34
|
show 3 more comments
As I see, dpkg locks the file /var/lib/dpkg/lock using lockf(3), which in turn uses fcntl(2).
$ sudo strace dpkg -r somepackage 2>&1 |
> grep F_SETLKW
fcntl64(5, F_SETLKW64, l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0) = 0
This means that you cannot lock the file from the shell using flock(1), because this calls flock(2), which in many systems doesn't interact with fnctl locks.
$ sudo strace flock -x /var/lib/dpkg/lock 2>&1 |
> grep 'flock('
flock(3, LOCK_EX)
You can however lock the file with the with-lock-ex program or this Python script, both of which acquire a compatible lock.
add a comment |
As I see, dpkg locks the file /var/lib/dpkg/lock using lockf(3), which in turn uses fcntl(2).
$ sudo strace dpkg -r somepackage 2>&1 |
> grep F_SETLKW
fcntl64(5, F_SETLKW64, l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0) = 0
This means that you cannot lock the file from the shell using flock(1), because this calls flock(2), which in many systems doesn't interact with fnctl locks.
$ sudo strace flock -x /var/lib/dpkg/lock 2>&1 |
> grep 'flock('
flock(3, LOCK_EX)
You can however lock the file with the with-lock-ex program or this Python script, both of which acquire a compatible lock.
add a comment |
As I see, dpkg locks the file /var/lib/dpkg/lock using lockf(3), which in turn uses fcntl(2).
$ sudo strace dpkg -r somepackage 2>&1 |
> grep F_SETLKW
fcntl64(5, F_SETLKW64, l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0) = 0
This means that you cannot lock the file from the shell using flock(1), because this calls flock(2), which in many systems doesn't interact with fnctl locks.
$ sudo strace flock -x /var/lib/dpkg/lock 2>&1 |
> grep 'flock('
flock(3, LOCK_EX)
You can however lock the file with the with-lock-ex program or this Python script, both of which acquire a compatible lock.
As I see, dpkg locks the file /var/lib/dpkg/lock using lockf(3), which in turn uses fcntl(2).
$ sudo strace dpkg -r somepackage 2>&1 |
> grep F_SETLKW
fcntl64(5, F_SETLKW64, l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0) = 0
This means that you cannot lock the file from the shell using flock(1), because this calls flock(2), which in many systems doesn't interact with fnctl locks.
$ sudo strace flock -x /var/lib/dpkg/lock 2>&1 |
> grep 'flock('
flock(3, LOCK_EX)
You can however lock the file with the with-lock-ex program or this Python script, both of which acquire a compatible lock.
answered Dec 29 '18 at 22:31
Diomidis SpinellisDiomidis Spinellis
47049
47049
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f71716%2fhow-var-lib-dpkg-lock-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown