paul@308 | 1 | /* |
paul@525 | 2 | * A test of executing code in new tasks and threads. |
paul@308 | 3 | * |
paul@615 | 4 | * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@308 | 5 | * |
paul@308 | 6 | * This program is free software; you can redistribute it and/or |
paul@308 | 7 | * modify it under the terms of the GNU General Public License as |
paul@308 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@308 | 9 | * the License, or (at your option) any later version. |
paul@308 | 10 | * |
paul@308 | 11 | * This program is distributed in the hope that it will be useful, |
paul@308 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@308 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@308 | 14 | * GNU General Public License for more details. |
paul@308 | 15 | * |
paul@308 | 16 | * You should have received a copy of the GNU General Public License |
paul@308 | 17 | * along with this program; if not, write to the Free Software |
paul@308 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@308 | 19 | * Boston, MA 02110-1301, USA |
paul@308 | 20 | */ |
paul@308 | 21 | |
paul@308 | 22 | #include <l4/sys/err.h> |
paul@308 | 23 | |
paul@584 | 24 | #include <fsclient/client.h> |
paul@483 | 25 | #include <fsclient/process.h> |
paul@584 | 26 | #include <systypes/fcntl.h> |
paul@480 | 27 | #include <systypes/format.h> |
paul@312 | 28 | |
paul@308 | 29 | #include <stdio.h> |
paul@308 | 30 | |
paul@308 | 31 | |
paul@308 | 32 | |
paul@584 | 33 | /* Transfer size for communication. */ |
paul@584 | 34 | |
paul@584 | 35 | const offset_t TO_TRANSFER = 1024; |
paul@584 | 36 | |
paul@584 | 37 | |
paul@584 | 38 | |
paul@584 | 39 | /* Test process initiation and interaction. */ |
paul@584 | 40 | |
paul@559 | 41 | static long test_process(int argc, const char *argv[]) |
paul@559 | 42 | { |
paul@578 | 43 | process_t *process; |
paul@615 | 44 | file_t *input_reader, *input_writer; |
paul@615 | 45 | file_t *output_reader, *output_writer; |
paul@584 | 46 | long err; |
paul@584 | 47 | |
paul@615 | 48 | /* Create pipes for process input and output. */ |
paul@584 | 49 | |
paul@615 | 50 | err = client_pipe(&input_reader, &input_writer, 0); |
paul@584 | 51 | |
paul@584 | 52 | if (err) |
paul@584 | 53 | { |
paul@615 | 54 | printf("Could not obtain pipe for input: %s\n", l4sys_errtostr(err)); |
paul@615 | 55 | return err; |
paul@615 | 56 | } |
paul@615 | 57 | |
paul@615 | 58 | err = client_pipe(&output_reader, &output_writer, 0); |
paul@615 | 59 | |
paul@615 | 60 | if (err) |
paul@615 | 61 | { |
paul@615 | 62 | printf("Could not obtain pipe for output: %s\n", l4sys_errtostr(err)); |
paul@584 | 63 | return err; |
paul@584 | 64 | } |
paul@584 | 65 | |
paul@559 | 66 | /* Start the process. */ |
paul@559 | 67 | |
paul@700 | 68 | err = process_spawn(argc, argv, input_reader, output_writer, NULL, &process); |
paul@559 | 69 | |
paul@559 | 70 | if (err) |
paul@559 | 71 | { |
paul@559 | 72 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@559 | 73 | return err; |
paul@559 | 74 | } |
paul@559 | 75 | |
paul@559 | 76 | printf("Finished program initiation.\n"); |
paul@559 | 77 | |
paul@615 | 78 | /* Release the relinquished ends of the pipes. */ |
paul@585 | 79 | |
paul@615 | 80 | client_close(input_reader); |
paul@615 | 81 | client_close(output_writer); |
paul@585 | 82 | |
paul@588 | 83 | /* Read until the pipe yields no more data. */ |
paul@559 | 84 | |
paul@588 | 85 | char buffer[TO_TRANSFER]; |
paul@584 | 86 | |
paul@584 | 87 | while (1) |
paul@584 | 88 | { |
paul@615 | 89 | offset_t nread = client_read(output_reader, buffer, TO_TRANSFER); |
paul@584 | 90 | |
paul@588 | 91 | if (nread) |
paul@588 | 92 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@588 | 93 | else |
paul@588 | 94 | break; |
paul@584 | 95 | } |
paul@584 | 96 | |
paul@615 | 97 | /* Close the pipes and obtain the process state. */ |
paul@588 | 98 | |
paul@615 | 99 | client_close(input_writer); |
paul@615 | 100 | client_close(output_reader); |
paul@588 | 101 | |
paul@588 | 102 | notify_flags_t flags; |
paul@588 | 103 | notify_values_t values; |
paul@588 | 104 | |
paul@588 | 105 | err = process_wait(process, &flags, &values); |
paul@584 | 106 | |
paul@559 | 107 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@584 | 108 | |
paul@584 | 109 | return err; |
paul@559 | 110 | } |
paul@559 | 111 | |
paul@559 | 112 | |
paul@559 | 113 | |
paul@308 | 114 | int main(int argc, char *argv[]) |
paul@308 | 115 | { |
paul@308 | 116 | long err; |
paul@308 | 117 | |
paul@483 | 118 | if (argc < 2) |
paul@308 | 119 | { |
paul@483 | 120 | printf("Need the program to run.\n"); |
paul@370 | 121 | return 1; |
paul@370 | 122 | } |
paul@370 | 123 | |
paul@559 | 124 | /* Start a process for a non-existent program. */ |
paul@525 | 125 | |
paul@559 | 126 | printf("Start non-existent program...\n"); |
paul@525 | 127 | |
paul@559 | 128 | const char *bad_argv[] = {"non_existent_program"}; |
paul@480 | 129 | |
paul@559 | 130 | err = test_process(1, bad_argv); |
paul@308 | 131 | |
paul@559 | 132 | if (!err) |
paul@308 | 133 | { |
paul@559 | 134 | printf("Non-existent program was apparently successfully started.\n"); |
paul@308 | 135 | return 1; |
paul@308 | 136 | } |
paul@308 | 137 | |
paul@559 | 138 | printf("Non-existent program result: %s\n", l4sys_errtostr(err)); |
paul@559 | 139 | |
paul@559 | 140 | /* Start a process for the given program. */ |
paul@483 | 141 | |
paul@559 | 142 | printf("Start process...\n"); |
paul@484 | 143 | |
paul@559 | 144 | err = test_process(argc - 1, (const char **) argv + 1); |
paul@484 | 145 | |
paul@484 | 146 | if (err) |
paul@484 | 147 | return 1; |
paul@308 | 148 | |
paul@483 | 149 | printf("End of test.\n"); |
paul@308 | 150 | return 0; |
paul@308 | 151 | } |
paul@308 | 152 | |
paul@308 | 153 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 154 | */ |