2 * dbus-update-activation-environment - update D-Bus, and optionally
3 * systemd, activation environment
5 * Copyright © 2014-2015 Collabora Ltd.
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction,
10 * including without limitation the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #ifdef HAVE_SYSEXITS_H
35 # include <sysexits.h>
38 #include <dbus/dbus.h>
42 # include <sys/stat.h>
43 # include <sys/types.h>
46 #include "tool-common.h"
48 #define PROGNAME "dbus-update-activation-environment"
54 #ifndef EX_UNAVAILABLE
55 # define EX_UNAVAILABLE 69
63 /* The Windows C runtime uses a different name */
64 #define environ _environ
66 /* apparently this is the portable way to get the entire environment...
67 * GNU platforms also put it in unistd.h but that's not portable */
68 extern char **environ;
71 /* we don't really have anything useful to say about the stage at which we
73 #define oom() tool_oom ("updating environment")
75 static dbus_bool_t verbose = FALSE;
77 static void say (const char *format, ...) _DBUS_GNUC_PRINTF (1, 2);
80 say (const char *format,
88 fprintf (stderr, "%s: ", PROGNAME);
89 va_start (ap, format);
90 vfprintf (stderr, format, ap);
97 systemd_user_running (void)
99 char *xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR");
102 dbus_bool_t ret = FALSE;
104 if (xdg_runtime_dir == NULL)
107 /* Assume that XDG_RUNTIME_DIR/systemd exists if and only if
108 * "systemd --user" is running. It's OK to use asprintf() here
109 * because we know we're on Linux. */
110 if (asprintf (&path, "%s/systemd", xdg_runtime_dir) < 0)
113 if (stat (path, &buf) == 0)
122 main (int argc, char **argv)
124 DBusConnection *conn;
127 DBusError error = DBUS_ERROR_INIT;
128 DBusMessageIter msg_iter;
129 DBusMessageIter array_iter;
131 int first_non_option = argc;
132 dbus_bool_t all = FALSE;
134 DBusMessage *sd_msg = NULL;
135 DBusMessageIter sd_msg_iter;
136 DBusMessageIter sd_array_iter;
137 dbus_bool_t systemd = FALSE;
140 for (i = 1; i < argc; i++)
142 if (argv[i][0] != '-')
144 first_non_option = i;
147 else if (strcmp (argv[i], "--") == 0)
149 first_non_option = i + 1;
152 else if (strcmp (argv[i], "--all") == 0)
156 else if (strcmp (argv[i], "--systemd") == 0)
161 say ("not on Linux, ignoring --systemd argument");
164 else if (strcmp (argv[i], "--verbose") == 0)
171 "%1$s: update environment variables that will be set for D-Bus\n"
172 " session services\n"
174 "%1$s [options] VAR[=VAL] [VAR2[=VAL2] ...]\n"
175 " Add specified variables to D-Bus activation environment.\n"
176 " If omitted, values are taken from current environment;\n"
177 " variables not found in the environment are ignored.\n"
179 " Add entire current environment to D-Bus activation\n"
185 " Upload all environment variables.\n"
187 " Also update the 'systemd --user' environment\n"
197 if (all && first_non_option < argc)
199 fprintf (stderr, "%s: error: --all cannot be used with VAR or "
200 "VAR=VAL arguments\n", PROGNAME);
204 conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
209 "%s: error: unable to connect to D-Bus: %s\n", PROGNAME,
214 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
215 DBUS_INTERFACE_DBUS, "UpdateActivationEnvironment");
220 dbus_message_iter_init_append (msg, &msg_iter);
222 if (!dbus_message_iter_open_container (&msg_iter, DBUS_TYPE_ARRAY,
223 "{ss}", &array_iter))
229 if (!systemd_user_running ())
231 /* This is only best-effort. */
232 say ("systemd --user not found, ignoring --systemd argument");
239 sd_msg = dbus_message_new_method_call ("org.freedesktop.systemd1",
240 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
246 dbus_message_iter_init_append (sd_msg, &sd_msg_iter);
248 if (!dbus_message_iter_open_container (&sd_msg_iter, DBUS_TYPE_ARRAY,
249 "s", &sd_array_iter))
254 for (i = all ? 0 : first_non_option;
255 all ? environ[i] != NULL : i < argc;
262 DBusMessageIter pair_iter;
274 if (!dbus_validate_utf8 (var, NULL))
276 /* var is either of the form VAR or VAR=VAL */
278 "%s: warning: environment variable not UTF-8: %s\n",
283 eq = strchr (copy, '=');
289 /* items in the environment block should be of the form
292 "%s: warning: environment variable without '=': %s\n",
298 /* items on the command-line may be of the form VAR
299 * in which case we infer the value from the environment */
304 /* nothing to be done here */
308 if (!dbus_validate_utf8 (val, NULL))
311 "%s: warning: environment variable not UTF-8: %s=%s\n",
319 /* split VAR=VAL into VAR and VAL */
329 /* recombine if necessary */
330 if (asprintf (&combined, "%s=%s", copy, val) < 0)
333 if (!dbus_message_iter_append_basic (&sd_array_iter,
334 DBUS_TYPE_STRING, &combined))
341 if (!dbus_message_iter_open_container (&array_iter,
342 DBUS_TYPE_DICT_ENTRY, NULL, &pair_iter))
345 say ("setting %s=%s", copy, val);
347 if (!dbus_message_iter_append_basic (&pair_iter, DBUS_TYPE_STRING,
351 if (!dbus_message_iter_append_basic (&pair_iter, DBUS_TYPE_STRING,
355 if (!dbus_message_iter_close_container (&array_iter, &pair_iter))
362 if (!dbus_message_iter_close_container (&msg_iter, &array_iter))
367 !dbus_message_iter_close_container (&sd_msg_iter, &sd_array_iter))
371 reply = dbus_connection_send_with_reply_and_block (conn, msg, -1, &error);
376 "%s: error sending to dbus-daemon: %s: %s\n",
377 PROGNAME, error.name, error.message);
378 exit (EX_UNAVAILABLE);
381 if (!dbus_message_get_args (msg, &error, DBUS_TYPE_INVALID))
384 "%s: error from dbus-daemon: %s: %s\n",
385 PROGNAME, error.name, error.message);
386 exit (EX_UNAVAILABLE);
389 dbus_message_unref (reply);
394 reply = dbus_connection_send_with_reply_and_block (conn, sd_msg, -1,
397 /* non-fatal, the main purpose of this thing is to communicate
398 * with dbus-daemon */
402 "%s: warning: error sending to systemd: %s: %s\n",
403 PROGNAME, error.name, error.message);
405 else if (!dbus_message_get_args (msg, &error, DBUS_TYPE_INVALID))
408 "%s: warning: error from systemd: %s: %s\n",
409 PROGNAME, error.name, error.message);
413 dbus_message_unref (reply);
415 dbus_message_unref (sd_msg);
416 dbus_error_free (&error);
420 dbus_message_unref (msg);
421 dbus_connection_unref (conn);