2012-01-07 17:21:32 -05:00
|
|
|
/*
|
|
|
|
* X Wayland Support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2013-08-12 03:46:07 -04:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-07 17:21:32 -05:00
|
|
|
|
2013-08-12 04:06:13 -04:00
|
|
|
#include "meta-xwayland-private.h"
|
|
|
|
#include "meta-window-actor-private.h"
|
|
|
|
#include "xserver-server-protocol.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
xserver_set_window_id (struct wl_client *client,
|
|
|
|
struct wl_resource *compositor_resource,
|
|
|
|
struct wl_resource *surface_resource,
|
|
|
|
guint32 xid)
|
|
|
|
{
|
|
|
|
MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource);
|
|
|
|
MetaDisplay *display = meta_get_display ();
|
|
|
|
MetaWindow *window;
|
|
|
|
|
2013-12-09 13:48:58 -05:00
|
|
|
window = meta_display_lookup_x_window (display, xid);
|
2014-01-31 11:24:02 -05:00
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
2014-03-11 10:23:35 -04:00
|
|
|
/* If the window has an existing surface, like if we're
|
|
|
|
* undecorating or decorating the window, then we need
|
|
|
|
* to detach the window from its old surface.
|
|
|
|
*/
|
|
|
|
if (window->surface)
|
|
|
|
window->surface->window = NULL;
|
|
|
|
|
2014-04-02 10:37:08 -04:00
|
|
|
meta_wayland_surface_set_window (surface, window);
|
2014-01-31 11:24:02 -05:00
|
|
|
window->surface = surface;
|
2014-02-24 19:22:56 -05:00
|
|
|
|
|
|
|
meta_compositor_window_surface_changed (display->compositor, window);
|
2013-08-12 04:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xserver_interface xserver_implementation = {
|
2014-01-29 10:23:58 -05:00
|
|
|
xserver_set_window_id
|
2013-08-12 04:06:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
bind_xserver (struct wl_client *client,
|
|
|
|
void *data,
|
|
|
|
guint32 version,
|
|
|
|
guint32 id)
|
|
|
|
{
|
2014-02-03 19:37:23 -05:00
|
|
|
MetaXWaylandManager *manager = data;
|
2013-08-12 04:06:13 -04:00
|
|
|
|
|
|
|
/* If it's a different client than the xserver we launched,
|
2014-02-03 19:37:23 -05:00
|
|
|
* just freeze up... */
|
|
|
|
if (client != manager->client)
|
2013-08-12 04:06:13 -04:00
|
|
|
return;
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
manager->xserver_resource = wl_resource_create (client, &xserver_interface,
|
|
|
|
MIN (META_XSERVER_VERSION, version), id);
|
|
|
|
wl_resource_set_implementation (manager->xserver_resource,
|
|
|
|
&xserver_implementation, manager, NULL);
|
2013-08-12 04:06:13 -04:00
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
xserver_send_listen_socket (manager->xserver_resource, manager->abstract_fd);
|
|
|
|
xserver_send_listen_socket (manager->xserver_resource, manager->unix_fd);
|
2013-08-12 04:06:13 -04:00
|
|
|
|
|
|
|
/* Make sure xwayland will recieve the above sockets in a finite
|
|
|
|
* time before unblocking the initialization mainloop since we are
|
|
|
|
* then going to immediately try and connect to those as the window
|
|
|
|
* manager. */
|
|
|
|
wl_client_flush (client);
|
|
|
|
|
|
|
|
/* At this point xwayland is all setup to start accepting
|
|
|
|
* connections so we can quit the transient initialization mainloop
|
|
|
|
* and unblock meta_wayland_init() to continue initializing mutter.
|
|
|
|
* */
|
2014-02-03 19:37:23 -05:00
|
|
|
g_main_loop_quit (manager->init_loop);
|
|
|
|
g_clear_pointer (&manager->init_loop, g_main_loop_unref);
|
2013-08-12 04:06:13 -04:00
|
|
|
}
|
|
|
|
|
2012-01-07 17:21:32 -05:00
|
|
|
static char *
|
|
|
|
create_lockfile (int display, int *display_out)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
int size;
|
|
|
|
char pid[11];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
pid_t other;
|
|
|
|
|
|
|
|
filename = g_strdup_printf ("/tmp/.X%d-lock", display);
|
|
|
|
fd = open (filename, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
|
|
|
|
|
|
|
|
if (fd < 0 && errno == EEXIST)
|
|
|
|
{
|
|
|
|
fd = open (filename, O_CLOEXEC, O_RDONLY);
|
|
|
|
if (fd < 0 || read (fd, pid, 11) != 11)
|
|
|
|
{
|
|
|
|
const char *msg = strerror (errno);
|
|
|
|
g_warning ("can't read lock file %s: %s", filename, msg);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
/* ignore error and try the next display number */
|
|
|
|
display++;
|
|
|
|
continue;
|
2014-01-29 10:23:58 -05:00
|
|
|
}
|
2012-01-07 17:21:32 -05:00
|
|
|
close (fd);
|
|
|
|
|
|
|
|
other = strtol (pid, &end, 0);
|
|
|
|
if (end != pid + 10)
|
|
|
|
{
|
|
|
|
g_warning ("can't parse lock file %s", filename);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
/* ignore error and try the next display number */
|
|
|
|
display++;
|
|
|
|
continue;
|
2014-01-29 10:23:58 -05:00
|
|
|
}
|
2012-01-07 17:21:32 -05:00
|
|
|
|
|
|
|
if (kill (other, 0) < 0 && errno == ESRCH)
|
|
|
|
{
|
|
|
|
if (unlink (filename) < 0)
|
|
|
|
{
|
|
|
|
const char *msg = strerror (errno);
|
|
|
|
g_warning ("failed to unlink stale lock file: %s", msg);
|
|
|
|
display++;
|
|
|
|
}
|
|
|
|
g_free (filename);
|
|
|
|
continue;
|
2014-01-29 10:23:58 -05:00
|
|
|
}
|
2012-01-07 17:21:32 -05:00
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
display++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (fd < 0)
|
|
|
|
{
|
|
|
|
const char *msg = strerror (errno);
|
|
|
|
g_warning ("failed to create lock file %s: %s", filename , msg);
|
|
|
|
g_free (filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
/* Subtle detail: we use the pid of the wayland compositor, not the xserver
|
|
|
|
* in the lock file. */
|
|
|
|
size = snprintf (pid, 11, "%10d\n", getpid ());
|
|
|
|
if (size != 11 || write (fd, pid, 11) != 11)
|
|
|
|
{
|
|
|
|
unlink (filename);
|
|
|
|
close (fd);
|
|
|
|
g_warning ("failed to write pid to lock file %s", filename);
|
|
|
|
g_free (filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
*display_out = display;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bind_to_abstract_socket (int display)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t size, name_size;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
name_size = snprintf (addr.sun_path, sizeof addr.sun_path,
|
|
|
|
"%c/tmp/.X11-unix/X%d", 0, display);
|
|
|
|
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
|
|
|
if (bind (fd, (struct sockaddr *) &addr, size) < 0)
|
|
|
|
{
|
2014-04-01 19:57:55 -04:00
|
|
|
g_warning ("failed to bind to @%s: %m", addr.sun_path + 1);
|
2012-01-07 17:21:32 -05:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen (fd, 1) < 0)
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bind_to_unix_socket (int display)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t size, name_size;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
name_size = snprintf (addr.sun_path, sizeof addr.sun_path,
|
|
|
|
"/tmp/.X11-unix/X%d", display) + 1;
|
|
|
|
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
|
|
|
unlink (addr.sun_path);
|
|
|
|
if (bind (fd, (struct sockaddr *) &addr, size) < 0)
|
|
|
|
{
|
2014-04-01 19:57:55 -04:00
|
|
|
g_warning ("failed to bind to %s: %m\n", addr.sun_path);
|
2012-01-07 17:21:32 -05:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-29 10:23:58 -05:00
|
|
|
if (listen (fd, 1) < 0)
|
|
|
|
{
|
2012-01-07 17:21:32 -05:00
|
|
|
unlink (addr.sun_path);
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
2014-01-29 10:23:58 -05:00
|
|
|
}
|
2012-01-07 17:21:32 -05:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-08-12 03:46:07 -04:00
|
|
|
static void
|
2013-08-09 11:56:24 -04:00
|
|
|
uncloexec (gpointer user_data)
|
2013-08-12 03:46:07 -04:00
|
|
|
{
|
|
|
|
int fd = GPOINTER_TO_INT (user_data);
|
|
|
|
|
|
|
|
/* Make sure the client end of the socket pair doesn't get closed
|
|
|
|
* when we exec xwayland. */
|
|
|
|
int flags = fcntl (fd, F_GETFD);
|
|
|
|
if (flags != -1)
|
|
|
|
fcntl (fd, F_SETFD, flags & ~FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xserver_died (GPid pid,
|
|
|
|
gint status,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
if (!WIFEXITED (status))
|
|
|
|
g_error ("X Wayland crashed; aborting");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For now we simply abort if we see the server exit.
|
|
|
|
*
|
|
|
|
* In the future X will only be loaded lazily for legacy X support
|
|
|
|
* but for now it's a hard requirement. */
|
|
|
|
g_error ("Spurious exit of X Wayland server");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 09:31:18 -04:00
|
|
|
static int
|
|
|
|
x_io_error (Display *display)
|
|
|
|
{
|
|
|
|
g_error ("Connection to xwayland lost");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-01 20:00:59 -04:00
|
|
|
static gboolean
|
|
|
|
choose_xdisplay (MetaXWaylandManager *manager)
|
2012-01-07 17:21:32 -05:00
|
|
|
{
|
|
|
|
int display = 0;
|
|
|
|
char *lockfile = NULL;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
lockfile = create_lockfile (display, &display);
|
|
|
|
if (!lockfile)
|
|
|
|
{
|
2014-01-29 10:23:58 -05:00
|
|
|
g_warning ("Failed to create an X lock file");
|
|
|
|
return FALSE;
|
2012-01-07 17:21:32 -05:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
manager->abstract_fd = bind_to_abstract_socket (display);
|
|
|
|
if (manager->abstract_fd < 0)
|
2012-01-07 17:21:32 -05:00
|
|
|
{
|
|
|
|
unlink (lockfile);
|
|
|
|
|
|
|
|
if (errno == EADDRINUSE)
|
|
|
|
{
|
|
|
|
display++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
manager->unix_fd = bind_to_unix_socket (display);
|
|
|
|
if (manager->abstract_fd < 0)
|
2012-01-07 17:21:32 -05:00
|
|
|
{
|
|
|
|
unlink (lockfile);
|
2014-02-03 19:37:23 -05:00
|
|
|
close (manager->abstract_fd);
|
2012-01-07 17:21:32 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
manager->display_index = display;
|
|
|
|
manager->lockfile = lockfile;
|
2012-01-07 17:21:32 -05:00
|
|
|
|
2014-04-01 20:00:59 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
meta_xwayland_start (MetaXWaylandManager *manager,
|
|
|
|
struct wl_display *wl_display)
|
|
|
|
{
|
|
|
|
int sp[2];
|
|
|
|
pid_t pid;
|
|
|
|
char **env;
|
|
|
|
char *fd_string;
|
|
|
|
|
|
|
|
if (!choose_xdisplay (manager))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-04-01 20:00:20 -04:00
|
|
|
wl_global_create (wl_display, &xserver_interface,
|
|
|
|
META_XSERVER_VERSION,
|
|
|
|
manager, bind_xserver);
|
|
|
|
|
2012-01-07 17:21:32 -05:00
|
|
|
/* We want xwayland to be a wayland client so we make a socketpair to setup a
|
|
|
|
* wayland protocol connection. */
|
|
|
|
if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sp) < 0)
|
|
|
|
{
|
|
|
|
g_warning ("socketpair failed\n");
|
2014-04-01 20:00:59 -04:00
|
|
|
unlink (manager->lockfile);
|
2012-01-07 17:21:32 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-12 03:46:07 -04:00
|
|
|
env = g_get_environ ();
|
|
|
|
fd_string = g_strdup_printf ("%d", sp[1]);
|
|
|
|
env = g_environ_setenv (env, "WAYLAND_SOCKET", fd_string, TRUE);
|
|
|
|
g_free (fd_string);
|
|
|
|
|
2014-02-07 19:26:35 -05:00
|
|
|
manager->display_name = g_strdup_printf (":%d", manager->display_index);
|
2013-08-12 03:46:07 -04:00
|
|
|
|
2014-03-25 06:59:04 -04:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
gchar *args[] = { XWAYLAND_PATH,
|
|
|
|
manager->display_name,
|
|
|
|
"-wayland",
|
|
|
|
"-rootless",
|
|
|
|
"-noreset",
|
|
|
|
"-nolisten",
|
|
|
|
"all",
|
|
|
|
NULL };
|
2014-03-25 12:41:52 -04:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;
|
|
|
|
flags |= G_SPAWN_DO_NOT_REAP_CHILD;
|
|
|
|
|
|
|
|
/* xwayland, please. */
|
|
|
|
if (getenv ("XWAYLAND_STFU"))
|
|
|
|
{
|
|
|
|
flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
|
|
|
|
flags |= G_SPAWN_STDERR_TO_DEV_NULL;
|
|
|
|
}
|
2014-03-25 06:59:04 -04:00
|
|
|
|
|
|
|
if (g_spawn_async (NULL, /* cwd */
|
|
|
|
args,
|
|
|
|
env,
|
2014-03-25 12:41:52 -04:00
|
|
|
flags,
|
2014-03-25 06:59:04 -04:00
|
|
|
uncloexec,
|
|
|
|
GINT_TO_POINTER (sp[1]),
|
|
|
|
&pid,
|
|
|
|
&error))
|
|
|
|
{
|
|
|
|
close (sp[1]);
|
|
|
|
manager->client = wl_client_create (wl_display, sp[0]);
|
|
|
|
|
|
|
|
manager->pid = pid;
|
|
|
|
g_child_watch_add (pid, xserver_died, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_error ("Failed to fork for xwayland server: %s", error->message);
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 03:46:07 -04:00
|
|
|
|
|
|
|
g_strfreev (env);
|
2012-01-07 17:21:32 -05:00
|
|
|
|
2013-08-12 04:06:13 -04:00
|
|
|
/* We need to run a mainloop until we know xwayland has a binding
|
|
|
|
* for our xserver interface at which point we can assume it's
|
|
|
|
* ready to start accepting connections. */
|
2014-02-03 19:37:23 -05:00
|
|
|
manager->init_loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
g_main_loop_run (manager->init_loop);
|
2013-08-12 04:06:13 -04:00
|
|
|
|
2012-01-07 17:21:32 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-08-12 09:31:18 -04:00
|
|
|
/* To be called right after connecting */
|
|
|
|
void
|
|
|
|
meta_xwayland_complete_init (void)
|
|
|
|
{
|
|
|
|
/* We install an X IO error handler in addition to the child watch,
|
|
|
|
because after Xlib connects our child watch may not be called soon
|
|
|
|
enough, and therefore we won't crash when X exits (and most important
|
|
|
|
we won't reset the tty).
|
|
|
|
*/
|
|
|
|
XSetIOErrorHandler (x_io_error);
|
|
|
|
}
|
|
|
|
|
2012-01-07 17:21:32 -05:00
|
|
|
void
|
2014-02-03 19:37:23 -05:00
|
|
|
meta_xwayland_stop (MetaXWaylandManager *manager)
|
2012-01-07 17:21:32 -05:00
|
|
|
{
|
|
|
|
char path[256];
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
snprintf (path, sizeof path, "/tmp/.X%d-lock", manager->display_index);
|
2012-01-07 17:21:32 -05:00
|
|
|
unlink (path);
|
2014-02-03 19:37:23 -05:00
|
|
|
snprintf (path, sizeof path, "/tmp/.X11-unix/X%d", manager->display_index);
|
2012-01-07 17:21:32 -05:00
|
|
|
unlink (path);
|
|
|
|
|
2014-02-03 19:37:23 -05:00
|
|
|
unlink (manager->lockfile);
|
2012-01-07 17:21:32 -05:00
|
|
|
}
|