~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to services/plugins/desktopEvents/x11Lock.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2010 VMware, Inc. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License as published
 
6
 * by the Free Software Foundation version 2.1 and no later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
10
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 
11
 * License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/**
 
20
 * @file x11lock.c
 
21
 *
 
22
 * Sets up an X11 lock atom and check it to avoid multiple running instances
 
23
 * of vmusr.
 
24
 */
 
25
 
 
26
#include "desktopEventsInt.h"
 
27
#include "vmware.h"
 
28
 
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <gdk/gdkx.h>
 
32
#include <gtk/gtk.h>
 
33
#include <X11/Xlib.h>
 
34
 
 
35
#define LOCK_ATOM_NAME  "vmware-user-lock"
 
36
 
 
37
 
 
38
/*
 
39
 ******************************************************************************
 
40
 * InitGroupLeader --                                                   */ /**
 
41
 *
 
42
 * This routine sets a few properties related to our main window created
 
43
 * by {gdk,gtk}_init.  Specifically this routine sets the window title,
 
44
 * sets the override_redirect X11 property, and reparents it to the root
 
45
 * window,
 
46
 *
 
47
 * In addition, this routine will return Xlib handles for the following
 
48
 * objects:
 
49
 *   - Main or group leader window
 
50
 *   - Display's root window
 
51
 *
 
52
 * As a result of this function:
 
53
 *   - groupLeader will have a title of VMUSER_TITLE.
 
54
 *   - groupLeader, if not already directly parented by the root, will be.
 
55
 *
 
56
 *   - dpy will point to our default display (ex: $DISPLAY).
 
57
 *   - groupLeader will point to the window created by gtk_init().
 
58
 *   - rootWindow will point to the root window on $DISPLAY.
 
59
 *
 
60
 * @param[out] groupLeader    Group leader window.
 
61
 * @param[out] rootWindow     Root window.
 
62
 *
 
63
 * @return TRUE on success, FALSE on failure.
 
64
 *
 
65
 ******************************************************************************
 
66
 */
 
67
 
 
68
static gboolean
 
69
InitGroupLeader(Window *groupLeader,
 
70
                Window *rootWindow)
 
71
{
 
72
   Window myGroupLeader;
 
73
   Window myRootWindow;
 
74
   XSetWindowAttributes attr;
 
75
   GdkDisplay *gdkDisplay;
 
76
   GdkWindow *gdkLeader;
 
77
 
 
78
   attr.override_redirect = True;
 
79
 
 
80
   ASSERT(groupLeader);
 
81
   ASSERT(rootWindow);
 
82
 
 
83
   gdkDisplay = gdk_display_get_default();
 
84
   gdkLeader = gdk_display_get_default_group(gdkDisplay);
 
85
   myGroupLeader = GDK_WINDOW_XWINDOW(gdkLeader);
 
86
   myRootWindow = GDK_ROOT_WINDOW();
 
87
 
 
88
   ASSERT(myGroupLeader);
 
89
   ASSERT(myRootWindow);
 
90
 
 
91
   /* XXX: With g_set_prgname() being called, this can probably go away. */
 
92
   XStoreName(GDK_DISPLAY(), myGroupLeader, VMUSER_TITLE);
 
93
 
 
94
   /*
 
95
    * Sanity check:  Set the override redirect property on our group leader
 
96
    * window (not default), then re-parent it to the root window (default).
 
97
    * This makes sure that (a) a window manager can't re-parent our window,
 
98
    * and (b) that we remain a top-level window.
 
99
    */
 
100
   XChangeWindowAttributes(GDK_DISPLAY(), myGroupLeader, CWOverrideRedirect,
 
101
                           &attr);
 
102
   XReparentWindow(GDK_DISPLAY(), myGroupLeader, myRootWindow, 10, 10);
 
103
   XSync(GDK_DISPLAY(), FALSE);
 
104
 
 
105
   *groupLeader = myGroupLeader;
 
106
   *rootWindow = myRootWindow;
 
107
 
 
108
   return TRUE;
 
109
}
 
