gsm-util.c 27.4 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 * gsm-util.c
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 2008 Lucas Rocha.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * 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
 */

19
#include <config.h>
20 21
#include <stdlib.h>
#include <ctype.h>
William Jon McCann's avatar
William Jon McCann committed
22 23
#include <sys/types.h>
#include <unistd.h>
William Jon McCann's avatar
William Jon McCann committed
24
#include <sys/time.h>
25 26
#include <errno.h>
#include <string.h>
27 28 29

#include <glib.h>
#include <glib/gi18n.h>
30
#include <glib/gstdio.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
31
#include <gio/gio.h>
32

Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gsm-util.h"
34

35
static gchar *_saved_session_dir = NULL;
36
static gchar **child_environment;
37

38 39 40 41 42 43
/* These are variables that will not be passed on to subprocesses
 * (either directly, via systemd or DBus).
 * Some of these are blacklisted as they might end up in the wrong session
 * (e.g. XDG_VTNR), others because they simply must never be passed on
 * (NOTIFY_SOCKET).
 */
44
static const char * const variable_blacklist[] = {
Debarshi Ray's avatar
Debarshi Ray committed
45
    "NOTIFY_SOCKET",
46 47 48 49 50 51
    "XDG_SEAT",
    "XDG_SESSION_ID",
    "XDG_VTNR",
    NULL
};

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* The following is copied from GDMs spawn_session function.
 *
 * Environment variables listed here will be copied into the user's service
 * environments if they are set in gnome-session's environment. If they are
 * not set in gnome-session's environment, they will be removed from the
 * service environments. This is to protect against environment variables
 * leaking from previous sessions (e.g. when switching from classic to
 * default GNOME $GNOME_SHELL_SESSION_MODE will become unset).
 */
static const char * const variable_unsetlist[] = {
    "DISPLAY",
    "XAUTHORITY",
    "WAYLAND_DISPLAY",
    "WAYLAND_SOCKET",
    "GNOME_SHELL_SESSION_MODE",
67
    "GNOME_SETUP_DISPLAY",
68 69 70
    NULL
};

71 72
char *
gsm_util_find_desktop_file_for_app_name (const char *name,
73 74
                                         gboolean    look_in_saved_session,
                                         gboolean    autostart_first)
75 76 77 78 79 80 81 82 83
{
        char     *app_path;
        char    **app_dirs;
        GKeyFile *key_file;
        char     *desktop_file;
        int       i;

        app_path = NULL;

84
        app_dirs = gsm_util_get_desktop_dirs (look_in_saved_session, autostart_first);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        key_file = g_key_file_new ();

        desktop_file = g_strdup_printf ("%s.desktop", name);

        g_debug ("GsmUtil: Looking for file '%s'", desktop_file);

        for (i = 0; app_dirs[i] != NULL; i++) {
                g_debug ("GsmUtil: Looking in '%s'", app_dirs[i]);
        }

        g_key_file_load_from_dirs (key_file,
                                   desktop_file,
                                   (const char **) app_dirs,
                                   &app_path,
                                   G_KEY_FILE_NONE,
                                   NULL);

        if (app_path != NULL) {
104
                g_debug ("GsmUtil: found in XDG dirs: '%s'", app_path);
105 106
        }

107
        /* look for gnome vendor prefix */
108 109 110 111 112 113 114 115 116 117 118
        if (app_path == NULL) {
                g_free (desktop_file);
                desktop_file = g_strdup_printf ("gnome-%s.desktop", name);

                g_key_file_load_from_dirs (key_file,
                                           desktop_file,
                                           (const char **) app_dirs,
                                           &app_path,
                                           G_KEY_FILE_NONE,
                                           NULL);
                if (app_path != NULL) {
119
                        g_debug ("GsmUtil: found in XDG dirs: '%s'", app_path);
120 121 122 123 124 125 126 127 128 129 130 131 132 133
                }
        }

        g_free (desktop_file);
        g_key_file_free (key_file);

        g_strfreev (app_dirs);

        return app_path;
}

