1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021, 2022 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/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <systypes/fcntl.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdlib.h> 30 31 #include <fsclient/file.h> 32 #include <mem/memory_utils.h> 33 34 #include "dataspace_client.h" 35 #include "mapped_file_client.h" 36 37 38 39 /* NOTE: Copied from libfsclient. */ 40 41 static void _update_extent(file_t *file) 42 { 43 /* Handle files ending after or within the region. */ 44 45 if (file->size > file->start_pos) 46 { 47 if (file->size > file->end_pos) 48 file->data_end = file->end_pos - file->start_pos; 49 else 50 file->data_end = file->size - file->start_pos; 51 } 52 53 /* Handle files ending before the region. */ 54 55 else 56 file->data_end = 0; 57 } 58 59 60 61 int main(int argc, char *argv[]) 62 { 63 if (argc < 4) 64 { 65 printf("Need filename, step and sample size.\n"); 66 return 1; 67 } 68 69 /* Obtain filename and access parameters. */ 70 71 char *filename = argv[1]; 72 unsigned long step = atoi(argv[2]); 73 unsigned long sample = atoi(argv[3]); 74 75 /* Allocate a buffer for sampling from the file. */ 76 77 char buf[sample + 1]; 78 79 /* Obtain access to the filesystem. */ 80 81 l4_cap_idx_t server = l4re_env_get_cap("server"); 82 83 /* Invoke the open method to receive the file reference. */ 84 85 file_t file; 86 long err = file_open(&file, filename, O_RDWR, server); 87 88 if (err) 89 { 90 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 91 return 1; 92 } 93 94 /* A region of the file is mapped. */ 95 96 client_MappedFile mapped_file(file.ref); 97 98 err = mapped_file.mmap(0, page(10), 0, 0, &file.start_pos, &file.end_pos, &file.size); 99 100 if (err) 101 { 102 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 103 return 1; 104 } 105 106 /* Fix up the file data structure manually. */ 107 108 file.memory = (char *) 0x2000000; 109 _update_extent(&file); 110 111 /* Explicitly map the file into this address space, without region mapper 112 usage. The receive window base must be a multiple of its size. */ 113 114 client_Dataspace dataspace(file.ref); 115 l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)}; 116 117 printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage)); 118 119 err = dataspace.map(0, 0, L4_FPAGE_RO, ®ion); 120 121 if (err) 122 { 123 printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err)); 124 return 1; 125 } 126 127 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 128 { 129 unsigned long remaining = file_populated_span(&file) - offset; 130 unsigned long sample_remaining = remaining < sample ? remaining : sample; 131 132 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 133 strncpy(buf, (file.memory + offset), sample_remaining); 134 buf[sample_remaining] = '\0'; 135 printf("%s\n", buf); 136 } 137 138 printf("File shown.\n"); 139 140 return 0; 141 } 142 143 // vim: tabstop=2 expandtab shiftwidth=2