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 "file.h" 30 #include "memory_utils.h" 31 32 33 34 static void show(file_t *file, offset_t step, offset_t sample) 35 { 36 /* Allocate a buffer for sampling from the file. */ 37 38 char buf[sample + 1]; 39 40 for (offset_t offset = 0; offset < file_populated_span(file); offset += step) 41 { 42 printf("show %ld of %ld...\n", offset, file_populated_span(file)); 43 44 unsigned long remaining = file_populated_span(file) - offset; 45 unsigned long sample_remaining = remaining < sample ? remaining : sample; 46 47 printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset)); 48 strncpy(buf, (file->memory + offset), sample_remaining); 49 buf[sample_remaining] = '\0'; 50 printf("%s\n", buf); 51 } 52 } 53 54 55 56 const unsigned int PIPE_PAGES = 2; 57 58 int main(void) 59 { 60 /* Obtain access to the filesystem. */ 61 62 l4_cap_idx_t server = l4re_env_get_cap("server"); 63 64 /* Invoke the open method to receive the file reference. */ 65 66 file_t reader, writer; 67 long err = pipe_open(page(PIPE_PAGES), &reader, &writer, server); 68 69 if (err) 70 { 71 printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); 72 return 1; 73 } 74 75 /* Use the writer to fill the pipe with data. */ 76 77 for (int region = 0; region < 3; region++) 78 { 79 printf("Write %ld to pipe at %p...\n", file_span(&writer), writer.memory); 80 81 memset(writer.memory, (int) 'a' + region, file_span(&writer)); 82 83 err = pipe_written(&writer, file_span(&writer)); 84 85 if (err) 86 { 87 printf("Written data error: %s\n", l4sys_errtostr(err)); 88 return 1; 89 } 90 91 show(&writer, page(1), 60); 92 93 err = pipe_next(&writer); 94 95 if (err) 96 { 97 printf("Region traversal error: %s\n", l4sys_errtostr(err)); 98 return 1; 99 } 100 } 101 102 /* Use the reader to obtain data from the pipe. */ 103 104 err = pipe_current(&reader); 105 106 while (!err) 107 { 108 printf("show...\n"); 109 show(&reader, page(1), 60); 110 err = pipe_next(&reader); 111 } 112 113 if (err) 114 printf("Reading termination condition: %s\n", l4sys_errtostr(err)); 115 116 printf("Data shown.\n"); 117 118 return 0; 119 } 120 121 // vim: tabstop=2 expandtab shiftwidth=2