1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/op_run.c Mon Jan 15 01:18:41 2024 +0100
1.3 @@ -0,0 +1,105 @@
1.4 +/*
1.5 + * Run a program residing in the filesystem.
1.6 + *
1.7 + * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/sys/err.h>
1.26 +
1.27 +#include <fsclient/client.h>
1.28 +#include <fsclient/process.h>
1.29 +#include <systypes/fcntl.h>
1.30 +#include <systypes/format.h>
1.31 +
1.32 +#include <stdio.h>
1.33 +
1.34 +#include "ops.h"
1.35 +
1.36 +
1.37 +
1.38 +/* Transfer size for communication. */
1.39 +
1.40 +static const offset_t TO_TRANSFER = 1024;
1.41 +
1.42 +
1.43 +
1.44 +/* Run the given program. */
1.45 +
1.46 +int run_program(int argc, char *argv[])
1.47 +{
1.48 + char buffer[TO_TRANSFER];
1.49 + process_t *process;
1.50 + file_t *reader, *writer;
1.51 + notify_flags_t flags;
1.52 + notify_values_t values;
1.53 + long err;
1.54 +
1.55 + /* Create a pipe for process output. */
1.56 +
1.57 + err = client_pipe(&reader, &writer, 0);
1.58 +
1.59 + if (err)
1.60 + {
1.61 + printf("Could not obtain pipe: %s\n", l4sys_errtostr(err));
1.62 + return 1;
1.63 + }
1.64 +
1.65 + /* Start the process. */
1.66 +
1.67 + err = process_spawn(argc, (const char **) argv, writer, &process);
1.68 +
1.69 + if (err)
1.70 + {
1.71 + printf("Could not start process: %s\n", l4sys_errtostr(err));
1.72 + return 1;
1.73 + }
1.74 +
1.75 + printf("Finished program initiation.\n");
1.76 +
1.77 + /* Release the writing end of the pipe. */
1.78 +
1.79 + client_close(writer);
1.80 +
1.81 + /* Read until the pipe yields no more data. */
1.82 +
1.83 + while (1)
1.84 + {
1.85 + offset_t nread = client_read(reader, buffer, TO_TRANSFER);
1.86 +
1.87 + if (nread)
1.88 + fwrite(buffer, sizeof(char), nread, stdout);
1.89 + else
1.90 + break;
1.91 + }
1.92 +
1.93 + /* Close the pipe and obtain the process state. */
1.94 +
1.95 + client_close(reader);
1.96 +
1.97 + err = process_wait(process, &flags, &values);
1.98 +
1.99 + printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val);
1.100 +
1.101 + if (err)
1.102 + return 1;
1.103 +
1.104 + return 0;
1.105 +}
1.106 +
1.107 +/* vim: tabstop=2 expandtab shiftwidth=2
1.108 +*/