| 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 <sys/wait.h> |
| 52 |
#include <glib-object.h> |
| 53 |
#include <gio/gunixsocketaddress.h> |
| 54 |
|
| 55 |
#include <packagekit-glib2/pk-client-helper.h> |
| 56 |
|
| 57 |
static void pk_client_helper_finalize (GObject *object); |
| 58 |
|
| 59 |
/** |
| 60 |
* PkClientHelperPrivate: |
| 61 |
* |
| 62 |
* Private #PkClientHelper data |
| 63 |
**/ |
| 64 |
struct _PkClientHelperPrivate |
| 65 |
{ |
| 66 |
gchar **argv; |
| 67 |
gchar **envp; |
| 68 |
GFile *socket_file; |
| 69 |
GSocket *socket; |
| 70 |
GIOChannel *socket_channel; |
| 71 |
GSource *socket_channel_source; |
| 72 |
GPtrArray *children; |
| 73 |
GPid kde_helper_pid; |
| 74 |
}; |
| 75 |
|
| 76 |
typedef struct |
| 77 |
{ |
| 78 |
PkClientHelper *helper; |
| 79 |
GSocket *socket; |
| 80 |
GIOChannel *socket_channel; |
| 81 |
GSource *socket_channel_source; |
| 82 |
GPid pid; |
| 83 |
GIOChannel *stdin_channel; |
| 84 |
GIOChannel *stdout_channel; |
| 85 |
GIOChannel *stderr_channel; |
| 86 |
GSource *stdout_channel_source; |
| 87 |
GSource *stderr_channel_source; |
| 88 |
} PkClientHelperChild; |
| 89 |
|
| 90 |
G_DEFINE_TYPE_WITH_PRIVATE (PkClientHelper, pk_client_helper, G_TYPE_OBJECT) |
| 91 |
#define GET_PRIVATE(o) (pk_client_helper_get_instance_private (o)) |
| 92 |
|
| 93 |
/* how long to wait for a helper to exit on SIGTERM before escalating to SIGKILL */ |
| 94 |
#define PK_CLIENT_HELPER_SIGKILL_TIMEOUT 5 /* seconds */ |
| 95 |
#define PK_CLIENT_HELPER_POLL_DELAY 50 /* ms */ |
| 96 |
|
| 97 |
/* |
| 98 |
* pk_client_helper_kill_child: |
| 99 |
* |
| 100 |
* Terminates a helper child gracefully: sends SIGTERM and, if the process is |
| 101 |
* still alive after PK_CLIENT_HELPER_SIGKILL_TIMEOUT, escalates to SIGKILL. |
| 102 |
* The child is reaped so it does not linger as a zombie. |
| 103 |
* |
| 104 |
* The children must be spawned with %G_SPAWN_DO_NOT_REAP_CHILD. |
| 105 |
**/ |
| 106 |
static void |
| 107 |
pk_client_helper_kill_child (GPid pid) |
| 108 |
{ |
| 109 |
guint elapsed_ms = 0; |
| 110 |
|
| 111 |
if (pid <= 0) |
| 112 |
return; |
| 113 |
|
| 114 |
g_debug ("sending SIGTERM %ld", (long)pid); |
| 115 |
if (kill (pid, SIGTERM) < 0 && errno == ESRCH) { |
| 116 |
/* already gone, just reap it */ |
| 117 |
waitpid (pid, NULL, WNOHANG); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
/* wait for the child to exit, polling so we can escalate if needed */ |
| 122 |
while (elapsed_ms < PK_CLIENT_HELPER_SIGKILL_TIMEOUT * 1000) { |
| 123 |
if (waitpid (pid, NULL, WNOHANG) == pid) |
| 124 |
return; |
| 125 |
g_usleep (PK_CLIENT_HELPER_POLL_DELAY * 1000); |
| 126 |
elapsed_ms += PK_CLIENT_HELPER_POLL_DELAY; |
| 127 |
} |
| 128 |
|
| 129 |
/* still running, murder it */ |
| 130 |
g_debug ("helper %ld did not exit on SIGTERM, sending SIGKILL", (long)pid); |
| 131 |
kill (pid, SIGKILL); |
| 132 |
waitpid (pid, NULL, 0); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
pk_client_helper_child_free (PkClientHelperChild *child) |
| 137 |
{ |
| 138 |
if (child->socket != NULL) |
| 139 |
g_object_unref (child->socket); |
| 140 |
if (child->socket_channel != NULL) |
| 141 |
g_io_channel_unref (child->socket_channel); |
| 142 |
if (child->socket_channel_source != NULL) { |
| 143 |
g_source_destroy (child->socket_channel_source); |
| 144 |
g_source_unref (child->socket_channel_source); |
| 145 |
} |
| 146 |
if (child->pid > 0) |
| 147 |
pk_client_helper_kill_child (child->pid); |
| 148 |
if (child->stdin_channel != NULL) |
| 149 |
g_io_channel_unref (child->stdin_channel); |
| 150 |
if (child->stdout_channel != NULL) |
| 151 |
g_io_channel_unref (child->stdout_channel); |
| 152 |
if (child->stderr_channel != NULL) |
| 153 |
g_io_channel_unref (child->stderr_channel); |
| 154 |
if (child->stdout_channel_source != NULL) { |
| 155 |
g_source_destroy (child->stdout_channel_source); |
| 156 |
g_source_unref (child->stdout_channel_source); |
| 157 |
} |
| 158 |
if (child->stderr_channel_source != NULL) { |
| 159 |
g_source_destroy (child->stderr_channel_source); |
| 160 |
g_source_unref (child->stderr_channel_source); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* pk_client_helper_stop: |
| 166 |
* @client_helper: a valid #PkClientHelper instance |
| 167 |
* @error: A #GError or %NULL |
| 168 |
* |
| 169 |
* Stops the helper process, by killing the helper process and deleting |
| 170 |
* the socket. |
| 171 |
* |
| 172 |
* Return value: %TRUE for success |
| 173 |
* |
| 174 |
* Since: 0.6.10 |
| 175 |
**/ |
| 176 |
gboolean |
| 177 |
pk_client_helper_stop (PkClientHelper *client_helper, GError **error) |
| 178 |
{ |
| 179 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 180 |
|
| 181 |
g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE); |
| 182 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 183 |
|
| 184 |
/* make sure started */ |
| 185 |
g_return_val_if_fail (priv->socket_file != NULL, FALSE); |
| 186 |
|
| 187 |
/* close the socket */ |
| 188 |
if (priv->socket_channel_source != NULL) |
| 189 |
g_source_destroy (priv->socket_channel_source); |
| 190 |
if (priv->socket != NULL) { |
| 191 |
if (!g_socket_close (priv->socket, error)) |
| 192 |
return FALSE; |
| 193 |
} |
| 194 |
|
| 195 |
/* kill any children */ |
| 196 |
for (guint i = 0; i < priv->children->len; i++) { |
| 197 |
PkClientHelperChild *child = g_ptr_array_index (priv->children, i); |
| 198 |
pk_client_helper_kill_child (child->pid); |
| 199 |
} |
| 200 |
|
| 201 |
/* remove any socket file */ |
| 202 |
if (g_file_query_exists (priv->socket_file, NULL)) { |
| 203 |
if (!g_file_delete (priv->socket_file, NULL, error)) |
| 204 |
return FALSE; |
| 205 |
} |
| 206 |
|
| 207 |
return TRUE; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
* pk_client_helper_copy_stdout_cb: |
| 212 |
**/ |
| 213 |
static gboolean |
| 214 |
pk_client_helper_copy_stdout_cb (GIOChannel *source, GIOCondition condition, PkClientHelperChild *child) |
| 215 |
{ |
| 216 |
gchar data[1024]; |
| 217 |
gsize len = 0; |
| 218 |
gsize written = 0; |
| 219 |
GIOStatus status; |
| 220 |
g_autoptr(GError) error = NULL; |
| 221 |
|
| 222 |
/* read data */ |
| 223 |
status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, &error); |
| 224 |
if (status == G_IO_STATUS_EOF) { |
| 225 |
g_debug ("helper process exited"); |
| 226 |
/* reap it now if the process is gone; we own reaping since it was spawned with G_SPAWN_DO_NOT_REAP_CHILD. |
| 227 |
* If it has not exited yet (stdout closed but still running), leave it for kill_child(). */ |
| 228 |
if (child->pid > 0 && waitpid (child->pid, NULL, WNOHANG) == child->pid) |
| 229 |
child->pid = -1; |
| 230 |
if (!g_socket_close (child->socket, &error)) |
| 231 |
g_warning ("failed to close socket"); |
| 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 input to push to the socket: %s", data); |
| 240 |
status = g_io_channel_write_chars (child->socket_channel, data, len, &written, &error); |
| 241 |
if (status != G_IO_STATUS_NORMAL) { |
| 242 |
g_warning ("failed to write to socket: %s", error ? error->message : "Unknown error"); |
| 243 |
return G_SOURCE_REMOVE; |
| 244 |
} |
| 245 |
if (written != len) { |
| 246 |
g_warning ("failed to write %" G_GSIZE_FORMAT " bytes, " |
| 247 |
"only wrote %" G_GSIZE_FORMAT " bytes", len, written); |
| 248 |
return G_SOURCE_REMOVE; |
| 249 |
} |
| 250 |
g_debug ("wrote %" G_GSIZE_FORMAT " bytes to socket", written); |
| 251 |
|
| 252 |
return G_SOURCE_CONTINUE; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* pk_client_helper_echo_stderr_cb: |
| 257 |
**/ |
| 258 |
static gboolean |
| 259 |
pk_client_helper_echo_stderr_cb (GIOChannel *source, GIOCondition condition, PkClientHelper *client_helper) |
| 260 |
{ |
| 261 |
gchar data[1024]; |
| 262 |
gsize len = 0; |
| 263 |
GIOStatus status; |
| 264 |
|
| 265 |
/* read data */ |
| 266 |
status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, NULL); |
| 267 |
if (status == G_IO_STATUS_EOF) { |
| 268 |
return G_SOURCE_REMOVE; |
| 269 |
} |
| 270 |
if (len == 0) |
| 271 |
return G_SOURCE_CONTINUE; |
| 272 |
|
| 273 |
/* write to socket */ |
| 274 |
data[len] = '\0'; |
| 275 |
g_debug ("child has error: %s", data); |
| 276 |
|
| 277 |
return G_SOURCE_CONTINUE; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* pk_client_helper_copy_conn_cb: |
| 282 |
**/ |
| 283 |
static gboolean |
| 284 |
pk_client_helper_copy_conn_cb (GIOChannel *source, GIOCondition condition, PkClientHelperChild *child) |
| 285 |
{ |
| 286 |
PkClientHelperPrivate *helper_priv = pk_client_helper_get_instance_private (child->helper); |
| 287 |
gchar data[1024]; |
| 288 |
gsize len = 0; |
| 289 |
GIOStatus status; |
| 290 |
gsize written = 0; |
| 291 |
g_autoptr(GError) error = NULL; |
| 292 |
|
| 293 |
status = g_io_channel_read_chars (source, data, sizeof (data) - 1, &len, &error); |
| 294 |
if (status == G_IO_STATUS_EOF) { |
| 295 |
g_debug ("socket hung up"); |
| 296 |
|
| 297 |
/* Shutdown helper */ |
| 298 |
if (!g_io_channel_shutdown (child->stdin_channel, TRUE, &error)) |
| 299 |
g_warning ("failed to close connection to child: %s", error->message); |
| 300 |
return G_SOURCE_REMOVE; |
| 301 |
} |
| 302 |
if (error != NULL) { |
| 303 |
g_warning ("failed to read: %s", error->message); |
| 304 |
return G_SOURCE_REMOVE; |
| 305 |
} |
| 306 |
if (len == 0) |
| 307 |
return G_SOURCE_CONTINUE; |
| 308 |
|
| 309 |
/* write to child */ |
| 310 |
data[len] = '\0'; |
| 311 |
g_debug ("socket has data to push to child: '%s'", data); |
| 312 |
status = g_io_channel_write_chars (child->stdin_channel, data, len, &written, &error); |
| 313 |
if (status != G_IO_STATUS_NORMAL) { |
| 314 |
g_warning ("failed to write to stdin: %s", error ? error->message : "Unknown error"); |
| 315 |
return G_SOURCE_REMOVE; |
| 316 |
} |
| 317 |
if (written != len) { |
| 318 |
g_warning ("failed to write %" G_GSIZE_FORMAT " bytes, " |
| 319 |
"only wrote %" G_GSIZE_FORMAT " bytes", len, written); |
| 320 |
return G_SOURCE_REMOVE; |
| 321 |
} |
| 322 |
g_debug ("wrote %" G_GSIZE_FORMAT " bytes to stdin of %s", written, helper_priv->argv[0]); |
| 323 |
|
| 324 |
return G_SOURCE_CONTINUE; |
| 325 |
} |
| 326 |
|
| 327 |
static GSource * |
| 328 |
make_input_source (GIOChannel *channel, GSourceFunc func, gpointer user_data) |
| 329 |
{ |
| 330 |
GSource *source; |
| 331 |
GMainContext *context; |
| 332 |
|
| 333 |
source = g_io_create_watch (channel, G_IO_IN); |
| 334 |
g_source_set_callback (source, func, user_data, NULL); |
| 335 |
|
| 336 |
context = g_main_context_get_thread_default (); |
| 337 |
if (context == NULL) |
| 338 |
context = g_main_context_default (); |
| 339 |
g_source_attach (source, context); |
| 340 |
|
| 341 |
return source; |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* pk_client_helper_accept_connection_cb: |
| 346 |
**/ |
| 347 |
static gboolean |
| 348 |
pk_client_helper_accept_connection_cb (GIOChannel *source, GIOCondition condition, PkClientHelper *client_helper) |
| 349 |
{ |
| 350 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 351 |
g_autoptr(GSocket) socket = NULL; |
| 352 |
GPid pid; |
| 353 |
gint standard_input = 0; |
| 354 |
gint standard_output = 0; |
| 355 |
gint standard_error = 0; |
| 356 |
gint fd; |
| 357 |
GIOStatus status; |
| 358 |
PkClientHelperChild *child; |
| 359 |
g_autoptr(GError) error = NULL; |
| 360 |
|
| 361 |
/* accept the connection request */ |
| 362 |
socket = g_socket_accept (priv->socket, NULL, &error); |
| 363 |
if (socket == NULL) { |
| 364 |
g_warning ("failed to accept socket: %s", error->message); |
| 365 |
return G_SOURCE_CONTINUE; |
| 366 |
} |
| 367 |
g_debug ("accepting connection %i for socket %i", |
| 368 |
g_socket_get_fd (socket), |
| 369 |
g_socket_get_fd (priv->socket)); |
| 370 |
|
| 371 |
/* spawn helper executable; we reap the child ourselves (see |
| 372 |
* pk_client_helper_kill_child()) so GLib must not reap it for us */ |
| 373 |
if (!g_spawn_async_with_pipes (NULL, /* working dir */ |
| 374 |
priv->argv, |
| 375 |
priv->envp, |
| 376 |
G_SPAWN_DO_NOT_REAP_CHILD, |
| 377 |
NULL, /* child_setup */ |
| 378 |
NULL, /* child_setup user data */ |
| 379 |
&pid, |
| 380 |
&standard_input, |
| 381 |
&standard_output, |
| 382 |
&standard_error, |
| 383 |
&error)) { |
| 384 |
g_warning ("failed to spawn: %s", error->message); |
| 385 |
return G_SOURCE_CONTINUE; |
| 386 |
} |
| 387 |
g_debug ("started process %s with pid %i", priv->argv[0], pid); |
| 388 |
|
| 389 |
child = g_slice_new0 (PkClientHelperChild); |
| 390 |
g_ptr_array_add (priv->children, child); |
| 391 |
child->helper = client_helper; |
| 392 |
child->socket = g_steal_pointer (&socket); |
| 393 |
child->pid = pid; |
| 394 |
child->stdin_channel = g_io_channel_unix_new (standard_input); |
| 395 |
child->stdout_channel = g_io_channel_unix_new (standard_output); |
| 396 |
child->stderr_channel = g_io_channel_unix_new (standard_error); |
| 397 |
|
| 398 |
/* binary data */ |
| 399 |
status = g_io_channel_set_encoding (child->stdin_channel, NULL, &error); |
| 400 |
if (status != G_IO_STATUS_NORMAL) { |
| 401 |
g_warning ("failed to set encoding: %s", error->message); |
| 402 |
return G_SOURCE_CONTINUE; |
| 403 |
} |
| 404 |
g_io_channel_set_buffered (child->stdin_channel, FALSE); |
| 405 |
|
| 406 |
/* binary data */ |
| 407 |
status = g_io_channel_set_encoding (child->stdout_channel, NULL, &error); |
| 408 |
if (status != G_IO_STATUS_NORMAL) { |
| 409 |
g_warning ("failed to set encoding: %s", error->message); |
| 410 |
return G_SOURCE_CONTINUE; |
| 411 |
} |
| 412 |
g_io_channel_set_buffered (child->stdout_channel, FALSE); |
| 413 |
|
| 414 |
/* binary data */ |
| 415 |
status = g_io_channel_set_encoding (child->stderr_channel, NULL, &error); |
| 416 |
if (status != G_IO_STATUS_NORMAL) { |
| 417 |
g_warning ("failed to set encoding: %s", error->message); |
| 418 |
return G_SOURCE_CONTINUE; |
| 419 |
} |
| 420 |
g_io_channel_set_buffered (child->stderr_channel, FALSE); |
| 421 |
|
| 422 |
/* socket has data */ |
| 423 |
fd = g_socket_get_fd (child->socket); |
| 424 |
child->socket_channel = g_io_channel_unix_new (fd); |
| 425 |
child->socket_channel_source = |
| 426 |
make_input_source (child->socket_channel, G_SOURCE_FUNC (pk_client_helper_copy_conn_cb), child); |
| 427 |
/* binary data */ |
| 428 |
status = g_io_channel_set_encoding (child->socket_channel, NULL, &error); |
| 429 |
if (status != G_IO_STATUS_NORMAL) { |
| 430 |
g_warning ("failed to set encoding: %s", error->message); |
| 431 |
return G_SOURCE_CONTINUE; |
| 432 |
} |
| 433 |
g_io_channel_set_buffered (child->socket_channel, FALSE); |
| 434 |
|
| 435 |
/* frontend has data */ |
| 436 |
child->stdout_channel_source = |
| 437 |
make_input_source (child->stdout_channel, G_SOURCE_FUNC (pk_client_helper_copy_stdout_cb), child); |
| 438 |
child->stderr_channel_source = |
| 439 |
make_input_source (child->stderr_channel, G_SOURCE_FUNC (pk_client_helper_echo_stderr_cb), child); |
| 440 |
return G_SOURCE_CONTINUE; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* pk_client_helper_start: |
| 445 |
* @client_helper: a valid #PkClientHelper instance |
| 446 |
* @socket_filename: a socket filename that does not already exist |
| 447 |
* @argv: the executable, along with any arguments |
| 448 |
* @envp: the environment |
| 449 |
* @error: A #GError or %NULL |
| 450 |
* |
| 451 |
* Starts the helper process, by running the helper process and setting |
| 452 |
* up the socket for use. |
| 453 |
* |
| 454 |
* Return value: %TRUE for success |
| 455 |
* |
| 456 |
* Since: 0.6.10 |
| 457 |
**/ |
| 458 |
gboolean |
| 459 |
pk_client_helper_start (PkClientHelper *client_helper, |
| 460 |
const gchar *socket_filename, |
| 461 |
gchar **argv, gchar **envp, |
| 462 |
GError **error) |
| 463 |
{ |
| 464 |
gboolean use_kde_helper = FALSE; |
| 465 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 466 |
g_autoptr(GError) error_local = NULL; |
| 467 |
g_autoptr(GSocketAddress) address = NULL; |
| 468 |
const gchar *kde_helper_path = "/usr/libexec/debconf-kde-helper"; |
| 469 |
|
| 470 |
g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE); |
| 471 |
g_return_val_if_fail (socket_filename != NULL, FALSE); |
| 472 |
g_return_val_if_fail (argv != NULL, FALSE); |
| 473 |
g_return_val_if_fail (socket_filename != NULL, FALSE); |
| 474 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 475 |
|
| 476 |
/* make sure not been started before */ |
| 477 |
g_return_val_if_fail (priv->argv == NULL, FALSE); |
| 478 |
g_return_val_if_fail (priv->socket_file == NULL, FALSE); |
| 479 |
|
| 480 |
/* already exists? */ |
| 481 |
if (g_file_test (socket_filename, G_FILE_TEST_EXISTS)) { |
| 482 |
g_set_error (error, 1, 0, "socket %s already exists", socket_filename); |
| 483 |
return FALSE; |
| 484 |
} |
| 485 |
|
| 486 |
g_debug ("using socket in %s", socket_filename); |
| 487 |
priv->socket_file = g_file_new_for_path (socket_filename); |
| 488 |
|
| 489 |
/* preconfigure KDE frontend, if requested */ |
| 490 |
if (envp != NULL) { |
| 491 |
for (guint i = 0; envp[i] != NULL; i++) { |
| 492 |
if (g_strcmp0 (envp[i], "DEBIAN_FRONTEND=kde") == 0) { |
| 493 |
if (g_file_test (kde_helper_path, G_FILE_TEST_EXISTS)) { |
| 494 |
use_kde_helper = TRUE; |
| 495 |
} else { |
| 496 |
/* try alternative path */ |
| 497 |
kde_helper_path = "/usr/bin/debconf-kde-helper"; |
| 498 |
if (g_file_test (kde_helper_path, G_FILE_TEST_EXISTS)) |
| 499 |
use_kde_helper = TRUE; |
| 500 |
} |
| 501 |
|
| 502 |
break; |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
/* create socket */ |
| 508 |
priv->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, error); |
| 509 |
if (priv->socket == NULL) |
| 510 |
return FALSE; |
| 511 |
|
| 512 |
/* bind to the socket */ |
| 513 |
address = g_unix_socket_address_new (socket_filename); |
| 514 |
if (!g_socket_bind (priv->socket, address, TRUE, error)) |
| 515 |
return FALSE; |
| 516 |
|
| 517 |
/* spawn KDE debconf communicator */ |
| 518 |
if (use_kde_helper) { |
| 519 |
g_debug ("Using KDE debconf communicator: %s", kde_helper_path); |
| 520 |
|
| 521 |
priv->envp = g_strdupv (envp); |
| 522 |
priv->argv = g_new0 (gchar *, 2); |
| 523 |
priv->argv[0] = g_strdup (kde_helper_path); |
| 524 |
priv->argv[1] = g_strconcat ("--socket-path", "=", socket_filename, NULL); |
| 525 |
|
| 526 |
if (!g_spawn_async (NULL, |
| 527 |
priv->argv, |
| 528 |
priv->envp, |
| 529 |
G_SPAWN_STDOUT_TO_DEV_NULL, |
| 530 |
NULL, |
| 531 |
NULL, |
| 532 |
&priv->kde_helper_pid, |
| 533 |
&error_local)) { |
| 534 |
g_warning ("failed to spawn: %s", error_local->message); |
| 535 |
return FALSE; |
| 536 |
} |
| 537 |
g_debug ("started process %s with pid %i", priv->argv[0], priv->kde_helper_pid); |
| 538 |
return TRUE; |
| 539 |
} |
| 540 |
|
| 541 |
/* listen to the socket */ |
| 542 |
if (!g_socket_listen (priv->socket, error)) |
| 543 |
return FALSE; |
| 544 |
|
| 545 |
return pk_client_helper_start_with_socket (client_helper, priv->socket, argv, envp, error); |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* pk_client_helper_start_with_socket: |
| 550 |
* @client_helper: a valid #PkClientHelper instance |
| 551 |
* @socket: the (bound and listening) #GSocket instance to use |
| 552 |
* @argv: the executable, along with any arguments |
| 553 |
* @envp: the environment |
| 554 |
* @error: A #GError or %NULL |
| 555 |
* |
| 556 |
* Starts the helper process, by running the helper process and setting |
| 557 |
* up the socket for use. |
| 558 |
* |
| 559 |
* Return value: %TRUE for success |
| 560 |
* |
| 561 |
* Since: 1.1.13 |
| 562 |
**/ |
| 563 |
gboolean |
| 564 |
pk_client_helper_start_with_socket (PkClientHelper *client_helper, |
| 565 |
GSocket *socket, |
| 566 |
gchar **argv, gchar **envp, |
| 567 |
GError **error) |
| 568 |
{ |
| 569 |
gint fd; |
| 570 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 571 |
g_autoptr(GError) error_local = NULL; |
| 572 |
g_autoptr(GSocketAddress) address = NULL; |
| 573 |
|
| 574 |
g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE); |
| 575 |
g_return_val_if_fail (socket != NULL, FALSE); |
| 576 |
g_return_val_if_fail (argv != NULL, FALSE); |
| 577 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 578 |
|
| 579 |
/* make sure not been started before */ |
| 580 |
g_return_val_if_fail (priv->argv == NULL, FALSE); |
| 581 |
|
| 582 |
/* cache for actual start */ |
| 583 |
priv->argv = g_strdupv (argv); |
| 584 |
priv->envp = g_strdupv (envp); |
| 585 |
|
| 586 |
/* Set the socket */ |
| 587 |
g_set_object (&priv->socket, socket); |
| 588 |
|
| 589 |
/* socket has data */ |
| 590 |
fd = g_socket_get_fd (priv->socket); |
| 591 |
priv->socket_channel = g_io_channel_unix_new (fd); |
| 592 |
priv->socket_channel_source = |
| 593 |
make_input_source (priv->socket_channel, G_SOURCE_FUNC (pk_client_helper_accept_connection_cb), client_helper); |
| 594 |
return TRUE; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* pk_client_helper_is_active: |
| 599 |
* @client_helper: a valid #PkClientHelper instance |
| 600 |
* |
| 601 |
* Return value: TRUE if there is an accepted connection, FALSE |
| 602 |
* otherwise. |
| 603 |
* |
| 604 |
* Since: 1.1.13 |
| 605 |
*/ |
| 606 |
gboolean |
| 607 |
pk_client_helper_is_active (PkClientHelper *client_helper) |
| 608 |
{ |
| 609 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 610 |
|
| 611 |
g_return_val_if_fail (PK_IS_CLIENT_HELPER (client_helper), FALSE); |
| 612 |
|
| 613 |
for (guint i = 0; i < priv->children->len; i++) { |
| 614 |
PkClientHelperChild *child = g_ptr_array_index (priv->children, i); |
| 615 |
if (!g_source_is_destroyed (child->socket_channel_source) && |
| 616 |
!g_source_is_destroyed (child->stdout_channel_source)) |
| 617 |
return TRUE; |
| 618 |
} |
| 619 |
|
| 620 |
return FALSE; |
| 621 |
} |
| 622 |
|
| 623 |
/* |
| 624 |
* pk_client_helper_class_init: |
| 625 |
**/ |
| 626 |
static void |
| 627 |
pk_client_helper_class_init (PkClientHelperClass *klass) |
| 628 |
{ |
| 629 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 630 |
object_class->finalize = pk_client_helper_finalize; |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* pk_client_helper_init: |
| 635 |
**/ |
| 636 |
static void |
| 637 |
pk_client_helper_init (PkClientHelper *client_helper) |
| 638 |
{ |
| 639 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 640 |
|
| 641 |
client_helper->priv = priv; |
| 642 |
priv->children = g_ptr_array_new_with_free_func ((GDestroyNotify) pk_client_helper_child_free); |
| 643 |
} |
| 644 |
|
| 645 |
/* |
| 646 |
* pk_client_helper_finalize: |
| 647 |
**/ |
| 648 |
static void |
| 649 |
pk_client_helper_finalize (GObject *object) |
| 650 |
{ |
| 651 |
PkClientHelper *client_helper = PK_CLIENT_HELPER (object); |
| 652 |
PkClientHelperPrivate *priv = GET_PRIVATE(client_helper); |
| 653 |
|
| 654 |
if (priv->socket_channel_source != NULL) { |
| 655 |
g_source_destroy (priv->socket_channel_source); |
| 656 |
g_clear_pointer (&priv->socket_channel_source, g_source_unref); |
| 657 |
} |
| 658 |
|
| 659 |
g_clear_pointer (&priv->argv, g_strfreev); |
| 660 |
g_clear_pointer (&priv->envp, g_strfreev); |
| 661 |
if (priv->socket_file != NULL) { |
| 662 |
g_file_delete (priv->socket_file, NULL, NULL); |
| 663 |
g_clear_object (&priv->socket_file); |
| 664 |
} |
| 665 |
if (priv->socket != NULL) { |
| 666 |
g_socket_close (priv->socket, NULL); |
| 667 |
g_clear_object (&priv->socket); |
| 668 |
} |
| 669 |
g_clear_pointer (&priv->socket_channel, g_io_channel_unref); |
| 670 |
g_clear_pointer (&priv->children, g_ptr_array_unref); |
| 671 |
if (priv->kde_helper_pid > 0) |
| 672 |
kill (priv->kde_helper_pid, SIGTERM); |
| 673 |
|
| 674 |
G_OBJECT_CLASS (pk_client_helper_parent_class)->finalize (object); |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* pk_client_helper_new: |
| 679 |
* |
| 680 |
* Return value: a new #PkClientHelper object. |
| 681 |
* |
| 682 |
* Since: 0.6.10 |
| 683 |
**/ |
| 684 |
PkClientHelper * |
| 685 |
pk_client_helper_new (void) |
| 686 |
{ |
| 687 |
PkClientHelper *client_helper; |
| 688 |
client_helper = g_object_new (PK_TYPE_CLIENT_HELPER, NULL); |
| 689 |
return PK_CLIENT_HELPER (client_helper); |
| 690 |
} |