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_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 file as payload. 62 NOTE: This does not yet communicate arguments or obtain input/output 63 pipes. */ 64 65 long process_start(process_t *process, file_t *file) 66 { 67 l4_cap_idx_t server = l4re_env_get_cap("pserver"); 68 69 if (l4_is_invalid_cap(server)) 70 return -L4_ENOMEM; 71 72 /* Obtain a client for the process creator. */ 73 74 client_ProcessCreator creator(server); 75 76 /* Start the process, obtaining a reference to it. */ 77 78 return creator.start(file->ref, &process->ref); 79 } 80 81 82 83 /* NOTE: Much of the code below could be unified with the file-related 84 notification code. */ 85 86 /* Opaque notifier type for process_notifier_t. */ 87 88 struct process_notifier 89 { 90 ObjectNotifier *obj; 91 }; 92 93 /* Conversion to the generic notification types. */ 94 95 notifiable_t *process_notifiable(process_t *process) 96 { 97 return &process->notifiable; 98 } 99 100 /* Return the notification flags for a process. */ 101 102 notify_flags_t process_notifications(process_t *process) 103 { 104 return process->notifiable.notifications; 105 } 106 107 /* Return the notification values for a process. */ 108 109 notify_values_t process_notification_values(process_t *process) 110 { 111 return process->notifiable.values; 112 } 113 114 /* Close a notifier object. */ 115 116 void process_notify_close(process_notifier_t *notifier) 117 { 118 delete notifier->obj; 119 delete notifier; 120 } 121 122 /* Obtain a local notifier object. */ 123 124 process_notifier_t *process_notify_local() 125 { 126 process_notifier_t *notifier = new process_notifier_t; 127 128 notifier->obj = notifier_get_local_notifier(); 129 return notifier; 130 } 131 132 /* Obtain the task-wide notifier object. */ 133 134 process_notifier_t *process_notify_task() 135 { 136 process_notifier_t *notifier = new process_notifier_t; 137 138 notifier->obj = notifier_get_task_notifier(); 139 return notifier; 140 } 141 142 /* Subscribe to notification events on a process. */ 143 144 long process_notify_subscribe(process_t *process, notify_flags_t flags, process_notifier_t *notifier) 145 { 146 return notifier->obj->subscribe(process_notifiable(process), flags); 147 } 148 149 /* Unsubscribe from notification events on a process. */ 150 151 long process_notify_unsubscribe(process_t *process, process_notifier_t *notifier) 152 { 153 return notifier->obj->unsubscribe(process_notifiable(process)); 154 } 155 156 /* Wait for a notification event on a process. */ 157 158 long process_notify_wait_process(process_t *process, process_notifier_t *notifier) 159 { 160 SpecificObjectNotifier *specific_notifier = dynamic_cast<SpecificObjectNotifier *>(notifier->obj); 161 long err = specific_notifier->wait_object(process_notifiable(process)); 162 163 /* Unsubscribe if a termination notification has been received. */ 164 165 if (!err && (process->notifiable.notifications & NOTIFY_TASK_SIGNAL) && 166 (process->notifiable.values.sig == 0)) 167 process_notify_unsubscribe(process, notifier); 168 169 return err; 170 } 171 172 /* Wait for notification events on processes. */ 173 174 long process_notify_wait_processes(process_t **process, process_notifier_t *notifier) 175 { 176 GeneralObjectNotifier *general_notifier = dynamic_cast<GeneralObjectNotifier *>(notifier->obj); 177 notifiable_t *notifiable; 178 long err = general_notifier->wait(¬ifiable); 179 180 *process = (process_t *) notifiable->base; 181 182 /* Unsubscribe if a termination notification has been received. */ 183 184 if (!err && ((*process)->notifiable.notifications & NOTIFY_TASK_SIGNAL) && 185 ((*process)->notifiable.values.sig == 0)) 186 process_notify_unsubscribe(*process, notifier); 187 188 return err; 189 } 190 191 // vim: tabstop=2 expandtab shiftwidth=2