1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libexec/lib/src/process_monitor.cc Fri Mar 24 00:46:26 2023 +0100
1.3 @@ -0,0 +1,158 @@
1.4 +/*
1.5 + * A process monitor abstraction.
1.6 + *
1.7 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <ipc/cap_alloc.h>
1.26 +#include <ipc/map.h>
1.27 +#include <systypes/base.h>
1.28 +
1.29 +#include <stdio.h>
1.30 +
1.31 +#include "process_monitor.h"
1.32 +#include "parent_notification_object_server.h"
1.33 +
1.34 +
1.35 +
1.36 +/* A process monitor receiving signals from a task. */
1.37 +
1.38 +ProcessMonitor::ProcessMonitor()
1.39 +: NotificationSupport()
1.40 +{
1.41 +}
1.42 +
1.43 +ipc_server_default_config_type ProcessMonitor::config()
1.44 +{
1.45 + return config_ParentNotificationObject;
1.46 +}
1.47 +
1.48 +
1.49 +
1.50 +/* Close the process monitor. */
1.51 +
1.52 +void ProcessMonitor::close()
1.53 +{
1.54 + printf("Process monitor closing...\n");
1.55 +
1.56 + if (l4_is_valid_cap(_task))
1.57 + {
1.58 + ipc_unmap_capability(_task, _mapped_task);
1.59 + ipc_cap_free_um(_task);
1.60 + _task = L4_INVALID_CAP;
1.61 + }
1.62 +}
1.63 +
1.64 +
1.65 +
1.66 +/* Capability management. */
1.67 +
1.68 +void ProcessMonitor::set_pager(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap)
1.69 +{
1.70 + _pager = cap;
1.71 + _mapped_pager = mapped_cap;
1.72 +}
1.73 +
1.74 +void ProcessMonitor::set_parent(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap)
1.75 +{
1.76 + _parent = cap;
1.77 + _mapped_parent = mapped_cap;
1.78 +}
1.79 +
1.80 +void ProcessMonitor::set_task(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap)
1.81 +{
1.82 + _task = cap;
1.83 + _mapped_task = mapped_cap;
1.84 +}
1.85 +
1.86 +void ProcessMonitor::set_thread(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap)
1.87 +{
1.88 + _thread = cap;
1.89 + _mapped_thread = mapped_cap;
1.90 +}
1.91 +
1.92 +
1.93 +
1.94 +/* Lifecycle management. */
1.95 +
1.96 +void ProcessMonitor::pager_ended()
1.97 +{
1.98 + /* Release the program thread and its reference to this object. This will
1.99 + cause this object to be closed. */
1.100 +
1.101 + if (l4_is_valid_cap(_thread))
1.102 + {
1.103 + ipc_unmap_capability(_task, _mapped_thread);
1.104 + ipc_cap_free_um(_thread);
1.105 + _thread = L4_INVALID_CAP;
1.106 +
1.107 + ipc_unmap_capability(_task, _mapped_parent);
1.108 + }
1.109 +}
1.110 +
1.111 +
1.112 +
1.113 +/* Receive signals from a task. */
1.114 +
1.115 +long ProcessMonitor::signal(unsigned long sig, unsigned long val)
1.116 +{
1.117 + notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val});
1.118 +
1.119 + /* Handle the termination event. */
1.120 +
1.121 + if (sig == 0)
1.122 + {
1.123 + printf("Signal from task.\n");
1.124 +
1.125 + /* Once the program exits, the IPC gate connecting the program with its
1.126 + internal pager can be released. This will cause the internal pager to
1.127 + finish, which is then handled by the external pager. */
1.128 +
1.129 + if (l4_is_valid_cap(_pager))
1.130 + {
1.131 + ipc_cap_free_um(_pager);
1.132 + _pager = L4_INVALID_CAP;
1.133 + }
1.134 +
1.135 + /* The thread is not released here since the initiator of the process
1.136 + needs to be able to receive a reference to this object, but if the
1.137 + process terminates and the thread is released, this object may be
1.138 + released before the initiator obtains a reference to it. */
1.139 + }
1.140 +
1.141 + return L4_EOK;
1.142 +}
1.143 +
1.144 +
1.145 +
1.146 +/* Subscribe to notifications. */
1.147 +
1.148 +long ProcessMonitor::subscribe(l4_cap_idx_t notifier, notify_flags_t flags)
1.149 +{
1.150 + return NotificationSupport::subscribe(notifier, flags);
1.151 +}
1.152 +
1.153 +/* Unsubscribe from notifications. */
1.154 +
1.155 +long ProcessMonitor::unsubscribe(l4_cap_idx_t notifier)
1.156 +{
1.157 + return NotificationSupport::unsubscribe(notifier);
1.158 +}
1.159 +
1.160 +/* vim: tabstop=2 expandtab shiftwidth=2
1.161 +*/