paul@600 | 1 | /* |
paul@600 | 2 | * Run a program residing in the filesystem. |
paul@600 | 3 | * |
paul@600 | 4 | * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@600 | 5 | * |
paul@600 | 6 | * This program is free software; you can redistribute it and/or |
paul@600 | 7 | * modify it under the terms of the GNU General Public License as |
paul@600 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@600 | 9 | * the License, or (at your option) any later version. |
paul@600 | 10 | * |
paul@600 | 11 | * This program is distributed in the hope that it will be useful, |
paul@600 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@600 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@600 | 14 | * GNU General Public License for more details. |
paul@600 | 15 | * |
paul@600 | 16 | * You should have received a copy of the GNU General Public License |
paul@600 | 17 | * along with this program; if not, write to the Free Software |
paul@600 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@600 | 19 | * Boston, MA 02110-1301, USA |
paul@600 | 20 | */ |
paul@600 | 21 | |
paul@600 | 22 | #include <l4/sys/err.h> |
paul@600 | 23 | |
paul@600 | 24 | #include <fsclient/client.h> |
paul@600 | 25 | #include <fsclient/process.h> |
paul@600 | 26 | #include <systypes/fcntl.h> |
paul@600 | 27 | #include <systypes/format.h> |
paul@600 | 28 | |
paul@600 | 29 | #include <stdio.h> |
paul@600 | 30 | |
paul@600 | 31 | #include "ops.h" |
paul@600 | 32 | |
paul@600 | 33 | |
paul@600 | 34 | |
paul@600 | 35 | /* Transfer size for communication. */ |
paul@600 | 36 | |
paul@600 | 37 | static const offset_t TO_TRANSFER = 1024; |
paul@600 | 38 | |
paul@600 | 39 | |
paul@600 | 40 | |
paul@600 | 41 | /* Run the given program. */ |
paul@600 | 42 | |
paul@600 | 43 | int run_program(int argc, char *argv[]) |
paul@600 | 44 | { |
paul@600 | 45 | char buffer[TO_TRANSFER]; |
paul@600 | 46 | process_t *process; |
paul@600 | 47 | file_t *reader, *writer; |
paul@600 | 48 | notify_flags_t flags; |
paul@600 | 49 | notify_values_t values; |
paul@600 | 50 | long err; |
paul@600 | 51 | |
paul@600 | 52 | /* Create a pipe for process output. */ |
paul@600 | 53 | |
paul@600 | 54 | err = client_pipe(&reader, &writer, 0); |
paul@600 | 55 | |
paul@600 | 56 | if (err) |
paul@600 | 57 | { |
paul@600 | 58 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@600 | 59 | return 1; |
paul@600 | 60 | } |
paul@600 | 61 | |
paul@600 | 62 | /* Start the process. */ |
paul@600 | 63 | |
paul@600 | 64 | err = process_spawn(argc, (const char **) argv, writer, &process); |
paul@600 | 65 | |
paul@600 | 66 | if (err) |
paul@600 | 67 | { |
paul@600 | 68 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@600 | 69 | return 1; |
paul@600 | 70 | } |
paul@600 | 71 | |
paul@600 | 72 | printf("Finished program initiation.\n"); |
paul@600 | 73 | |
paul@600 | 74 | /* Release the writing end of the pipe. */ |
paul@600 | 75 | |
paul@600 | 76 | client_close(writer); |
paul@600 | 77 | |
paul@600 | 78 | /* Read until the pipe yields no more data. */ |
paul@600 | 79 | |
paul@600 | 80 | while (1) |
paul@600 | 81 | { |
paul@600 | 82 | offset_t nread = client_read(reader, buffer, TO_TRANSFER); |
paul@600 | 83 | |
paul@600 | 84 | if (nread) |
paul@600 | 85 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@600 | 86 | else |
paul@600 | 87 | break; |
paul@600 | 88 | } |
paul@600 | 89 | |
paul@600 | 90 | /* Close the pipe and obtain the process state. */ |
paul@600 | 91 | |
paul@600 | 92 | client_close(reader); |
paul@600 | 93 | |
paul@600 | 94 | err = process_wait(process, &flags, &values); |
paul@600 | 95 | |
paul@600 | 96 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@600 | 97 | |
paul@600 | 98 | if (err) |
paul@600 | 99 | return 1; |
paul@600 | 100 | |
paul@600 | 101 | return 0; |
paul@600 | 102 | } |
paul@600 | 103 | |
paul@600 | 104 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@600 | 105 | */ |