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@615 | 50 | static char *programs[NUMBER_OF_JOBS] = {NULL}; |
paul@606 | 51 | static int next_job = 0; |
paul@606 | 52 | |
paul@606 | 53 | |
paul@606 | 54 | |
paul@615 | 55 | /* Show output from a program. */ |
paul@615 | 56 | |
paul@615 | 57 | static void _show_output(file_t *reader) |
paul@615 | 58 | { |
paul@615 | 59 | char buffer[TO_TRANSFER]; |
paul@615 | 60 | offset_t nread; |
paul@615 | 61 | |
paul@615 | 62 | while ((nread = client_read(reader, buffer, TO_TRANSFER))) |
paul@615 | 63 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@615 | 64 | } |
paul@615 | 65 | |
paul@606 | 66 | /* Wait for a program to finish, showing its output. */ |
paul@606 | 67 | |
paul@606 | 68 | static int _wait_program(file_t *reader, process_t *process) |
paul@606 | 69 | { |
paul@615 | 70 | notifier_t *notifier = client_notifier_local(); |
paul@615 | 71 | notifiable_t *notifiable; |
paul@615 | 72 | int exitcode; |
paul@606 | 73 | long err; |
paul@606 | 74 | |
paul@615 | 75 | /* Subscribe to reader and process notifications. */ |
paul@615 | 76 | |
paul@615 | 77 | if (reader != NULL) |
paul@615 | 78 | { |
paul@615 | 79 | err = client_subscribe(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED, notifier); |
paul@615 | 80 | |
paul@615 | 81 | if (err) |
paul@615 | 82 | { |
paul@615 | 83 | printf("Could not subscribe to pipe notifications: %s\n", l4sys_errtostr(err)); |
paul@615 | 84 | client_notifier_close(notifier); |
paul@615 | 85 | return -1; |
paul@615 | 86 | } |
paul@615 | 87 | } |
paul@615 | 88 | |
paul@615 | 89 | err = notify_subscribe(process_notifiable(process), NOTIFY_TASK_ALL, notifier); |
paul@615 | 90 | |
paul@615 | 91 | if (err) |
paul@615 | 92 | { |
paul@615 | 93 | printf("Could not subscribe to process notifications: %s\n", l4sys_errtostr(err)); |
paul@615 | 94 | client_unsubscribe(reader, notifier); |
paul@615 | 95 | client_notifier_close(notifier); |
paul@615 | 96 | return -1; |
paul@615 | 97 | } |
paul@615 | 98 | |
paul@615 | 99 | /* Read from and write to pipes until the program terminates. */ |
paul@606 | 100 | |
paul@606 | 101 | while (1) |
paul@606 | 102 | { |
paul@615 | 103 | err = notify_wait_many(¬ifiable, notifier); |
paul@615 | 104 | |
paul@615 | 105 | if (err) |
paul@615 | 106 | { |
paul@615 | 107 | printf("Notification error: %s\n", l4sys_errtostr(err)); |
paul@615 | 108 | |
paul@615 | 109 | if (reader != NULL) |
paul@615 | 110 | client_unsubscribe(reader, notifier); |
paul@615 | 111 | |
paul@615 | 112 | notify_unsubscribe(process_notifiable(process), notifier); |
paul@615 | 113 | client_notifier_close(notifier); |
paul@615 | 114 | return -1; |
paul@615 | 115 | } |
paul@606 | 116 | |
paul@615 | 117 | /* Handle input from the reader. */ |
paul@615 | 118 | |
paul@615 | 119 | if ((reader != NULL) && (file_notifications(reader) & (NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED))) |
paul@615 | 120 | _show_output(reader); |
paul@615 | 121 | |
paul@615 | 122 | /* Handle process termination, obtaining the process state. */ |
paul@615 | 123 | |
paul@615 | 124 | if (process_terminated(notifiable)) |
paul@615 | 125 | { |
paul@615 | 126 | if (reader != NULL) |
paul@615 | 127 | _show_output(reader); |
paul@615 | 128 | |
paul@615 | 129 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", |
paul@615 | 130 | notifiable->notifications, notifiable->values.sig, notifiable->values.val); |
paul@606 | 131 | break; |
paul@615 | 132 | } |
paul@606 | 133 | } |
paul@606 | 134 | |
paul@615 | 135 | exitcode = notifiable->values.val; |
paul@606 | 136 | |
paul@615 | 137 | if (reader != NULL) |
paul@615 | 138 | client_unsubscribe(reader, notifier); |
paul@606 | 139 | |
paul@615 | 140 | notify_unsubscribe(process_notifiable(process), notifier); |
paul@606 | 141 | |
paul@615 | 142 | /* Close the process and pipe. */ |
paul@615 | 143 | |
paul@615 | 144 | client_notifier_close(notifier); |
paul@615 | 145 | process_free(process); |
paul@606 | 146 | |
paul@615 | 147 | if (reader != NULL) |
paul@615 | 148 | client_close(reader); |
paul@606 | 149 | |
paul@615 | 150 | return exitcode; |
paul@606 | 151 | } |
paul@606 | 152 | |
paul@600 | 153 | /* Run the given program. */ |
paul@600 | 154 | |
paul@615 | 155 | static int _run_program(int argc, char *argv[], file_t *input_reader) |
paul@600 | 156 | { |
paul@600 | 157 | process_t *process; |
paul@615 | 158 | file_t *output_reader, *output_writer; |
paul@606 | 159 | int last_job; |
paul@600 | 160 | long err; |
paul@600 | 161 | |
paul@600 | 162 | /* Create a pipe for process output. */ |
paul@600 | 163 | |
paul@615 | 164 | err = client_pipe(&output_reader, &output_writer, O_NONBLOCK); |
paul@600 | 165 | |
paul@600 | 166 | if (err) |
paul@600 | 167 | { |
paul@615 | 168 | printf("Could not obtain pipe for output: %s\n", l4sys_errtostr(err)); |
paul@602 | 169 | return -1; |
paul@600 | 170 | } |
paul@600 | 171 | |
paul@600 | 172 | /* Start the process. */ |
paul@600 | 173 | |
paul@615 | 174 | err = process_spawn(argc, (const char **) argv, input_reader, output_writer, &process); |
paul@600 | 175 | |
paul@600 | 176 | if (err) |
paul@600 | 177 | { |
paul@600 | 178 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@602 | 179 | return -1; |
paul@600 | 180 | } |
paul@600 | 181 | |
paul@600 | 182 | printf("Finished program initiation.\n"); |
paul@600 | 183 | |
paul@615 | 184 | /* Release the relinquished end of the pipe. */ |
paul@600 | 185 | |
paul@615 | 186 | client_close(output_writer); |
paul@600 | 187 | |
paul@606 | 188 | /* Record the output stream. */ |
paul@600 | 189 | |
paul@606 | 190 | last_job = next_job; |
paul@606 | 191 | |
paul@615 | 192 | while (processes[next_job] != NULL) |
paul@600 | 193 | { |
paul@606 | 194 | next_job++; |
paul@606 | 195 | |
paul@606 | 196 | if (next_job >= NUMBER_OF_JOBS) |
paul@606 | 197 | next_job = 0; |
paul@600 | 198 | |
paul@606 | 199 | /* Wait for the process to complete if no more job slots are available. */ |
paul@606 | 200 | |
paul@606 | 201 | if (next_job == last_job) |
paul@615 | 202 | return _wait_program(output_reader, process); |
paul@600 | 203 | } |
paul@600 | 204 | |
paul@615 | 205 | readers[next_job] = output_reader; |
paul@606 | 206 | processes[next_job] = process; |
paul@606 | 207 | programs[next_job] = strdup(argv[0]); |
paul@606 | 208 | |
paul@606 | 209 | return 0; |
paul@606 | 210 | } |
paul@600 | 211 | |
paul@615 | 212 | |
paul@615 | 213 | |
paul@615 | 214 | /* Run the given program, providing input from a file. */ |
paul@615 | 215 | |
paul@615 | 216 | int file_to_program(int argc, char *argv[]) |
paul@615 | 217 | { |
paul@615 | 218 | file_t *reader; |
paul@615 | 219 | |
paul@615 | 220 | /* Obtain a file reader and run the program with this as its input reader. */ |
paul@615 | 221 | |
paul@615 | 222 | if (argc < 1) |
paul@615 | 223 | return -1; |
paul@615 | 224 | |
paul@615 | 225 | reader = client_open(argv[0], O_RDONLY); |
paul@615 | 226 | |
paul@615 | 227 | if (!client_opened(reader)) |
paul@615 | 228 | { |
paul@615 | 229 | printf("Could not open file: %s\n", argv[0]); |
paul@615 | 230 | return -1; |
paul@615 | 231 | } |
paul@615 | 232 | |
paul@615 | 233 | return _run_program(argc - 1, &argv[1], reader); |
paul@615 | 234 | } |
paul@615 | 235 | |
paul@615 | 236 | /* Run the given program, connecting input from another program. */ |
paul@615 | 237 | |
paul@615 | 238 | int pipe_to_program(int argc, char *argv[]) |
paul@615 | 239 | { |
paul@615 | 240 | int job_number; |
paul@615 | 241 | int exitcode; |
paul@615 | 242 | |
paul@615 | 243 | /* Obtain the job number for the output writer and run the program with this |
paul@615 | 244 | as its input reader. */ |
paul@615 | 245 | |
paul@615 | 246 | if (argc < 1) |
paul@615 | 247 | return -1; |
paul@615 | 248 | |
paul@615 | 249 | job_number = atoi(argv[0]); |
paul@615 | 250 | |
paul@615 | 251 | if (readers[job_number] == NULL) |
paul@615 | 252 | { |
paul@615 | 253 | printf("No output available for this job: %s\n", argv[0]); |
paul@615 | 254 | return -1; |
paul@615 | 255 | } |
paul@615 | 256 | |
paul@615 | 257 | exitcode = _run_program(argc - 1, &argv[1], readers[job_number]); |
paul@615 | 258 | |
paul@615 | 259 | /* Remove the job's reader to prevent direct access to it and to allow it to |
paul@615 | 260 | be closed by the receiving program. */ |
paul@615 | 261 | |
paul@615 | 262 | client_close(readers[job_number]); |
paul@615 | 263 | readers[job_number] = NULL; |
paul@615 | 264 | |
paul@615 | 265 | return exitcode; |
paul@615 | 266 | } |
paul@615 | 267 | |
paul@615 | 268 | /* Run the given program. */ |
paul@615 | 269 | |
paul@615 | 270 | int run_program(int argc, char *argv[]) |
paul@615 | 271 | { |
paul@615 | 272 | return _run_program(argc, argv, NULL); |
paul@615 | 273 | } |
paul@615 | 274 | |
paul@606 | 275 | /* Show initiated programs. */ |
paul@606 | 276 | |
paul@606 | 277 | int show_programs(int argc, char *argv[]) |
paul@606 | 278 | { |
paul@606 | 279 | (void) argc; (void) argv; |
paul@606 | 280 | |
paul@606 | 281 | int job_number; |
paul@600 | 282 | |
paul@606 | 283 | for (job_number = 0; job_number < NUMBER_OF_JOBS; job_number++) |
paul@606 | 284 | { |
paul@615 | 285 | if (processes[job_number] != NULL) |
paul@615 | 286 | printf("[%d] %s%s\n", job_number, programs[job_number], |
paul@615 | 287 | readers[job_number] != NULL ? " [!]" : ""); |
paul@606 | 288 | } |
paul@606 | 289 | |
paul@606 | 290 | return 0; |
paul@606 | 291 | } |
paul@600 | 292 | |
paul@606 | 293 | /* Wait for the indicated program to finish, showing its output. */ |
paul@600 | 294 | |
paul@606 | 295 | int wait_program(int argc, char *argv[]) |
paul@606 | 296 | { |
paul@606 | 297 | int job_number; |
paul@606 | 298 | int exitcode; |
paul@606 | 299 | |
paul@606 | 300 | if (argc < 1) |
paul@602 | 301 | return -1; |
paul@600 | 302 | |
paul@606 | 303 | job_number = atoi(argv[0]); |
paul@606 | 304 | |
paul@615 | 305 | if (processes[job_number] == NULL) |
paul@615 | 306 | { |
paul@615 | 307 | printf("No such job: %s\n", argv[0]); |
paul@606 | 308 | return -1; |
paul@615 | 309 | } |
paul@606 | 310 | |
paul@606 | 311 | exitcode = _wait_program(readers[job_number], processes[job_number]); |
paul@606 | 312 | |
paul@606 | 313 | readers[job_number] = NULL; |
paul@606 | 314 | processes[job_number] = NULL; |
paul@606 | 315 | |
paul@606 | 316 | free(programs[job_number]); |
paul@606 | 317 | programs[job_number] = NULL; |
paul@606 | 318 | |
paul@606 | 319 | return exitcode; |
paul@600 | 320 | } |
paul@600 | 321 | |
paul@600 | 322 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@600 | 323 | */ |