static gboolean
ensure_dir_exists (const char *dir)
{
134
        if (g_mkdir_with_parents (dir, 0700) == 0)
135 136 137 138 139 140 141
                return TRUE;

        g_warning ("GsmSessionSave: Failed to create directory %s: %s", dir, strerror (errno));

        return FALSE;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
gchar *
gsm_util_get_empty_tmp_session_dir (void)
{
        char *tmp;
        gboolean exists;

        tmp = g_build_filename (g_get_user_config_dir (),
                                "gnome-session",
                                "saved-session.new",
                                NULL);

        exists = ensure_dir_exists (tmp);

        if (G_UNLIKELY (!exists)) {
                g_warning ("GsmSessionSave: could not create directory for saved session: %s", tmp);
                g_free (tmp);
                return NULL;
        } else {
                /* make sure it's empty */
                GDir       *dir;
                const char *filename;

                dir = g_dir_open (tmp, 0, NULL);
                if (dir) {
                        while ((filename = g_dir_read_name (dir))) {
                                char *path = g_build_filename (tmp, filename,
                                                               NULL);
                                g_unlink (path);
                        }
                        g_dir_close (dir);
                }
        }

        return tmp;
}

178 179 180 181 182 183 184
const gchar *
gsm_util_get_saved_session_dir (void)
{
        if (_saved_session_dir == NULL) {
                gboolean exists;

                _saved_session_dir =
185
                        g_build_filename (g_get_user_config_dir (),
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                                          "gnome-session",
                                          "saved-session",
                                          NULL);

                exists = ensure_dir_exists (_saved_session_dir);

                if (G_UNLIKELY (!exists)) {
                        static gboolean printed_warning = FALSE;

                        if (!printed_warning) {
                                g_warning ("GsmSessionSave: could not create directory for saved session: %s", _saved_session_dir);
                                printed_warning = TRUE;
                        }

                        _saved_session_dir = NULL;

                        return NULL;
                }
        }

        return _saved_session_dir;
}

209 210 211 212 213 214 215 216 217
static char ** autostart_dirs;

void
gsm_util_set_autostart_dirs (char ** dirs)
{
        autostart_dirs = g_strdupv (dirs);
}

static char **
Colin Walters's avatar
Colin Walters committed
218
gsm_util_get_standard_autostart_dirs (void)
219
{
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        GPtrArray          *dirs;
        const char * const *system_config_dirs;
        const char * const *system_data_dirs;
        int                 i;

        dirs = g_ptr_array_new ();

        g_ptr_array_add (dirs,
                         g_build_filename (g_get_user_config_dir (),
                                           "autostart", NULL));

        system_data_dirs = g_get_system_data_dirs ();
        for (i = 0; system_data_dirs[i]; i++) {
                g_ptr_array_add (dirs,
                                 g_build_filename (system_data_dirs[i],
                                                   "gnome", "autostart", NULL));
        }

        system_config_dirs = g_get_system_config_dirs ();
        for (i = 0; system_config_dirs[i]; i++) {
                g_ptr_array_add (dirs,
                                 g_build_filename (system_config_dirs[i],
                                                   "autostart", NULL));
        }

        g_ptr_array_add (dirs, NULL);

        return (char **) g_ptr_array_free (dirs, FALSE);
248 249
}

250 251 252 253 254 255 256 257 258 259
char **
gsm_util_get_autostart_dirs ()
{
        if (autostart_dirs) {
                return g_strdupv ((char **)autostart_dirs);
        }

        return gsm_util_get_standard_autostart_dirs ();
}

260
char **
261 262
gsm_util_get_app_dirs ()
{
263 264 265
        GPtrArray          *dirs;
        const char * const *system_data_dirs;
        int                 i;
266

267
        dirs = g_ptr_array_new ();
268

269
        g_ptr_array_add (dirs,
Ray Strode's avatar
Ray Strode committed
270 271 272
                         g_build_filename (g_get_user_data_dir (),
                                           "applications",
                                           NULL));
273

274 275 276 277 278 279 280
        system_data_dirs = g_get_system_data_dirs ();
        for (i = 0; system_data_dirs[i]; i++) {
                g_ptr_array_add (dirs,
                                 g_build_filename (system_data_dirs[i],
                                                   "applications",
                                                   NULL));
        }
281

282
        g_ptr_array_add (dirs, NULL);
283

284
        return (char **) g_ptr_array_free (dirs, FALSE);
285 286
}

287
char **
288 289
gsm_util_get_desktop_dirs (gboolean include_saved_session,
                           gboolean autostart_first)
290
{
Ray Strode's avatar
Ray Strode committed
291 292 293 294 295 296
        char **apps;
        char **autostart;
        char **standard_autostart;
        char **result;
        int    size;
        int    i;
297

Ray Strode's avatar
Ray Strode committed
298 299
        apps = gsm_util_get_app_dirs ();
        autostart = gsm_util_get_autostart_dirs ();
300

301 302 303 304 305 306 307
        /* Still, check the standard autostart dirs for things like fulfilling session reqs,
         * if using a non-standard autostart dir for autostarting */
        if (autostart_dirs != NULL)
                standard_autostart = gsm_util_get_standard_autostart_dirs ();
        else
                standard_autostart = NULL;

Ray Strode's avatar
Ray Strode committed
308 309 310
        size = 0;
        for (i = 0; apps[i] != NULL; i++) { size++; }
        for (i = 0; autostart[i] != NULL; i++) { size++; }
311 312
        if (autostart_dirs != NULL)
                for (i = 0; standard_autostart[i] != NULL; i++) { size++; }
313 314
        if (include_saved_session)
                size += 1;
315

Ray Strode's avatar
Ray Strode committed
316
        result = g_new (char *, size + 1); /* including last NULL */
317

318 319 320 321 322
        size = 0;

        if (autostart_first) {
                if (include_saved_session)
                        result[size++] = g_strdup (gsm_util_get_saved_session_dir ());
323

324 325 326
                for (i = 0; autostart[i] != NULL; i++, size++) {
                        result[size] = autostart[i];
                }
327 328 329 330 331
                if (standard_autostart != NULL) {
                        for (i = 0; standard_autostart[i] != NULL; i++, size++) {
                                result[size] = standard_autostart[i];
                        }
                }
332 333 334 335 336 337 338
                for (i = 0; apps[i] != NULL; i++, size++) {
                        result[size] = apps[i];
                }
        } else {
                for (i = 0; apps[i] != NULL; i++, size++) {
                        result[size] = apps[i];
                }
339 340 341 342 343
                if (standard_autostart != NULL) {
                        for (i = 0; standard_autostart[i] != NULL; i++, size++) {
                                result[size] = standard_autostart[i];
                        }
                }
344 345 346 347 348 349 350 351
                for (i = 0; autostart[i] != NULL; i++, size++) {
                        result[size] = autostart[i];
                }

                if (include_saved_session)
                        result[size++] = g_strdup (gsm_util_get_saved_session_dir ());
        }

Ray Strode's avatar
Ray Strode committed
352 353 354
        g_free (apps);
        g_free (autostart);
        g_free (standard_autostart);
355

Ray Strode's avatar
Ray Strode committed
356
        result[size] = NULL;
357

Ray Strode's avatar
Ray Strode committed
358
        return result;
359 360
}

361
gboolean
362
gsm_util_text_is_blank (const char *str)
363
{
364 365 366
        if (str == NULL) {
                return TRUE;
        }
367

368 369 370 371
        while (*str) {
                if (!isspace(*str)) {
                        return FALSE;
                }
372

373 374
                str++;
        }
375

376
        return TRUE;
377
}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

/**
 * gsm_util_init_error:
 * @fatal: whether or not the error is fatal to the login session
 * @format: printf-style error message format
 * @...: error message args
 *
 * Displays the error message to the user. If @fatal is %TRUE, gsm
 * will exit after displaying the message.
 *
 * This should be called for major errors that occur before the
 * session is up and running. (Notably, it positions the dialog box
 * itself, since no window manager will be running yet.)
 **/
void
393 394
gsm_util_init_error (gboolean    fatal,
                     const char *format, ...)
395
{
396 397
        char           *msg;
        va_list         args;
Matthias Clasen's avatar
Matthias Clasen committed
398
        gchar          *argv[13];
399

400 401 402
        va_start (args, format);
        msg = g_strdup_vprintf (format, args);
        va_end (args);
403

Matthias Clasen's avatar
Matthias Clasen committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417
        argv[0] = "zenity";
        argv[1] = "--error";
        argv[2] = "--class";
        argv[3] = "mutter-dialog";
        argv[4] = "--title";
        argv[5] = "\"\"";
        argv[6] = "--text";
        argv[7] = msg;
        argv[8] = "--icon-name";
        argv[9] = "face-sad-symbolic";
        argv[10] = "--ok-label";
        argv[11] = _("_Log out");
        argv[12] = NULL;

418
        g_spawn_sync (NULL, argv, child_environment, 0, NULL, NULL, NULL, NULL, NULL, NULL);
419

420
        g_free (msg);
421

422
        if (fatal) {
Matthias Clasen's avatar
Matthias Clasen committed
423
                exit (1);
424
        }
425 426 427
}

/**
428
 * gsm_util_generate_startup_id:
429 430 431 432 433 434
 *
 * Generates a new SM client ID.
 *
 * Return value: an SM client ID.
 **/
char *
435
gsm_util_generate_startup_id (void)
436
{
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        static int     sequence = -1;
        static guint   rand1 = 0;
        static guint   rand2 = 0;
        static pid_t   pid = 0;
        struct timeval tv;

        /* The XSMP spec defines the ID as:
         *
         * Version: "1"
         * Address type and address:
         *   "1" + an IPv4 address as 8 hex digits
         *   "2" + a DECNET address as 12 hex digits
         *   "6" + an IPv6 address as 32 hex digits
         * Time stamp: milliseconds since UNIX epoch as 13 decimal digits
         * Process-ID type and process-ID:
         *   "1" + POSIX PID as 10 decimal digits
         * Sequence number as 4 decimal digits
         *
         * XSMP client IDs are supposed to be globally unique: if
         * SmsGenerateClientID() is unable to determine a network
         * address for the machine, it gives up and returns %NULL.
         * GNOME and KDE have traditionally used a fourth address
         * format in this case:
         *   "0" + 16 random hex digits
         *
         * We don't even bother trying SmsGenerateClientID(), since the
         * user's IP address is probably "192.168.1.*" anyway, so a random
         * number is actually more likely to be globally unique.
         */

        if (!rand1) {
                rand1 = g_random_int ();
                rand2 = g_random_int ();
                pid = getpid ();
        }

        sequence = (sequence + 1) % 10000;
        gettimeofday (&tv, NULL);
        return g_strdup_printf ("10%.04x%.04x%.10lu%.3u%.10lu%.4d",
                                rand1,
                                rand2,
                                (unsigned long) tv.tv_sec,
                                (unsigned) tv.tv_usec,
                                (unsigned long) pid,
                                sequence);
482
}
483 484 485

static gboolean
gsm_util_update_activation_environment (const char  *variable,
486 487
                                        const char  *value,
                                        GError     **error)
488
{
Cosimo Cecchi's avatar
Cosimo Cecchi committed
489
        GDBusConnection *connection;
490
        gboolean         environment_updated;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
491 492 493
        GVariantBuilder  builder;
        GVariant        *reply;
        GError          *bus_error = NULL;
494

495
        environment_updated = FALSE;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
496
        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
497

Cosimo Cecchi's avatar
Cosimo Cecchi committed
498
        if (connection == NULL) {
499 500
                return FALSE;
        }
501

Cosimo Cecchi's avatar
Cosimo Cecchi committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
        g_variant_builder_add (&builder, "{ss}", variable, value);

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.DBus",
                                             "/org/freedesktop/DBus",
                                             "org.freedesktop.DBus",
                                             "UpdateActivationEnvironment",
                                             g_variant_new ("(@a{ss})",
                                                            g_variant_builder_end (&builder)),
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
        } else {
                environment_updated = TRUE;
                g_variant_unref (reply);
521
        }
522

Cosimo Cecchi's avatar
Cosimo Cecchi committed
523
        g_clear_object (&connection);
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        return environment_updated;
}

