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@483 | 24 | #include <fsclient/process.h> |
paul@480 | 25 | #include <systypes/format.h> |
paul@312 | 26 | |
paul@308 | 27 | #include <stdio.h> |
paul@308 | 28 | |
paul@308 | 29 | |
paul@308 | 30 | |
paul@559 | 31 | static long test_process(int argc, const char *argv[]) |
paul@559 | 32 | { |
paul@559 | 33 | /* Obtain the common notifier. */ |
paul@559 | 34 | |
paul@559 | 35 | process_notifier_t *notifier = process_notify_task(); |
paul@559 | 36 | |
paul@559 | 37 | /* Create a new process structure. */ |
paul@559 | 38 | |
paul@559 | 39 | process_t process; |
paul@559 | 40 | |
paul@559 | 41 | process_init(&process); |
paul@559 | 42 | |
paul@559 | 43 | /* Start the process. */ |
paul@559 | 44 | |
paul@559 | 45 | long err = process_start(&process, argc, argv); |
paul@559 | 46 | |
paul@559 | 47 | if (err) |
paul@559 | 48 | { |
paul@559 | 49 | printf("Could not start process: %s\n", l4sys_errtostr(err)); |
paul@559 | 50 | return err; |
paul@559 | 51 | } |
paul@559 | 52 | |
paul@559 | 53 | printf("Finished program initiation.\n"); |
paul@559 | 54 | |
paul@559 | 55 | /* Subscribe to the process for notifications. */ |
paul@559 | 56 | |
paul@559 | 57 | err = process_notify_subscribe(&process, NOTIFY_TASK_ALL, notifier); |
paul@559 | 58 | |
paul@559 | 59 | if (err) |
paul@559 | 60 | { |
paul@559 | 61 | printf("Could not subscribe to process: %s\n", l4sys_errtostr(err)); |
paul@559 | 62 | return err; |
paul@559 | 63 | } |
paul@559 | 64 | |
paul@559 | 65 | /* Wait for a signal from the process. */ |
paul@559 | 66 | |
paul@559 | 67 | err = process_notify_wait_process(&process, notifier); |
paul@559 | 68 | |
paul@559 | 69 | if (err) |
paul@559 | 70 | { |
paul@559 | 71 | printf("Could not wait for process: %s\n", l4sys_errtostr(err)); |
paul@559 | 72 | return err; |
paul@559 | 73 | } |
paul@559 | 74 | |
paul@559 | 75 | notify_flags_t flags = process_notifications(&process); |
paul@559 | 76 | notify_values_t values = process_notification_values(&process); |
paul@559 | 77 | |
paul@559 | 78 | printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); |
paul@559 | 79 | |
paul@559 | 80 | err = process_error(&process); |
paul@559 | 81 | process_close(&process); |
paul@559 | 82 | return err; |
paul@559 | 83 | } |
paul@559 | 84 | |
paul@559 | 85 | |
paul@559 | 86 | |
paul@308 | 87 | int main(int argc, char *argv[]) |
paul@308 | 88 | { |
paul@308 | 89 | long err; |
paul@308 | 90 | |
paul@483 | 91 | if (argc < 2) |
paul@308 | 92 | { |
paul@483 | 93 | printf("Need the program to run.\n"); |
paul@370 | 94 | return 1; |
paul@370 | 95 | } |
paul@370 | 96 | |
paul@540 | 97 | /* Obtain the common notifier. */ |
paul@540 | 98 | |
paul@540 | 99 | process_notifier_t *notifier = process_notify_task(); |
paul@540 | 100 | |
paul@559 | 101 | /* Start a process for a non-existent program. */ |
paul@525 | 102 | |
paul@559 | 103 | printf("Start non-existent program...\n"); |
paul@525 | 104 | |
paul@559 | 105 | const char *bad_argv[] = {"non_existent_program"}; |
paul@480 | 106 | |
paul@559 | 107 | err = test_process(1, bad_argv); |
paul@308 | 108 | |
paul@559 | 109 | if (!err) |
paul@308 | 110 | { |
paul@559 | 111 | printf("Non-existent program was apparently successfully started.\n"); |
paul@308 | 112 | return 1; |
paul@308 | 113 | } |
paul@308 | 114 | |
paul@559 | 115 | printf("Non-existent program result: %s\n", l4sys_errtostr(err)); |
paul@559 | 116 | |
paul@559 | 117 | /* Start a process for the given program. */ |
paul@483 | 118 | |
paul@559 | 119 | printf("Start process...\n"); |
paul@484 | 120 | |
paul@559 | 121 | err = test_process(argc - 1, (const char **) argv + 1); |
paul@484 | 122 | |
paul@484 | 123 | if (err) |
paul@484 | 124 | return 1; |
paul@308 | 125 | |
paul@540 | 126 | process_notify_close(notifier); |
paul@540 | 127 | |
paul@483 | 128 | printf("End of test.\n"); |
paul@308 | 129 | return 0; |
paul@308 | 130 | } |
paul@308 | 131 | |
paul@308 | 132 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 133 | */ |