paul@543 | 1 | /* |
paul@543 | 2 | * A process monitor abstraction. |
paul@543 | 3 | * |
paul@543 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@543 | 5 | * |
paul@543 | 6 | * This program is free software; you can redistribute it and/or |
paul@543 | 7 | * modify it under the terms of the GNU General Public License as |
paul@543 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@543 | 9 | * the License, or (at your option) any later version. |
paul@543 | 10 | * |
paul@543 | 11 | * This program is distributed in the hope that it will be useful, |
paul@543 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@543 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@543 | 14 | * GNU General Public License for more details. |
paul@543 | 15 | * |
paul@543 | 16 | * You should have received a copy of the GNU General Public License |
paul@543 | 17 | * along with this program; if not, write to the Free Software |
paul@543 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@543 | 19 | * Boston, MA 02110-1301, USA |
paul@543 | 20 | */ |
paul@543 | 21 | |
paul@543 | 22 | #include <ipc/cap_alloc.h> |
paul@543 | 23 | #include <ipc/map.h> |
paul@543 | 24 | #include <systypes/base.h> |
paul@543 | 25 | |
paul@543 | 26 | #include <stdio.h> |
paul@543 | 27 | |
paul@543 | 28 | #include "process_monitor.h" |
paul@543 | 29 | #include "parent_notification_object_server.h" |
paul@543 | 30 | |
paul@543 | 31 | |
paul@543 | 32 | |
paul@543 | 33 | /* A process monitor receiving signals from a task. */ |
paul@543 | 34 | |
paul@543 | 35 | ProcessMonitor::ProcessMonitor() |
paul@543 | 36 | : NotificationSupport() |
paul@543 | 37 | { |
paul@543 | 38 | } |
paul@543 | 39 | |
paul@543 | 40 | ipc_server_default_config_type ProcessMonitor::config() |
paul@543 | 41 | { |
paul@543 | 42 | return config_ParentNotificationObject; |
paul@543 | 43 | } |
paul@543 | 44 | |
paul@543 | 45 | |
paul@543 | 46 | |
paul@543 | 47 | /* Close the process monitor. */ |
paul@543 | 48 | |
paul@543 | 49 | void ProcessMonitor::close() |
paul@543 | 50 | { |
paul@543 | 51 | printf("Process monitor closing...\n"); |
paul@543 | 52 | |
paul@543 | 53 | if (l4_is_valid_cap(_task)) |
paul@543 | 54 | { |
paul@543 | 55 | ipc_unmap_capability(_task, _mapped_task); |
paul@543 | 56 | ipc_cap_free_um(_task); |
paul@543 | 57 | _task = L4_INVALID_CAP; |
paul@543 | 58 | } |
paul@543 | 59 | } |
paul@543 | 60 | |
paul@543 | 61 | |
paul@543 | 62 | |
paul@543 | 63 | /* Capability management. */ |
paul@543 | 64 | |
paul@543 | 65 | void ProcessMonitor::set_pager(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 66 | { |
paul@543 | 67 | _pager = cap; |
paul@543 | 68 | _mapped_pager = mapped_cap; |
paul@543 | 69 | } |
paul@543 | 70 | |
paul@543 | 71 | void ProcessMonitor::set_parent(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 72 | { |
paul@543 | 73 | _parent = cap; |
paul@543 | 74 | _mapped_parent = mapped_cap; |
paul@543 | 75 | } |
paul@543 | 76 | |
paul@543 | 77 | void ProcessMonitor::set_task(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 78 | { |
paul@543 | 79 | _task = cap; |
paul@543 | 80 | _mapped_task = mapped_cap; |
paul@543 | 81 | } |
paul@543 | 82 | |
paul@543 | 83 | void ProcessMonitor::set_thread(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 84 | { |
paul@543 | 85 | _thread = cap; |
paul@543 | 86 | _mapped_thread = mapped_cap; |
paul@543 | 87 | } |
paul@543 | 88 | |
paul@543 | 89 | |
paul@543 | 90 | |
paul@543 | 91 | /* Lifecycle management. */ |
paul@543 | 92 | |
paul@543 | 93 | void ProcessMonitor::pager_ended() |
paul@543 | 94 | { |
paul@543 | 95 | /* Release the program thread and its reference to this object. This will |
paul@543 | 96 | cause this object to be closed. */ |
paul@543 | 97 | |
paul@543 | 98 | if (l4_is_valid_cap(_thread)) |
paul@543 | 99 | { |
paul@543 | 100 | ipc_unmap_capability(_task, _mapped_thread); |
paul@543 | 101 | ipc_cap_free_um(_thread); |
paul@543 | 102 | _thread = L4_INVALID_CAP; |
paul@543 | 103 | |
paul@543 | 104 | ipc_unmap_capability(_task, _mapped_parent); |
paul@543 | 105 | } |
paul@543 | 106 | } |
paul@543 | 107 | |
paul@543 | 108 | |
paul@543 | 109 | |
paul@543 | 110 | /* Receive signals from a task. */ |
paul@543 | 111 | |
paul@543 | 112 | long ProcessMonitor::signal(unsigned long sig, unsigned long val) |
paul@543 | 113 | { |
paul@543 | 114 | notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); |
paul@543 | 115 | |
paul@543 | 116 | /* Handle the termination event. */ |
paul@543 | 117 | |
paul@543 | 118 | if (sig == 0) |
paul@543 | 119 | { |
paul@543 | 120 | printf("Signal from task.\n"); |
paul@543 | 121 | |
paul@543 | 122 | /* Once the program exits, the IPC gate connecting the program with its |
paul@543 | 123 | internal pager can be released. This will cause the internal pager to |
paul@543 | 124 | finish, which is then handled by the external pager. */ |
paul@543 | 125 | |
paul@543 | 126 | if (l4_is_valid_cap(_pager)) |
paul@543 | 127 | { |
paul@543 | 128 | ipc_cap_free_um(_pager); |
paul@543 | 129 | _pager = L4_INVALID_CAP; |
paul@543 | 130 | } |
paul@543 | 131 | |
paul@543 | 132 | /* The thread is not released here since the initiator of the process |
paul@543 | 133 | needs to be able to receive a reference to this object, but if the |
paul@543 | 134 | process terminates and the thread is released, this object may be |
paul@543 | 135 | released before the initiator obtains a reference to it. */ |
paul@543 | 136 | } |
paul@543 | 137 | |
paul@543 | 138 | return L4_EOK; |
paul@543 | 139 | } |
paul@543 | 140 | |
paul@543 | 141 | |
paul@543 | 142 | |
paul@543 | 143 | /* Subscribe to notifications. */ |
paul@543 | 144 | |
paul@543 | 145 | long ProcessMonitor::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@543 | 146 | { |
paul@543 | 147 | return NotificationSupport::subscribe(notifier, flags); |
paul@543 | 148 | } |
paul@543 | 149 | |
paul@543 | 150 | /* Unsubscribe from notifications. */ |
paul@543 | 151 | |
paul@543 | 152 | long ProcessMonitor::unsubscribe(l4_cap_idx_t notifier) |
paul@543 | 153 | { |
paul@543 | 154 | return NotificationSupport::unsubscribe(notifier); |
paul@543 | 155 | } |
paul@543 | 156 | |
paul@543 | 157 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@543 | 158 | */ |