1 /* 2 * A test of executing code in new tasks and threads. 3 * 4 * Copyright (C) 2022, 2023, 2024 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/sys/err.h> 23 24 #include <fsclient/client.h> 25 #include <fsclient/process.h> 26 #include <systypes/fcntl.h> 27 #include <systypes/format.h> 28 29 #include <stdio.h> 30 31 32 33 /* Transfer size for communication. */ 34 35 const offset_t TO_TRANSFER = 1024; 36 37 38 39 /* Test process initiation and interaction. */ 40 41 static long test_process(int argc, const char *argv[]) 42 { 43 process_t *process; 44 file_t *input_reader, *input_writer; 45 file_t *output_reader, *output_writer; 46 long err; 47 48 /* Create pipes for process input and output. */ 49 50 err = client_pipe(&input_reader, &input_writer, 0); 51 52 if (err) 53 { 54 printf("Could not obtain pipe for input: %s\n", l4sys_errtostr(err)); 55 return err; 56 } 57 58 err = client_pipe(&output_reader, &output_writer, 0); 59 60 if (err) 61 { 62 printf("Could not obtain pipe for output: %s\n", l4sys_errtostr(err)); 63 return err; 64 } 65 66 /* Start the process. */ 67 68 err = process_spawn(argc, argv, input_reader, output_writer, &process); 69 70 if (err) 71 { 72 printf("Could not start process: %s\n", l4sys_errtostr(err)); 73 return err; 74 } 75 76 printf("Finished program initiation.\n"); 77 78 /* Release the relinquished ends of the pipes. */ 79 80 client_close(input_reader); 81 client_close(output_writer); 82 83 /* Read until the pipe yields no more data. */ 84 85 char buffer[TO_TRANSFER]; 86 87 while (1) 88 { 89 offset_t nread = client_read(output_reader, buffer, TO_TRANSFER); 90 91 if (nread) 92 fwrite(buffer, sizeof(char), nread, stdout); 93 else 94 break; 95 } 96 97 /* Close the pipes and obtain the process state. */ 98 99 client_close(input_writer); 100 client_close(output_reader); 101 102 notify_flags_t flags; 103 notify_values_t values; 104 105 err = process_wait(process, &flags, &values); 106 107 printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); 108 109 return err; 110 } 111 112 113 114 int main(int argc, char *argv[]) 115 { 116 long err; 117 118 if (argc < 2) 119 { 120 printf("Need the program to run.\n"); 121 return 1; 122 } 123 124 /* Start a process for a non-existent program. */ 125 126 printf("Start non-existent program...\n"); 127 128 const char *bad_argv[] = {"non_existent_program"}; 129 130 err = test_process(1, bad_argv); 131 132 if (!err) 133 { 134 printf("Non-existent program was apparently successfully started.\n"); 135 return 1; 136 } 137 138 printf("Non-existent program result: %s\n", l4sys_errtostr(err)); 139 140 /* Start a process for the given program. */ 141 142 printf("Start process...\n"); 143 144 err = test_process(argc - 1, (const char **) argv + 1); 145 146 if (err) 147 return 1; 148 149 printf("End of test.\n"); 150 return 0; 151 } 152 153 /* vim: tabstop=2 expandtab shiftwidth=2 154 */