paul@600 | 1 | /* |
paul@606 | 2 | * Run programs 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@606 | 30 | #include <stdlib.h> |
paul@606 | 31 | #include <string.h> |
paul@600 | 32 | |
paul@600 | 33 | #include "ops.h" |
paul@600 | 34 | |
paul@600 | 35 | |
paul@600 | 36 | |
paul@600 | 37 | /* Transfer size for communication. */ |
paul@600 | 38 | |
paul@600 | 39 | static const offset_t TO_TRANSFER = 1024; |
paul@600 | 40 | |
paul@600 | 41 | |
paul@600 | 42 | |
paul@606 | 43 | /* Initiated programs (jobs). |
paul@606 | 44 | NOTE: Arbitrary limit. */ |
paul@606 | 45 | |
paul@606 | 46 | #define NUMBER_OF_JOBS 32 |
paul@606 | 47 | |
paul@606 | 48 | static file_t *readers[NUMBER_OF_JOBS] = {NULL}; |
paul@606 | 49 | static process_t *processes[NUMBER_OF_JOBS] = {NULL}; |
paul@606 | 50 | static const char *programs[NUMBER_OF_JOBS] = {NULL}; |
paul@606 | 51 | static int next_job = 0; |
paul@606 | 52 | |
paul@606 | 53 | |
paul@606 | 54 | |
paul@606 | 55 | /* Wait for a program to finish, showing its output. */ |
paul@606 | 56 | |
paul@606 | 57 | static int _wait_program(file_t *reader, process_t *process) |
paul@606 | 58 | { |
paul@606 | 59 | char buffer[TO_TRANSFER]; |
paul@606 | 60 | notify_flags_t flags; |
paul@606 | 61 | notify_values_t values; |
paul@606 | 62 | long err; |
paul@606 | 63 | |
paul@606 | 64 | /* Read until the pipe yields no more data. */ |
paul@606 | 65 | |
paul@606 | 66 | while (1) |
paul@606 | 67 | { |
paul@606 | 68 | offset_t nread = client_read(reader, buffer, TO_TRANSFER); |
paul@606 | 69 | |
paul@606 | 70 | if (nread) |
paul@606 | 71 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@606 | 72 | else |
paul@606 | 73 | break; |
paul@606 | 74 | } |
paul@606 | 75 | |
paul@606 | 76 | /* Close the pipe and obtain the process state. */ |
paul@606 | 77 | |
paul@606 | 78 | client_close(reader); |
paul@606 | 79 | |
paul@606 | 80 | err = process_wait(process, &flags, &values); |
paul@606 | 81 | |
paul@606 | 82 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@606 | 83 | |
paul@606 | 84 | if (err) |
paul@606 | 85 | return -1; |
paul@606 | 86 | |
paul@606 | 87 | return values.val; |
paul@606 | 88 | } |
paul@606 | 89 | |
paul@600 | 90 | /* Run the given program. */ |
paul@600 | 91 | |
paul@600 | 92 | int run_program(int argc, char *argv[]) |
paul@600 | 93 | { |
paul@600 | 94 | process_t *process; |
paul@600 | 95 | file_t *reader, *writer; |
paul@606 | 96 | int last_job; |
paul@600 | 97 | long err; |
paul@600 | 98 | |
paul@600 | 99 | /* Create a pipe for process output. */ |
paul@600 | 100 | |
paul@600 | 101 | err = client_pipe(&reader, &writer, 0); |
paul@600 | 102 | |
paul@600 | 103 | if (err) |
paul@600 | 104 | { |
paul@600 | 105 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@602 | 106 | return -1; |
paul@600 | 107 | } |
paul@600 | 108 | |
paul@600 | 109 | /* Start the process. */ |
paul@600 | 110 | |
paul@600 | 111 | err = process_spawn(argc, (const char **) argv, writer, &process); |
paul@600 | 112 | |
paul@600 | 113 | if (err) |
paul@600 | 114 | { |
paul@600 | 115 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@602 | 116 | return -1; |
paul@600 | 117 | } |
paul@600 | 118 | |
paul@600 | 119 | printf("Finished program initiation.\n"); |
paul@600 | 120 | |
paul@600 | 121 | /* Release the writing end of the pipe. */ |
paul@600 | 122 | |
paul@600 | 123 | client_close(writer); |
paul@600 | 124 | |
paul@606 | 125 | /* Record the output stream. */ |
paul@600 | 126 | |
paul@606 | 127 | last_job = next_job; |
paul@606 | 128 | |
paul@606 | 129 | while (readers[next_job] != NULL) |
paul@600 | 130 | { |
paul@606 | 131 | next_job++; |
paul@606 | 132 | |
paul@606 | 133 | if (next_job >= NUMBER_OF_JOBS) |
paul@606 | 134 | next_job = 0; |
paul@600 | 135 | |
paul@606 | 136 | /* Wait for the process to complete if no more job slots are available. */ |
paul@606 | 137 | |
paul@606 | 138 | if (next_job == last_job) |
paul@606 | 139 | return _wait_program(reader, process); |
paul@600 | 140 | } |
paul@600 | 141 | |
paul@606 | 142 | readers[next_job] = reader; |
paul@606 | 143 | processes[next_job] = process; |
paul@606 | 144 | programs[next_job] = strdup(argv[0]); |
paul@606 | 145 | |
paul@606 | 146 | return 0; |
paul@606 | 147 | } |
paul@600 | 148 | |
paul@606 | 149 | /* Show initiated programs. */ |
paul@606 | 150 | |
paul@606 | 151 | int show_programs(int argc, char *argv[]) |
paul@606 | 152 | { |
paul@606 | 153 | (void) argc; (void) argv; |
paul@606 | 154 | |
paul@606 | 155 | int job_number; |
paul@600 | 156 | |
paul@606 | 157 | for (job_number = 0; job_number < NUMBER_OF_JOBS; job_number++) |
paul@606 | 158 | { |
paul@606 | 159 | if (readers[job_number] != NULL) |
paul@606 | 160 | printf("[%d] %s\n", job_number, programs[job_number]); |
paul@606 | 161 | } |
paul@606 | 162 | |
paul@606 | 163 | return 0; |
paul@606 | 164 | } |
paul@600 | 165 | |
paul@606 | 166 | /* Wait for the indicated program to finish, showing its output. */ |
paul@600 | 167 | |
paul@606 | 168 | int wait_program(int argc, char *argv[]) |
paul@606 | 169 | { |
paul@606 | 170 | int job_number; |
paul@606 | 171 | int exitcode; |
paul@606 | 172 | |
paul@606 | 173 | if (argc < 1) |
paul@602 | 174 | return -1; |
paul@600 | 175 | |
paul@606 | 176 | job_number = atoi(argv[0]); |
paul@606 | 177 | |
paul@606 | 178 | if (readers[job_number] == NULL) |
paul@606 | 179 | return -1; |
paul@606 | 180 | |
paul@606 | 181 | exitcode = _wait_program(readers[job_number], processes[job_number]); |
paul@606 | 182 | |
paul@606 | 183 | readers[job_number] = NULL; |
paul@606 | 184 | processes[job_number] = NULL; |
paul@606 | 185 | |
paul@606 | 186 | free(programs[job_number]); |
paul@606 | 187 | programs[job_number] = NULL; |
paul@606 | 188 | |
paul@606 | 189 | return exitcode; |
paul@600 | 190 | } |
paul@600 | 191 | |
paul@600 | 192 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@600 | 193 | */ |