110
 
 
111
 
 
112
/*
 
113
 ******************************************************************************
 
114
 * QueryX11Lock --                                                      */ /**
 
115
 *
 
116
 * This is just a wrapper around XGetWindowProperty which queries the
 
117
 * window described by <dpy,w> for the property described by lockAtom.
 
118
 *
 
119
 * @param[in] dpy          X11 display to query
 
120
 * @param[in] w            Window to query
 
121
 * @param[in] lockAtom     Atom used for locking
 
122
 *
 
123
 * @return TRUE if property defined by parameters exists; FALSE otherwise.
 
124
 *
 
125
 ******************************************************************************
 
126
 */
 
127
 
 
128
static gboolean
 
129
QueryX11Lock(Display *dpy,
 
130
             Window w,
 
131
             Atom lockAtom)
 
132
{
 
133
   Atom ptype;                  // returned property type
 
134
   int pfmt;                    // returned property format
 
135
   unsigned long np;            // returned # of properties
 
136
   unsigned long remaining;     // amount of data remaining in property
 
137
   unsigned char *data = NULL;
 
138
 
 
139
   if (XGetWindowProperty(dpy, w, lockAtom, 0, 1, False, lockAtom,
 
140
                          &ptype, &pfmt, &np, &remaining, &data) != Success) {
 
141
      g_warning("%s: Unable to query window %lx for property %s\n", __func__, w,
 
142
                LOCK_ATOM_NAME);
 
143
      return FALSE;
 
144
   }
 
145
 
 
146
   /*
 
147
    * Xlib is wacky.  If the following test is true, then our property
 
148
    * didn't exist for the window in question.  As a result, `data' is
 
149
    * unset, so don't worry about the lack of XFree(data) here.
 
150
    */
 
151
   if (ptype == None) {
 
152
      return FALSE;
 
153
   }
 
154
 
 
155
   /*
 
156
    * We care only about the existence of the property, not its value.
 
157
    */
 
158
   XFree(data);
 
159
 
 
160
   return TRUE;
 
161
}
 
162
 
 
163
 
 
164
/*
 
165
 ******************************************************************************
 
166
 * AcquireDisplayLock --                                                */ /**
 
167
 *
 
168
 * This function "locks" the display against being "claimed" by another
 
169
 * instance of vmware-user.  It will succeed if we're the first/only
 
170
 * instance of vmware-user, and fail otherwise.
 
171
 *
 
172
 * NB: This routine must be called -after- gtk_init().
 
173
 *
 
174
 * Vmware-user enjoys per-display exclusivity using the following algorithm:
 
175
 *
 
176
 *   1.  Grab X server.  (I.e., get exclusive access.)
 
177
 *   2.  Search for top-level X windows meeting the following criteria:
 
178
 *       a.  named "vmware-user"
 
179
 *       b.  has the property "vmware-user-lock" set.
 
180
 *   3a. If any such windows described above found, then another vmware-user
 
181
 *       process is attached to this display, so we consider the display
 
182
 *       locked.
 
183
 *   3b. Else we're the only one.  Set the "vmware-user-lock" property on
 
184
 *       our top-level window.
 
185
 *   4.  Ungrab the X server.
 
186
 *
 
187
 * The first time this routine is ever called during the lifetime of an X
 
188
 * session, a new X11 Atom, "vmware-user-lock" is created for the lifetime
 
189
 * of the X server.
 
190
 *
 
191
 * The "vmware-user-lock" property may be set on this process's group leader
 
192
 * window.
 
193
 *
 
194
 * @return TRUE if "lock" acquired (i.e., we're the first/only vmware-user
 
195
 *         process); otherwise FALSE.
 
196
 *
 
197
 ******************************************************************************
 
198
 */
 
