Debian Codegraph @

lib/packagekit-glib2/pk-client-helper.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010 Richard Hughes <[email protected]>
4 *
5 * Loosely based on apt-daemon/debconf.py, which is:
6 * Copyright (C) 2009 Sebastian Heinlein <[email protected]>
7 * Copyright (C) 2009 Michael Vogt <[email protected]>
8 *
9 * Licensed under the GNU Lesser General Public License Version 2.1
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 /**
27 * SECTION:pk-client-helper
28 * @short_description: helper object to run a helper session process for the lifetime of a transaction
29 *
30 * This GObject can be used to run a session helper program out of band
31 * with the normal PackageKit transaction. This allows an external program
32 * such as debconf to be used that needs direct console access.
33 *
34 *
35 * client ----> packagekit-glib ---> dbus ---> packagekitd ---> apt
36 * .------------^ ^
37 * debconf ___________________ (SetHints) |
38 * | \___| PkClientHelper \__.____.____.______/_.____.____.__/
39 * ^-----|___________________/ (socket in tmp)
40 * (stdin & stdout )
41 *
42 * \------------.------------------/ \------------.---------/
43 * | |
44 * user session system context
45 */
46
47 #include "config.h"
48
49 #include <signal.h>
50 #include <errno.h>
51 #include <glib-object.h>
52 #include <gio/gunixsocketaddress.h>
53
54 #include <packagekit-glib2/pk-client-helper.h>
55
56 static void pk_client_helper_finalize (GObject *object);
57
58 /**
59 * PkClientHelperPrivate:
60 *
61 * Private #PkClientHelper data
62 **/
63 struct _PkClientHelperPrivate
64 {
65 gchar **argv;
66 gchar **envp;
67 GFile *socket_file;
68 GSocket *socket;
69 GIOChannel *socket_channel;
70 GSource *socket_channel_source;
71 GPtrArray *children;
72 GPid kde_helper_pid;
73 };
74
75 typedef struct
76 {
77 PkClientHelper *helper;
78 GSocket *socket;
79 GIOChannel *socket_channel;
80 GSource *socket_channel_source;
81 GPid pid;
82 GIOChannel *stdin_channel;
83 GIOChannel *stdout_channel;
84 GIOChannel *stderr_channel;
85 GSource *stdout_channel_source;
86 GSource *stderr_channel_source;
87 } PkClientHelperChild;
88
89 G_DEFINE_TYPE_WITH_PRIVATE (PkClientHelper, pk_client_helper, G_TYPE_OBJECT)
90 #define GET_PRIVATE(o) (pk_client_helper_get_instance_private (o))
91
92 static void
93 pk_client_helper_child_free (PkClientHelperChild *child)
94 {
95 if (child->socket != NULL)
96 g_object_unref (child->socket);
97 if (child->socket_channel != NULL)
98 g_io_channel_unref (child->socket_channel);
99 if (child->socket_channel_source != NULL) {
100 g_source_destroy (child->socket_channel_source);
101 g_source_unref (child->socket_channel_source);
102 }
103 if (child->pid > 0)
104 kill (child->pid, SIGTERM);
105 if (child->stdin_channel != NULL)
106 g_io_channel_unref (child->stdin_channel);
107 if (child->stdout_channel != NULL)
108 g_io_channel_unref (child->stdout_channel);
109 if (child->stderr_channel != NULL)
110 g_io_channel_unref (child->stderr_channel);
111 if (child->stdout_channel_source != NULL) {
112 g_source_destroy (child->stdout_channel_source);
113 g_source_unref (child->stdout_channel_source);
114 }
115 if (child->stderr_channel_source != NULL) {
116 g_source_destroy (child->stderr_channel_source);
117 g_source_unref (child->stderr_channel_source);
118 }
119 }
120
121 /**
122 * pk_client_helper_stop:
123 * @client_helper: a valid #PkClientHelper instance
124 * @error: A #GError or %NULL
125 *
126 * Stops the helper process, by killing the helper process and deleting
127 * the socket.
128 *
129 * Return value: %TRUE for success
130 *
131 * Since: 0.6.10
132 **/
133 gboolean
134 pk_client_helper_stop (PkClientHelper *client_helper, GError **error)
135 {
136 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
137
138 g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE);
139 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
140
141 /* make sure started */
142 g_return_val_if_fail (priv->socket_file != NULL, FALSE);
143
144 /* close the socket */
145 if (priv->socket_channel_source != NULL)
146 g_source_destroy (priv->socket_channel_source);
147 if (priv->socket != NULL) {
148 if (!g_socket_close (priv->socket, error))
149 return FALSE;
150 }
151
152 /* kill any children */
153 for (guint i = 0; i < priv->children->len; i++) {
154 PkClientHelperChild *child = g_ptr_array_index (priv->children, i);
155 int retval;
156
157 g_debug ("sending SIGTERM %ld", (long)child->pid);
158 retval = kill (child->pid, SIGTERM);
159 if (retval == EINVAL) {
160 g_set_error (error, 1, 0, "failed to kill, signum argument is invalid");
161 return FALSE;
162 }
163 if (retval == EPERM) {
164 g_set_error (error, 1, 0, "failed to kill, no permission");
165 return FALSE;
166 }
167 }
168
169 /* remove any socket file */
170 if (g_file_query_exists (priv->socket_file, NULL)) {
171 if (!g_file_delete (priv->socket_file, NULL, error))
172 return FALSE;
173 }
174
175 return TRUE;
176 }
177
178 /*
179 * pk_client_helper_copy_stdout_cb:
180 **/
181 static gboolean
182 pk_client_helper_copy_stdout_cb (GIOChannel *source, GIOCondition condition, PkClientHelperChild *child)
183 {
184 gchar data[1024];
185 gsize len = 0;
186 gsize written = 0;
187 GIOStatus status;
188 g_autoptr(GError) error = NULL;
189
190 /* read data */
191 status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, &error);
192 if (status == G_IO_STATUS_EOF) {
193 g_debug ("helper process exited");
194 if (!g_socket_close (child->socket, &error))
195 g_warning ("failed to close socket");
196 return G_SOURCE_REMOVE;
197 }
198 if (len == 0)
199 return G_SOURCE_CONTINUE;
200
201 /* write to socket */
202 data[len] = '\0';
203 g_debug ("child has input to push to the socket: %s", data);
204 status = g_io_channel_write_chars (child->socket_channel, data, len, &written, &error);
205 if (status != G_IO_STATUS_NORMAL) {
206 g_warning ("failed to write to socket: %s", error ? error->message : "Unknown error");
207 return G_SOURCE_REMOVE;
208 }
209 if (written != len) {
210 g_warning ("failed to write %" G_GSIZE_FORMAT " bytes, "
211 "only wrote %" G_GSIZE_FORMAT " bytes", len, written);
212 return G_SOURCE_REMOVE;
213 }
214 g_debug ("wrote %" G_GSIZE_FORMAT " bytes to socket", written);
215
216 return G_SOURCE_CONTINUE;
217 }
218
219 /*
220 * pk_client_helper_echo_stderr_cb:
221 **/
222 static gboolean
223 pk_client_helper_echo_stderr_cb (GIOChannel *source, GIOCondition condition, PkClientHelper *client_helper)
224 {
225 gchar data[1024];
226 gsize len = 0;
227 GIOStatus status;
228
229 /* read data */
230 status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, NULL);
231 if (status == G_IO_STATUS_EOF) {
232 return G_SOURCE_REMOVE;
233 }
234 if (len == 0)
235 return G_SOURCE_CONTINUE;
236
237 /* write to socket */
238 data[len] = '\0';
239 g_debug ("child has error: %s", data);
240
241 return G_SOURCE_CONTINUE;
242 }
243
244 /*
245 * pk_client_helper_copy_conn_cb:
246 **/
247 static gboolean
248 pk_client_helper_copy_conn_cb (GIOChannel *source, GIOCondition condition, PkClientHelperChild *child)
249 {
250 PkClientHelperPrivate *helper_priv = pk_client_helper_get_instance_private (child->helper);
251 gchar data[1024];
252 gsize len = 0;
253 GIOStatus status;
254 gsize written = 0;
255 g_autoptr(GError) error = NULL;
256
257 status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, &error);
258 if (status == G_IO_STATUS_EOF) {
259 g_debug ("socket hung up");
260
261 /* Shutdown helper */
262 if (!g_io_channel_shutdown (child->stdin_channel, TRUE, &error))
263 g_warning ("failed to close connection to child: %s", error->message);
264 return G_SOURCE_REMOVE;
265 }
266 if (error != NULL) {
267 g_warning ("failed to read: %s", error->message);
268 return G_SOURCE_REMOVE;
269 }
270 if (len == 0)
271 return G_SOURCE_CONTINUE;
272
273 /* write to child */
274 data[len] = '\0';
275 g_debug ("socket has data to push to child: '%s'", data);
276 status = g_io_channel_write_chars (child->stdin_channel, data, len, &written, &error);
277 if (status != G_IO_STATUS_NORMAL) {
278 g_warning ("failed to write to stdin: %s", error ? error->message : "Unknown error");
279 return G_SOURCE_REMOVE;
280 }
281 if (written != len) {
282 g_warning ("failed to write %" G_GSIZE_FORMAT " bytes, "
283 "only wrote %" G_GSIZE_FORMAT " bytes", len, written);
284 return G_SOURCE_REMOVE;
285 }
286 g_debug ("wrote %" G_GSIZE_FORMAT " bytes to stdin of %s", written, helper_priv->argv[0]);
287
288 return G_SOURCE_CONTINUE;
289 }
290
291 static GSource *
292 make_input_source (GIOChannel *channel, GSourceFunc func, gpointer user_data)
293 {
294 GSource *source;
295 GMainContext *context;
296
297 source = g_io_create_watch (channel, G_IO_IN);
298 g_source_set_callback (source, func, user_data, NULL);
299
300 context = g_main_context_get_thread_default ();
301 if (context == NULL)
302 context = g_main_context_default ();
303 g_source_attach (source, context);
304
305 return source;
306 }
307
308 /*
309 * pk_client_helper_accept_connection_cb:
310 **/
311 static gboolean
312 pk_client_helper_accept_connection_cb (GIOChannel *source, GIOCondition condition, PkClientHelper *client_helper)
313 {
314 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
315 g_autoptr(GSocket) socket = NULL;
316 GPid pid;
317 gint standard_input = 0;
318 gint standard_output = 0;
319 gint standard_error = 0;
320 gint fd;
321 GIOStatus status;
322 PkClientHelperChild *child;
323 g_autoptr(GError) error = NULL;
324
325 /* accept the connection request */
326 socket = g_socket_accept (priv->socket, NULL, &error);
327 if (socket == NULL) {
328 g_warning ("failed to accept socket: %s", error->message);
329 return G_SOURCE_CONTINUE;
330 }
331 g_debug ("accepting connection %i for socket %i",
332 g_socket_get_fd (socket),
333 g_socket_get_fd (priv->socket));
334
335 /* spawn helper executable */
336 if (!g_spawn_async_with_pipes (NULL, priv->argv, priv->envp, 0, NULL, NULL, &pid,
337 &standard_input, &standard_output, &standard_error, &error)) {
338 g_warning ("failed to spawn: %s", error->message);
339 return G_SOURCE_CONTINUE;
340 }
341 g_debug ("started process %s with pid %i", priv->argv[0], pid);
342
343 child = g_slice_new0 (PkClientHelperChild);
344 g_ptr_array_add (priv->children, child);
345 child->helper = client_helper;
346 child->socket = g_steal_pointer (&socket);
347 child->pid = pid;
348 child->stdin_channel = g_io_channel_unix_new (standard_input);
349 child->stdout_channel = g_io_channel_unix_new (standard_output);
350 child->stderr_channel = g_io_channel_unix_new (standard_error);
351
352 /* binary data */
353 status = g_io_channel_set_encoding (child->stdin_channel, NULL, &error);
354 if (status != G_IO_STATUS_NORMAL) {
355 g_warning ("failed to set encoding: %s", error->message);
356 return G_SOURCE_CONTINUE;
357 }
358 g_io_channel_set_buffered (child->stdin_channel, FALSE);
359
360 /* binary data */
361 status = g_io_channel_set_encoding (child->stdout_channel, NULL, &error);
362 if (status != G_IO_STATUS_NORMAL) {
363 g_warning ("failed to set encoding: %s", error->message);
364 return G_SOURCE_CONTINUE;
365 }
366 g_io_channel_set_buffered (child->stdout_channel, FALSE);
367
368 /* binary data */
369 status = g_io_channel_set_encoding (child->stderr_channel, NULL, &error);
370 if (status != G_IO_STATUS_NORMAL) {
371 g_warning ("failed to set encoding: %s", error->message);
372 return G_SOURCE_CONTINUE;
373 }
374 g_io_channel_set_buffered (child->stderr_channel, FALSE);
375
376 /* socket has data */
377 fd = g_socket_get_fd (child->socket);
378 child->socket_channel = g_io_channel_unix_new (fd);
379 child->socket_channel_source =
380 make_input_source (child->socket_channel, G_SOURCE_FUNC (pk_client_helper_copy_conn_cb), child);
381 /* binary data */
382 status = g_io_channel_set_encoding (child->socket_channel, NULL, &error);
383 if (status != G_IO_STATUS_NORMAL) {
384 g_warning ("failed to set encoding: %s", error->message);
385 return G_SOURCE_CONTINUE;
386 }
387 g_io_channel_set_buffered (child->socket_channel, FALSE);
388
389 /* frontend has data */
390 child->stdout_channel_source =
391 make_input_source (child->stdout_channel, G_SOURCE_FUNC (pk_client_helper_copy_stdout_cb), child);
392 child->stderr_channel_source =
393 make_input_source (child->stderr_channel, G_SOURCE_FUNC (pk_client_helper_echo_stderr_cb), child);
394 return G_SOURCE_CONTINUE;
395 }
396
397 /**
398 * pk_client_helper_start:
399 * @client_helper: a valid #PkClientHelper instance
400 * @socket_filename: a socket filename that does not already exist
401 * @argv: the executable, along with any arguments
402 * @envp: the environment
403 * @error: A #GError or %NULL
404 *
405 * Starts the helper process, by running the helper process and setting
406 * up the socket for use.
407 *
408 * Return value: %TRUE for success
409 *
410 * Since: 0.6.10
411 **/
412 gboolean
413 pk_client_helper_start (PkClientHelper *client_helper,
414 const gchar *socket_filename,
415 gchar **argv, gchar **envp,
416 GError **error)
417 {
418 gboolean use_kde_helper = FALSE;
419 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
420 g_autoptr(GError) error_local = NULL;
421 g_autoptr(GSocketAddress) address = NULL;
422 const gchar *kde_helper_path = "/usr/libexec/debconf-kde-helper";
423
424 g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE);
425 g_return_val_if_fail (socket_filename != NULL, FALSE);
426 g_return_val_if_fail (argv != NULL, FALSE);
427 g_return_val_if_fail (socket_filename != NULL, FALSE);
428 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
429
430 /* make sure not been started before */
431 g_return_val_if_fail (priv->argv == NULL, FALSE);
432 g_return_val_if_fail (priv->socket_file == NULL, FALSE);
433
434 /* already exists? */
435 if (g_file_test (socket_filename, G_FILE_TEST_EXISTS)) {
436 g_set_error (error, 1, 0, "socket %s already exists", socket_filename);
437 return FALSE;
438 }
439
440 g_debug ("using socket in %s", socket_filename);
441 priv->socket_file = g_file_new_for_path (socket_filename);
442
443 /* preconfigure KDE frontend, if requested */
444 if (envp != NULL) {
445 for (guint i = 0; envp[i] != NULL; i++) {
446 if (g_strcmp0 (envp[i], "DEBIAN_FRONTEND=kde") == 0) {
447 if (g_file_test (kde_helper_path, G_FILE_TEST_EXISTS)) {
448 use_kde_helper = TRUE;
449 } else {
450 /* try alternative path */
451 kde_helper_path = "/usr/bin/debconf-kde-helper";
452 if (g_file_test (kde_helper_path, G_FILE_TEST_EXISTS))
453 use_kde_helper = TRUE;
454 }
455
456 break;
457 }
458 }
459 }
460
461 /* create socket */
462 priv->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, error);
463 if (priv->socket == NULL)
464 return FALSE;
465
466 /* bind to the socket */
467 address = g_unix_socket_address_new (socket_filename);
468 if (!g_socket_bind (priv->socket, address, TRUE, error))
469 return FALSE;
470
471 /* spawn KDE debconf communicator */
472 if (use_kde_helper) {
473 g_debug ("Using KDE debconf communicator: %s", kde_helper_path);
474
475 priv->envp = g_strdupv (envp);
476 priv->argv = g_new0 (gchar *, 2);
477 priv->argv[0] = g_strdup (kde_helper_path);
478 priv->argv[1] = g_strconcat ("--socket-path", "=", socket_filename, NULL);
479
480 if (!g_spawn_async (NULL,
481 priv->argv,
482 priv->envp,
483 G_SPAWN_STDOUT_TO_DEV_NULL,
484 NULL,
485 NULL,
486 &priv->kde_helper_pid,
487 &error_local)) {
488 g_warning ("failed to spawn: %s", error_local->message);
489 return FALSE;
490 }
491 g_debug ("started process %s with pid %i", priv->argv[0], priv->kde_helper_pid);
492 return TRUE;
493 }
494
495 /* listen to the socket */
496 if (!g_socket_listen (priv->socket, error))
497 return FALSE;
498
499 return pk_client_helper_start_with_socket (client_helper, priv->socket, argv, envp, error);
500 }
501
502 /**
503 * pk_client_helper_start_with_socket:
504 * @client_helper: a valid #PkClientHelper instance
505 * @socket: the (bound and listening) #GSocket instance to use
506 * @argv: the executable, along with any arguments
507 * @envp: the environment
508 * @error: A #GError or %NULL
509 *
510 * Starts the helper process, by running the helper process and setting
511 * up the socket for use.
512 *
513 * Return value: %TRUE for success
514 *
515 * Since: 1.1.13
516 **/
517 gboolean
518 pk_client_helper_start_with_socket (PkClientHelper *client_helper,
519 GSocket *socket,
520 gchar **argv, gchar **envp,
521 GError **error)
522 {
523 gint fd;
524 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
525 g_autoptr(GError) error_local = NULL;
526 g_autoptr(GSocketAddress) address = NULL;
527
528 g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE);
529 g_return_val_if_fail (socket != NULL, FALSE);
530 g_return_val_if_fail (argv != NULL, FALSE);
531 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
532
533 /* make sure not been started before */
534 g_return_val_if_fail (priv->argv == NULL, FALSE);
535
536 /* cache for actual start */
537 priv->argv = g_strdupv (argv);
538 priv->envp = g_strdupv (envp);
539
540 /* Set the socket */
541 g_set_object (&priv->socket, socket);
542
543 /* socket has data */
544 fd = g_socket_get_fd (priv->socket);
545 priv->socket_channel = g_io_channel_unix_new (fd);
546 priv->socket_channel_source =
547 make_input_source (priv->socket_channel, G_SOURCE_FUNC (pk_client_helper_accept_connection_cb), client_helper);
548 return TRUE;
549 }
550
551 /**
552 * pk_client_helper_is_active:
553 * @client_helper: a valid #PkClientHelper instance
554 *
555 * Return value: TRUE if there is an accepted connection, FALSE
556 * otherwise.
557 *
558 * Since: 1.1.13
559 */
560 gboolean
561 pk_client_helper_is_active (PkClientHelper *client_helper)
562 {
563 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
564
565 g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE);
566
567 for (guint i = 0; i < priv->children->len; i++) {
568 PkClientHelperChild *child = g_ptr_array_index (priv->children, i);
569 if (!g_source_is_destroyed (child->socket_channel_source) &&
570 !g_source_is_destroyed (child->stdout_channel_source))
571 return TRUE;
572 }
573
574 return FALSE;
575 }
576
577 /*
578 * pk_client_helper_class_init:
579 **/
580 static void
581 pk_client_helper_class_init (PkClientHelperClass *klass)
582 {
583 GObjectClass *object_class = G_OBJECT_CLASS (klass);
584 object_class->finalize = pk_client_helper_finalize;
585 }
586
587 /*
588 * pk_client_helper_init:
589 **/
590 static void
591 pk_client_helper_init (PkClientHelper *client_helper)
592 {
593 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
594
595 client_helper->priv = priv;
596 priv->children = g_ptr_array_new_with_free_func ((GDestroyNotify) pk_client_helper_child_free);
597 }
598
599 /*
600 * pk_client_helper_finalize:
601 **/
602 static void
603 pk_client_helper_finalize (GObject *object)
604 {
605 PkClientHelper *client_helper = PK_CLIENT_HELPER (object);
606 PkClientHelperPrivate *priv = GET_PRIVATE(client_helper);
607
608 if (priv->socket_channel_source != NULL) {
609 g_source_destroy (priv->socket_channel_source);
610 g_clear_pointer (&priv->socket_channel_source, g_source_unref);
611 }
612
613 g_clear_pointer (&priv->argv, g_strfreev);
614 g_clear_pointer (&priv->envp, g_strfreev);
615 if (priv->socket_file != NULL) {
616 g_file_delete (priv->socket_file, NULL, NULL);
617 g_clear_object (&priv->socket_file);
618 }
619 if (priv->socket != NULL) {
620 g_socket_close (priv->socket, NULL);
621 g_clear_object (&priv->socket);
622 }
623 g_clear_pointer (&priv->socket_channel, g_io_channel_unref);
624 g_clear_pointer (&priv->children, g_ptr_array_unref);
625 if (priv->kde_helper_pid > 0)
626 kill (priv->kde_helper_pid, SIGTERM);
627
628 G_OBJECT_CLASS (pk_client_helper_parent_class)->finalize (object);
629 }
630
631 /**
632 * pk_client_helper_new:
633 *
634 * Return value: a new #PkClientHelper object.
635 *
636 * Since: 0.6.10
637 **/
638 PkClientHelper *
639 pk_client_helper_new (void)
640 {
641 PkClientHelper *client_helper;
642 client_helper = g_object_new (PK_TYPE_CLIENT_HELPER, NULL);
643 return PK_CLIENT_HELPER (client_helper);
644 }