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