1 /* 2 * Test directory monitoring 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 <thread> 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdlib.h> 30 #include <unistd.h> 31 32 #include <fsclient/client.h> 33 #include <systypes/fcntl.h> 34 35 36 37 /* Open files in the directory given by filename. */ 38 39 static void open_files(const char *filename) 40 { 41 char *buffer = (char *) calloc(strlen(filename) + 10, sizeof(char)); 42 43 if (buffer == NULL) 44 { 45 printf("Could not allocate buffer.\n"); 46 return; 47 } 48 49 for (int i = 0; i < 10; i++) 50 { 51 sprintf(buffer, "%s/file%02d", filename, i); 52 53 client_open(buffer, O_CREAT | O_RDWR); 54 sleep(1); 55 } 56 } 57 58 /* Monitor files in the given directory. */ 59 60 static void monitor_files(file_t *directory) 61 { 62 long err = client_subscribe(directory, NOTIFY_FILE_OPENED, client_notifier_task()); 63 64 if (err) 65 { 66 printf("Could not subscribe to events on directory.\n"); 67 return; 68 } 69 70 while (1) 71 { 72 /* Wait for notification of content. */ 73 74 err = client_wait_file(directory, client_notifier_task()); 75 76 if (err) 77 { 78 printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); 79 return; 80 } 81 82 printf("Notified with conditions:%s\n", directory->notifications & NOTIFY_FILE_OPENED ? " file opened" : ""); 83 } 84 } 85 86 87 88 int main(int argc, char *argv[]) 89 { 90 if (argc < 2) 91 { 92 printf("Need a directory name.\n"); 93 return 1; 94 } 95 96 char *filename = argv[1]; 97 98 printf("Opening %s...\n", filename); 99 100 file_t *directory = client_open(filename, O_DIRECTORY); 101 102 if (directory == NULL) 103 { 104 printf("Could not open directory.\n"); 105 return 1; 106 } 107 108 /* Schedule threads. */ 109 110 std::thread *activities[2]; 111 112 activities[0] = new std::thread(open_files, filename); 113 activities[1] = new std::thread(monitor_files, directory); 114 115 for (int i = 0; i < 2; i++) 116 activities[i]->join(); 117 118 return 0; 119 } 120 121 // vim: tabstop=2 expandtab shiftwidth=2