2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-07 11:53:54 +02:00
|
|
|
#include <AK/Span.h>
|
2019-06-01 18:46:10 +02:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
|
|
|
#include <Kernel/KResult.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-02-21 15:45:31 +01:00
|
|
|
class Process;
|
|
|
|
|
2020-10-20 10:08:13 -06:00
|
|
|
constexpr u32 encoded_device(unsigned major, unsigned minor)
|
2019-06-01 18:46:10 +02:00
|
|
|
{
|
|
|
|
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
|
|
|
|
}
|
|
|
|
|
2020-06-16 22:03:51 +03:00
|
|
|
inline bool is_directory(mode_t mode) { return (mode & S_IFMT) == S_IFDIR; }
|
|
|
|
inline bool is_character_device(mode_t mode) { return (mode & S_IFMT) == S_IFCHR; }
|
|
|
|
inline bool is_block_device(mode_t mode) { return (mode & S_IFMT) == S_IFBLK; }
|
|
|
|
inline bool is_regular_file(mode_t mode) { return (mode & S_IFMT) == S_IFREG; }
|
|
|
|
inline bool is_fifo(mode_t mode) { return (mode & S_IFMT) == S_IFIFO; }
|
|
|
|
inline bool is_symlink(mode_t mode) { return (mode & S_IFMT) == S_IFLNK; }
|
|
|
|
inline bool is_socket(mode_t mode) { return (mode & S_IFMT) == S_IFSOCK; }
|
|
|
|
inline bool is_sticky(mode_t mode) { return mode & S_ISVTX; }
|
|
|
|
inline bool is_setuid(mode_t mode) { return mode & S_ISUID; }
|
|
|
|
inline bool is_setgid(mode_t mode) { return mode & S_ISGID; }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
struct InodeMetadata {
|
2019-01-31 17:31:23 +01:00
|
|
|
bool is_valid() const { return inode.is_valid(); }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-01-07 19:17:35 +01:00
|
|
|
bool may_read(const Process&) const;
|
|
|
|
bool may_write(const Process&) const;
|
|
|
|
bool may_execute(const Process&) const;
|
2019-02-21 15:45:31 +01:00
|
|
|
|
2020-09-07 11:53:54 +02:00
|
|
|
bool may_read(uid_t u, gid_t g, Span<const gid_t> eg) const
|
2019-02-21 15:45:31 +01:00
|
|
|
{
|
2019-02-27 17:08:13 +01:00
|
|
|
if (u == 0)
|
|
|
|
return true;
|
2019-02-21 15:45:31 +01:00
|
|
|
if (uid == u)
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IRUSR;
|
2020-09-07 11:53:54 +02:00
|
|
|
if (gid == g || eg.contains_slow(gid))
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IRGRP;
|
|
|
|
return mode & S_IROTH;
|
2019-02-21 15:45:31 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:53:54 +02:00
|
|
|
bool may_write(uid_t u, gid_t g, Span<const gid_t> eg) const
|
2019-02-21 15:45:31 +01:00
|
|
|
{
|
2019-02-27 17:08:13 +01:00
|
|
|
if (u == 0)
|
|
|
|
return true;
|
2019-02-21 15:45:31 +01:00
|
|
|
if (uid == u)
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IWUSR;
|
2020-09-07 11:53:54 +02:00
|
|
|
if (gid == g || eg.contains_slow(gid))
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IWGRP;
|
|
|
|
return mode & S_IWOTH;
|
2019-02-21 15:45:31 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:53:54 +02:00
|
|
|
bool may_execute(uid_t u, gid_t g, Span<const gid_t> eg) const
|
2018-10-27 16:43:03 +02:00
|
|
|
{
|
2019-02-27 17:08:13 +01:00
|
|
|
if (u == 0)
|
|
|
|
return true;
|
2018-10-27 16:43:03 +02:00
|
|
|
if (uid == u)
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IXUSR;
|
2020-09-07 11:53:54 +02:00
|
|
|
if (gid == g || eg.contains_slow(gid))
|
2020-06-16 22:03:51 +03:00
|
|
|
return mode & S_IXGRP;
|
|
|
|
return mode & S_IXOTH;
|
2018-10-27 16:43:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
bool is_directory() const { return Kernel::is_directory(mode); }
|
|
|
|
bool is_character_device() const { return Kernel::is_character_device(mode); }
|
|
|
|
bool is_block_device() const { return Kernel::is_block_device(mode); }
|
2019-02-16 09:57:42 +01:00
|
|
|
bool is_device() const { return is_character_device() || is_block_device(); }
|
2020-02-16 01:27:42 +01:00
|
|
|
bool is_regular_file() const { return Kernel::is_regular_file(mode); }
|
|
|
|
bool is_fifo() const { return Kernel::is_fifo(mode); }
|
|
|
|
bool is_symlink() const { return Kernel::is_symlink(mode); }
|
|
|
|
bool is_socket() const { return Kernel::is_socket(mode); }
|
|
|
|
bool is_sticky() const { return Kernel::is_sticky(mode); }
|
|
|
|
bool is_setuid() const { return Kernel::is_setuid(mode); }
|
|
|
|
bool is_setgid() const { return Kernel::is_setgid(mode); }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-06-01 18:46:10 +02:00
|
|
|
KResult stat(stat& buffer) const
|
|
|
|
{
|
|
|
|
if (!is_valid())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2019-06-01 18:46:10 +02:00
|
|
|
buffer.st_rdev = encoded_device(major_device, minor_device);
|
2021-02-12 09:18:47 +01:00
|
|
|
buffer.st_ino = inode.index().value();
|
2019-06-01 18:46:10 +02:00
|
|
|
buffer.st_mode = mode;
|
|
|
|
buffer.st_nlink = link_count;
|
|
|
|
buffer.st_uid = uid;
|
|
|
|
buffer.st_gid = gid;
|
|
|
|
buffer.st_dev = 0; // FIXME
|
|
|
|
buffer.st_size = size;
|
|
|
|
buffer.st_blksize = block_size;
|
|
|
|
buffer.st_blocks = block_count;
|
2021-04-17 09:35:46 +02:00
|
|
|
buffer.st_atim.tv_sec = atime;
|
|
|
|
buffer.st_atim.tv_nsec = 0;
|
|
|
|
buffer.st_mtim.tv_sec = mtime;
|
|
|
|
buffer.st_mtim.tv_nsec = 0;
|
|
|
|
buffer.st_ctim.tv_sec = ctime;
|
|
|
|
buffer.st_ctim.tv_nsec = 0;
|
2019-06-01 18:46:10 +02:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
InodeIdentifier inode;
|
2019-01-23 06:53:01 +01:00
|
|
|
off_t size { 0 };
|
|
|
|
mode_t mode { 0 };
|
|
|
|
uid_t uid { 0 };
|
|
|
|
gid_t gid { 0 };
|
2019-01-31 17:31:23 +01:00
|
|
|
nlink_t link_count { 0 };
|
2019-01-23 06:53:01 +01:00
|
|
|
time_t atime { 0 };
|
|
|
|
time_t ctime { 0 };
|
|
|
|
time_t mtime { 0 };
|
|
|
|
time_t dtime { 0 };
|
2019-01-31 17:31:23 +01:00
|
|
|
blkcnt_t block_count { 0 };
|
|
|
|
blksize_t block_size { 0 };
|
|
|
|
unsigned major_device { 0 };
|
|
|
|
unsigned minor_device { 0 };
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|