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(Fs_op_open, msg); 72 break; 73 74 case Fs_op_getfs: 75 open(Fs_op_getfs, msg); 76 break; 77 78 default: 79 send_error(msg, -L4_EBADPROTO); 80 break; 81 } 82 } 83 84 void open(int op, ipc_message_t *msg) 85 { 86 int flags; 87 l4_cap_idx_t server, irq; 88 l4re_ds_t ds; 89 size_t size; 90 char *buffer; 91 long err; 92 93 /* Obtain the flags. */ 94 95 flags = ipc_message_get_word(msg, 0); 96 97 /* Obtain the dataspace and IRQ object. */ 98 99 err = import_dataspace(msg, 0, &ds, (l4_addr_t *) &buffer) || import_capability(msg, 1, &irq); 100 if (err) 101 { 102 send_error(msg, -L4_EIO); 103 return; 104 } 105 106 /* Match the path to a mountpoint. */ 107 108 int selected = find_mountpoint(buffer); 109 110 if (selected < 0) 111 { 112 send_error(msg, -L4_ENOENT); 113 return; 114 } 115 116 /* Rewrite the path for the selected filesystem. */ 117 118 rewrite_path(buffer, _mountpoints[selected]); 119 120 /* Find the path within the selected filesystem. */ 121 122 /* Propagate the message to the identified filesystem. 123 NOTE: The L4_MSGTAG_PROPAGATE flag is not supported by Fiasco.OC, so this 124 NOTE: object has to act as intermediary. */ 125 126 err = fs_ipc_open(flags, _servers[selected], op, ds, irq, &server, &size); 127 if (err) 128 { 129 send_error(msg, -L4_EIO); 130 return; 131 } 132 133 /* Free the capability and buffer from this task. */ 134 135 discard_dataspace(ds, (l4_addr_t) buffer); 136 137 /* Return the file size. */ 138 139 ipc_message_add_word(msg, size); 140 141 /* Export and eventually free the server reference from this task. */ 142 143 ipc_message_propagate_item(msg, server); 144 } 145 146 void mount(ipc_message_t *msg) 147 { 148 l4_cap_idx_t server; 149 l4re_ds_t ds; 150 char *buffer; 151 long err; 152 153 /* Obtain the filesystem capability. */ 154 155 err = import_capability(msg, 0, &server); 156 if (err) 157 { 158 send_error(msg, -L4_EIO); 159 return; 160 } 161 162 /* Obtain the dataspace. */ 163 164 err = import_dataspace(msg, 1, &ds, (l4_addr_t *) &buffer); 165 if (err) 166 { 167 send_error(msg, -L4_EIO); 168 return; 169 } 170 171 /* Obtain the object's path. */ 172 173 char *path = buffer; 174 175 /* Mount the supplied capability. */ 176 177 mount_at_path(server, path); 178 179 /* Free the capability and buffer from this task. */ 180 181 discard_dataspace(ds, (l4_addr_t) buffer); 182 } 183 184 /* Non-exported methods. */ 185 186 void mount_at_path(l4_cap_idx_t server, const char *path) 187 { 188 /* NOTE: Should return error, terminate with path separator if absent. */ 189 190 if (_mounted >= 10) 191 return; 192 193 _servers[_mounted] = server; 194 _mountpoints[_mounted] = strdup(path); 195 196 _mounted++; 197 } 198 199 int find_mountpoint(const char *path) 200 { 201 int i, matching; 202 size_t length, longest; 203 204 if (!_mounted) 205 return -1; 206 207 matching = -1; 208 longest = 0; 209 210 /* Find the largest matching path. */ 211 212 for (i = 0; i < _mounted; i++) 213 { 214 length = strlen(_mountpoints[i]); 215 216 /* The empty mountpoint always matches; 217 suffixed mountpoints may match as prefixes; 218 suffixed mountpoints may match unsuffixed paths; 219 unsuffixed mountpoints must match exactly. */ 220 221 /* NOTE: Unsuffixed mountpoints might support subpaths by insisting on 222 a path separator as the first subsequent character. */ 223 224 if (!length || 225 ((_mountpoints[i][length - 1] == '/') && !strncmp(_mountpoints[i], path, length)) || 226 ((_mountpoints[i][length - 1] == '/') && (strlen(path) == length - 1) && !strncmp(_mountpoints[i], path, length - 1)) || 227 !strcmp(_mountpoints[i], path)) 228 { 229 if (length >= longest) 230 matching = i; 231 } 232 } 233 234 return matching; 235 } 236 }; 237 238 239 240 int main(int argc, char *argv[]) 241 { 242 FILE *fp; 243 char buffer[256], *sep; 244 l4_cap_idx_t fscap, server; 245 246 if (argc < 2) 247 { 248 printf("Need a mount table.\n"); 249 return 1; 250 } 251 252 /* Initialise and register a new server object. */ 253 254 Vfs_server server_obj; 255 256 if (bind_to_capability("export", l4_umword_t(&server_obj), &server)) 257 { 258 printf("Could not bind thread.\n"); 259 return 1; 260 } 261 262 /* Mount filesystems. */ 263 264 fp = fopen(argv[1], "r"); 265 if (fp == NULL) 266 { 267 printf("Could not open %s.\n", argv[1]); 268 return 1; 269 } 270 271 /* Obtain capabilities for mountpoints. */ 272 273 while (fgets(buffer, 256, fp) != NULL) 274 { 275 sep = strchr(buffer, (int) '\t'); 276 if (sep == NULL) 277 continue; 278 279 /* Terminate newline-suffixed strings. */ 280 281 if (sep[strlen(sep) - 1] == '\n') 282 sep[strlen(sep) - 1] = '\0'; 283 284 /* Obtain the named capability. */ 285 286 fscap = l4re_env_get_cap(sep + 1); 287 288 if (l4_is_invalid_cap(fscap)) 289 continue; 290 291 /* Terminate the path and register the mountpoint. */ 292 293 *sep = '\0'; 294 server_obj.mount_at_path(fscap, buffer); 295 } 296 297 fclose(fp); 298 299 /* Enter the IPC server loop. */ 300 301 server_obj.loop(); 302 return 0; 303 }