1 /* 2 * Process-related convenience functions. 3 * 4 * Copyright (C) 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 <ipc/cap_alloc.h> 23 #include <ipc/mem_ipc.h> 24 #include <notifier/notifier.h> 25 #include <systypes/env.h> 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 30 #include "process_creator_context_client.h" 31 32 #include "file.h" 33 #include "process.h" 34 35 36 37 /* Utility functions. */ 38 39 static bool _process_terminated(notifiable_t *notifiable) 40 { 41 return ((notifiable->notifications & NOTIFY_TASK_SIGNAL) && 42 (notifiable->values.sig == 0)) || 43 (notifiable->notifications & NOTIFY_TASK_ERROR); 44 } 45 46 47 48 /* Create a new process object. */ 49 50 process_t *process_new() 51 { 52 process_t *process = (process_t *) malloc(sizeof(process_t)); 53 54 if (process == NULL) 55 return NULL; 56 57 process_init(process); 58 return process; 59 } 60 61 /* Discard the given process. */ 62 63 void process_close(process_t *process) 64 { 65 if (process == NULL) 66 return; 67 68 if (l4_is_valid_cap(process->ref)) 69 ipc_cap_free_um(process->ref); 70 71 process_init(process); 72 } 73 74 /* Return any process initiation error. */ 75 76 long process_error(process_t *process) 77 { 78 if (process->notifiable.notifications & NOTIFY_TASK_ALL) 79 return process->notifiable.values.val; 80 else 81 return L4_EOK; 82 } 83 84 /* Discard and free the given process, similar to process_close but freeing the 85 structure instead of reinitialising it. */ 86 87 void process_free(process_t *process) 88 { 89 if (process == NULL) 90 return; 91 92 if (l4_is_valid_cap(process->ref)) 93 ipc_cap_free_um(process->ref); 94 95 free(process); 96 } 97 98 /* Initialise the given process structure. */ 99 100 void process_init(process_t *process) 101 { 102 process->ref = L4_INVALID_CAP; 103 104 /* Initialise the notifiable section of the structure. */ 105 106 process->notifiable.notifications = 0; 107 process->notifiable.pending_notifications = 0; 108 process->notifiable.base = (notifiable_base_t *) process; 109 process->notifiable.handler = NULL; 110 } 111 112 /* A convenience function for creating and starting a process. */ 113 114 long process_spawn(int argc, const char *argv[], file_t *writer, 115 process_t **process) 116 { 117 *process = process_new(); 118 119 /* Start the process with the given arguments. */ 120 121 if (*process != NULL) 122 return process_start(*process, argc, argv, writer); 123 else 124 return -L4_ENOMEM; 125 } 126 127 /* Start a process using the given arguments. 128 NOTE: This does not yet employ a pipe for the process's input stream. */ 129 130 long process_start(process_t *process, int argc, const char *argv[], 131 file_t *writer) 132 { 133 l4_cap_idx_t server = l4re_env_get_cap(ENV_PROCESS_SERVER_NAME); 134 135 if (l4_is_invalid_cap(server)) 136 return -L4_ENOENT; 137 138 /* Obtain a context for process creation. */ 139 140 file_t context; 141 long err = file_context(&context, server); 142 143 if (err) 144 return err; 145 146 offset_t pos = 0, written; 147 148 for (int i = 0; i < argc; i++) 149 { 150 if (!file_string_set(&context, argv[i], pos, &written)) 151 return -L4_ENOMEM; 152 153 pos += written + 1; 154 } 155 156 /* Obtain a client for the process creator context. */ 157 158 client_ProcessCreatorContext creator(context.ref); 159 160 /* Start the process, obtaining a reference to it. */ 161 162 err = creator.start(argc, writer != NULL ? writer->ref : (l4_cap_idx_t) L4_INVALID_CAP, 163 &process->ref); 164 165 /* Initialise the notifiable section of the structure. */ 166 167 process->notifiable.notifications = 0; 168 process->notifiable.pending_notifications = 0; 169 process->notifiable.base = (notifiable_base_t *) process; 170 171 /* Close the context, although a separate mechanism could permit contexts to 172 open several processes. */ 173 174 file_close(&context); 175 return err; 176 } 177 178 /* A convenience function for waiting for a started process. */ 179 180 long process_wait(process_t *process, notify_flags_t *flags, notify_values_t *values) 181 { 182 /* Obtain the common notifier. */ 183 184 notifier_t *notifier = notify_get_task(); 185 186 /* Subscribe to the process for notifications. */ 187 188 long err = notify_subscribe(process_notifiable(process), NOTIFY_TASK_ALL, notifier); 189 190 if (err) 191 return err; 192 193 /* Wait for a signal from the process. */ 194 195 err = process_notify_wait_process(process, notifier); 196 197 if (err) 198 return err; 199 200 /* Obtain the notification flags and values. */ 201 202 *flags = process_notifications(process); 203 *values = process_notification_values(process); 204 205 /* Obtain any error, closing the process. */ 206 207 err = process_error(process); 208 process_free(process); 209 return err; 210 } 211 212 213 214 /* Conversion to the generic notification types. */ 215 216 notifiable_t *process_notifiable(process_t *process) 217 { 218 return notify_notifiable((notifiable_base_t *) process); 219 } 220 221 /* Return the notification flags for a process. */ 222 223 notify_flags_t process_notifications(process_t *process) 224 { 225 return notify_notifications((notifiable_base_t *) process); 226 } 227 228 /* Return the notification values for a process. */ 229 230 notify_values_t process_notification_values(process_t *process) 231 { 232 return notify_notification_values((notifiable_base_t *) process); 233 } 234 235 /* Wait for a notification event on a process. */ 236 237 long process_notify_wait_process(process_t *process, notifier_t *notifier) 238 { 239 long err = notify_wait(process_notifiable(process), notifier); 240 241 /* Unsubscribe if a termination notification has been received. */ 242 243 if (!err && _process_terminated(process_notifiable(process))) 244 notify_unsubscribe(process_notifiable(process), notifier); 245 246 return err; 247 } 248 249 /* Wait for notification events on processes. */ 250 251 long process_notify_wait_processes(process_t **process, notifier_t *notifier) 252 { 253 notifiable_t *notifiable; 254 long err = notify_wait_many(¬ifiable, notifier); 255 256 *process = (process_t *) notifiable->base; 257 258 /* Unsubscribe if a termination notification has been received. */ 259 260 if (!err && _process_terminated(notifiable)) 261 notify_unsubscribe(notifiable, notifier); 262 263 return err; 264 } 265 266 // vim: tabstop=2 expandtab shiftwidth=2