L4Re/departure

Annotated dstest_client.cc

10:54182479d4bd
2021-02-01 Paul Boddie Made the opener, opener context and file pager resources, introducing the resource server functionality as a wrapper around server configurations. Introduced testing of the opener-related abstractions.
paul@0 1
/*
paul@0 2
 * Test dataspace operations.
paul@0 3
 *
paul@0 4
 * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
paul@0 5
 *
paul@0 6
 * This program is free software; you can redistribute it and/or
paul@0 7
 * modify it under the terms of the GNU General Public License as
paul@0 8
 * published by the Free Software Foundation; either version 2 of
paul@0 9
 * the License, or (at your option) any later version.
paul@0 10
 *
paul@0 11
 * This program is distributed in the hope that it will be useful,
paul@0 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@0 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@0 14
 * GNU General Public License for more details.
paul@0 15
 *
paul@0 16
 * You should have received a copy of the GNU General Public License
paul@0 17
 * along with this program; if not, write to the Free Software
paul@0 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@0 19
 * Boston, MA  02110-1301, USA
paul@0 20
 */
paul@0 21
paul@0 22
#include <l4/re/env.h>
paul@0 23
#include <l4/sys/err.h>
paul@0 24
paul@0 25
#include <stdio.h>
paul@0 26
#include <string.h>
paul@0 27
#include <stdlib.h>
paul@0 28
paul@0 29
#include <ipc/mem_ipc.h>
paul@0 30
paul@10 31
#include "dataspace_client.h"
paul@10 32
#include "opener_client.h"
paul@10 33
#include "opener_context_client.h"
paul@7 34
#include "mapped_file_client.h"
paul@7 35
#include "memory_utils.h"
paul@7 36
paul@0 37
paul@0 38
paul@0 39
int main(void)
paul@0 40
{
paul@0 41
  l4_cap_idx_t server = l4re_env_get_cap("server");
paul@10 42
  l4_cap_idx_t context_ref;
paul@10 43
  client_Opener opener(server);
paul@0 44
paul@10 45
  long err = opener.context(&context_ref);
paul@10 46
paul@10 47
  if (err)
paul@10 48
  {
paul@10 49
    printf("Could not obtain context: %s\n", l4sys_errtostr(err));
paul@10 50
    return 1;
paul@10 51
  }
paul@10 52
paul@10 53
  client_OpenerContext context(context_ref);
paul@10 54
  client_Dataspace context_ds(context_ref);
paul@10 55
  unsigned long size, flags;
paul@10 56
paul@10 57
  err = context_ds.info(&size, &flags);
paul@10 58
paul@10 59
  if (err)
paul@10 60
  {
paul@10 61
    printf("Could not obtain context info: %s\n", l4sys_errtostr(err));
paul@10 62
    return 1;
paul@10 63
  }
paul@10 64
paul@10 65
  /* Map context memory to write the filename. */
paul@10 66
paul@10 67
  char *memory;
paul@10 68
paul@10 69
  printf("Attach region of size %ld...\n", size);
paul@10 70
paul@10 71
  err = ipc_attach_dataspace(context_ref, size, (void **) &memory);
paul@10 72
paul@10 73
  if (err)
paul@10 74
  {
paul@10 75
    printf("Could not map memory: %s\n", l4sys_errtostr(err));
paul@10 76
    return 1;
paul@10 77
  }
paul@10 78
paul@10 79
  printf("Mapped memory at %p\n", memory);
paul@10 80
paul@10 81
  /* Write the filename. */
paul@10 82
paul@10 83
  strcpy(memory, "123");
paul@10 84
paul@10 85
  /* Invoke the open method to receive the file reference. */
paul@10 86
paul@10 87
  l4_cap_idx_t file;
paul@10 88
paul@10 89
  err = context.open(L4_FPAGE_RW, &size, &file);
paul@10 90
paul@10 91
  if (err)
paul@10 92
  {
paul@10 93
    printf("Could not obtain file: %s\n", l4sys_errtostr(err));
paul@10 94
    return 1;
paul@10 95
  }
paul@10 96
paul@10 97
  client_MappedFile obj(file);
paul@7 98
paul@0 99
  /* Some memory to be mapped. */
paul@0 100
paul@7 101
  size_t start_pos, end_pos, data_end;
paul@7 102
paul@7 103
  printf("Map region from %ld to %ld...\n", 0L, page(10));
paul@7 104
paul@10 105
  err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end);
paul@7 106
paul@7 107
  if (err)
paul@7 108
  {
paul@7 109
    printf("Could not map file region: %s\n", l4sys_errtostr(err));
paul@7 110
    return 1;
paul@7 111
  }
paul@7 112
paul@10 113
  size = end_pos - start_pos;
paul@7 114
paul@7 115
  printf("Attach region of size %ld...\n", size);
paul@7 116
paul@10 117
  err = ipc_attach_dataspace(file, size, (void **) &memory);
paul@0 118
paul@0 119
  if (err)
paul@0 120
  {
paul@0 121
    printf("Could not map memory: %s\n", l4sys_errtostr(err));
paul@0 122
    return 1;
paul@0 123
  }
paul@0 124
paul@0 125
  printf("Mapped memory at %p\n", memory);
paul@0 126
paul@0 127
  for (unsigned long offset = 0; offset < size; offset += 1024)
paul@0 128
  {
paul@7 129
    char buf[11];
paul@0 130
    printf("10 bytes from %p...\n", (memory + offset));
paul@7 131
    strncpy(buf, (memory + offset), 10);
paul@7 132
    buf[10] = '\0';
paul@7 133
    printf("%s\n", buf);
paul@0 134
  }
paul@0 135
paul@0 136
  return 0;
paul@0 137
}