paul@308 | 1 | /* |
paul@525 | 2 | * A test of executing code in new tasks and threads. |
paul@308 | 3 | * |
paul@477 | 4 | * Copyright (C) 2022, 2023 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@584 | 44 | file_t *reader, *writer; |
paul@584 | 45 | long err; |
paul@584 | 46 | |
paul@584 | 47 | /* Create a pipe for process output. */ |
paul@584 | 48 | |
paul@588 | 49 | err = client_pipe(&reader, &writer, 0); |
paul@584 | 50 | |
paul@584 | 51 | if (err) |
paul@584 | 52 | { |
paul@584 | 53 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@584 | 54 | return err; |
paul@584 | 55 | } |
paul@584 | 56 | |
paul@559 | 57 | /* Start the process. */ |
paul@559 | 58 | |
paul@584 | 59 | err = process_spawn(argc, argv, writer, &process); |
paul@559 | 60 | |
paul@559 | 61 | if (err) |
paul@559 | 62 | { |
paul@559 | 63 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@559 | 64 | return err; |
paul@559 | 65 | } |
paul@559 | 66 | |
paul@559 | 67 | printf("Finished program initiation.\n"); |
paul@559 | 68 | |
paul@585 | 69 | /* Release the writing end of the pipe. */ |
paul@585 | 70 | |
paul@585 | 71 | client_close(writer); |
paul@585 | 72 | |
paul@588 | 73 | /* Read until the pipe yields no more data. */ |
paul@559 | 74 | |
paul@588 | 75 | char buffer[TO_TRANSFER]; |
paul@584 | 76 | |
paul@584 | 77 | while (1) |
paul@584 | 78 | { |
paul@588 | 79 | offset_t nread = client_read(reader, buffer, TO_TRANSFER); |
paul@584 | 80 | |
paul@588 | 81 | if (nread) |
paul@588 | 82 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@588 | 83 | else |
paul@588 | 84 | break; |
paul@584 | 85 | } |
paul@584 | 86 | |
paul@588 | 87 | /* Close the pipe and obtain the process state. */ |
paul@588 | 88 | |
paul@588 | 89 | client_close(reader); |
paul@588 | 90 | |
paul@588 | 91 | notify_flags_t flags; |
paul@588 | 92 | notify_values_t values; |
paul@588 | 93 | |
paul@588 | 94 | err = process_wait(process, &flags, &values); |
paul@584 | 95 | |
paul@559 | 96 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@584 | 97 | |
paul@584 | 98 | return err; |
paul@559 | 99 | } |
paul@559 | 100 | |
paul@559 | 101 | |
paul@559 | 102 | |
paul@308 | 103 | int main(int argc, char *argv[]) |
paul@308 | 104 | { |
paul@308 | 105 | long err; |
paul@308 | 106 | |
paul@483 | 107 | if (argc < 2) |
paul@308 | 108 | { |
paul@483 | 109 | printf("Need the program to run.\n"); |
paul@370 | 110 | return 1; |
paul@370 | 111 | } |
paul@370 | 112 | |
paul@559 | 113 | /* Start a process for a non-existent program. */ |
paul@525 | 114 | |
paul@559 | 115 | printf("Start non-existent program...\n"); |
paul@525 | 116 | |
paul@559 | 117 | const char *bad_argv[] = {"non_existent_program"}; |
paul@480 | 118 | |
paul@559 | 119 | err = test_process(1, bad_argv); |
paul@308 | 120 | |
paul@559 | 121 | if (!err) |
paul@308 | 122 | { |
paul@559 | 123 | printf("Non-existent program was apparently successfully started.\n"); |
paul@308 | 124 | return 1; |
paul@308 | 125 | } |
paul@308 | 126 | |
paul@559 | 127 | printf("Non-existent program result: %s\n", l4sys_errtostr(err)); |
paul@559 | 128 | |
paul@559 | 129 | /* Start a process for the given program. */ |
paul@483 | 130 | |
paul@559 | 131 | printf("Start process...\n"); |
paul@484 | 132 | |
paul@559 | 133 | err = test_process(argc - 1, (const char **) argv + 1); |
paul@484 | 134 | |
paul@484 | 135 | if (err) |
paul@484 | 136 | return 1; |
paul@308 | 137 | |
paul@483 | 138 | printf("End of test.\n"); |
paul@308 | 139 | return 0; |
paul@308 | 140 | } |
paul@308 | 141 | |
paul@308 | 142 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 143 | */ |