Kernel: Move setting file flags and r/w mode to VFS::open()

Previously, VFS::open() would only use the passed flags for permission checking
purposes, and Process::sys$open() would set them on the created FileDescription
explicitly. Now, they should be set by VFS::open() on any files being opened,
including files that the kernel opens internally.

This also lets us get rid of the explicit check for whether or not the returned
FileDescription was a preopen fd, and in fact, fixes a bug where a read-only
preopen fd without any other flags would be considered freshly opened (due to
O_RDONLY being indistinguishable from 0) and granted a new set of flags.
This commit is contained in:
Sergey Bugaev 2020-01-19 01:15:52 +03:00 committed by Andreas Kling
parent 544b8286da
commit d0d13e2bf5
Notes: sideshowbarker 2024-07-19 09:58:21 +09:00
4 changed files with 10 additions and 12 deletions

View file

@ -39,6 +39,7 @@ KResultOr<NonnullRefPtr<FileDescription>> File::open(int options)
{
auto description = FileDescription::create(*this);
description->set_rw_mode(options);
description->set_file_flags(options);
return description;
}

View file

@ -277,7 +277,10 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options
inode.truncate(0);
inode.set_mtime(kgettimeofday().tv_sec);
}
return FileDescription::create(custody);
auto description = FileDescription::create(custody);
description->set_rw_mode(options);
description->set_file_flags(options);
return description;
}
KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
@ -309,8 +312,6 @@ KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
{
(void)options;
if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {
// Turn it into a regular file. (This feels rather hackish.)
mode |= 0100000;
@ -332,7 +333,10 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int optio
return KResult(error);
auto new_custody = Custody::create(&parent_custody, p.basename(), *new_file, parent_custody.mount_flags());
return FileDescription::create(*new_custody);
auto description = FileDescription::create(*new_custody);
description->set_rw_mode(options);
description->set_file_flags(options);
return description;
}
KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)

View file

@ -1971,14 +1971,6 @@ int Process::sys$open(const Syscall::SC_open_params* user_params)
if (result.is_error())
return result.error();
auto description = result.value();
if (description->file_flags()) {
// We already have file flags set on this description, so
// it must be a preopen description (probably, /proc/pid/fd).
// So don't reset its flags and r/w mode.
} else {
description->set_rw_mode(options);
description->set_file_flags(options);
}
u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
m_fds[fd].set(move(description), fd_flags);
return fd;

View file

@ -66,6 +66,7 @@ KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options)
#endif
auto description = FileDescription::create(move(master));
description->set_rw_mode(options);
description->set_file_flags(options);
return description;
}