1 /* 2 * Run a program residing in the filesystem. 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 #include "ops.h" 32 33 34 35 /* Transfer size for communication. */ 36 37 static const offset_t TO_TRANSFER = 1024; 38 39 40 41 /* Run the given program. */ 42 43 int run_program(int argc, char *argv[]) 44 { 45 char buffer[TO_TRANSFER]; 46 process_t *process; 47 file_t *reader, *writer; 48 notify_flags_t flags; 49 notify_values_t values; 50 long err; 51 52 /* Create a pipe for process output. */ 53 54 err = client_pipe(&reader, &writer, 0); 55 56 if (err) 57 { 58 printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); 59 return -1; 60 } 61 62 /* Start the process. */ 63 64 err = process_spawn(argc, (const char **) argv, writer, &process); 65 66 if (err) 67 { 68 printf("Could not start process: %s\n", l4sys_errtostr(err)); 69 return -1; 70 } 71 72 printf("Finished program initiation.\n"); 73 74 /* Release the writing end of the pipe. */ 75 76 client_close(writer); 77 78 /* Read until the pipe yields no more data. */ 79 80 while (1) 81 { 82 offset_t nread = client_read(reader, buffer, TO_TRANSFER); 83 84 if (nread) 85 fwrite(buffer, sizeof(char), nread, stdout); 86 else 87 break; 88 } 89 90 /* Close the pipe and obtain the process state. */ 91 92 client_close(reader); 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 if (err) 99 return -1; 100 101 return values.val; 102 } 103 104 /* vim: tabstop=2 expandtab shiftwidth=2 105 */