L4Re/departure

Annotated tests/dstest_file_monitor.cc

360:92c5f6aa8c36
2022-06-12 Paul Boddie Reintroduced PagerObject code generation required to initiate servers. mmap-region-flags
paul@211 1
/*
paul@211 2
 * Test directory monitoring operations.
paul@211 3
 *
paul@237 4
 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@211 5
 *
paul@211 6
 * This program is free software; you can redistribute it and/or
paul@211 7
 * modify it under the terms of the GNU General Public License as
paul@211 8
 * published by the Free Software Foundation; either version 2 of
paul@211 9
 * the License, or (at your option) any later version.
paul@211 10
 *
paul@211 11
 * This program is distributed in the hope that it will be useful,
paul@211 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@211 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@211 14
 * GNU General Public License for more details.
paul@211 15
 *
paul@211 16
 * You should have received a copy of the GNU General Public License
paul@211 17
 * along with this program; if not, write to the Free Software
paul@211 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@211 19
 * Boston, MA  02110-1301, USA
paul@211 20
 */
paul@211 21
paul@211 22
#include <l4/re/env.h>
paul@211 23
#include <l4/sys/err.h>
paul@211 24
paul@211 25
#include <thread>
paul@211 26
paul@211 27
#include <stdio.h>
paul@211 28
#include <string.h>
paul@211 29
#include <stdlib.h>
paul@211 30
#include <unistd.h>
paul@211 31
paul@211 32
#include <fsclient/client.h>
paul@211 33
#include <systypes/fcntl.h>
paul@211 34
paul@211 35
paul@211 36
paul@211 37
/* Open files in the directory given by filename. */
paul@211 38
paul@237 39
static void open_files(const char *filename)
paul@211 40
{
paul@211 41
  char *buffer = (char *) calloc(strlen(filename) + 10, sizeof(char));
paul@211 42
paul@211 43
  if (buffer == NULL)
paul@211 44
  {
paul@211 45
    printf("Could not allocate buffer.\n");
paul@211 46
    return;
paul@211 47
  }
paul@211 48
paul@211 49
  for (int i = 0; i < 10; i++)
paul@211 50
  {
paul@211 51
    sprintf(buffer, "%s/file%02d", filename, i);
paul@211 52
paul@237 53
    client_open(buffer, O_CREAT | O_RDWR);
paul@211 54
    sleep(1);
paul@211 55
  }
paul@211 56
}
paul@211 57
paul@211 58
/* Monitor files in the given directory. */
paul@211 59
paul@211 60
static void monitor_files(file_t *directory)
paul@211 61
{
paul@211 62
  long err = client_subscribe(directory, NOTIFY_FILE_OPENED, client_notifier_task());
paul@211 63
paul@211 64
  if (err)
paul@211 65
  {
paul@211 66
    printf("Could not subscribe to events on directory.\n");
paul@211 67
    return;
paul@211 68
  }
paul@211 69
paul@211 70
  while (1)
paul@211 71
  {
paul@211 72
    /* Wait for notification of content. */
paul@211 73
paul@211 74
    err = client_wait_file(directory, client_notifier_task());
paul@211 75
paul@211 76
    if (err)
paul@211 77
    {
paul@211 78
      printf("Error waiting for notifications: %s\n", l4sys_errtostr(err));
paul@211 79
      return;
paul@211 80
    }
paul@211 81
paul@211 82
    printf("Notified with conditions:%s\n", directory->notifications & NOTIFY_FILE_OPENED ? " file opened" : "");
paul@211 83
  }
paul@211 84
}
paul@211 85
paul@211 86
paul@211 87
paul@211 88
int main(int argc, char *argv[])
paul@211 89
{
paul@211 90
  if (argc < 2)
paul@211 91
  {
paul@237 92
    printf("Need a directory name.\n");
paul@211 93
    return 1;
paul@211 94
  }
paul@211 95
paul@211 96
  char *filename = argv[1];
paul@211 97
paul@211 98
  printf("Opening %s...\n", filename);
paul@211 99
paul@237 100
  file_t *directory = client_open(filename, O_DIRECTORY);
paul@211 101
paul@211 102
  if (directory == NULL)
paul@211 103
  {
paul@211 104
    printf("Could not open directory.\n");
paul@211 105
    return 1;
paul@211 106
  }
paul@211 107
paul@211 108
  /* Schedule threads. */
paul@211 109
paul@211 110
  std::thread *activities[2];
paul@211 111
paul@237 112
  activities[0] = new std::thread(open_files, filename);
paul@211 113
  activities[1] = new std::thread(monitor_files, directory);
paul@211 114
paul@211 115
  for (int i = 0; i < 2; i++)
paul@211 116
    activities[i]->join();
paul@211 117
paul@211 118
  return 0;
paul@211 119
}
paul@211 120
paul@211 121
// vim: tabstop=2 expandtab shiftwidth=2