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 <stdio.h> 26 #include <string.h> 27 #include <stdlib.h> 28 29 #include <fsclient/client.h> 30 #include <fsclient/file.h> 31 #include <mem/memory_utils.h> 32 33 34 35 const unsigned int PIPE_PAGES = 2; 36 37 int main(void) 38 { 39 /* Obtain access to the filesystem. */ 40 41 l4_cap_idx_t server = l4re_env_get_cap("server"); 42 43 /* Invoke the open method to receive the file reference. */ 44 45 file_t reader, writer; 46 long err = pipe_open(page(PIPE_PAGES), &reader, &writer, server); 47 48 if (err) 49 { 50 printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); 51 return 1; 52 } 53 54 /* Use the writer to fill the pipe with data. */ 55 56 offset_t size = 600; 57 char buffer[size]; 58 int region = 0; 59 60 for (int loop = 0; loop < 3; loop++) 61 { 62 while (1) 63 { 64 printf("Writing %ld to pipe...\n", size); 65 66 memset(buffer, (int) 'a' + region, size); 67 68 offset_t nwritten = client_write(&writer, buffer, size); 69 70 printf("Written %ld to pipe...\n", nwritten); 71 72 for (offset_t i = 0; i < nwritten; i += 60) 73 { 74 fwrite(buffer + i, sizeof(char), nwritten - i > 60 ? 60 : nwritten - i, stdout); 75 fputs("\n", stdout); 76 } 77 78 if (!nwritten) 79 break; 80 81 region++; 82 83 if (region == 26) 84 region = 0; 85 } 86 87 /* Use the reader to obtain data from the pipe. */ 88 89 offset_t nread; 90 91 do 92 { 93 printf("Reading %ld from pipe...\n", size); 94 95 nread = client_read(&reader, buffer, size); 96 97 printf("Read %ld from pipe...\n", nread); 98 99 for (offset_t i = 0; i < nread; i += 60) 100 { 101 fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout); 102 fputs("\n", stdout); 103 } 104 } 105 while (nread); 106 } 107 108 printf("Data shown.\n"); 109 110 return 0; 111 } 112 113 // vim: tabstop=2 expandtab shiftwidth=2