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 int main(void) 55 { 56 /* Obtain access to the filesystem. */ 57 58 l4_cap_idx_t server = l4re_env_get_cap("server"); 59 60 /* Invoke the open method to receive the file reference. */ 61 62 file_t reader, writer; 63 long err = pipe_open(&reader, &writer, server); 64 65 if (err) 66 { 67 printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); 68 return 1; 69 } 70 71 /* Use the writer to fill the pipe with data. */ 72 73 for (int region = 0; region < 3; region++) 74 { 75 printf("Write %ld to pipe at %p...\n", file_span(&writer), writer.memory); 76 77 memset(writer.memory, (int) 'a' + region, file_span(&writer)); 78 79 err = pipe_written(&writer, file_span(&writer)); 80 81 if (err) 82 { 83 printf("Written data error: %s\n", l4sys_errtostr(err)); 84 return 1; 85 } 86 87 show(&writer, page(1), 60); 88 89 err = pipe_next(&writer); 90 91 if (err) 92 { 93 printf("Region traversal error: %s\n", l4sys_errtostr(err)); 94 return 1; 95 } 96 } 97 98 /* Use the reader to obtain data from the pipe. */ 99 100 err = pipe_current(&reader); 101 102 while (!err) 103 { 104 printf("show...\n"); 105 show(&reader, page(1), 60); 106 err = pipe_next(&reader); 107 } 108 109 if (err) 110 printf("Reading termination condition: %s\n", l4sys_errtostr(err)); 111 112 printf("Data shown.\n"); 113 114 return 0; 115 } 116 117 // vim: tabstop=2 expandtab shiftwidth=2