1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/dstest_host_client.cc Fri Feb 12 00:21:03 2021 +0100
1.3 @@ -0,0 +1,163 @@
1.4 +/*
1.5 + * Test dataspace operations.
1.6 + *
1.7 + * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/re/env.h>
1.26 +#include <l4/sys/err.h>
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <string.h>
1.30 +#include <stdlib.h>
1.31 +
1.32 +#include <ipc/mem_ipc.h>
1.33 +
1.34 +#include "dataspace_client.h"
1.35 +#include "opener_client.h"
1.36 +#include "opener_context_client.h"
1.37 +#include "mapped_file_client.h"
1.38 +#include "memory_utils.h"
1.39 +
1.40 +
1.41 +
1.42 +int main(int argc, char *argv[])
1.43 +{
1.44 + if (argc < 4)
1.45 + {
1.46 + printf("Need filename, step and sample size.\n");
1.47 + return 1;
1.48 + }
1.49 +
1.50 + /* Obtain filename and access parameters. */
1.51 +
1.52 + char *filename = argv[1];
1.53 + unsigned long step = atoi(argv[2]);
1.54 + unsigned long sample = atoi(argv[3]);
1.55 +
1.56 + /* Allocate a buffer for sampling from the file. */
1.57 +
1.58 + char buf[sample + 1];
1.59 +
1.60 + /* Obtain access to the filesystem. */
1.61 +
1.62 + l4_cap_idx_t server = l4re_env_get_cap("server");
1.63 + l4_cap_idx_t context_ref;
1.64 + client_Opener opener(server);
1.65 +
1.66 + long err = opener.context(&context_ref);
1.67 +
1.68 + if (err)
1.69 + {
1.70 + printf("Could not obtain context: %s\n", l4sys_errtostr(err));
1.71 + return 1;
1.72 + }
1.73 +
1.74 + client_OpenerContext context(context_ref);
1.75 + client_Dataspace context_ds(context_ref);
1.76 + unsigned long size, flags;
1.77 +
1.78 + err = context_ds.info(&size, &flags);
1.79 +
1.80 + if (err)
1.81 + {
1.82 + printf("Could not obtain context info: %s\n", l4sys_errtostr(err));
1.83 + return 1;
1.84 + }
1.85 +
1.86 + /* Map context memory to write the filename. */
1.87 +
1.88 + char *memory;
1.89 +
1.90 + printf("Attach region of size %ld...\n", size);
1.91 +
1.92 + err = ipc_attach_dataspace(context_ref, size, (void **) &memory);
1.93 +
1.94 + if (err)
1.95 + {
1.96 + printf("Could not map memory: %s\n", l4sys_errtostr(err));
1.97 + return 1;
1.98 + }
1.99 +
1.100 + printf("Mapped memory at %p\n", memory);
1.101 +
1.102 + /* Write the filename. */
1.103 +
1.104 + strcpy(memory, filename);
1.105 +
1.106 + /* Invoke the open method to receive the file reference. */
1.107 +
1.108 + l4_cap_idx_t file;
1.109 +
1.110 + err = context.open(L4_FPAGE_RW, &size, &file);
1.111 +
1.112 + if (err)
1.113 + {
1.114 + printf("Could not obtain file: %s\n", l4sys_errtostr(err));
1.115 + return 1;
1.116 + }
1.117 +
1.118 + client_MappedFile obj(file);
1.119 +
1.120 + /* Some memory to be mapped. */
1.121 +
1.122 + size_t start_pos, end_pos, data_end;
1.123 +
1.124 + printf("Map region from %ld to %ld...\n", 0L, page(10));
1.125 +
1.126 + err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end);
1.127 +
1.128 + if (err)
1.129 + {
1.130 + printf("Could not map file region: %s\n", l4sys_errtostr(err));
1.131 + return 1;
1.132 + }
1.133 +
1.134 + printf("Mapped region from %ld to %ld with content %ld.\n",
1.135 + start_pos, end_pos, data_end);
1.136 +
1.137 + size = end_pos - start_pos;
1.138 +
1.139 + printf("Attach region of size %ld...\n", size);
1.140 +
1.141 + err = ipc_attach_dataspace(file, size, (void **) &memory);
1.142 +
1.143 + if (err)
1.144 + {
1.145 + printf("Could not map memory: %s\n", l4sys_errtostr(err));
1.146 + return 1;
1.147 + }
1.148 +
1.149 + printf("Mapped memory at %p\n", memory);
1.150 +
1.151 + if (data_end < size)
1.152 + size = data_end;
1.153 +
1.154 + for (unsigned long offset = 0; offset < size; offset += step)
1.155 + {
1.156 + unsigned long remaining = size - offset;
1.157 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
1.158 +
1.159 + printf("%ld bytes from %p...\n", sample_remaining, (memory + offset));
1.160 + strncpy(buf, (memory + offset), sample_remaining);
1.161 + buf[sample_remaining] = '\0';
1.162 + printf("%s\n", buf);
1.163 + }
1.164 +
1.165 + return 0;
1.166 +}