Debian Codegraph @

src/libsysprof/sysprof-spawnable.c

raw
1 /* sysprof-spawnable.c
2 *
3 * Copyright 2019 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 <unistd.h>
24
25 #include <glib/gstdio.h>
26
27 #include "sysprof-spawnable.h"
28
29 typedef struct
30 {
31 gint dest_fd;
32 gint fd;
33 } FDMapping;
34
35 struct _SysprofSpawnable
36 {
37 GObject parent_instance;
38 GArray *fds;
39 GPtrArray *argv;
40 char **environ;
41 char *cwd;
42 gint next_fd;
43 GSubprocessFlags flags;
44 };
45
46 G_DEFINE_TYPE (SysprofSpawnable, sysprof_spawnable, G_TYPE_OBJECT)
47
48 /**
49 * sysprof_spawnable_new:
50 *
51 * Create a new #SysprofSpawnable.
52 *
53 * Returns: (transfer full): a newly created #SysprofSpawnable
54 */
55 SysprofSpawnable *
56 sysprof_spawnable_new (void)
57 {
58 return g_object_new (SYSPROF_TYPE_SPAWNABLE, NULL);
59 }
60
61 /**
62 * sysprof_spawnable_get_flags:
63 * @self: a #SysprofSpawnable
64 *
65 * Gets the subprocess flags for spawning.
66 *
67 * Returns: the #GSubprocessFlags bitwise-or'd
68 */
69 GSubprocessFlags
70 sysprof_spawnable_get_flags (SysprofSpawnable *self)
71 {
72 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), 0);
73
74 return self->flags;
75 }
76
77 /**
78 * sysprof_spawnable_set_flags:
79 * @self: a #SysprofSpawnable
80 *
81 * Set the flags to use when spawning the process.
82 */
83 void
84 sysprof_spawnable_set_flags (SysprofSpawnable *self,
85 GSubprocessFlags flags)
86 {
87 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
88
89 self->flags = flags;
90 }
91
92 static void
93 fd_mapping_clear (gpointer data)
94 {
95 FDMapping *map = data;
96
97 if (map->fd != -1)
98 close (map->fd);
99 }
100
101 static void
102 sysprof_spawnable_finalize (GObject *object)
103 {
104 SysprofSpawnable *self = (SysprofSpawnable *)object;
105
106 g_clear_pointer (&self->fds, g_array_unref);
107 g_clear_pointer (&self->argv, g_ptr_array_unref);
108 g_clear_pointer (&self->environ, g_strfreev);
109
110 G_OBJECT_CLASS (sysprof_spawnable_parent_class)->finalize (object);
111 }
112
113 static void
114 sysprof_spawnable_class_init (SysprofSpawnableClass *klass)
115 {
116 GObjectClass *object_class = G_OBJECT_CLASS (klass);
117
118 object_class->finalize = sysprof_spawnable_finalize;
119 }
120
121 static void
122 sysprof_spawnable_init (SysprofSpawnable *self)
123 {
124 self->next_fd = 3;
125
126 self->environ = g_get_environ ();
127
128 self->argv = g_ptr_array_new_with_free_func (g_free);
129 g_ptr_array_add (self->argv, NULL);
130
131 self->fds = g_array_new (FALSE, FALSE, sizeof (FDMapping));
132 g_array_set_clear_func (self->fds, fd_mapping_clear);
133 }
134
135 void
136 sysprof_spawnable_prepend_argv (SysprofSpawnable *self,
137 const gchar *argv)
138 {
139 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
140
141 if (argv != NULL)
142 g_ptr_array_insert (self->argv, 0, g_strdup (argv));
143 }
144
145 void
146 sysprof_spawnable_append_argv (SysprofSpawnable *self,
147 const gchar *argv)
148 {
149 gint pos;
150
151 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
152
153 if (argv == NULL)
154 return;
155
156 pos = self->argv->len - 1;
157 g_ptr_array_add (self->argv, NULL);
158 g_ptr_array_index (self->argv, pos) = g_strdup (argv);
159 }
160
161 void
162 sysprof_spawnable_append_args (SysprofSpawnable *self,
163 const gchar * const *args)
164 {
165 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
166
167 if (args == NULL)
168 return;
169
170 for (guint i = 0; args[i]; i++)
171 sysprof_spawnable_append_argv (self, args[i]);
172 }
173
174 const gchar * const *
175 sysprof_spawnable_get_argv (SysprofSpawnable *self)
176 {
177 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), NULL);
178
179 return (const gchar * const *)(gpointer)self->argv->pdata;
180 }
181
182 const gchar * const *
183 sysprof_spawnable_get_environ (SysprofSpawnable *self)
184 {
185 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), NULL);
186
187 return (const gchar * const *)self->environ;
188 }
189
190 void
191 sysprof_spawnable_setenv (SysprofSpawnable *self,
192 const gchar *key,
193 const gchar *value)
194 {
195 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
196 g_return_if_fail (key != NULL);
197
198 self->environ = g_environ_setenv (self->environ, key, value, TRUE);
199 }
200
201 void
202 sysprof_spawnable_set_environ (SysprofSpawnable *self,
203 const gchar * const *environ_)
204 {
205 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
206
207 if (environ_ != (const gchar * const *)self->environ)
208 {
209 g_strfreev (self->environ);
210 self->environ = g_strdupv ((gchar **)environ_);
211 }
212 }
213
214 const gchar *
215 sysprof_spawnable_getenv (SysprofSpawnable *self,
216 const gchar *key)
217 {
218 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), NULL);
219 g_return_val_if_fail (key != NULL, NULL);
220
221 return g_environ_getenv (self->environ, key);
222 }
223
224 gint
225 sysprof_spawnable_take_fd (SysprofSpawnable *self,
226 gint fd,
227 gint dest_fd)
228 {
229 FDMapping map;
230
231 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), -1);
232
233 if (dest_fd < 0)
234 dest_fd = self->next_fd++;
235
236 map.dest_fd = dest_fd;
237 map.fd = fd;
238
239 if (dest_fd >= self->next_fd)
240 self->next_fd = dest_fd + 1;
241
242 g_array_append_val (self->fds, map);
243
244 return dest_fd;
245 }
246
247 void
248 sysprof_spawnable_foreach_fd (SysprofSpawnable *self,
249 SysprofSpawnableFDForeach foreach,
250 gpointer user_data)
251 {
252 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
253 g_return_if_fail (foreach != NULL);
254
255 for (guint i = 0; i < self->fds->len; i++)
256 {
257 const FDMapping *map = &g_array_index (self->fds, FDMapping, i);
258
259 foreach (map->dest_fd, map->fd, user_data);
260 }
261 }
262
263 /**
264 * sysprof_spawnable_set_starting_fd:
265 *
266 * Sets the next FD number to use when mapping a child FD. This helps
267 * in situations where the embedder knows that some lower-numbered FDs
268 * will be taken and therefore unknown to the spawnable.
269 *
270 * The default for this is 2.
271 */
272 void
273 sysprof_spawnable_set_starting_fd (SysprofSpawnable *self,
274 gint starting_fd)
275 {
276 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
277
278 if (starting_fd < 0)
279 starting_fd = 2;
280
281 self->next_fd = starting_fd;
282 }
283
284 /**
285 * sysprof_spawnable_spawn:
286 *
287 * Creates a new subprocess using the configured options.
288 *
289 * Returns: (transfer full): a #GSubprocess or %NULL on failure and
290 * @error is set.
291 */
292 GSubprocess *
293 sysprof_spawnable_spawn (SysprofSpawnable *self,
294 GError **error)
295 {
296 g_autoptr(GSubprocessLauncher) launcher = NULL;
297 const gchar * const *argv;
298
299 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), NULL);
300
301 launcher = g_subprocess_launcher_new (self->flags);
302
303 g_subprocess_launcher_set_environ (launcher, self->environ);
304
305 if (self->cwd != NULL)
306 g_subprocess_launcher_set_cwd (launcher, self->cwd);
307 else
308 g_subprocess_launcher_set_cwd (launcher, g_get_home_dir ());
309
310 for (guint i = 0; i < self->fds->len; i++)
311 {
312 FDMapping *map = &g_array_index (self->fds, FDMapping, i);
313
314 g_subprocess_launcher_take_fd (launcher, map->fd, map->dest_fd);
315 map->fd = -1;
316 }
317
318 argv = sysprof_spawnable_get_argv (self);
319
320 return g_subprocess_launcher_spawnv (launcher, argv, error);
321 }
322
323 const char *
324 sysprof_spawnable_get_cwd (SysprofSpawnable *self)
325 {
326 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), NULL);
327
328 return self->cwd;
329 }
330
331 void
332 sysprof_spawnable_set_cwd (SysprofSpawnable *self,
333 const gchar *cwd)
334 {
335 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
336
337 g_set_str (&self->cwd, cwd);
338 }
339
340 /**
341 * sysprof_spawnable_add_trace_fd:
342 * @self: a #SysprofSpawnable
343 * @envvar: (nullable): the environment variable
344 *
345 * Adds an environment variable to the spawnable that will contain a
346 * "tracing file-descriptor". The spawned process can use
347 * `sysprof_capture_writer_new_from_env()` if @envvar is %NULL
348 * or with `getenv()` and `sysprof_capture_writer_new_from_fd()`.
349 *
350 * If @envvar is %NULL, "SYSPROF_TRACE_FD" will be used.
351 *
352 * The caller is responsible for closin the resulting FD.
353 *
354 * Returns: A file-descriptor which can be used to read the trace or
355 * -1 upon failure and `errno` is set. Caller must `close()` the
356 * FD if >= 0.
357 */
358 int
359 sysprof_spawnable_add_trace_fd (SysprofSpawnable *self,
360 const char *envvar)
361 {
362 g_autofd int fd = -1;
363 g_autofd int dest = -1;
364 g_autofree char *name = NULL;
365 g_autofree char *fdstr = NULL;
366 int id_in_child;
367
368 g_return_val_if_fail (SYSPROF_IS_SPAWNABLE (self), -1);
369
370 if (envvar == NULL)
371 envvar = "SYSPROF_TRACE_FD";
372
373 name = g_strdup_printf ("[sysprof-tracefd:%s]", envvar);
374
375 if (-1 == (fd = sysprof_memfd_create (name)))
376 return -1;
377
378 if (-1 == (dest = dup (fd)))
379 return -1;
380
381 id_in_child = sysprof_spawnable_take_fd (self, g_steal_fd (&dest), -1);
382
383 fdstr = g_strdup_printf ("%d", id_in_child);
384 sysprof_spawnable_setenv (self, envvar, fdstr);
385
386 return g_steal_fd (&fd);
387 }
388
389 void
390 sysprof_spawnable_add_ld_preload (SysprofSpawnable *self,
391 const char *library_path)
392 {
393 g_autofree char *amended = NULL;
394 const char *val;
395
396 g_return_if_fail (SYSPROF_IS_SPAWNABLE (self));
397 g_return_if_fail (library_path != NULL);
398
399 if ((val = sysprof_spawnable_getenv (self, "LD_PRELOAD")))
400 library_path = amended = g_strdup_printf ("%s:%s", val, library_path);
401
402 sysprof_spawnable_setenv (self, "LD_PRELOAD", library_path);
403 }