gboolean
gsm_util_export_activation_environment (GError     **error)
{

        GDBusConnection *connection;
        gboolean         environment_updated = FALSE;
        char           **entry_names;
        int              i = 0;
        GVariantBuilder  builder;
        GRegex          *name_regex, *value_regex;
        GVariant        *reply;
        GError          *bus_error = NULL;

        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);

        if (connection == NULL) {
                return FALSE;
        }

        name_regex = g_regex_new ("^[a-zA-Z_][a-zA-Z0-9_]*$", G_REGEX_OPTIMIZE, 0, error);

        if (name_regex == NULL) {
                return FALSE;
        }

        value_regex = g_regex_new ("^([[:blank:]]|[^[:cntrl:]])*$", G_REGEX_OPTIMIZE, 0, error);

        if (value_regex == NULL) {
                return FALSE;
        }

559 560 561 562
        if (child_environment == NULL) {
                child_environment = g_listenv ();
        }

563 564 565 566 567
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
        for (entry_names = g_listenv (); entry_names[i] != NULL; i++) {
                const char *entry_name = entry_names[i];
                const char *entry_value = g_getenv (entry_name);

568 569 570
                if (g_strv_contains (variable_blacklist, entry_name))
                    continue;

571 572 573 574 575 576 577 578 579 580 581 582
                if (!g_utf8_validate (entry_name, -1, NULL))
                    continue;

                if (!g_regex_match (name_regex, entry_name, 0, NULL))
                    continue;

                if (!g_utf8_validate (entry_value, -1, NULL))
                    continue;

                if (!g_regex_match (value_regex, entry_value, 0, NULL))
                    continue;

583 584 585
                child_environment = g_environ_setenv (child_environment,
                                                      entry_name, entry_value,
                                                      TRUE);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
                g_variant_builder_add (&builder, "{ss}", entry_name, entry_value);
        }
        g_regex_unref (name_regex);
        g_regex_unref (value_regex);

        g_strfreev (entry_names);

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.DBus",
                                             "/org/freedesktop/DBus",
                                             "org.freedesktop.DBus",
                                             "UpdateActivationEnvironment",
                                             g_variant_new ("(@a{ss})",
                                                            g_variant_builder_end (&builder)),
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
        } else {
                environment_updated = TRUE;
                g_variant_unref (reply);
        }

        g_clear_object (&connection);

