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 25 #include <stdio.h> 26 #include <stdlib.h> 27 28 #include "process_creator_context_client.h" 29 30 #include "file.h" 31 #include "process.h" 32 #include "notifier.h" 33 34 35 36 /* Create a new process object. */ 37 38 process_t *process_new() 39 { 40 process_t *process = (process_t *) malloc(sizeof(process_t)); 41 42 if (process == NULL) 43 return NULL; 44 45 process_init(process); 46 return process; 47 } 48 49 /* Discard the given process. */ 50 51 void process_close(process_t *process) 52 { 53 if (process == NULL) 54 return; 55 56 if (l4_is_valid_cap(process->ref)) 57 ipc_cap_free_um(process->ref); 58 59 process_init(process); 60 } 61 62 /* Initialise the given process structure. */ 63 64 void process_init(process_t *process) 65 { 66 process->ref = L4_INVALID_CAP; 67 } 68 69 /* Start a process using the given arguments. 70 NOTE: This does not yet obtain input/output pipes. */ 71 72 long process_start(process_t *process, int argc, char *argv[]) 73 { 74 l4_cap_idx_t server = l4re_env_get_cap("pserver"); 75 76 if (l4_is_invalid_cap(server)) 77 return -L4_ENOMEM; 78 79 /* Obtain a context for process creation. */ 80 81 file_t context; 82 long err = file_context(&context, server); 83 84 if (err) 85 return err; 86 87 offset_t pos = 0, written; 88 89 for (int i = 0; i < argc; i++) 90 { 91 if (!file_string_set(&context, argv[i], pos, &written)) 92 return -L4_ENOMEM; 93 94 pos += written + 1; 95 } 96 97 /* Obtain a client for the process creator context. */ 98 99 client_ProcessCreatorContext creator(context.ref); 100 101 /* Start the process, obtaining a reference to it. */ 102 103 err = creator.start(argc, &process->ref); 104 105 /* Initialise the notifiable section of the structure. */ 106 107 process->notifiable.notifications = 0; 108 process->notifiable.base = (notifiable_base_t *) process; 109 110 /* Close the context, although a separate mechanism could permit contexts to 111 open several processes. */ 112 113 file_close(&context); 114 return err; 115 } 116 117 118 119 /* NOTE: Much of the code below could be unified with the file-related 120 notification code. */ 121 122 /* Opaque notifier type for process_notifier_t. */ 123 124 struct process_notifier 125 { 126 ObjectNotifier *obj; 127 }; 128 129 /* Conversion to the generic notification types. */ 130 131 notifiable_t *process_notifiable(process_t *process) 132 { 133 return &process->notifiable; 134 } 135 136 /* Return the notification flags for a process. */ 137 138 notify_flags_t process_notifications(process_t *process) 139 { 140 return process->notifiable.notifications; 141 } 142 143 /* Return the notification values for a process. */ 144 145 notify_values_t process_notification_values(process_t *process) 146 { 147 return process->notifiable.values; 148 } 149 150 /* Close a notifier object. */ 151 152 void process_notify_close(process_notifier_t *notifier) 153 { 154 delete notifier->obj; 155 delete notifier; 156 } 157 158 /* Obtain a local notifier object. */ 159 160 process_notifier_t *process_notify_local() 161 { 162 process_notifier_t *notifier = new process_notifier_t; 163 164 notifier->obj = notifier_get_local_notifier(); 165 return notifier; 166 } 167 168 /* Obtain the task-wide notifier object. */ 169 170 process_notifier_t *process_notify_task() 171 { 172 process_notifier_t *notifier = new process_notifier_t; 173 174 notifier->obj = notifier_get_task_notifier(); 175 return notifier; 176 } 177 178 /* Subscribe to notification events on a process. */ 179 180 long process_notify_subscribe(process_t *process, notify_flags_t flags, process_notifier_t *notifier) 181 { 182 return notifier->obj->subscribe(process_notifiable(process), flags); 183 } 184 185 /* Unsubscribe from notification events on a process. */ 186 187 long process_notify_unsubscribe(process_t *process, process_notifier_t *notifier) 188 { 189 return notifier->obj->unsubscribe(process_notifiable(process)); 190 } 191 192 /* Wait for a notification event on a process. */ 193 194 long process_notify_wait_process(process_t *process, process_notifier_t *notifier) 195 { 196 SpecificObjectNotifier *specific_notifier = dynamic_cast<SpecificObjectNotifier *>(notifier->obj); 197 long err = specific_notifier->wait_object(process_notifiable(process)); 198 199 /* Unsubscribe if a termination notification has been received. */ 200 201 if (!err && (process->notifiable.notifications & NOTIFY_TASK_SIGNAL) && 202 (process->notifiable.values.sig == 0)) 203 { 204 process_notify_unsubscribe(process, notifier); 205 process_close(process); 206 } 207 208 return err; 209 } 210 211 /* Wait for notification events on processes. */ 212 213 long process_notify_wait_processes(process_t **process, process_notifier_t *notifier) 214 { 215 GeneralObjectNotifier *general_notifier = dynamic_cast<GeneralObjectNotifier *>(notifier->obj); 216 notifiable_t *notifiable; 217 long err = general_notifier->wait(¬ifiable); 218 219 *process = (process_t *) notifiable->base; 220 221 /* Unsubscribe if a termination notification has been received. */ 222 223 if (!err && ((*process)->notifiable.notifications & NOTIFY_TASK_SIGNAL) && 224 ((*process)->notifiable.values.sig == 0)) 225 { 226 process_notify_unsubscribe(*process, notifier); 227 process_close(*process); 228 } 229 230 return err; 231 } 232 233 // vim: tabstop=2 expandtab shiftwidth=2