Why does apt not display an error message even if flock is used?
Running script
#!/bin/bash
(
flock 9
# ... commands executed under lock ...
fuser -v /var/lib/dpkg/lock
apt-get -f --assume-no install
) 9>/var/lib/dpkg/lock
as superuser does not display an error message. But if there is e.g. synaptic running,apt-get will display an error message:
"E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)".
apt dpkg synaptic lock
add a comment |
Running script
#!/bin/bash
(
flock 9
# ... commands executed under lock ...
fuser -v /var/lib/dpkg/lock
apt-get -f --assume-no install
) 9>/var/lib/dpkg/lock
as superuser does not display an error message. But if there is e.g. synaptic running,apt-get will display an error message:
"E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)".
apt dpkg synaptic lock
add a comment |
Running script
#!/bin/bash
(
flock 9
# ... commands executed under lock ...
fuser -v /var/lib/dpkg/lock
apt-get -f --assume-no install
) 9>/var/lib/dpkg/lock
as superuser does not display an error message. But if there is e.g. synaptic running,apt-get will display an error message:
"E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)".
apt dpkg synaptic lock
Running script
#!/bin/bash
(
flock 9
# ... commands executed under lock ...
fuser -v /var/lib/dpkg/lock
apt-get -f --assume-no install
) 9>/var/lib/dpkg/lock
as superuser does not display an error message. But if there is e.g. synaptic running,apt-get will display an error message:
"E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)".
apt dpkg synaptic lock
apt dpkg synaptic lock
edited Nov 15 '17 at 10:40
derHugo
2,28021429
2,28021429
asked Sep 30 '17 at 9:43
jarno
1,69631945
1,69631945
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
dpkg (and in turn apt) doesn't use flock(2) for locking. Checking the system calls, involved, it seems they use fcntl(2):
$ sudo strace -f -e trace=desc apt install foo |& grep -B2 F_SETLK
close(4) = 0
open("/var/lib/dpkg/lock", O_RDWR|O_CREAT|O_NOFOLLOW, 0640) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
fcntl(4, F_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = -1 EAGAIN (Resource temporarily unavailable)
close(4) = 0
And from this SO post:
In Linux,
lockf()is just a wrapper aroundfcntl(), whileflock()
locks are separate (and will only work on local filesystems, not on
e.g. NFS mounts). That is, one process can have an advisory exclusive
flock()lock on a file, while another process has an advisory
exclusivefcntl()lock on that same file. Both are advisory locks,
but they do not interact.
So flock isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent apt-get would have failed anyway.)
The simplest way I can think of is to create an immutable /var/lib/dpkg/lock file for the duration of the task.
touch /var/lib/dpkg/lock
chattr +i /var/lib/dpkg/lock
Or you can write a short C program (or any language that provides an easy interface to fcntl) that uses fcntl to lock it the way dpkg does.
But there are no commandsfcntlorlockfin terminal.
– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
@jarno in that case, the simplest way I can think of is to create an immutable/var/lib/dpkg/lockfile for the duration of the task. Or you can write a short C program that usesfcntlto lock it the way dpkg does.
– muru
Nov 22 '17 at 11:39
|
show 5 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f960746%2fwhy-does-apt-not-display-an-error-message-even-if-flock-is-used%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
dpkg (and in turn apt) doesn't use flock(2) for locking. Checking the system calls, involved, it seems they use fcntl(2):
$ sudo strace -f -e trace=desc apt install foo |& grep -B2 F_SETLK
close(4) = 0
open("/var/lib/dpkg/lock", O_RDWR|O_CREAT|O_NOFOLLOW, 0640) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
fcntl(4, F_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = -1 EAGAIN (Resource temporarily unavailable)
close(4) = 0
And from this SO post:
In Linux,
lockf()is just a wrapper aroundfcntl(), whileflock()
locks are separate (and will only work on local filesystems, not on
e.g. NFS mounts). That is, one process can have an advisory exclusive
flock()lock on a file, while another process has an advisory
exclusivefcntl()lock on that same file. Both are advisory locks,
but they do not interact.
So flock isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent apt-get would have failed anyway.)
The simplest way I can think of is to create an immutable /var/lib/dpkg/lock file for the duration of the task.
touch /var/lib/dpkg/lock
chattr +i /var/lib/dpkg/lock
Or you can write a short C program (or any language that provides an easy interface to fcntl) that uses fcntl to lock it the way dpkg does.
But there are no commandsfcntlorlockfin terminal.
– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
@jarno in that case, the simplest way I can think of is to create an immutable/var/lib/dpkg/lockfile for the duration of the task. Or you can write a short C program that usesfcntlto lock it the way dpkg does.
– muru
Nov 22 '17 at 11:39
|
show 5 more comments
dpkg (and in turn apt) doesn't use flock(2) for locking. Checking the system calls, involved, it seems they use fcntl(2):
$ sudo strace -f -e trace=desc apt install foo |& grep -B2 F_SETLK
close(4) = 0
open("/var/lib/dpkg/lock", O_RDWR|O_CREAT|O_NOFOLLOW, 0640) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
fcntl(4, F_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = -1 EAGAIN (Resource temporarily unavailable)
close(4) = 0
And from this SO post:
In Linux,
lockf()is just a wrapper aroundfcntl(), whileflock()
locks are separate (and will only work on local filesystems, not on
e.g. NFS mounts). That is, one process can have an advisory exclusive
flock()lock on a file, while another process has an advisory
exclusivefcntl()lock on that same file. Both are advisory locks,
but they do not interact.
So flock isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent apt-get would have failed anyway.)
The simplest way I can think of is to create an immutable /var/lib/dpkg/lock file for the duration of the task.
touch /var/lib/dpkg/lock
chattr +i /var/lib/dpkg/lock
Or you can write a short C program (or any language that provides an easy interface to fcntl) that uses fcntl to lock it the way dpkg does.
But there are no commandsfcntlorlockfin terminal.
– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
@jarno in that case, the simplest way I can think of is to create an immutable/var/lib/dpkg/lockfile for the duration of the task. Or you can write a short C program that usesfcntlto lock it the way dpkg does.
– muru
Nov 22 '17 at 11:39
|
show 5 more comments
dpkg (and in turn apt) doesn't use flock(2) for locking. Checking the system calls, involved, it seems they use fcntl(2):
$ sudo strace -f -e trace=desc apt install foo |& grep -B2 F_SETLK
close(4) = 0
open("/var/lib/dpkg/lock", O_RDWR|O_CREAT|O_NOFOLLOW, 0640) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
fcntl(4, F_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = -1 EAGAIN (Resource temporarily unavailable)
close(4) = 0
And from this SO post:
In Linux,
lockf()is just a wrapper aroundfcntl(), whileflock()
locks are separate (and will only work on local filesystems, not on
e.g. NFS mounts). That is, one process can have an advisory exclusive
flock()lock on a file, while another process has an advisory
exclusivefcntl()lock on that same file. Both are advisory locks,
but they do not interact.
So flock isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent apt-get would have failed anyway.)
The simplest way I can think of is to create an immutable /var/lib/dpkg/lock file for the duration of the task.
touch /var/lib/dpkg/lock
chattr +i /var/lib/dpkg/lock
Or you can write a short C program (or any language that provides an easy interface to fcntl) that uses fcntl to lock it the way dpkg does.
dpkg (and in turn apt) doesn't use flock(2) for locking. Checking the system calls, involved, it seems they use fcntl(2):
$ sudo strace -f -e trace=desc apt install foo |& grep -B2 F_SETLK
close(4) = 0
open("/var/lib/dpkg/lock", O_RDWR|O_CREAT|O_NOFOLLOW, 0640) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
fcntl(4, F_SETLK, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = -1 EAGAIN (Resource temporarily unavailable)
close(4) = 0
And from this SO post:
In Linux,
lockf()is just a wrapper aroundfcntl(), whileflock()
locks are separate (and will only work on local filesystems, not on
e.g. NFS mounts). That is, one process can have an advisory exclusive
flock()lock on a file, while another process has an advisory
exclusivefcntl()lock on that same file. Both are advisory locks,
but they do not interact.
So flock isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent apt-get would have failed anyway.)
The simplest way I can think of is to create an immutable /var/lib/dpkg/lock file for the duration of the task.
touch /var/lib/dpkg/lock
chattr +i /var/lib/dpkg/lock
Or you can write a short C program (or any language that provides an easy interface to fcntl) that uses fcntl to lock it the way dpkg does.
edited Dec 6 at 18:01
answered Nov 16 '17 at 9:55
muru
1
1
But there are no commandsfcntlorlockfin terminal.
– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
@jarno in that case, the simplest way I can think of is to create an immutable/var/lib/dpkg/lockfile for the duration of the task. Or you can write a short C program that usesfcntlto lock it the way dpkg does.
– muru
Nov 22 '17 at 11:39
|
show 5 more comments
But there are no commandsfcntlorlockfin terminal.
– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
@jarno in that case, the simplest way I can think of is to create an immutable/var/lib/dpkg/lockfile for the duration of the task. Or you can write a short C program that usesfcntlto lock it the way dpkg does.
– muru
Nov 22 '17 at 11:39
But there are no commands
fcntl or lockf in terminal.– jarno
Nov 20 '17 at 14:14
But there are no commands
fcntl or lockf in terminal.– jarno
Nov 20 '17 at 14:14
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
@jarno click on the fcntl link in the answer.
– muru
Nov 20 '17 at 14:55
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
dpkg doesn't have to use commands to do locking. There are system calls for those. The flock command is merely a convenient wrapper around one of those system calls. There's no reason for all calls to have wrapper commands.
– muru
Nov 20 '17 at 15:42
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
What I want to achieve is that I want to lock dpkg database in a script so that user can choose interactively some packages, and thereafter do certain things with them by apt or dpkg without worrying that the packages have been changed e.g. by a background process in the mean time.
– jarno
Nov 21 '17 at 17:44
1
1
@jarno in that case, the simplest way I can think of is to create an immutable
/var/lib/dpkg/lock file for the duration of the task. Or you can write a short C program that uses fcntl to lock it the way dpkg does.– muru
Nov 22 '17 at 11:39
@jarno in that case, the simplest way I can think of is to create an immutable
/var/lib/dpkg/lock file for the duration of the task. Or you can write a short C program that uses fcntl to lock it the way dpkg does.– muru
Nov 22 '17 at 11:39
|
show 5 more comments
Thanks for contributing an answer to Ask Ubuntu!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2faskubuntu.com%2fquestions%2f960746%2fwhy-does-apt-not-display-an-error-message-even-if-flock-is-used%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