613
        return environment_updated;
614 615
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
#ifdef HAVE_SYSTEMD
gboolean
gsm_util_export_user_environment (GError     **error)
{

        GDBusConnection *connection;
        gboolean         environment_updated = FALSE;
        char           **entries;
        int              i = 0;
        GVariantBuilder  builder;
        GRegex          *regex;
        GVariant        *reply;
        GError          *bus_error = NULL;

        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);

        if (connection == NULL) {
                return FALSE;
        }

        regex = g_regex_new ("^[a-zA-Z_][a-zA-Z0-9_]*=([[:blank:]]|[^[:cntrl:]])*$", G_REGEX_OPTIMIZE, 0, error);

        if (regex == NULL) {
                return FALSE;
        }

642 643
        entries = g_get_environ ();

644
        for (i = 0; variable_blacklist[i] != NULL; i++)
645 646
                entries = g_environ_unsetenv (entries, variable_blacklist[i]);

647 648 649 650 651 652 653 654
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("(asas)"));

        g_variant_builder_open (&builder, G_VARIANT_TYPE ("as"));
        for (i = 0; variable_unsetlist[i] != NULL; i++)
                g_variant_builder_add (&builder, "s", variable_unsetlist[i]);
        g_variant_builder_close (&builder);

        g_variant_builder_open (&builder, G_VARIANT_TYPE ("as"));
