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