| 1 |
/* |
| 2 |
* ipc-unwinder-impl.c |
| 3 |
* |
| 4 |
* Copyright 2024 Christian Hergert <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (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, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#define G_LOG_DOMAIN "ipc-unwinder-impl" |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
|
| 29 |
#include <signal.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
|
| 32 |
#include <glib/gstdio.h> |
| 33 |
|
| 34 |
#include <polkit/polkit.h> |
| 35 |
|
| 36 |
#include "ipc-unwinder-impl.h" |
| 37 |
|
| 38 |
struct _IpcUnwinderImpl |
| 39 |
{ |
| 40 |
IpcUnwinderSkeleton parent_instance; |
| 41 |
}; |
| 42 |
|
| 43 |
static void |
| 44 |
ipc_unwinder_impl_wait_cb (GObject *object, |
| 45 |
GAsyncResult *result, |
| 46 |
gpointer user_data) |
| 47 |
{ |
| 48 |
GSubprocess *subprocess = (GSubprocess *)object; |
| 49 |
g_autoptr(IpcUnwinderImpl) self = user_data; |
| 50 |
g_autoptr(GError) error = NULL; |
| 51 |
|
| 52 |
g_assert (G_IS_SUBPROCESS (subprocess)); |
| 53 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 54 |
g_assert (IPC_IS_UNWINDER_IMPL (self)); |
| 55 |
|
| 56 |
if (!g_subprocess_wait_check_finish (subprocess, result, &error)) |
| 57 |
g_warning ("wait_check failure: %s", error->message); |
| 58 |
else |
| 59 |
g_info ("sysprof-live-unwinder exited"); |
| 60 |
} |
| 61 |
|
| 62 |
static gboolean |
| 63 |
ipc_unwinder_impl_handle_unwind (IpcUnwinder *unwinder, |
| 64 |
GDBusMethodInvocation *invocation, |
| 65 |
GUnixFDList *fd_list, |
| 66 |
guint stack_size, |
| 67 |
GVariant *arg_perf_fds, |
| 68 |
GVariant *arg_event_fd, |
| 69 |
GVariant *arg_capture_fd) |
| 70 |
{ |
| 71 |
g_autoptr(GSubprocessLauncher) launcher = NULL; |
| 72 |
g_autoptr(GSubprocess) subprocess = NULL; |
| 73 |
g_autoptr(GPtrArray) argv = NULL; |
| 74 |
g_autoptr(GError) error = NULL; |
| 75 |
g_autofd int capture_fd = -1; |
| 76 |
g_autofd int event_fd = -1; |
| 77 |
GVariantIter iter; |
| 78 |
int next_target_fd = 3; |
| 79 |
int perf_fd_handle; |
| 80 |
int cpu; |
| 81 |
|
| 82 |
g_assert (IPC_IS_UNWINDER_IMPL (unwinder)); |
| 83 |
g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation)); |
| 84 |
g_assert (!fd_list || G_IS_UNIX_FD_LIST (fd_list)); |
| 85 |
|
| 86 |
if (stack_size == 0 || stack_size % sysconf (_SC_PAGESIZE) != 0) |
| 87 |
{ |
| 88 |
g_dbus_method_invocation_return_error_literal (g_steal_pointer (&invocation), |
| 89 |
G_DBUS_ERROR, |
| 90 |
G_DBUS_ERROR_INVALID_ARGS, |
| 91 |
"Stack size must be a multiple of the page size"); |
| 92 |
return TRUE; |
| 93 |
} |
| 94 |
|
| 95 |
if (fd_list == NULL) |
| 96 |
{ |
| 97 |
g_dbus_method_invocation_return_error_literal (g_steal_pointer (&invocation), |
| 98 |
G_DBUS_ERROR, |
| 99 |
G_DBUS_ERROR_FILE_NOT_FOUND, |
| 100 |
"Missing perf FDs"); |
| 101 |
return TRUE; |
| 102 |
} |
| 103 |
|
| 104 |
launcher = g_subprocess_launcher_new (0); |
| 105 |
argv = g_ptr_array_new_with_free_func (g_free); |
| 106 |
|
| 107 |
g_ptr_array_add (argv, g_strdup (PACKAGE_LIBEXECDIR "/sysprof-live-unwinder")); |
| 108 |
g_ptr_array_add (argv, g_strdup_printf ("--stack-size=%u", stack_size)); |
| 109 |
|
| 110 |
if (-1 == (event_fd = g_unix_fd_list_get (fd_list, g_variant_get_handle (arg_event_fd), &error)) || |
| 111 |
-1 == (capture_fd = g_unix_fd_list_get (fd_list, g_variant_get_handle (arg_capture_fd), &error))) |
| 112 |
{ |
| 113 |
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error); |
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
g_ptr_array_add (argv, g_strdup_printf ("--event-fd=%d", next_target_fd)); |
| 118 |
g_subprocess_launcher_take_fd (launcher, g_steal_fd (&event_fd), next_target_fd++); |
| 119 |
|
| 120 |
g_variant_iter_init (&iter, arg_perf_fds); |
| 121 |
|
| 122 |
while (g_variant_iter_loop (&iter, "(hi)", &perf_fd_handle, &cpu)) |
| 123 |
{ |
| 124 |
g_autofd int perf_fd = g_unix_fd_list_get (fd_list, perf_fd_handle, &error); |
| 125 |
|
| 126 |
if (perf_fd < 0) |
| 127 |
{ |
| 128 |
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error); |
| 129 |
return TRUE; |
| 130 |
} |
| 131 |
|
| 132 |
g_ptr_array_add (argv, g_strdup_printf ("--perf-fd=%d:%d", next_target_fd, cpu)); |
| 133 |
g_subprocess_launcher_take_fd (launcher, |
| 134 |
g_steal_fd (&perf_fd), |
| 135 |
next_target_fd++); |
| 136 |
} |
| 137 |
|
| 138 |
g_ptr_array_add (argv, g_strdup_printf ("--capture-fd=%d", next_target_fd)); |
| 139 |
g_subprocess_launcher_take_fd (launcher, g_steal_fd (&capture_fd), next_target_fd++); |
| 140 |
|
| 141 |
g_ptr_array_add (argv, NULL); |
| 142 |
|
| 143 |
if (!(subprocess = g_subprocess_launcher_spawnv (launcher, (const char * const *)argv->pdata, &error))) |
| 144 |
{ |
| 145 |
g_critical ("Failed to start sysprof-live-unwinder: %s", error->message); |
| 146 |
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error); |
| 147 |
return TRUE; |
| 148 |
} |
| 149 |
|
| 150 |
g_message ("sysprof-live-unwinder started as process %s", |
| 151 |
g_subprocess_get_identifier (subprocess)); |
| 152 |
|
| 153 |
ipc_unwinder_complete_unwind (unwinder, g_steal_pointer (&invocation), NULL); |
| 154 |
|
| 155 |
g_subprocess_wait_check_async (subprocess, |
| 156 |
NULL, |
| 157 |
ipc_unwinder_impl_wait_cb, |
| 158 |
g_object_ref (unwinder)); |
| 159 |
|
| 160 |
return TRUE; |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
unwinder_iface_init (IpcUnwinderIface *iface) |
| 165 |
{ |
| 166 |
iface->handle_unwind = ipc_unwinder_impl_handle_unwind; |
| 167 |
} |
| 168 |
|
| 169 |
G_DEFINE_FINAL_TYPE_WITH_CODE (IpcUnwinderImpl, ipc_unwinder_impl, IPC_TYPE_UNWINDER_SKELETON, |
| 170 |
G_IMPLEMENT_INTERFACE (IPC_TYPE_UNWINDER, unwinder_iface_init)) |
| 171 |
|
| 172 |
static gboolean |
| 173 |
ipc_unwinder_impl_g_authorize_method (GDBusInterfaceSkeleton *skeleton, |
| 174 |
GDBusMethodInvocation *invocation) |
| 175 |
{ |
| 176 |
PolkitAuthorizationResult *res = NULL; |
| 177 |
PolkitAuthority *authority = NULL; |
| 178 |
PolkitSubject *subject = NULL; |
| 179 |
const gchar *peer_name; |
| 180 |
gboolean ret = TRUE; |
| 181 |
|
| 182 |
g_assert (IPC_IS_UNWINDER_IMPL (skeleton)); |
| 183 |
g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation)); |
| 184 |
|
| 185 |
peer_name = g_dbus_method_invocation_get_sender (invocation); |
| 186 |
|
| 187 |
if (!(authority = polkit_authority_get_sync (NULL, NULL)) || |
| 188 |
!(subject = polkit_system_bus_name_new (peer_name)) || |
| 189 |
!(res = polkit_authority_check_authorization_sync (authority, |
| 190 |
POLKIT_SUBJECT (subject), |
| 191 |
"org.gnome.sysprof3.profile", |
| 192 |
NULL, |
| 193 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 194 |
NULL, |
| 195 |
NULL)) || |
| 196 |
!polkit_authorization_result_get_is_authorized (res)) |
| 197 |
{ |
| 198 |
g_dbus_method_invocation_return_error (g_steal_pointer (&invocation), |
| 199 |
G_DBUS_ERROR, |
| 200 |
G_DBUS_ERROR_ACCESS_DENIED, |
| 201 |
"Not authorized to make request"); |
| 202 |
ret = FALSE; |
| 203 |
} |
| 204 |
|
| 205 |
g_clear_object (&authority); |
| 206 |
g_clear_object (&subject); |
| 207 |
g_clear_object (&res); |
| 208 |
|
| 209 |
return ret; |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
ipc_unwinder_impl_finalize (GObject *object) |
| 214 |
{ |
| 215 |
G_OBJECT_CLASS (ipc_unwinder_impl_parent_class)->finalize (object); |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
ipc_unwinder_impl_class_init (IpcUnwinderImplClass *klass) |
| 220 |
{ |
| 221 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 222 |
GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass); |
| 223 |
|
| 224 |
object_class->finalize = ipc_unwinder_impl_finalize; |
| 225 |
|
| 226 |
skeleton_class->g_authorize_method = ipc_unwinder_impl_g_authorize_method; |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
ipc_unwinder_impl_init (IpcUnwinderImpl *self) |
| 231 |
{ |
| 232 |
} |
| 233 |
|
| 234 |
IpcUnwinder * |
| 235 |
ipc_unwinder_impl_new (void) |
| 236 |
{ |
| 237 |
return g_object_new (IPC_TYPE_UNWINDER_IMPL, NULL); |
| 238 |
} |