summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-06-16 22:03:51 +0300
committerAndreas Kling <kling@serenityos.org>2020-06-17 15:02:03 +0200
commite0d0d52455d44fa0688477ed9ff58c0677c9d400 (patch)
tree3611f70ef05c0d5fc34c6ec24029925283ed8ae5
parentfd985b1f48f832f526766b5d4ecf968d2e2a1425 (diff)
downloadserenity-e0d0d52455d44fa0688477ed9ff58c0677c9d400.zip
Kernel: Use symbolic constants for file modes
This fixes a bug where the mode of a FIFO was reported as 001000 instead of 0010000 (you see the difference? me nethier), and hopefully doesn't introduce new bugs. I've left 0777 and similar in a few places, because that is *more* readable than its symbolic version.
-rw-r--r--Kernel/FileSystem/FileDescription.cpp4
-rw-r--r--Kernel/FileSystem/InodeMetadata.h38
-rw-r--r--Kernel/FileSystem/ProcFS.cpp12
-rw-r--r--Kernel/FileSystem/TmpFS.cpp6
-rw-r--r--Kernel/UnixTypes.h22
5 files changed, 52 insertions, 30 deletions
diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp
index 4b76e4ca4a..c4c3f8f700 100644
--- a/Kernel/FileSystem/FileDescription.cpp
+++ b/Kernel/FileSystem/FileDescription.cpp
@@ -78,12 +78,12 @@ KResult FileDescription::fstat(stat& buffer)
{
if (is_fifo()) {
memset(&buffer, 0, sizeof(buffer));
- buffer.st_mode = 001000;
+ buffer.st_mode = S_IFIFO;
return KSuccess;
}
if (is_socket()) {
memset(&buffer, 0, sizeof(buffer));
- buffer.st_mode = 0140000;
+ buffer.st_mode = S_IFSOCK;
return KSuccess;
}
diff --git a/Kernel/FileSystem/InodeMetadata.h b/Kernel/FileSystem/InodeMetadata.h
index eb4f672ed8..843a2b691f 100644
--- a/Kernel/FileSystem/InodeMetadata.h
+++ b/Kernel/FileSystem/InodeMetadata.h
@@ -40,16 +40,16 @@ inline constexpr u32 encoded_device(unsigned major, unsigned minor)
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
}
-inline bool is_directory(mode_t mode) { return (mode & 0170000) == 0040000; }
-inline bool is_character_device(mode_t mode) { return (mode & 0170000) == 0020000; }
-inline bool is_block_device(mode_t mode) { return (mode & 0170000) == 0060000; }
-inline bool is_regular_file(mode_t mode) { return (mode & 0170000) == 0100000; }
-inline bool is_fifo(mode_t mode) { return (mode & 0170000) == 0010000; }
-inline bool is_symlink(mode_t mode) { return (mode & 0170000) == 0120000; }
-inline bool is_socket(mode_t mode) { return (mode & 0170000) == 0140000; }
-inline bool is_sticky(mode_t mode) { return mode & 01000; }
-inline bool is_setuid(mode_t mode) { return mode & 04000; }
-inline bool is_setgid(mode_t mode) { return mode & 02000; }
+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; }
struct InodeMetadata {
bool is_valid() const { return inode.is_valid(); }
@@ -63,10 +63,10 @@ struct InodeMetadata {
if (u == 0)
return true;
if (uid == u)
- return mode & 0400;
+ return mode & S_IRUSR;
if (gid == g || eg.contains(gid))
- return mode & 0040;
- return mode & 0004;
+ return mode & S_IRGRP;
+ return mode & S_IROTH;
}
bool may_write(uid_t u, gid_t g, const FixedArray<gid_t>& eg) const
@@ -74,10 +74,10 @@ struct InodeMetadata {
if (u == 0)
return true;
if (uid == u)
- return mode & 0200;
+ return mode & S_IWUSR;
if (gid == g || eg.contains(gid))
- return mode & 0020;
- return mode & 0002;
+ return mode & S_IWGRP;
+ return mode & S_IWOTH;
}
bool may_execute(uid_t u, gid_t g, const FixedArray<gid_t>& eg) const
@@ -85,10 +85,10 @@ struct InodeMetadata {
if (u == 0)
return true;
if (uid == u)
- return mode & 0100;
+ return mode & S_IXUSR;
if (gid == g || eg.contains(gid))
- return mode & 0010;
- return mode & 0001;
+ return mode & S_IXGRP;
+ return mode & S_IXOTH;
}
bool is_directory() const { return Kernel::is_directory(mode); }
diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp
index 8c5b3b9a01..567774ccd6 100644
--- a/Kernel/FileSystem/ProcFS.cpp
+++ b/Kernel/FileSystem/ProcFS.cpp
@@ -1144,30 +1144,30 @@ InodeMetadata ProcFSInode::metadata() const
}
if (proc_parent_directory == PDI_PID_fd) {
- metadata.mode = 00120700;
+ metadata.mode = S_IFLNK | S_IRUSR | S_IWUSR | S_IXUSR;
return metadata;
}
switch (proc_file_type) {
case FI_Root_self:
- metadata.mode = 0120444;
+ metadata.mode = S_IFLNK | S_IRUSR | S_IRGRP | S_IROTH;
break;
case FI_PID_cwd:
case FI_PID_exe:
case FI_PID_root:
- metadata.mode = 0120400;
+ metadata.mode = S_IFLNK | S_IRUSR;
break;
case FI_Root:
case FI_Root_sys:
case FI_Root_net:
- metadata.mode = 040555;
+ metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
break;
case FI_PID:
case FI_PID_fd:
- metadata.mode = 040500;
+ metadata.mode = S_IFDIR | S_IRUSR | S_IXUSR;
break;
default:
- metadata.mode = 0100444;
+ metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
break;
}
diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp
index a0cd624137..86d141010a 100644
--- a/Kernel/FileSystem/TmpFS.cpp
+++ b/Kernel/FileSystem/TmpFS.cpp
@@ -125,8 +125,8 @@ KResultOr<NonnullRefPtr<Inode>> TmpFS::create_inode(InodeIdentifier parent_id, c
KResult TmpFS::create_directory(InodeIdentifier parent_id, const String& name, mode_t mode, uid_t uid, gid_t gid)
{
// Ensure it's a directory.
- mode &= ~0170000;
- mode |= 0040000;
+ mode &= ~S_IFMT;
+ mode |= S_IFDIR;
auto result = create_inode(parent_id, name, mode, 0, 0, uid, gid);
if (result.is_error())
return result.error();
@@ -155,7 +155,7 @@ NonnullRefPtr<TmpFSInode> TmpFSInode::create(TmpFS& fs, InodeMetadata metadata,
NonnullRefPtr<TmpFSInode> TmpFSInode::create_root(TmpFS& fs)
{
InodeMetadata metadata;
- metadata.mode = 0041777;
+ metadata.mode = S_IFDIR | S_ISVTX | 0777;
return create(fs, metadata, { fs.fsid(), 1 });
}
diff --git a/Kernel/UnixTypes.h b/Kernel/UnixTypes.h
index f74c28e402..7830cab91c 100644
--- a/Kernel/UnixTypes.h
+++ b/Kernel/UnixTypes.h
@@ -108,6 +108,28 @@
#define FUTEX_WAIT 1
#define FUTEX_WAKE 2
+#define S_IFMT 0170000
+#define S_IFDIR 0040000
+#define S_IFCHR 0020000
+#define S_IFBLK 0060000
+#define S_IFREG 0100000
+#define S_IFIFO 0010000
+#define S_IFLNK 0120000
+#define S_IFSOCK 0140000
+
+#define S_ISUID 04000
+#define S_ISGID 02000
+#define S_ISVTX 01000
+#define S_IRUSR 0400
+#define S_IWUSR 0200
+#define S_IXUSR 0100
+#define S_IRGRP 0040
+#define S_IWGRP 0020
+#define S_IXGRP 0010
+#define S_IROTH 0004
+#define S_IWOTH 0002
+#define S_IXOTH 0001
+
/* c_cc characters */
#define VINTR 0
#define VQUIT 1