Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/FixedArray.h>
30#include <Kernel/FileSystem/InodeIdentifier.h>
31#include <Kernel/KResult.h>
32#include <Kernel/UnixTypes.h>
33
34namespace Kernel {
35
36class Process;
37
38inline constexpr u32 encoded_device(unsigned major, unsigned minor)
39{
40 return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
41}
42
43inline bool is_directory(mode_t mode) { return (mode & 0170000) == 0040000; }
44inline bool is_character_device(mode_t mode) { return (mode & 0170000) == 0020000; }
45inline bool is_block_device(mode_t mode) { return (mode & 0170000) == 0060000; }
46inline bool is_regular_file(mode_t mode) { return (mode & 0170000) == 0100000; }
47inline bool is_fifo(mode_t mode) { return (mode & 0170000) == 0010000; }
48inline bool is_symlink(mode_t mode) { return (mode & 0170000) == 0120000; }
49inline bool is_socket(mode_t mode) { return (mode & 0170000) == 0140000; }
50inline bool is_sticky(mode_t mode) { return mode & 01000; }
51inline bool is_setuid(mode_t mode) { return mode & 04000; }
52inline bool is_setgid(mode_t mode) { return mode & 02000; }
53
54struct InodeMetadata {
55 bool is_valid() const { return inode.is_valid(); }
56
57 bool may_read(const Process&) const;
58 bool may_write(const Process&) const;
59 bool may_execute(const Process&) const;
60
61 bool may_read(uid_t u, gid_t g, const FixedArray<gid_t>& eg) const
62 {
63 if (u == 0)
64 return true;
65 if (uid == u)
66 return mode & 0400;
67 if (gid == g || eg.contains(gid))
68 return mode & 0040;
69 return mode & 0004;
70 }
71
72 bool may_write(uid_t u, gid_t g, const FixedArray<gid_t>& eg) const
73 {
74 if (u == 0)
75 return true;
76 if (uid == u)
77 return mode & 0200;
78 if (gid == g || eg.contains(gid))
79 return mode & 0020;
80 return mode & 0002;
81 }
82
83 bool may_execute(uid_t u, gid_t g, const FixedArray<gid_t>& eg) const
84 {
85 if (u == 0)
86 return true;
87 if (uid == u)
88 return mode & 0100;
89 if (gid == g || eg.contains(gid))
90 return mode & 0010;
91 return mode & 0001;
92 }
93
94 bool is_directory() const { return Kernel::is_directory(mode); }
95 bool is_character_device() const { return Kernel::is_character_device(mode); }
96 bool is_block_device() const { return Kernel::is_block_device(mode); }
97 bool is_device() const { return is_character_device() || is_block_device(); }
98 bool is_regular_file() const { return Kernel::is_regular_file(mode); }
99 bool is_fifo() const { return Kernel::is_fifo(mode); }
100 bool is_symlink() const { return Kernel::is_symlink(mode); }
101 bool is_socket() const { return Kernel::is_socket(mode); }
102 bool is_sticky() const { return Kernel::is_sticky(mode); }
103 bool is_setuid() const { return Kernel::is_setuid(mode); }
104 bool is_setgid() const { return Kernel::is_setgid(mode); }
105
106 KResult stat(stat& buffer) const
107 {
108 if (!is_valid())
109 return KResult(-EIO);
110 buffer.st_rdev = encoded_device(major_device, minor_device);
111 buffer.st_ino = inode.index();
112 buffer.st_mode = mode;
113 buffer.st_nlink = link_count;
114 buffer.st_uid = uid;
115 buffer.st_gid = gid;
116 buffer.st_dev = 0; // FIXME
117 buffer.st_size = size;
118 buffer.st_blksize = block_size;
119 buffer.st_blocks = block_count;
120 buffer.st_atime = atime;
121 buffer.st_mtime = mtime;
122 buffer.st_ctime = ctime;
123 return KSuccess;
124 }
125
126 InodeIdentifier inode;
127 off_t size { 0 };
128 mode_t mode { 0 };
129 uid_t uid { 0 };
130 gid_t gid { 0 };
131 nlink_t link_count { 0 };
132 time_t atime { 0 };
133 time_t ctime { 0 };
134 time_t mtime { 0 };
135 time_t dtime { 0 };
136 blkcnt_t block_count { 0 };
137 blksize_t block_size { 0 };
138 unsigned major_device { 0 };
139 unsigned minor_device { 0 };
140};
141
142}