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 | /* Obtain the common notifier. */ |
paul@584 | 48 | |
paul@584 | 49 | notifier_t *notifier = client_notifier_task(); |
paul@584 | 50 | |
paul@584 | 51 | /* Create a pipe for process output. */ |
paul@584 | 52 | |
paul@584 | 53 | err = client_pipe(&reader, &writer, O_NONBLOCK); |
paul@584 | 54 | |
paul@584 | 55 | if (err) |
paul@584 | 56 | { |
paul@584 | 57 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@584 | 58 | return err; |
paul@584 | 59 | } |
paul@584 | 60 | |
paul@584 | 61 | err = client_subscribe(reader, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED, notifier); |
paul@584 | 62 | |
paul@584 | 63 | if (err) |
paul@584 | 64 | { |
paul@584 | 65 | printf("Could not subscribe to pipe notifications: %s\n", l4sys_errtostr(err)); |
paul@584 | 66 | return err; |
paul@584 | 67 | } |
paul@559 | 68 | |
paul@559 | 69 | /* Start the process. */ |
paul@559 | 70 | |
paul@584 | 71 | err = process_spawn(argc, argv, writer, &process); |
paul@559 | 72 | |
paul@559 | 73 | if (err) |
paul@559 | 74 | { |
paul@559 | 75 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@559 | 76 | return err; |
paul@559 | 77 | } |
paul@559 | 78 | |
paul@559 | 79 | printf("Finished program initiation.\n"); |
paul@559 | 80 | |
paul@584 | 81 | /* Wait for a signal from the process or input from the process. */ |
paul@559 | 82 | |
paul@584 | 83 | err = notify_subscribe(process_notifiable(process), NOTIFY_TASK_ALL, notifier); |
paul@559 | 84 | |
paul@559 | 85 | if (err) |
paul@559 | 86 | { |
paul@584 | 87 | printf("Could not subscribe to task notifications: %s\n", l4sys_errtostr(err)); |
paul@559 | 88 | return err; |
paul@559 | 89 | } |
paul@559 | 90 | |
paul@584 | 91 | notifiable_t *notifiable; |
paul@584 | 92 | |
paul@584 | 93 | while (1) |
paul@584 | 94 | { |
paul@584 | 95 | err = notify_wait_many(¬ifiable, notifier); |
paul@584 | 96 | |
paul@584 | 97 | if (err) |
paul@584 | 98 | { |
paul@584 | 99 | printf("Could not wait for process: %s\n", l4sys_errtostr(err)); |
paul@584 | 100 | return err; |
paul@584 | 101 | } |
paul@584 | 102 | |
paul@584 | 103 | /* Handle any signal. */ |
paul@584 | 104 | |
paul@584 | 105 | if (notifiable == process_notifiable(process)) |
paul@584 | 106 | break; |
paul@584 | 107 | |
paul@584 | 108 | /* Handle any input. */ |
paul@584 | 109 | |
paul@584 | 110 | else if (notifiable == file_notifiable(reader)) |
paul@584 | 111 | { |
paul@584 | 112 | char buffer[TO_TRANSFER]; |
paul@584 | 113 | offset_t nread = client_read(reader, buffer, TO_TRANSFER); |
paul@584 | 114 | |
paul@584 | 115 | while (nread) |
paul@584 | 116 | { |
paul@584 | 117 | fwrite(buffer, sizeof(char), nread, stdout); |
paul@584 | 118 | nread = client_read(reader, buffer, TO_TRANSFER); |
paul@584 | 119 | } |
paul@584 | 120 | } |
paul@584 | 121 | } |
paul@584 | 122 | |
paul@584 | 123 | notify_flags_t flags = process_notifications(process); |
paul@584 | 124 | notify_values_t values = process_notification_values(process); |
paul@584 | 125 | |
paul@559 | 126 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@584 | 127 | |
paul@584 | 128 | err = process_error(process); |
paul@584 | 129 | |
paul@584 | 130 | client_close(reader); |
paul@584 | 131 | process_free(process); |
paul@584 | 132 | client_notifier_close(notifier); |
paul@584 | 133 | |
paul@584 | 134 | return err; |
paul@559 | 135 | } |
paul@559 | 136 | |
paul@559 | 137 | |
paul@559 | 138 | |
paul@308 | 139 | int main(int argc, char *argv[]) |
paul@308 | 140 | { |
paul@308 | 141 | long err; |
paul@308 | 142 | |
paul@483 | 143 | if (argc < 2) |
paul@308 | 144 | { |
paul@483 | 145 | printf("Need the program to run.\n"); |
paul@370 | 146 | return 1; |
paul@370 | 147 | } |
paul@370 | 148 | |
paul@559 | 149 | /* Start a process for a non-existent program. */ |
paul@525 | 150 | |
paul@559 | 151 | printf("Start non-existent program...\n"); |
paul@525 | 152 | |
paul@559 | 153 | const char *bad_argv[] = {"non_existent_program"}; |
paul@480 | 154 | |
paul@559 | 155 | err = test_process(1, bad_argv); |
paul@308 | 156 | |
paul@559 | 157 | if (!err) |
paul@308 | 158 | { |
paul@559 | 159 | printf("Non-existent program was apparently successfully started.\n"); |
paul@308 | 160 | return 1; |
paul@308 | 161 | } |
paul@308 | 162 | |
paul@559 | 163 | printf("Non-existent program result: %s\n", l4sys_errtostr(err)); |
paul@559 | 164 | |
paul@559 | 165 | /* Start a process for the given program. */ |
paul@483 | 166 | |
paul@559 | 167 | printf("Start process...\n"); |
paul@484 | 168 | |
paul@559 | 169 | err = test_process(argc - 1, (const char **) argv + 1); |
paul@484 | 170 | |
paul@484 | 171 | if (err) |
paul@484 | 172 | return 1; |
paul@308 | 173 | |
paul@483 | 174 | printf("End of test.\n"); |
paul@308 | 175 | return 0; |
paul@308 | 176 | } |
paul@308 | 177 | |
paul@308 | 178 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 179 | */ |