1 /* 2 * Test pipe 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> /* sleep */ 31 32 #include <fsclient/client.h> 33 #include <fsclient/file.h> 34 #include <mem/memory_utils.h> 35 36 37 38 /* Minimum pipe region size in pages. */ 39 40 const unsigned int PIPE_PAGES = 2; 41 42 /* Use the writer to fill the pipe with data. */ 43 44 static void write_pipe(file_t *writer, int number) 45 { 46 offset_t size = 600; 47 char buffer[size]; 48 long err; 49 50 /* Make writers blocking to permit synchronisation. */ 51 52 if ((err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE))) 53 { 54 printf("Could not set pipe #%d as blocking: %s\n", number, l4sys_errtostr(err)); 55 return; 56 } 57 58 for (int loop = 0; loop < 3; loop++) 59 { 60 for (int region = 0; region < 26; region++) 61 { 62 memset(buffer, (int) 'a' + region, size); 63 64 offset_t nwritten = client_write(writer, buffer, size); 65 66 printf("Written %ld/%ld in #%d of %d/%d to pipe #%d...\n", nwritten, size, region, loop, 2, number); 67 } 68 69 sleep(1); 70 } 71 72 /* Flush to make the final output available. */ 73 74 client_flush(writer); 75 76 file_close(writer); 77 } 78 79 /* Use the reader to obtain data from the pipe. */ 80 81 static void read_pipes(file_t *reader1, file_t *reader2) 82 { 83 offset_t size = 600, totals[] = {0, 0}; 84 unsigned int active = 2; 85 long err; 86 file_t *reader; 87 88 /* Register the readers for notification. */ 89 90 // NOTE: Use the flags to detect initial conditions! 91 92 if ((err = client_subscribe(reader1, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED)) || 93 (err = client_subscribe(reader2, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED))) 94 { 95 printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); 96 return; 97 } 98 99 while (1) 100 { 101 char buffer[size]; 102 offset_t nread; 103 104 /* Wait for notification of content. */ 105 106 err = client_wait_files(&reader); 107 108 if (err) 109 { 110 printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); 111 return; 112 } 113 114 if ((reader != reader1) && (reader != reader2)) 115 { 116 printf("Spurious notification received for %p versus %p, %p.\n", reader, reader1, reader2); 117 continue; 118 } 119 120 int p = reader == reader1 ? 0 : 1; 121 122 printf("Pipe #%d notified with conditions:%s%s\n", p, reader->notifications & NOTIFY_PEER_CLOSED ? " closed" : "", 123 reader->notifications & NOTIFY_CONTENT_AVAILABLE ? " content" : ""); 124 125 if (reader->notifications & NOTIFY_PEER_CLOSED) 126 { 127 active--; 128 if (!active) 129 break; 130 } 131 132 nread = client_read(reader, buffer, size); 133 134 while (nread) 135 { 136 totals[p] += nread; 137 138 printf("Read %ld/%ld, total %ld, first %c, last %c, from pipe #%d...\n", nread, size, totals[p], *buffer, *(buffer + nread - 1), p + 1); 139 #if 0 140 for (offset_t i = 0; i < nread; i += 60) 141 { 142 fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout); 143 fputs("\n", stdout); 144 } 145 #endif 146 nread = client_read(reader, buffer, size); 147 } 148 } 149 150 printf("Data shown.\n"); 151 } 152 153 int main(void) 154 { 155 /* Obtain access to the filesystem. */ 156 157 l4_cap_idx_t server = l4re_env_get_cap("server"); 158 159 /* Invoke the open method to receive the file reference. */ 160 161 file_t reader1, reader2, writer1, writer2; 162 long err = pipe_open(page(PIPE_PAGES), &reader1, &writer1, server) || 163 pipe_open(page(PIPE_PAGES), &reader2, &writer2, server); 164 165 if (err) 166 { 167 printf("Could not obtain pipes: %s\n", l4sys_errtostr(err)); 168 return 1; 169 } 170 171 /* Schedule reader and writer threads. */ 172 173 std::thread *activities[3]; 174 175 activities[0] = new std::thread(read_pipes, &reader1, &reader2); 176 activities[1] = new std::thread(write_pipe, &writer1, 1); 177 activities[2] = new std::thread(write_pipe, &writer2, 2); 178 179 for (int i = 0; i < 3; i++) 180 activities[i]->join(); 181 } 182 183 // vim: tabstop=2 expandtab shiftwidth=2