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