fs/vfs/fs_lock.c: fix behavior according to manual and support SHM#19110
Merged
Conversation
We need to use gettid instead of getpid, otherwise flocks applied from different threads are considered as single thread lock and are ignored (or updated). Also fix the behavior if process opens the file multiple times Linux/BSD manual states multiple file descriptors opened by a single process shall be treated independently. Therefore we also need to compare struct file pointer to determine whether the lock applies to the same descriptor or not. Signed-off-by: Michal Lenc <michallenc@seznam.cz>
We can apply file lock on SHM inode as well. Ensure file_lock_get_path function passes and doesn't return EBADF errno. Signed-off-by: Michal Lenc <michallenc@seznam.cz>
acassis
approved these changes
Jun 10, 2026
Contributor
|
ping @jerpelea for v13 |
linguini1
approved these changes
Jun 10, 2026
lupyuen
approved these changes
Jun 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two commits:
fs/vfs/fs_lock.c: fix flock behavior for more threads and fds
We need to use gettid instead of getpid, otherwise flocks applied
from different threads are considered as single thread lock and are
ignored (or updated).
Also fix the behavior if process opens the file multiple times
Linux/BSD manual states multiple file descriptors opened by a single
process shall be treated independently. Therefore we also need to
compare struct file pointer to determine whether the lock applies
to the same descriptor or not.
fs/vfs/fs_lock.c: support flock for SHM driver
We can apply file lock on SHM inode as well. Ensure
file_lock_get_pathfunction passes and doesn't return EBADF errno.
Impact
Fixes
flockbehavior according to the manual and allows the usage of the function on SHM.Testing
Testing first commit (behavior fix) on standard files with two threads:
Linux output:
NuttX output (didn't lock before the change):
Testing first commit (behavior fix) on separate FDs within one thread:
Linux output (blocked indefinitely)
NuttX output (didn't block before the change)
SHM test:
Linux output
NuttX output (failed with
EBADFerrno before the change)