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