1 /* 2 * Test directory monitoring operations. 3 * 4 * Copyright (C) 2020, 2021 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 static file_t *open_object(char *filename, flags_t flags, bool have_uid, sys_uid_t uid) 38 { 39 /* With a user, open a user-specific file opener. */ 40 41 if (have_uid) 42 { 43 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 44 45 if (l4_is_invalid_cap(opener)) 46 { 47 printf("Could not obtain opener for file.\n"); 48 return NULL; 49 } 50 51 /* Invoke the open method to receive the reference. */ 52 53 return client_open_using(filename, flags, opener); 54 } 55 else 56 { 57 return client_open(filename, flags); 58 } 59 } 60 61 62 63 /* Open files in the directory given by filename. */ 64 65 static void open_files(const char *filename, bool have_uid, sys_uid_t uid) 66 { 67 char *buffer = (char *) calloc(strlen(filename) + 10, sizeof(char)); 68 69 if (buffer == NULL) 70 { 71 printf("Could not allocate buffer.\n"); 72 return; 73 } 74 75 for (int i = 0; i < 10; i++) 76 { 77 sprintf(buffer, "%s/file%02d", filename, i); 78 79 open_object(buffer, O_CREAT | O_RDWR, have_uid, uid); 80 sleep(1); 81 } 82 } 83 84 /* Monitor files in the given directory. */ 85 86 static void monitor_files(file_t *directory) 87 { 88 long err = client_subscribe(directory, NOTIFY_FILE_OPENED, client_notifier_task()); 89 90 if (err) 91 { 92 printf("Could not subscribe to events on directory.\n"); 93 return; 94 } 95 96 while (1) 97 { 98 /* Wait for notification of content. */ 99 100 err = client_wait_file(directory, client_notifier_task()); 101 102 if (err) 103 { 104 printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); 105 return; 106 } 107 108 printf("Notified with conditions:%s\n", directory->notifications & NOTIFY_FILE_OPENED ? " file opened" : ""); 109 } 110 } 111 112 113 114 int main(int argc, char *argv[]) 115 { 116 if (argc < 2) 117 { 118 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 119 return 1; 120 } 121 122 char *filename = argv[1]; 123 bool have_uid = (argc > 2) && strlen(argv[2]); 124 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 125 126 printf("Opening %s...\n", filename); 127 128 file_t *directory = open_object(filename, O_DIRECTORY, have_uid, uid); 129 130 if (directory == NULL) 131 { 132 printf("Could not open directory.\n"); 133 return 1; 134 } 135 136 /* Schedule threads. */ 137 138 std::thread *activities[2]; 139 140 activities[0] = new std::thread(open_files, filename, have_uid, uid); 141 activities[1] = new std::thread(monitor_files, directory); 142 143 for (int i = 0; i < 2; i++) 144 activities[i]->join(); 145 146 return 0; 147 } 148 149 // vim: tabstop=2 expandtab shiftwidth=2