199
 
 
200
static gboolean
 
201
AcquireDisplayLock(void)
 
202
{
 
203
   Display *defaultDisplay;     // Current default X11 display.
 
204
   Window rootWindow;           // Root window of defaultDisplay; used as root node
 
205
                                // passed to XQueryTree().
 
206
   Window groupLeader;          // Our instance's window group leader.  This is
 
207
                                // implicitly created by gtk_init().
 
208
 
 
209
   Window *children = NULL;     // Array of windows returned by XQueryTree().
 
210
   unsigned int nchildren;      // Length of children.
 
211
 
 
212
   Window dummy1, dummy2;       // Throwaway window IDs for XQueryTree().
 
213
   Atom lockAtom;               // Refers to the "vmware-user-lock" X11 Atom.
 
214
 
 
215
   unsigned int index;
 
216
   Bool alreadyLocked = FALSE;  // Set to TRUE if we discover lock is held.
 
217
   Bool retval = FALSE;
 
218
 
 
219
   defaultDisplay = GDK_DISPLAY();
 
220
 
 
221
   /*
 
222
    * Reset some of our main window's settings & fetch Xlib handles for
 
223
    * the GDK group leader and root windows.
 
224
    */
 
225
   if (InitGroupLeader(&groupLeader, &rootWindow) == FALSE) {
 
226
      g_warning("%s: unable to initialize main window.\n", __func__);
 
227
      return FALSE;
 
228
   }
 
229
 
 
230
   /*
 
231
    * Look up the lock atom, creating it if it doesn't already exist.
 
232
    */
 
233
   lockAtom = XInternAtom(defaultDisplay, LOCK_ATOM_NAME, False);
 
234
   if (lockAtom == None) {
 
235
      g_warning("%s: unable to create X11 atom: " LOCK_ATOM_NAME "\n", __func__);
 
236
      return FALSE;
 
237
   }
 
238
 
 
239
   /*
 
240
    * Okay, so at this point the following is done:
 
241
    *
 
242
    *   1.  Our top-level / group leader window is a child of the display's
 
243
    *       root window.
 
244
    *   2.  The window manager can't get its hands on said window.
 
245
    *   3.  We have a handle on the X11 atom which will be used to identify
 
246
    *       the X11 property used as our lock.
 
247
    */
 
248
 
 
249
   g_debug("%s: Grabbing X server.\n", __func__);
 
250
 
 
251
   /*
 
252
    * Neither of these can fail, or at least not in the sense that they'd
 
253
    * return an error.  Instead we'd likely see an X11 I/O error, tearing
 
254
    * the connection down.
 
255
    *
 
256
    * XSync simply blocks until the XGrabServer request is acknowledged
 
257
    * by the server.  It makes sure that we don't continue issuing requests,
 
258
    * such as XQueryTree, until the server grants our "grab".
 
259
    */
 
260
   XGrabServer(defaultDisplay);
 
261
   XSync(defaultDisplay, False);
 
262
 
 
263
   /*
 
264
    * WARNING:  At this point, we have grabbed the X server.  Consider the
 
265
    * UI to be completely frozen.  Under -no- circumstances should we return
 
266
    * without ungrabbing the server first.
 
267
    */
 
268
 
 
269
   if (XQueryTree(defaultDisplay, rootWindow, &dummy1, &dummy2, &children,
 
270
                  &nchildren) == 0) {
 
271
      g_warning("%s: XQueryTree failed\n", __func__);
 
272
      goto out;
 
273
   }
 
274
 
 
275
   /*
 
276
    * Iterate over array of top-level windows.  Search for those named
 
277
    * vmware-user and with the property "vmware-user-lock" set.
 
278
    *
 
279
    * If any such windows are found, then another process has already
 
280
    * claimed this X session.
 
281
    */
 
282
   for (index = 0; (index < nchildren) && !alreadyLocked; index++) {
 
283
      char *name = NULL;
 
284
 
 
285
      /* Skip unless window is named vmware-user. */
 
286
      if ((XFetchName(defaultDisplay, children[index], &name) == 0) ||
 
287
          (name == NULL) ||
 
288
          strcmp(name, VMUSER_TITLE)) {
 
289
         XFree(name);
 
290
         continue;
 
291
      }
 
292
 
 
293
      /*
 
294
       * Query the window for the "vmware-user-lock" property.
 
295
       */
 
296
      alreadyLocked = QueryX11Lock(defaultDisplay, children[index], lockAtom);
 
297
      XFree(name);
 
298
   }
 
299
 
 
300
   /*
 
301
    * Yay.  Lock isn't held, so go ahead and acquire it.
 
302
    */
 
303
   if (!alreadyLocked) {
 
304
      unsigned char dummy[] = "1";
 
305
      g_debug("%s: Setting property " LOCK_ATOM_NAME "\n", __func__);
 
306
      /*
 
307
       * NB: Current Xlib always returns one.  This may generate a -fatal- IO
 
308
       * error, though.
 
309
       */
 
310
      XChangeProperty(defaultDisplay, groupLeader, lockAtom, lockAtom, 8,
 
311
                      PropModeReplace, dummy, sizeof dummy);
 
312
      retval = TRUE;
 
313
   }
 
314
 
 
315
out:
 
316
   XUngrabServer(defaultDisplay);
 
317
   XSync(defaultDisplay, False);
 
318
   XFree(children);
 
319
 
 
320
   return retval;
 
321
}
 
