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
23 #include "dbus-launch.h"
27 #include <sys/types.h>
36 #include <X11/Xatom.h>
38 Display *xdisplay = NULL;
39 static Atom selection_atom;
40 static Atom address_atom;
44 x_io_error_handler (Display *xdisplay)
46 verbose ("X IO error\n");
47 kill_bus_and_exit (0);
52 remove_prefix (char *s,
57 plen = strlen (prefix);
59 if (strncmp (s, prefix, plen) == 0)
61 memmove (s, s + plen, strlen (s) - plen + 1);
70 home = getenv ("HOME");
73 /* try from the user database */
74 struct passwd *user = getpwuid (getuid());
81 fprintf (stderr, "Can't get user home directory\n");
88 #define DBUS_DIR ".dbus"
89 #define DBUS_SESSION_BUS_DIR "session-bus"
92 get_session_file (void)
94 static const char prefix[] = "/" DBUS_DIR "/" DBUS_SESSION_BUS_DIR "/";
101 machine = get_machine_uuid ();
105 display = xstrdup (getenv ("DISPLAY"));
108 verbose ("X11 integration disabled because X11 is not running\n");
112 /* remove the screen part of the display name */
113 p = strrchr (display, ':');
126 /* Note that we leave the hostname in the display most of the
127 * time. The idea is that we want to be per-(machine,display,user)
128 * triplet to be extra-sure we get a bus we can connect to. Ideally
129 * we'd recognize when the hostname matches the machine we're on in
130 * all cases; we do try to drop localhost and localhost.localdomain
131 * as a special common case so that alternate spellings of DISPLAY
132 * don't result in extra bus instances.
134 * We also kill the ":" if there's nothing in front of it. This
135 * avoids an ugly double underscore in the filename.
137 remove_prefix (display, "localhost.localdomain:");
138 remove_prefix (display, "localhost:");
139 remove_prefix (display, ":");
141 /* replace the : in the display with _ if the : is still there.
142 * use _ instead of - since it can't be in hostnames.
144 for (p = display; *p; ++p)
150 home = get_homedir ();
152 result = malloc (strlen (home) + strlen (prefix) + strlen (machine) +
153 strlen (display) + 2);
161 strcpy (result, home);
162 strcat (result, prefix);
163 strcat (result, machine);
164 strcat (result, "-");
165 strcat (result, display);
168 verbose ("session file: %s\n", result);
173 ensure_session_directory (void)
178 home = get_homedir ();
180 /* be sure we have space for / and nul */
181 dir = malloc (strlen (home) + strlen (DBUS_DIR) + strlen (DBUS_SESSION_BUS_DIR) + 3);
184 fprintf (stderr, "no memory\n");
190 strcat (dir, DBUS_DIR);
192 if (mkdir (dir, 0700) < 0)
194 /* only print a warning here, writing the session file itself will fail later */
196 fprintf (stderr, "Unable to create %s\n", dir);
200 strcat (dir, DBUS_SESSION_BUS_DIR);
202 if (mkdir (dir, 0700) < 0)
204 /* only print a warning here, writing the session file itself will fail later */
206 fprintf (stderr, "Unable to create %s\n", dir);
215 if (xdisplay != NULL)
218 xdisplay = XOpenDisplay (NULL);
219 if (xdisplay != NULL)
221 verbose ("Connected to X11 display '%s'\n", DisplayString (xdisplay));
222 XSetIOErrorHandler (x_io_error_handler);
228 init_x_atoms (Display *display)
230 static const char selection_prefix[] = "_DBUS_SESSION_BUS_SELECTION_";
231 static const char address_prefix[] = "_DBUS_SESSION_BUS_ADDRESS";
232 static const char pid_prefix[] = "_DBUS_SESSION_BUS_PID";
233 static int init = FALSE;
242 machine = get_machine_uuid ();
246 user = getpwuid (getuid ());
249 verbose ("Could not determine the user informations; aborting X11 integration.\n");
252 user_name = xstrdup(user->pw_name);
254 atom_name = malloc (strlen (machine) + strlen (user_name) + 2 +
255 MAX (strlen (selection_prefix),
256 MAX (strlen (address_prefix),
257 strlen (pid_prefix))));
258 if (atom_name == NULL)
260 verbose ("Could not create X11 atoms; aborting X11 integration.\n");
265 /* create the selection atom */
266 strcpy (atom_name, selection_prefix);
267 strcat (atom_name, user_name);
268 strcat (atom_name, "_");
269 strcat (atom_name, machine);
270 selection_atom = XInternAtom (display, atom_name, FALSE);
272 /* create the address property atom */
273 strcpy (atom_name, address_prefix);
274 address_atom = XInternAtom (display, atom_name, FALSE);
276 /* create the PID property atom */
277 strcpy (atom_name, pid_prefix);
278 pid_atom = XInternAtom (display, atom_name, FALSE);
287 * Gets the daemon address from the X11 display.
288 * Returns FALSE if there was an error. Returning
289 * TRUE does not mean the address exists.
292 x11_get_address (char **paddress, pid_t *pid, long *wid)
303 /* locate the selection owner */
304 owner = XGetSelectionOwner (xdisplay, selection_atom);
306 return TRUE; /* no owner */
310 /* get the bus address */
311 XGetWindowProperty (xdisplay, owner, address_atom, 0, 1024, False,
312 XA_STRING, &type, &format, &items, &after,
313 (unsigned char **) &data);
314 if (type == None || after != 0 || data == NULL || format != 8)
315 return FALSE; /* error */
317 *paddress = xstrdup (data);
324 XGetWindowProperty (xdisplay, owner, pid_atom, 0, sizeof pid, False,
325 XA_CARDINAL, &type, &format, &items, &after,
326 (unsigned char **) &data);
327 if (type != None && after == 0 && data != NULL && format == 32)
328 *pid = (pid_t) *(long*) data;
332 return TRUE; /* success */
336 * Saves the address in the X11 display. Returns 0 on success.
337 * If an error occurs, returns -1. If the selection already exists,
338 * returns 1. (i.e. another daemon is already running)
341 set_address_in_x11(char *address, pid_t pid)
343 char *current_address;
345 unsigned long pid32; /* Xlib property functions want _long_ not 32-bit for format "32" */
347 /* lock the X11 display to make sure we're doing this atomically */
348 XGrabServer (xdisplay);
350 if (!x11_get_address (¤t_address, NULL, NULL))
356 if (current_address != NULL)
358 /* someone saved the address in the meantime */
359 free (current_address);
363 /* Create our window */
364 wid = XCreateWindow (xdisplay, RootWindow (xdisplay, 0), -20, -20, 10, 10,
365 0, CopyFromParent, InputOnly, CopyFromParent,
367 verbose ("Created window %d\n", wid);
369 /* Save the property in the window */
370 XChangeProperty (xdisplay, wid, address_atom, XA_STRING, 8, PropModeReplace,
371 (unsigned char *)address, strlen (address));
373 XChangeProperty (xdisplay, wid, pid_atom, XA_CARDINAL, 32, PropModeReplace,
374 (unsigned char *)&pid32, 1);
376 /* Now grab the selection */
377 XSetSelectionOwner (xdisplay, selection_atom, wid, CurrentTime);
380 /* Ungrab the server to let other people use it too */
381 XUngrabServer (xdisplay);
383 /* And make sure that the ungrab gets sent to X11 */
390 * Saves the session address in session file. Returns TRUE on
391 * success, FALSE if an error occurs.
394 set_address_in_file (char *address, pid_t pid, Window wid)
399 ensure_session_directory ();
400 session_file = get_session_file();
401 if (session_file == NULL)
404 f = fopen (session_file, "w");
406 return FALSE; /* some kind of error */
408 "# This file allows processes on the machine with id %s using \n"
409 "# display %s to find the D-Bus session bus with the below address.\n"
410 "# If the DBUS_SESSION_BUS_ADDRESS environment variable is set, it will\n"
411 "# be used rather than this file.\n"
412 "# See \"man dbus-launch\" for more details.\n"
413 "DBUS_SESSION_BUS_ADDRESS=%s\n"
414 "DBUS_SESSION_BUS_PID=%ld\n"
415 "DBUS_SESSION_BUS_WINDOWID=%ld\n",
418 address, (long)pid, (long)wid);
427 x11_save_address (char *address, pid_t pid, long *wid)
429 Window id = set_address_in_x11 (address, pid);
432 if (!set_address_in_file (address, pid, id))
445 return open_x11 () != NULL && init_x_atoms (xdisplay);
449 x11_handle_event (void)
451 if (xdisplay != NULL)
453 while (XPending (xdisplay))
456 XNextEvent (xdisplay, &ignored);
462 void dummy_dbus_launch_x11 (void) { }