1 /* 2 * A test of executing code in new tasks and threads. 3 * 4 * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/sys/err.h> 23 24 #include <fsclient/process.h> 25 #include <systypes/format.h> 26 27 #include <stdio.h> 28 29 30 31 static long test_process(int argc, const char *argv[]) 32 { 33 /* Obtain the common notifier. */ 34 35 process_notifier_t *notifier = process_notify_task(); 36 37 /* Create a new process structure. */ 38 39 process_t process; 40 41 process_init(&process); 42 43 /* Start the process. */ 44 45 long err = process_start(&process, argc, argv); 46 47 if (err) 48 { 49 printf("Could not start process: %s\n", l4sys_errtostr(err)); 50 return err; 51 } 52 53 printf("Finished program initiation.\n"); 54 55 /* Subscribe to the process for notifications. */ 56 57 err = process_notify_subscribe(&process, NOTIFY_TASK_ALL, notifier); 58 59 if (err) 60 { 61 printf("Could not subscribe to process: %s\n", l4sys_errtostr(err)); 62 return err; 63 } 64 65 /* Wait for a signal from the process. */ 66 67 err = process_notify_wait_process(&process, notifier); 68 69 if (err) 70 { 71 printf("Could not wait for process: %s\n", l4sys_errtostr(err)); 72 return err; 73 } 74 75 notify_flags_t flags = process_notifications(&process); 76 notify_values_t values = process_notification_values(&process); 77 78 printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); 79 80 err = process_error(&process); 81 process_close(&process); 82 return err; 83 } 84 85 86 87 int main(int argc, char *argv[]) 88 { 89 long err; 90 91 if (argc < 2) 92 { 93 printf("Need the program to run.\n"); 94 return 1; 95 } 96 97 /* Obtain the common notifier. */ 98 99 process_notifier_t *notifier = process_notify_task(); 100 101 /* Start a process for a non-existent program. */ 102 103 printf("Start non-existent program...\n"); 104 105 const char *bad_argv[] = {"non_existent_program"}; 106 107 err = test_process(1, bad_argv); 108 109 if (!err) 110 { 111 printf("Non-existent program was apparently successfully started.\n"); 112 return 1; 113 } 114 115 printf("Non-existent program result: %s\n", l4sys_errtostr(err)); 116 117 /* Start a process for the given program. */ 118 119 printf("Start process...\n"); 120 121 err = test_process(argc - 1, (const char **) argv + 1); 122 123 if (err) 124 return 1; 125 126 process_notify_close(notifier); 127 128 printf("End of test.\n"); 129 return 0; 130 } 131 132 /* vim: tabstop=2 expandtab shiftwidth=2 133 */