paul@211 | 1 | /* |
paul@211 | 2 | * Test directory monitoring operations. |
paul@211 | 3 | * |
paul@479 | 4 | * Copyright (C) 2020, 2021, 2022, 2023 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@402 | 37 | /* Number of files to open in the test. */ |
paul@402 | 38 | |
paul@402 | 39 | static const int FILES_TO_OPEN = 10; |
paul@402 | 40 | |
paul@402 | 41 | |
paul@402 | 42 | |
paul@211 | 43 | /* Open files in the directory given by filename. */ |
paul@211 | 44 | |
paul@237 | 45 | static void open_files(const char *filename) |
paul@211 | 46 | { |
paul@211 | 47 | char *buffer = (char *) calloc(strlen(filename) + 10, sizeof(char)); |
paul@211 | 48 | |
paul@211 | 49 | if (buffer == NULL) |
paul@211 | 50 | { |
paul@211 | 51 | printf("Could not allocate buffer.\n"); |
paul@211 | 52 | return; |
paul@211 | 53 | } |
paul@211 | 54 | |
paul@402 | 55 | for (int i = 0; i < FILES_TO_OPEN; i++) |
paul@211 | 56 | { |
paul@211 | 57 | sprintf(buffer, "%s/file%02d", filename, i); |
paul@211 | 58 | |
paul@237 | 59 | client_open(buffer, O_CREAT | O_RDWR); |
paul@211 | 60 | sleep(1); |
paul@211 | 61 | } |
paul@211 | 62 | } |
paul@211 | 63 | |
paul@211 | 64 | /* Monitor files in the given directory. */ |
paul@211 | 65 | |
paul@211 | 66 | static void monitor_files(file_t *directory) |
paul@211 | 67 | { |
paul@211 | 68 | long err = client_subscribe(directory, NOTIFY_FILE_OPENED, client_notifier_task()); |
paul@211 | 69 | |
paul@211 | 70 | if (err) |
paul@211 | 71 | { |
paul@211 | 72 | printf("Could not subscribe to events on directory.\n"); |
paul@211 | 73 | return; |
paul@211 | 74 | } |
paul@211 | 75 | |
paul@402 | 76 | int expected; |
paul@402 | 77 | |
paul@402 | 78 | for (expected = FILES_TO_OPEN; expected; expected--) |
paul@211 | 79 | { |
paul@211 | 80 | /* Wait for notification of content. */ |
paul@211 | 81 | |
paul@211 | 82 | err = client_wait_file(directory, client_notifier_task()); |
paul@211 | 83 | |
paul@211 | 84 | if (err) |
paul@211 | 85 | { |
paul@211 | 86 | printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); |
paul@211 | 87 | return; |
paul@211 | 88 | } |
paul@211 | 89 | |
paul@479 | 90 | printf("Notified with conditions:%s\n", file_notifications(directory) & NOTIFY_FILE_OPENED ? " file opened" : ""); |
paul@211 | 91 | } |
paul@402 | 92 | |
paul@402 | 93 | printf("Notified for all expected files: %s\n", expected ? "False" : "True"); |
paul@211 | 94 | } |
paul@211 | 95 | |
paul@211 | 96 | |
paul@211 | 97 | |
paul@211 | 98 | int main(int argc, char *argv[]) |
paul@211 | 99 | { |
paul@211 | 100 | if (argc < 2) |
paul@211 | 101 | { |
paul@237 | 102 | printf("Need a directory name.\n"); |
paul@211 | 103 | return 1; |
paul@211 | 104 | } |
paul@211 | 105 | |
paul@211 | 106 | char *filename = argv[1]; |
paul@211 | 107 | |
paul@211 | 108 | printf("Opening %s...\n", filename); |
paul@211 | 109 | |
paul@237 | 110 | file_t *directory = client_open(filename, O_DIRECTORY); |
paul@211 | 111 | |
paul@402 | 112 | if (!client_opened(directory)) |
paul@211 | 113 | { |
paul@402 | 114 | client_close(directory); |
paul@211 | 115 | printf("Could not open directory.\n"); |
paul@211 | 116 | return 1; |
paul@211 | 117 | } |
paul@211 | 118 | |
paul@211 | 119 | /* Schedule threads. */ |
paul@211 | 120 | |
paul@211 | 121 | std::thread *activities[2]; |
paul@211 | 122 | |
paul@237 | 123 | activities[0] = new std::thread(open_files, filename); |
paul@211 | 124 | activities[1] = new std::thread(monitor_files, directory); |
paul@211 | 125 | |
paul@211 | 126 | for (int i = 0; i < 2; i++) |
paul@211 | 127 | activities[i]->join(); |
paul@211 | 128 | |
paul@402 | 129 | printf("Expected %d 'file opened' notifications.\n", FILES_TO_OPEN); |
paul@402 | 130 | printf("End of test.\n"); |
paul@211 | 131 | return 0; |
paul@211 | 132 | } |
paul@211 | 133 | |
paul@211 | 134 | // vim: tabstop=2 expandtab shiftwidth=2 |