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