655
        for (i = 0; entries[i] != NULL; i++) {
656 657 658 659 660 661 662 663 664 665
                const char *entry = entries[i];

                if (!g_utf8_validate (entry, -1, NULL))
                    continue;

                if (!g_regex_match (regex, entry, 0, NULL))
                    continue;

                g_variant_builder_add (&builder, "s", entry);
        }
666
        g_variant_builder_close (&builder);
667 668 669 670 671 672 673 674
        g_regex_unref (regex);

        g_strfreev (entries);

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.systemd1",
                                             "/org/freedesktop/systemd1",
                                             "org.freedesktop.systemd1.Manager",
675 676
                                             "UnsetAndSetEnvironment",
                                             g_variant_builder_end (&builder),
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
        } else {
                environment_updated = TRUE;
                g_variant_unref (reply);
        }

        g_clear_object (&connection);

        return environment_updated;
}

static gboolean
gsm_util_update_user_environment (const char  *variable,
                                  const char  *value,
                                  GError     **error)
{
        GDBusConnection *connection;
        gboolean         environment_updated;
        char            *entry;
        GVariantBuilder  builder;
        GVariant        *reply;
        GError          *bus_error = NULL;

        environment_updated = FALSE;
        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);

        if (connection == NULL) {
                return FALSE;
        }

        g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
        entry = g_strdup_printf ("%s=%s", variable, value);
        g_variant_builder_add (&builder, "s", entry);
        g_free (entry);

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.systemd1",
                                             "/org/freedesktop/systemd1",
                                             "org.freedesktop.systemd1.Manager",
                                             "SetEnvironment",
                                             g_variant_new ("(@as)",
                                                            g_variant_builder_end (&builder)),
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
        } else {
                environment_updated = TRUE;
                g_variant_unref (reply);
        }

        g_clear_object (&connection);

        return environment_updated;
}
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

