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)
305 /* locate the selection owner */
306 owner = XGetSelectionOwner (xdisplay, selection_atom);
308 return TRUE; /* no owner */
312 /* get the bus address */
313 XGetWindowProperty (xdisplay, owner, address_atom, 0, 1024, False,
314 XA_STRING, &type, &format, &items, &after,
315 (unsigned char **) &data);
316 if (type == None || after != 0 || data == NULL || format != 8)
317 return FALSE; /* error */
319 *paddress = xstrdup (data);
326 XGetWindowProperty (xdisplay, owner, pid_atom, 0, sizeof pid, False,
327 XA_CARDINAL, &type, &format, &items, &after,
328 (unsigned char **) &data);
329 if (type != None && after == 0 && data != NULL && format == 32)
330 *pid = (pid_t) *(long*) data;
334 return TRUE; /* success */
338 * Saves the address in the X11 display. Returns 0 on success.
339 * If an error occurs, returns -1. If the selection already exists,
340 * returns 1. (i.e. another daemon is already running)
343 set_address_in_x11(char *address, pid_t pid)
345 char *current_address;
347 unsigned long pid32; /* Xlib property functions want _long_ not 32-bit for format "32" */
349 /* lock the X11 display to make sure we're doing this atomically */
350 XGrabServer (xdisplay);
352 if (!x11_get_address (¤t_address, NULL, NULL))
358 if (current_address != NULL)
360 /* someone saved the address in the meantime */
361 free (current_address);
365 /* Create our window */
366 wid = XCreateWindow (xdisplay, RootWindow (xdisplay, 0), -20, -20, 10, 10,
367 0, CopyFromParent, InputOnly, CopyFromParent,
369 verbose ("Created window %d\n", wid);
371 /* Save the property in the window */
372 XChangeProperty (xdisplay, wid, address_atom, XA_STRING, 8, PropModeReplace,
373 (unsigned char *)address, strlen (address));
375 XChangeProperty (xdisplay, wid, pid_atom, XA_CARDINAL, 32, PropModeReplace,
376 (unsigned char *)&pid32, 1);
378 /* Now grab the selection */
379 XSetSelectionOwner (xdisplay, selection_atom, wid, CurrentTime);
382 /* Ungrab the server to let other people use it too */
383 XUngrabServer (xdisplay);
385 /* And make sure that the ungrab gets sent to X11 */
392 * Saves the session address in session file. Returns TRUE on
393 * success, FALSE if an error occurs.
396 set_address_in_file (char *address, pid_t pid, Window wid)
401 ensure_session_directory ();
402 session_file = get_session_file();
403 if (session_file == NULL)
406 f = fopen (session_file, "w");
408 return FALSE; /* some kind of error */
410 "# This file allows processes on the machine with id %s using \n"
411 "# display %s to find the D-Bus session bus with the below address.\n"
412 "# If the DBUS_SESSION_BUS_ADDRESS environment variable is set, it will\n"
413 "# be used rather than this file.\n"
414 "# See \"man dbus-launch\" for more details.\n"
415 "DBUS_SESSION_BUS_ADDRESS=%s\n"
416 "DBUS_SESSION_BUS_PID=%ld\n"
417 "DBUS_SESSION_BUS_WINDOWID=%ld\n",
420 address, (long)pid, (long)wid);
429 x11_save_address (char *address, pid_t pid, long *wid)
431 Window id = set_address_in_x11 (address, pid);
434 if (!set_address_in_file (address, pid, id))
447 return open_x11 () != NULL && init_x_atoms (xdisplay);
451 x11_handle_event (void)
453 if (xdisplay != NULL)
455 while (XPending (xdisplay))
458 XNextEvent (xdisplay, &ignored);
464 void dummy_dbus_launch_x11 (void);
466 void dummy_dbus_launch_x11 (void) { }