1 /* 2 * A test of executing code in new tasks and threads. 3 * 4 * Copyright (C) 2022, 2023 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 *reader, *writer; 45 long err; 46 47 /* Create a pipe for process output. */ 48 49 err = client_pipe(&reader, &writer, 0); 50 51 if (err) 52 { 53 printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); 54 return err; 55 } 56 57 /* Start the process. */ 58 59 err = process_spawn(argc, argv, writer, &process); 60 61 if (err) 62 { 63 printf("Could not start process: %s\n", l4sys_errtostr(err)); 64 return err; 65 } 66 67 printf("Finished program initiation.\n"); 68 69 /* Release the writing end of the pipe. */ 70 71 client_close(writer); 72 73 /* Read until the pipe yields no more data. */ 74 75 char buffer[TO_TRANSFER]; 76 77 while (1) 78 { 79 offset_t nread = client_read(reader, buffer, TO_TRANSFER); 80 81 if (nread) 82 fwrite(buffer, sizeof(char), nread, stdout); 83 else 84 break; 85 } 86 87 /* Close the pipe and obtain the process state. */ 88 89 client_close(reader); 90 91 notify_flags_t flags; 92 notify_values_t values; 93 94 err = process_wait(process, &flags, &values); 95 96 printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); 97 98 return err; 99 } 100 101 102 103 int main(int argc, char *argv[]) 104 { 105 long err; 106 107 if (argc < 2) 108 { 109 printf("Need the program to run.\n"); 110 return 1; 111 } 112 113 /* Start a process for a non-existent program. */ 114 115 printf("Start non-existent program...\n"); 116 117 const char *bad_argv[] = {"non_existent_program"}; 118 119 err = test_process(1, bad_argv); 120 121 if (!err) 122 { 123 printf("Non-existent program was apparently successfully started.\n"); 124 return 1; 125 } 126 127 printf("Non-existent program result: %s\n", l4sys_errtostr(err)); 128 129 /* Start a process for the given program. */ 130 131 printf("Start process...\n"); 132 133 err = test_process(argc - 1, (const char **) argv + 1); 134 135 if (err) 136 return 1; 137 138 printf("End of test.\n"); 139 return 0; 140 } 141 142 /* vim: tabstop=2 expandtab shiftwidth=2 143 */