gboolean
gsm_util_start_systemd_unit (const char  *unit,
                             const char  *mode,
                             GError     **error)
{
        g_autoptr(GDBusConnection) connection = NULL;
        g_autoptr(GVariant)        reply = NULL;
        GError          *bus_error = NULL;

        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);

        if (connection == NULL)
                return FALSE;

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.systemd1",
                                             "/org/freedesktop/systemd1",
                                             "org.freedesktop.systemd1.Manager",
                                             "StartUnit",
                                             g_variant_new ("(ss)",
                                                            unit, mode),
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
                return FALSE;
        }

        return TRUE;
}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

gboolean
gsm_util_systemd_reset_failed (GError **error)
{
        g_autoptr(GDBusConnection) connection = NULL;
        g_autoptr(GVariant)        reply = NULL;
        GError *bus_error = NULL;

        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);

        if (connection == NULL)
                return FALSE;

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.systemd1",
                                             "/org/freedesktop/systemd1",
                                             "org.freedesktop.systemd1.Manager",
                                             "ResetFailed",
                                             NULL,
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1, NULL, &bus_error);

        if (bus_error != NULL) {
                g_propagate_error (error, bus_error);
                return FALSE;
        }

        return TRUE;
}
802 803
#endif

804 805 806 807
void
gsm_util_setenv (const char *variable,
                 const char *value)
{
808
        GError *error = NULL;
809

810
        if (child_environment == NULL)
811 812 813 814 815 816
                child_environment = g_listenv ();

        if (!value)
                child_environment = g_environ_unsetenv (child_environment, variable);
        else
                child_environment = g_environ_setenv (child_environment, variable, value, TRUE);
817

818 819 820
        /* If this fails it isn't fatal, it means some things like session
         * management and keyring won't work in activated clients.
         */
821 822 823 824 825 826 827 828 829 830 831
        if (!gsm_util_update_activation_environment (variable, value, &error)) {
                g_warning ("Could not make bus activated clients aware of %s=%s environment variable: %s", variable, value, error->message);
                g_clear_error (&error);
        }

#ifdef HAVE_SYSTEMD
        /* If this fails, the system user session won't get the updated environment
         */
        if (!gsm_util_update_user_environment (variable, value, &error)) {
                g_debug ("Could not make systemd aware of %s=%s environment variable: %s", variable, value, error->message);
                g_clear_error (&error);
832
        }
833
#endif
834
}
835 836 837 838 839 840 841

const char * const *
gsm_util_listenv (void)
{
        return (const char * const *) child_environment;

}
842 843 844 845 846 847

const char * const *
gsm_util_get_variable_blacklist (void)
{
        return variable_blacklist;
}