1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-launch.h dbus-launch utility
4 * Copyright (C) 2006 Thiago Macieira <thiago@kde.org>
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-launch.h"
29 #include <sys/types.h>
38 #include <X11/Xatom.h>
40 Display *xdisplay = NULL;
41 static Atom selection_atom;
42 static Atom address_atom;
46 x_io_error_handler (Display *xdisplay)
48 verbose ("X IO error\n");
49 kill_bus_and_exit (0);
54 remove_prefix (char *s,
59 plen = strlen (prefix);
61 if (strncmp (s, prefix, plen) == 0)
63 memmove (s, s + plen, strlen (s) - plen + 1);
72 home = getenv ("HOME");
75 /* try from the user database */
76 struct passwd *user = getpwuid (getuid());
83 fprintf (stderr, "Can't get user home directory\n");
90 #define DBUS_DIR ".dbus"
91 #define DBUS_SESSION_BUS_DIR "session-bus"
94 get_session_file (void)
96 static const char prefix[] = "/" DBUS_DIR "/" DBUS_SESSION_BUS_DIR "/";
103 machine = get_machine_uuid ();
107 display = xstrdup (getenv ("DISPLAY"));
110 verbose ("X11 integration disabled because X11 is not running\n");
114 /* remove the screen part of the display name */
115 p = strrchr (display, ':');
128 /* Note that we leave the hostname in the display most of the
129 * time. The idea is that we want to be per-(machine,display,user)
130 * triplet to be extra-sure we get a bus we can connect to. Ideally
131 * we'd recognize when the hostname matches the machine we're on in
132 * all cases; we do try to drop localhost and localhost.localdomain
133 * as a special common case so that alternate spellings of DISPLAY
134 * don't result in extra bus instances.
136 * We also kill the ":" if there's nothing in front of it. This
137 * avoids an ugly double underscore in the filename.
139 remove_prefix (display, "localhost.localdomain:");
140 remove_prefix (display, "localhost:");
141 remove_prefix (display, ":");
143 /* replace the : in the display with _ if the : is still there.
144 * use _ instead of - since it can't be in hostnames.
146 for (p = display; *p; ++p)
152 home = get_homedir ();
154 result = malloc (strlen (home) + strlen (prefix) + strlen (machine) +
155 strlen (display) + 2);
163 strcpy (result, home);
164 strcat (result, prefix);
165 strcat (result, machine);
166 strcat (result, "-");
167 strcat (result, display);
170 verbose ("session file: %s\n", result);
175 ensure_session_directory (void)
180 home = get_homedir ();
182 /* be sure we have space for / and nul */
183 dir = malloc (strlen (home) + strlen (DBUS_DIR) + strlen (DBUS_SESSION_BUS_DIR) + 3);
186 fprintf (stderr, "no memory\n");
192 strcat (dir, DBUS_DIR);
194 if (mkdir (dir, 0700) < 0)
196 /* only print a warning here, writing the session file itself will fail later */
198 fprintf (stderr, "Unable to create %s\n", dir);
202 strcat (dir, DBUS_SESSION_BUS_DIR);
204 if (mkdir (dir, 0700) < 0)
206 /* only print a warning here, writing the session file itself will fail later */
208 fprintf (stderr, "Unable to create %s\n", dir);
217 if (xdisplay != NULL)
220 xdisplay = XOpenDisplay (NULL);
221 if (xdisplay != NULL)
223 verbose ("Connected to X11 display '%s'\n", DisplayString (xdisplay));
224 XSetIOErrorHandler (x_io_error_handler);
230 init_x_atoms (Display *display)
232 static const char selection_prefix[] = "_DBUS_SESSION_BUS_SELECTION_";
233 static const char address_prefix[] = "_DBUS_SESSION_BUS_ADDRESS";
234 static const char pid_prefix[] = "_DBUS_SESSION_BUS_PID";
235 static int init = FALSE;
244 machine = get_machine_uuid ();
248 user = getpwuid (getuid ());
251 verbose ("Could not determine the user informations; aborting X11 integration.\n");
254 user_name = xstrdup(user->pw_name);
256 atom_name = malloc (strlen (machine) + strlen (user_name) + 2 +
257 MAX (strlen (selection_prefix),
258 MAX (strlen (address_prefix),
259 strlen (pid_prefix))));
260 if (atom_name == NULL)
262 verbose ("Could not create X11 atoms; aborting X11 integration.\n");
267 /* create the selection atom */
268 strcpy (atom_name, selection_prefix);
269 strcat (atom_name, user_name);
270 strcat (atom_name, "_");
271 strcat (atom_name, machine);
272 selection_atom = XInternAtom (display, atom_name, FALSE);
274 /* create the address property atom */
275 strcpy (atom_name, address_prefix);
276 address_atom = XInternAtom (display, atom_name, FALSE);
278 /* create the PID property atom */
279 strcpy (atom_name, pid_prefix);
280 pid_atom = XInternAtom (display, atom_name, FALSE);
289 * Gets the daemon address from the X11 display.
290 * Returns FALSE if there was an error. Returning
291 * TRUE does not mean the address exists.
294 x11_get_address (char **paddress, pid_t *pid, long *wid)
306 /* locate the selection owner */
307 owner = XGetSelectionOwner (xdisplay, selection_atom);
309 return TRUE; /* no owner */
313 /* get the bus address */
314 result = XGetWindowProperty (xdisplay, owner, address_atom, 0, 1024, False,
315 XA_STRING, &type, &format, &items, &after,
316 (unsigned char **) &data);
317 if (result != Success || type == None || after != 0 || data == NULL || format != 8)
318 return FALSE; /* error */
320 *paddress = xstrdup (data);
327 result = XGetWindowProperty (xdisplay, owner, pid_atom, 0, sizeof pid, False,
328 XA_CARDINAL, &type, &format, &items, &after,
329 (unsigned char **) &data);
330 if (result == Success && type != None && after == 0 && data != NULL && format == 32)
331 *pid = (pid_t) *(long*) data;
335 return TRUE; /* success */
339 * Saves the address in the X11 display. Returns 0 on success.
340 * If an error occurs, returns -1. If the selection already exists,
341 * returns 1. (i.e. another daemon is already running)
344 set_address_in_x11(char *address, pid_t pid)
346 char *current_address;
348 unsigned long pid32; /* Xlib property functions want _long_ not 32-bit for format "32" */
350 /* lock the X11 display to make sure we're doing this atomically */
351 XGrabServer (xdisplay);
353 if (!x11_get_address (¤t_address, NULL, NULL))
359 if (current_address != NULL)
361 /* someone saved the address in the meantime */
362 free (current_address);
366 /* Create our window */
367 wid = XCreateWindow (xdisplay, RootWindow (xdisplay, 0), -20, -20, 10, 10,
368 0, CopyFromParent, InputOnly, CopyFromParent,
370 verbose ("Created window %d\n", wid);
372 /* Save the property in the window */
373 XChangeProperty (xdisplay, wid, address_atom, XA_STRING, 8, PropModeReplace,
374 (unsigned char *)address, strlen (address));
376 XChangeProperty (xdisplay, wid, pid_atom, XA_CARDINAL, 32, PropModeReplace,
377 (unsigned char *)&pid32, 1);
379 /* Now grab the selection */
380 XSetSelectionOwner (xdisplay, selection_atom, wid, CurrentTime);
383 /* Ungrab the server to let other people use it too */
384 XUngrabServer (xdisplay);
386 /* And make sure that the ungrab gets sent to X11 */
393 * Saves the session address in session file. Returns TRUE on
394 * success, FALSE if an error occurs.
397 set_address_in_file (char *address, pid_t pid, Window wid)
402 ensure_session_directory ();
403 session_file = get_session_file();
404 if (session_file == NULL)
407 f = fopen (session_file, "w");
410 return FALSE; /* some kind of error */
412 "# This file allows processes on the machine with id %s using \n"
413 "# display %s to find the D-Bus session bus with the below address.\n"
414 "# If the DBUS_SESSION_BUS_ADDRESS environment variable is set, it will\n"
415 "# be used rather than this file.\n"
416 "# See \"man dbus-launch\" for more details.\n"
417 "DBUS_SESSION_BUS_ADDRESS=%s\n"
418 "DBUS_SESSION_BUS_PID=%ld\n"
419 "DBUS_SESSION_BUS_WINDOWID=%ld\n",
422 address, (long)pid, (long)wid);
430 x11_save_address (char *address, pid_t pid, long *wid)
432 Window id = set_address_in_x11 (address, pid);
435 if (!set_address_in_file (address, pid, id))
448 return open_x11 () != NULL && init_x_atoms (xdisplay);
452 x11_handle_event (void)
454 if (xdisplay != NULL)
456 while (XPending (xdisplay))
459 XNextEvent (xdisplay, &ignored);
465 void dummy_dbus_launch_x11 (void);
467 void dummy_dbus_launch_x11 (void) { }