322
 
 
323
 
 
324
/*
 
325
 ******************************************************************************
 
326
 * X11Lock_Init --                                                      */ /**
 
327
 *
 
328
 * Initializes GTK, and sets up a lock atom to make sure only one vmusr
 
329
 * instance is running.
 
330
 *
 
331
 * On error, this function will request that the application's main loop stop
 
332
 * running.
 
333
 *
 
334
 * @param[in] ctx       Application context.
 
335
 * @param[in] pdata     Registration data.
 
336
 *
 
337
 * @return TRUE on success, FALSE if another vmusr instance owns the display or
 
338
 *         not running in the right container (vmusr).
 
339
 *
 
340
 ******************************************************************************
 
341
 */
 
342
 
 
343
gboolean
 
344
X11Lock_Init(ToolsAppCtx *ctx,
 
345
             ToolsPluginData *pdata)
 
346
{
 
347
   int argc = 0;
 
348
   char *argv[] = { NULL, NULL };
 
349
 
 
350
   if (strcmp(ctx->name, VMTOOLS_USER_SERVICE) != 0) {
 
351
      VMTOOLSAPP_ERROR(ctx, EXIT_FAILURE);
 
352
      return FALSE;
 
353
   }
 
354
 
 
355
   /*
 
356
    * We depend on the window title when performing (primitive) vmware-user
 
357
    * session detection, and unfortunately for us, GTK has a nasty habit of
 
358
    * retitling toplevel windows.  That said, we can control GTK's default
 
359
    * title by setting Glib's application or program name.
 
360
    *
 
361
    * XXX Consider using g_set_application_name("VMware User Agent") or
 
362
    * similar.
 
363
    */
 
364
   g_set_prgname(VMUSER_TITLE);
 
365
   argv[0] = VMUSER_TITLE;
 
366
 
 
367
   /* XXX: is calling gtk_init() multiple times safe? */
 
368
   gtk_init(&argc, (char ***) &argv);
 
369
 
 
370
   if (!AcquireDisplayLock()) {
 
371
      g_warning("Another instance of vmware-user already running. Exiting.\n");
 
372
      VMTOOLSAPP_ERROR(ctx, EXIT_FAILURE);
 
373
      return FALSE;
 
374
   }
 
375
 
 
376
   return TRUE;
 
377
}
 
378