| 1 |
/* sysprof-instrument.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "sysprof-instrument-private.h" |
| 24 |
|
| 25 |
#if HAVE_POLKIT |
| 26 |
# include "sysprof-polkit-private.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
G_DEFINE_ABSTRACT_TYPE (SysprofInstrument, sysprof_instrument, G_TYPE_OBJECT) |
| 30 |
|
| 31 |
static char ** |
| 32 |
sysprof_instrument_real_list_required_policy (SysprofInstrument *instrument) |
| 33 |
{ |
| 34 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 35 |
|
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
static DexFuture * |
| 40 |
sysprof_instrument_real_prepare (SysprofInstrument *instrument, |
| 41 |
SysprofRecording *recording) |
| 42 |
{ |
| 43 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 44 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 45 |
|
| 46 |
return dex_future_new_for_boolean (TRUE); |
| 47 |
} |
| 48 |
|
| 49 |
static DexFuture * |
| 50 |
sysprof_instrument_real_record (SysprofInstrument *instrument, |
| 51 |
SysprofRecording *recording, |
| 52 |
GCancellable *cancellable) |
| 53 |
{ |
| 54 |
g_assert (SYSPROF_IS_INSTRUMENT (instrument)); |
| 55 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 56 |
g_assert (!cancellable || G_IS_CANCELLABLE (cancellable)); |
| 57 |
|
| 58 |
return dex_future_new_for_boolean (TRUE); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
sysprof_instrument_class_init (SysprofInstrumentClass *klass) |
| 63 |
{ |
| 64 |
klass->list_required_policy = sysprof_instrument_real_list_required_policy; |
| 65 |
klass->prepare = sysprof_instrument_real_prepare; |
| 66 |
klass->record = sysprof_instrument_real_record; |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
sysprof_instrument_init (SysprofInstrument *self) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
static char ** |
| 75 |
_sysprof_instrument_list_required_policy (SysprofInstrument *self) |
| 76 |
{ |
| 77 |
g_assert (SYSPROF_IS_INSTRUMENT (self)); |
| 78 |
|
| 79 |
if (SYSPROF_INSTRUMENT_GET_CLASS (self)->list_required_policy) |
| 80 |
return SYSPROF_INSTRUMENT_GET_CLASS (self)->list_required_policy (self); |
| 81 |
|
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
static DexFuture * |
| 86 |
_sysprof_instrument_prepare (SysprofInstrument *self, |
| 87 |
SysprofRecording *recording) |
| 88 |
{ |
| 89 |
g_assert (SYSPROF_IS_INSTRUMENT (self)); |
| 90 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 91 |
|
| 92 |
if (SYSPROF_INSTRUMENT_GET_CLASS (self)->prepare) |
| 93 |
return SYSPROF_INSTRUMENT_GET_CLASS (self)->prepare (self, recording); |
| 94 |
|
| 95 |
return dex_future_new_for_boolean (TRUE); |
| 96 |
} |
| 97 |
|
| 98 |
static DexFuture * |
| 99 |
_sysprof_instrument_record (SysprofInstrument *self, |
| 100 |
SysprofRecording *recording, |
| 101 |
GCancellable *cancellable) |
| 102 |
{ |
| 103 |
g_assert (SYSPROF_IS_INSTRUMENT (self)); |
| 104 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 105 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 106 |
|
| 107 |
if (SYSPROF_INSTRUMENT_GET_CLASS (self)->record) |
| 108 |
return SYSPROF_INSTRUMENT_GET_CLASS (self)->record (self, recording, cancellable); |
| 109 |
|
| 110 |
return dex_future_new_for_boolean (TRUE); |
| 111 |
} |
| 112 |
|
| 113 |
static DexFuture * |
| 114 |
_sysprof_instrument_augment (SysprofInstrument *self, |
| 115 |
SysprofRecording *recording) |
| 116 |
{ |
| 117 |
g_assert (SYSPROF_IS_INSTRUMENT (self)); |
| 118 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 119 |
|
| 120 |
if (SYSPROF_INSTRUMENT_GET_CLASS (self)->augment) |
| 121 |
return SYSPROF_INSTRUMENT_GET_CLASS (self)->augment (self, recording); |
| 122 |
|
| 123 |
return dex_future_new_for_boolean (TRUE); |
| 124 |
} |
| 125 |
|
| 126 |
static DexFuture * |
| 127 |
_sysprof_instrument_process_started (SysprofInstrument *self, |
| 128 |
SysprofRecording *recording, |
| 129 |
int pid, |
| 130 |
const char *comm) |
| 131 |
{ |
| 132 |
g_assert (SYSPROF_IS_INSTRUMENT (self)); |
| 133 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 134 |
|
| 135 |
if (SYSPROF_INSTRUMENT_GET_CLASS (self)->process_started) |
| 136 |
return SYSPROF_INSTRUMENT_GET_CLASS (self)->process_started (self, recording, pid, comm); |
| 137 |
|
| 138 |
return dex_future_new_for_boolean (TRUE); |
| 139 |
} |
| 140 |
|
| 141 |
static char ** |
| 142 |
_sysprof_instruments_list_required_policy (GPtrArray *instruments) |
| 143 |
{ |
| 144 |
g_autoptr(GPtrArray) all_policy = NULL; |
| 145 |
|
| 146 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 147 |
|
| 148 |
all_policy = g_ptr_array_new_null_terminated (0, g_free, TRUE); |
| 149 |
|
| 150 |
for (guint i = 0; i < instruments->len; i++) |
| 151 |
{ |
| 152 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 153 |
g_auto(GStrv) policy = _sysprof_instrument_list_required_policy (instrument); |
| 154 |
|
| 155 |
if (policy == NULL || policy[0] == NULL) |
| 156 |
continue; |
| 157 |
|
| 158 |
for (guint j = 0; policy[j]; j++) |
| 159 |
{ |
| 160 |
gboolean found = FALSE; |
| 161 |
|
| 162 |
for (guint k = 0; !found && k < all_policy->len; k++) |
| 163 |
found = strcmp (policy[j], g_ptr_array_index (all_policy, k)) == 0; |
| 164 |
|
| 165 |
if (!found) |
| 166 |
g_ptr_array_add (all_policy, g_strdup (policy[j])); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (all_policy->len == 0) |
| 171 |
return NULL; |
| 172 |
|
| 173 |
return (char **)g_ptr_array_free (g_steal_pointer (&all_policy), FALSE); |
| 174 |
} |
| 175 |
|
| 176 |
DexFuture * |
| 177 |
_sysprof_instruments_acquire_policy (GPtrArray *instruments, |
| 178 |
SysprofRecording *recording) |
| 179 |
{ |
| 180 |
g_autoptr(GDBusConnection) connection = NULL; |
| 181 |
g_autoptr(GError) error = NULL; |
| 182 |
g_auto(GStrv) required_policy = NULL; |
| 183 |
|
| 184 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 185 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (recording), NULL); |
| 186 |
|
| 187 |
/* Ensure we have access to the System D-Bus so that we can get |
| 188 |
* access to sysprofd for system information. |
| 189 |
*/ |
| 190 |
if (!(connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SYSTEM), &error))) |
| 191 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 192 |
|
| 193 |
/* First ensure that all our required policy have been acquired on |
| 194 |
* the bus so that we don't need to individually acquire them from |
| 195 |
* each of the instruments. |
| 196 |
*/ |
| 197 |
if ((required_policy = _sysprof_instruments_list_required_policy (instruments))) |
| 198 |
{ |
| 199 |
#if HAVE_POLKIT |
| 200 |
for (guint i = 0; required_policy[i]; i++) |
| 201 |
{ |
| 202 |
if (!dex_await_boolean (_sysprof_polkit_authorize (connection, |
| 203 |
required_policy[i], |
| 204 |
NULL, |
| 205 |
TRUE), &error)) |
| 206 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 207 |
} |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
return dex_future_new_for_boolean (TRUE); |
| 212 |
} |
| 213 |
|
| 214 |
DexFuture * |
| 215 |
_sysprof_instruments_prepare (GPtrArray *instruments, |
| 216 |
SysprofRecording *recording) |
| 217 |
{ |
| 218 |
g_autoptr(GPtrArray) futures = NULL; |
| 219 |
|
| 220 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 221 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (recording), NULL); |
| 222 |
|
| 223 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 224 |
|
| 225 |
for (guint i = 0; i < instruments->len; i++) |
| 226 |
{ |
| 227 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 228 |
|
| 229 |
g_ptr_array_add (futures, _sysprof_instrument_prepare (instrument, recording)); |
| 230 |
} |
| 231 |
|
| 232 |
if (futures->len == 0) |
| 233 |
return dex_future_new_for_boolean (TRUE); |
| 234 |
|
| 235 |
return dex_future_allv ((DexFuture **)futures->pdata, futures->len); |
| 236 |
} |
| 237 |
|
| 238 |
DexFuture * |
| 239 |
_sysprof_instruments_record (GPtrArray *instruments, |
| 240 |
SysprofRecording *recording, |
| 241 |
GCancellable *cancellable) |
| 242 |
{ |
| 243 |
g_autoptr(GPtrArray) futures = NULL; |
| 244 |
|
| 245 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 246 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (recording), NULL); |
| 247 |
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL); |
| 248 |
|
| 249 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 250 |
|
| 251 |
for (guint i = 0; i < instruments->len; i++) |
| 252 |
{ |
| 253 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 254 |
|
| 255 |
g_ptr_array_add (futures, _sysprof_instrument_record (instrument, recording, cancellable)); |
| 256 |
} |
| 257 |
|
| 258 |
if (futures->len == 0) |
| 259 |
return dex_future_new_for_boolean (TRUE); |
| 260 |
|
| 261 |
return dex_future_allv ((DexFuture **)futures->pdata, futures->len); |
| 262 |
} |
| 263 |
|
| 264 |
DexFuture * |
| 265 |
_sysprof_instruments_augment (GPtrArray *instruments, |
| 266 |
SysprofRecording *recording) |
| 267 |
{ |
| 268 |
g_autoptr(GPtrArray) futures = NULL; |
| 269 |
|
| 270 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 271 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (recording), NULL); |
| 272 |
|
| 273 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 274 |
|
| 275 |
for (guint i = 0; i < instruments->len; i++) |
| 276 |
{ |
| 277 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 278 |
|
| 279 |
g_ptr_array_add (futures, _sysprof_instrument_augment (instrument, recording)); |
| 280 |
} |
| 281 |
|
| 282 |
if (futures->len == 0) |
| 283 |
return dex_future_new_for_boolean (TRUE); |
| 284 |
|
| 285 |
return dex_future_allv ((DexFuture **)futures->pdata, futures->len); |
| 286 |
} |
| 287 |
|
| 288 |
DexFuture * |
| 289 |
_sysprof_instruments_process_started (GPtrArray *instruments, |
| 290 |
SysprofRecording *recording, |
| 291 |
int pid, |
| 292 |
const char *comm) |
| 293 |
{ |
| 294 |
g_autoptr(GPtrArray) futures = NULL; |
| 295 |
|
| 296 |
g_return_val_if_fail (instruments != NULL, NULL); |
| 297 |
g_return_val_if_fail (SYSPROF_IS_RECORDING (recording), NULL); |
| 298 |
|
| 299 |
futures = g_ptr_array_new_with_free_func (dex_unref); |
| 300 |
|
| 301 |
for (guint i = 0; i < instruments->len; i++) |
| 302 |
{ |
| 303 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 304 |
|
| 305 |
g_ptr_array_add (futures, _sysprof_instrument_process_started (instrument, recording, pid, comm)); |
| 306 |
} |
| 307 |
|
| 308 |
if (futures->len == 0) |
| 309 |
return dex_future_new_for_boolean (TRUE); |
| 310 |
|
| 311 |
return dex_future_allv ((DexFuture **)futures->pdata, futures->len); |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
_sysprof_instruments_set_connection (GPtrArray *instruments, |
| 316 |
GDBusConnection *connection) |
| 317 |
{ |
| 318 |
g_return_if_fail (instruments != NULL); |
| 319 |
g_return_if_fail (!connection || G_IS_DBUS_CONNECTION (connection)); |
| 320 |
|
| 321 |
for (guint i = 0; i < instruments->len; i++) |
| 322 |
{ |
| 323 |
SysprofInstrument *instrument = g_ptr_array_index (instruments, i); |
| 324 |
|
| 325 |
if (SYSPROF_INSTRUMENT_GET_CLASS (instrument)->set_connection) |
| 326 |
SYSPROF_INSTRUMENT_GET_CLASS (instrument)->set_connection (instrument, connection); |
| 327 |
} |
| 328 |
} |