paul@0 | 1 | /* |
paul@0 | 2 | * Access several filesystems. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/util/util.h> |
paul@0 | 23 | |
paul@0 | 24 | #include <l4/re/c/dataspace.h> |
paul@0 | 25 | #include <l4/re/env.h> |
paul@0 | 26 | #include <l4/sys/err.h> |
paul@0 | 27 | #include <l4/sys/types.h> |
paul@0 | 28 | |
paul@0 | 29 | #include <stdlib.h> |
paul@0 | 30 | #include <stdio.h> |
paul@0 | 31 | #include <string.h> |
paul@0 | 32 | #include <unistd.h> |
paul@0 | 33 | |
paul@0 | 34 | #include <fsclient/fsdesc.h> |
paul@0 | 35 | #include <fsclient/fs_ipc.h> |
paul@14 | 36 | #include <fsclient/ops.h> |
paul@0 | 37 | #include <fsserver/server.h> |
paul@0 | 38 | #include "fspath.h" |
paul@0 | 39 | |
paul@0 | 40 | |
paul@0 | 41 | |
paul@0 | 42 | /* Virtual filesystem server. */ |
paul@0 | 43 | |
paul@5 | 44 | class Vfs_server : public OpeningServer |
paul@0 | 45 | { |
paul@0 | 46 | private: |
paul@0 | 47 | /* NOTE: Improvised structure! */ |
paul@0 | 48 | |
paul@0 | 49 | l4_cap_idx_t _servers[10]; |
paul@0 | 50 | const char *_mountpoints[10]; |
paul@0 | 51 | int _mounted = 0; |
paul@0 | 52 | |
paul@0 | 53 | public: |
paul@0 | 54 | explicit Vfs_server() |
paul@5 | 55 | : OpeningServer() |
paul@0 | 56 | { |
paul@0 | 57 | } |
paul@0 | 58 | |
paul@0 | 59 | void dispatch(ipc_message_t *msg) |
paul@0 | 60 | { |
paul@0 | 61 | switch (l4_msgtag_label(msg->tag)) |
paul@0 | 62 | { |
paul@0 | 63 | case Fs_op_mount: |
paul@0 | 64 | mount(msg); |
paul@0 | 65 | break; |
paul@0 | 66 | |
paul@0 | 67 | case Fs_op_open: |
paul@6 | 68 | open(msg); |
paul@0 | 69 | break; |
paul@0 | 70 | |
paul@0 | 71 | case Fs_op_getfs: |
paul@6 | 72 | getfs(msg); |
paul@0 | 73 | break; |
paul@0 | 74 | |
paul@0 | 75 | default: |
paul@10 | 76 | ipc_message_send_error(msg, -L4_EBADPROTO); |
paul@0 | 77 | break; |
paul@0 | 78 | } |
paul@0 | 79 | } |
paul@0 | 80 | |
paul@6 | 81 | void getfs(ipc_message_t *msg) |
paul@6 | 82 | { |
paul@6 | 83 | _open(Fs_op_getfs, msg); |
paul@6 | 84 | } |
paul@6 | 85 | |
paul@6 | 86 | void open(ipc_message_t *msg) |
paul@6 | 87 | { |
paul@6 | 88 | _open(Fs_op_open, msg); |
paul@6 | 89 | } |
paul@6 | 90 | |
paul@6 | 91 | void _open(int op, ipc_message_t *msg) |
paul@0 | 92 | { |
paul@11 | 93 | file_descriptor_t desc; |
paul@1 | 94 | int flags; |
paul@0 | 95 | long err; |
paul@0 | 96 | |
paul@1 | 97 | /* Obtain the flags. */ |
paul@1 | 98 | |
paul@1 | 99 | flags = ipc_message_get_word(msg, 0); |
paul@1 | 100 | |
paul@5 | 101 | /* Obtain the dataspace and IRQ object. */ |
paul@0 | 102 | |
paul@11 | 103 | err = fsdesc_import_dataspace(msg, 0, &desc.obj) || fsdesc_import_irq(msg, 1, &desc.obj); |
paul@0 | 104 | if (err) |
paul@0 | 105 | { |
paul@10 | 106 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 107 | return; |
paul@0 | 108 | } |
paul@0 | 109 | |
paul@0 | 110 | /* Match the path to a mountpoint. */ |
paul@0 | 111 | |
paul@11 | 112 | int selected = find_mountpoint(desc.obj.buffer); |
paul@0 | 113 | |
paul@0 | 114 | if (selected < 0) |
paul@0 | 115 | { |
paul@10 | 116 | ipc_message_send_error(msg, -L4_ENOENT); |
paul@0 | 117 | return; |
paul@0 | 118 | } |
paul@0 | 119 | |
paul@0 | 120 | /* Rewrite the path for the selected filesystem. */ |
paul@0 | 121 | |
paul@11 | 122 | rewrite_path(desc.obj.buffer, _mountpoints[selected]); |
paul@0 | 123 | |
paul@0 | 124 | /* Find the path within the selected filesystem. */ |
paul@0 | 125 | |
paul@0 | 126 | /* Propagate the message to the identified filesystem. |
paul@0 | 127 | NOTE: The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this |
paul@0 | 128 | NOTE: object has to act as intermediary. */ |
paul@0 | 129 | |
paul@11 | 130 | desc.server = _servers[selected]; |
paul@11 | 131 | |
paul@11 | 132 | err = fs_ipc_open(&desc, flags, op); |
paul@0 | 133 | if (err) |
paul@0 | 134 | { |
paul@10 | 135 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 136 | return; |
paul@0 | 137 | } |
paul@0 | 138 | |
paul@0 | 139 | /* Free the capability and buffer from this task. */ |
paul@0 | 140 | |
paul@11 | 141 | ipc_message_discard_dataspace(msg, desc.obj.ds, (l4_addr_t) desc.obj.buffer); |
paul@0 | 142 | |
paul@0 | 143 | /* Return the file size. */ |
paul@0 | 144 | |
paul@11 | 145 | ipc_message_add_word(msg, desc.obj.size); |
paul@0 | 146 | |
paul@0 | 147 | /* Export and eventually free the server reference from this task. */ |
paul@0 | 148 | |
paul@11 | 149 | ipc_message_propagate_item(msg, desc.ref); |
paul@0 | 150 | } |
paul@0 | 151 | |
paul@0 | 152 | void mount(ipc_message_t *msg) |
paul@0 | 153 | { |
paul@0 | 154 | l4_cap_idx_t server; |
paul@0 | 155 | l4re_ds_t ds; |
paul@0 | 156 | char *buffer; |
paul@0 | 157 | long err; |
paul@0 | 158 | |
paul@0 | 159 | /* Obtain the filesystem capability. */ |
paul@0 | 160 | |
paul@7 | 161 | err = ipc_message_import_capability(msg, 0, &server); |
paul@0 | 162 | if (err) |
paul@0 | 163 | { |
paul@10 | 164 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 165 | return; |
paul@0 | 166 | } |
paul@0 | 167 | |
paul@0 | 168 | /* Obtain the dataspace. */ |
paul@0 | 169 | |
paul@7 | 170 | err = ipc_message_import_dataspace(msg, 1, &ds, (l4_addr_t *) &buffer); |
paul@0 | 171 | if (err) |
paul@0 | 172 | { |
paul@10 | 173 | ipc_message_send_error(msg, -L4_EIO); |
paul@0 | 174 | return; |
paul@0 | 175 | } |
paul@0 | 176 | |
paul@0 | 177 | /* Obtain the object's path. */ |
paul@0 | 178 | |
paul@0 | 179 | char *path = buffer; |
paul@0 | 180 | |
paul@0 | 181 | /* Mount the supplied capability. */ |
paul@0 | 182 | |
paul@0 | 183 | mount_at_path(server, path); |
paul@0 | 184 | |
paul@0 | 185 | /* Free the capability and buffer from this task. */ |
paul@0 | 186 | |
paul@9 | 187 | ipc_message_discard_dataspace(msg, ds, (l4_addr_t) buffer); |
paul@0 | 188 | } |
paul@0 | 189 | |
paul@0 | 190 | /* Non-exported methods. */ |
paul@0 | 191 | |
paul@0 | 192 | void mount_at_path(l4_cap_idx_t server, const char *path) |
paul@0 | 193 | { |
paul@0 | 194 | /* NOTE: Should return error, terminate with path separator if absent. */ |
paul@0 | 195 | |
paul@0 | 196 | if (_mounted >= 10) |
paul@0 | 197 | return; |
paul@0 | 198 | |
paul@0 | 199 | _servers[_mounted] = server; |
paul@0 | 200 | _mountpoints[_mounted] = strdup(path); |
paul@0 | 201 | |
paul@0 | 202 | _mounted++; |
paul@0 | 203 | } |
paul@0 | 204 | |
paul@0 | 205 | int find_mountpoint(const char *path) |
paul@0 | 206 | { |
paul@0 | 207 | int i, matching; |
paul@0 | 208 | size_t length, longest; |
paul@0 | 209 | |
paul@0 | 210 | if (!_mounted) |
paul@0 | 211 | return -1; |
paul@0 | 212 | |
paul@0 | 213 | matching = -1; |
paul@0 | 214 | longest = 0; |
paul@0 | 215 | |
paul@0 | 216 | /* Find the largest matching path. */ |
paul@0 | 217 | |
paul@0 | 218 | for (i = 0; i < _mounted; i++) |
paul@0 | 219 | { |
paul@0 | 220 | length = strlen(_mountpoints[i]); |
paul@0 | 221 | |
paul@0 | 222 | /* The empty mountpoint always matches; |
paul@0 | 223 | suffixed mountpoints may match as prefixes; |
paul@0 | 224 | suffixed mountpoints may match unsuffixed paths; |
paul@0 | 225 | unsuffixed mountpoints must match exactly. */ |
paul@0 | 226 | |
paul@0 | 227 | /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on |
paul@0 | 228 | a path separator as the first subsequent character. */ |
paul@0 | 229 | |
paul@0 | 230 | if (!length || |
paul@0 | 231 | ((_mountpoints[i][length - 1] == '/') && !strncmp(_mountpoints[i], path, length)) || |
paul@0 | 232 | ((_mountpoints[i][length - 1] == '/') && (strlen(path) == length - 1) && !strncmp(_mountpoints[i], path, length - 1)) || |
paul@0 | 233 | !strcmp(_mountpoints[i], path)) |
paul@0 | 234 | { |
paul@0 | 235 | if (length >= longest) |
paul@0 | 236 | matching = i; |
paul@0 | 237 | } |
paul@0 | 238 | } |
paul@0 | 239 | |
paul@0 | 240 | return matching; |
paul@0 | 241 | } |
paul@0 | 242 | }; |
paul@0 | 243 | |
paul@0 | 244 | |
paul@0 | 245 | |
paul@0 | 246 | int main(int argc, char *argv[]) |
paul@0 | 247 | { |
paul@0 | 248 | FILE *fp; |
paul@0 | 249 | char buffer[256], *sep; |
paul@12 | 250 | l4_cap_idx_t fscap; |
paul@0 | 251 | |
paul@0 | 252 | if (argc < 2) |
paul@0 | 253 | { |
paul@0 | 254 | printf("Need a mount table.\n"); |
paul@0 | 255 | return 1; |
paul@0 | 256 | } |
paul@0 | 257 | |
paul@0 | 258 | /* Initialise and register a new server object. */ |
paul@0 | 259 | |
paul@0 | 260 | Vfs_server server_obj; |
paul@0 | 261 | |
paul@13 | 262 | if (server_obj.bind("export")) |
paul@0 | 263 | { |
paul@0 | 264 | printf("Could not bind thread.\n"); |
paul@0 | 265 | return 1; |
paul@0 | 266 | } |
paul@0 | 267 | |
paul@0 | 268 | /* Mount filesystems. */ |
paul@0 | 269 | |
paul@0 | 270 | fp = fopen(argv[1], "r"); |
paul@0 | 271 | if (fp == NULL) |
paul@0 | 272 | { |
paul@0 | 273 | printf("Could not open %s.\n", argv[1]); |
paul@0 | 274 | return 1; |
paul@0 | 275 | } |
paul@0 | 276 | |
paul@0 | 277 | /* Obtain capabilities for mountpoints. */ |
paul@0 | 278 | |
paul@0 | 279 | while (fgets(buffer, 256, fp) != NULL) |
paul@0 | 280 | { |
paul@0 | 281 | sep = strchr(buffer, (int) '\t'); |
paul@0 | 282 | if (sep == NULL) |
paul@0 | 283 | continue; |
paul@0 | 284 | |
paul@0 | 285 | /* Terminate newline-suffixed strings. */ |
paul@0 | 286 | |
paul@0 | 287 | if (sep[strlen(sep) - 1] == '\n') |
paul@0 | 288 | sep[strlen(sep) - 1] = '\0'; |
paul@0 | 289 | |
paul@0 | 290 | /* Obtain the named capability. */ |
paul@0 | 291 | |
paul@0 | 292 | fscap = l4re_env_get_cap(sep + 1); |
paul@0 | 293 | |
paul@0 | 294 | if (l4_is_invalid_cap(fscap)) |
paul@0 | 295 | continue; |
paul@0 | 296 | |
paul@0 | 297 | /* Terminate the path and register the mountpoint. */ |
paul@0 | 298 | |
paul@0 | 299 | *sep = '\0'; |
paul@0 | 300 | server_obj.mount_at_path(fscap, buffer); |
paul@0 | 301 | } |
paul@0 | 302 | |
paul@0 | 303 | fclose(fp); |
paul@0 | 304 | |
paul@5 | 305 | /* Enter the IPC server loop. */ |
paul@0 | 306 | |
paul@5 | 307 | server_obj.loop(); |
paul@0 | 308 | return 0; |
paul@0 | 309 | } |