Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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#include <AK/JsonArray.h>
28#include <AK/JsonObject.h>
29#include <AK/JsonValue.h>
30#include <LibCore/ArgsParser.h>
31#include <LibCore/File.h>
32#include <stdio.h>
33#include <unistd.h>
34
35int parse_options(const StringView& options)
36{
37 int flags = 0;
38 Vector<StringView> parts = options.split_view(',');
39 for (auto& part : parts) {
40 if (part == "defaults")
41 continue;
42 else if (part == "nodev")
43 flags |= MS_NODEV;
44 else if (part == "noexec")
45 flags |= MS_NOEXEC;
46 else if (part == "nosuid")
47 flags |= MS_NOSUID;
48 else if (part == "bind")
49 flags |= MS_BIND;
50 else
51 fprintf(stderr, "Ignoring invalid option: %s\n", String(part).characters());
52 }
53 return flags;
54}
55
56bool mount_all()
57{
58 // Mount all filesystems listed in /etc/fstab.
59 dbg() << "Mounting all filesystems...";
60
61 auto fstab = Core::File::construct("/etc/fstab");
62 if (!fstab->open(Core::IODevice::OpenMode::ReadOnly)) {
63 fprintf(stderr, "Failed to open /etc/fstab: %s\n", fstab->error_string());
64 return false;
65 }
66
67 bool all_ok = true;
68 while (fstab->can_read_line()) {
69 ByteBuffer buffer = fstab->read_line(1024);
70 StringView line_view = (const char*)buffer.data();
71
72 // Trim the trailing newline, if any.
73 if (line_view.length() > 0 && line_view[line_view.length() - 1] == '\n')
74 line_view = line_view.substring_view(0, line_view.length() - 1);
75 String line = line_view;
76
77 // Skip comments and blank lines.
78 if (line.is_empty() || line.starts_with("#"))
79 continue;
80
81 Vector<String> parts = line.split('\t');
82 if (parts.size() < 3) {
83 fprintf(stderr, "Invalid fstab entry: %s\n", line.characters());
84 all_ok = false;
85 continue;
86 }
87
88 const char* devname = parts[0].characters();
89 const char* mountpoint = parts[1].characters();
90 const char* fstype = parts[2].characters();
91 int flags = parts.size() >= 4 ? parse_options(parts[3]) : 0;
92
93 if (strcmp(mountpoint, "/") == 0) {
94 dbg() << "Skipping mounting root";
95 continue;
96 }
97
98 dbg() << "Mounting " << devname << "(" << fstype << ")"
99 << " on " << mountpoint;
100 int rc = mount(devname, mountpoint, fstype, flags);
101 if (rc != 0) {
102 fprintf(stderr, "Failed to mount %s (%s) on %s: %s\n", devname, fstype, mountpoint, strerror(errno));
103 all_ok = false;
104 continue;
105 }
106 }
107
108 return all_ok;
109}
110
111bool print_mounts()
112{
113 // Output info about currently mounted filesystems.
114 auto df = Core::File::construct("/proc/df");
115 if (!df->open(Core::IODevice::ReadOnly)) {
116 fprintf(stderr, "Failed to open /proc/df: %s\n", df->error_string());
117 return false;
118 }
119
120 auto content = df->read_all();
121 auto json = JsonValue::from_string(content).as_array();
122
123 json.for_each([](auto& value) {
124 auto fs_object = value.as_object();
125 auto class_name = fs_object.get("class_name").to_string();
126 auto mount_point = fs_object.get("mount_point").to_string();
127 auto device = fs_object.get("device").as_string_or(class_name);
128 auto readonly = fs_object.get("readonly").to_bool();
129 auto mount_flags = fs_object.get("mount_flags").to_int();
130
131 printf("%s on %s type %s (", device.characters(), mount_point.characters(), class_name.characters());
132
133 if (readonly)
134 printf("ro");
135 else
136 printf("rw");
137
138 if (mount_flags & MS_NODEV)
139 printf(",nodev");
140 if (mount_flags & MS_NOEXEC)
141 printf(",noexec");
142 if (mount_flags & MS_NOSUID)
143 printf(",nosuid");
144 if (mount_flags & MS_BIND)
145 printf(",bind");
146
147 printf(")\n");
148 });
149
150 return true;
151}
152
153int main(int argc, char** argv)
154{
155 const char* source = nullptr;
156 const char* mountpoint = nullptr;
157 const char* fs_type = nullptr;
158 const char* options = nullptr;
159 bool should_mount_all = false;
160
161 Core::ArgsParser args_parser;
162 args_parser.add_positional_argument(source, "Source path", "source", Core::ArgsParser::Required::No);
163 args_parser.add_positional_argument(mountpoint, "Mount point", "mountpoint", Core::ArgsParser::Required::No);
164 args_parser.add_option(fs_type, "File system type", nullptr, 't', "fstype");
165 args_parser.add_option(options, "Mount options", nullptr, 'o', "options");
166 args_parser.add_option(should_mount_all, "Mount all file systems listed in /etc/fstab", nullptr, 'a');
167 args_parser.parse(argc, argv);
168
169 if (should_mount_all) {
170 return mount_all() ? 0 : 1;
171 }
172
173 if (!source && !mountpoint)
174 return print_mounts() ? 0 : 1;
175
176 if (source && mountpoint) {
177 if (!fs_type)
178 fs_type = "ext2";
179 int flags = options ? parse_options(options) : 0;
180
181 if (mount(source, mountpoint, fs_type, flags) < 0) {
182 perror("mount");
183 return 1;
184 }
185 return 0;
186 }
187
188 args_parser.print_usage(stderr, argv[0]);
189 return 1;
190}