Blame SOURCES/0001-xwayland-Don-t-spew-warnings-when-looking-for-X11-di.patch

1443a4
From d366b2bc4e89ed5807f0221afc25e66cb3d289ed Mon Sep 17 00:00:00 2001
ae8d13
From: =?UTF-8?q?Jonas=20=C3=85dahl?= <jadahl@gmail.com>
ae8d13
Date: Wed, 9 Dec 2020 11:23:37 +0100
ae8d13
Subject: [PATCH 1/2] xwayland: Don't spew warnings when looking for X11
ae8d13
 displays
ae8d13
ae8d13
It's not important, so only show it when doing MUTTER_DEBUG=wayland.
ae8d13
Instead report what display numbers were eventually found.
ae8d13
---
ae8d13
 src/wayland/meta-xwayland.c | 123 +++++++++++++++++++++++++++---------
ae8d13
 1 file changed, 92 insertions(+), 31 deletions(-)
ae8d13
ae8d13
diff --git a/src/wayland/meta-xwayland.c b/src/wayland/meta-xwayland.c
1443a4
index 15c85df69..699d5561c 100644
ae8d13
--- a/src/wayland/meta-xwayland.c
ae8d13
+++ b/src/wayland/meta-xwayland.c
ae8d13
@@ -146,9 +146,10 @@ meta_xwayland_is_xwayland_surface (MetaWaylandSurface *surface)
ae8d13
 }
ae8d13
 
ae8d13
 static gboolean
ae8d13
-try_display (int    display,
ae8d13
-             char **filename_out,
ae8d13
-             int   *fd_out)
ae8d13
+try_display (int      display,
ae8d13
+             char   **filename_out,
ae8d13
+             int     *fd_out,
ae8d13
+             GError **error)
ae8d13
 {
ae8d13
   gboolean ret = FALSE;
ae8d13
   char *filename;
ae8d13
@@ -164,11 +165,32 @@ try_display (int    display,
ae8d13
       char pid[11];
ae8d13
       char *end;
ae8d13
       pid_t other;
ae8d13
+      int read_bytes;
ae8d13
 
ae8d13
       fd = open (filename, O_CLOEXEC, O_RDONLY);
ae8d13
-      if (fd < 0 || read (fd, pid, 11) != 11)
ae8d13
+      if (fd < 0)
ae8d13
         {
ae8d13
-          g_warning ("can't read lock file %s: %m", filename);
ae8d13
+          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                       "Failed to open lock file %s: %s",
ae8d13
+                       filename, g_strerror (errno));
ae8d13
+          goto out;
ae8d13
+        }
ae8d13
+
ae8d13
+      read_bytes = read (fd, pid, 11);
ae8d13
+      if (read_bytes != 11)
ae8d13
+        {
ae8d13
+          if (read_bytes < 0)
ae8d13
+            {
ae8d13
+              g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                           "Failed to read lock file %s: %s",
ae8d13
+                           filename, g_strerror (errno));
ae8d13
+            }
ae8d13
+          else
ae8d13
+            {
ae8d13
+              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
ae8d13
+                           "Failed to read lock file %s",
ae8d13
+                           filename);
ae8d13
+            }
ae8d13
           goto out;
ae8d13
         }
ae8d13
       close (fd);
ae8d13
@@ -178,7 +200,8 @@ try_display (int    display,
ae8d13
       other = strtol (pid, &end, 0);
ae8d13
       if (end != pid + 10)
ae8d13
         {
ae8d13
-          g_warning ("can't parse lock file %s", filename);
ae8d13
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
ae8d13
+                       "Can't parse lock file %s", filename);
ae8d13
           goto out;
ae8d13
         }
ae8d13
 
ae8d13
@@ -187,18 +210,23 @@ try_display (int    display,
ae8d13
           /* Process is dead. Try unlinking the lock file and trying again. */
ae8d13
           if (unlink (filename) < 0)
ae8d13
             {
ae8d13
-              g_warning ("failed to unlink stale lock file %s: %m", filename);
ae8d13
+              g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                           "Failed to unlink stale lock file %s: %m", filename);
ae8d13
               goto out;
ae8d13
             }
ae8d13
 
ae8d13
           goto again;
ae8d13
         }
ae8d13
 
ae8d13
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
ae8d13
+                   "Lock file %s already occupied", filename);
ae8d13
       goto out;
ae8d13
     }
ae8d13
   else if (fd < 0)
ae8d13
     {
ae8d13
-      g_warning ("failed to create lock file %s: %m", filename);
ae8d13
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
ae8d13
+                   "Failed to create lock file %s: %s",
ae8d13
+                   filename, g_strerror (errno));
ae8d13
       goto out;
ae8d13
     }
ae8d13
 
ae8d13
@@ -223,24 +251,34 @@ try_display (int    display,
ae8d13
 }
ae8d13
 
ae8d13
 static char *
ae8d13
-create_lock_file (int display, int *display_out)
ae8d13
+create_lock_file (int      display,
ae8d13
+                  int     *display_out,
ae8d13
+                  GError **error)
ae8d13
 {
ae8d13
+  g_autoptr (GError) local_error = NULL;
ae8d13
   char *filename;
ae8d13
   int fd;
ae8d13
-
ae8d13
   char pid[12];
ae8d13
   int size;
ae8d13
   int number_of_tries = 0;
ae8d13
 
ae8d13
-  while (!try_display (display, &filename, &fd))
ae8d13
+  while (!try_display (display, &filename, &fd, &local_error))
ae8d13
     {
ae8d13
+      meta_verbose ("Failed to open display %d: %s\n",
ae8d13
+                    display, local_error->message);
ae8d13
+      g_clear_error (&local_error);
ae8d13
+
ae8d13
       display++;
ae8d13
       number_of_tries++;
ae8d13
 
ae8d13
       /* If we can't get a display after 50 times, then something's wrong. Just
ae8d13
        * abort in this case. */
ae8d13
       if (number_of_tries >= 50)
ae8d13
-        return NULL;
ae8d13
+        {
ae8d13
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
ae8d13
+                       "Tried to bind 50 display numbers, giving up");
ae8d13
+          return NULL;
ae8d13
+        }
ae8d13
     }
ae8d13
 
ae8d13
   /* Subtle detail: we use the pid of the wayland compositor, not the xserver
ae8d13
@@ -248,11 +286,22 @@ create_lock_file (int display, int *display_out)
ae8d13
    * it _would've_ written without either the NUL or the size clamping, hence
ae8d13
    * the disparity in size. */
ae8d13
   size = snprintf (pid, 12, "%10d\n", getpid ());
ae8d13
+  errno = 0;
ae8d13
   if (size != 11 || write (fd, pid, 11) != 11)
ae8d13
     {
ae8d13
+      if (errno != 0)
ae8d13
+        {
ae8d13
+          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                       "Failed to write pid to lock file: %s",
ae8d13
+                       g_strerror (errno));
ae8d13
+        }
ae8d13
+      else
ae8d13
+        {
ae8d13
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
ae8d13
+                       "Failed to write pid to lock file");
ae8d13
+        }
ae8d13
       unlink (filename);
ae8d13
       close (fd);
ae8d13
-      g_warning ("failed to write pid to lock file %s", filename);
ae8d13
       g_free (filename);
ae8d13
       return NULL;
ae8d13
     }
ae8d13
@@ -264,8 +313,8 @@ create_lock_file (int display, int *display_out)
ae8d13
 }
ae8d13
 
ae8d13
 static int
ae8d13
-bind_to_abstract_socket (int       display,
ae8d13
-                         gboolean *fatal)
ae8d13
+bind_to_abstract_socket (int        display,
ae8d13
+                         GError   **error)
ae8d13
 {
ae8d13
   struct sockaddr_un addr;
ae8d13
   socklen_t size, name_size;
ae8d13
@@ -274,8 +323,8 @@ bind_to_abstract_socket (int       display,
ae8d13
   fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
ae8d13
   if (fd < 0)
ae8d13
     {
ae8d13
-      *fatal = TRUE;
ae8d13
-      g_warning ("Failed to create socket: %m");
ae8d13
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                   "Failed to create socket: %s", g_strerror (errno));
ae8d13
       return -1;
ae8d13
     }
ae8d13
 
ae8d13
@@ -285,17 +334,18 @@ bind_to_abstract_socket (int       display,
ae8d13
   size = offsetof (struct sockaddr_un, sun_path) + name_size;
ae8d13
   if (bind (fd, (struct sockaddr *) &addr, size) < 0)
ae8d13
     {
ae8d13
-      *fatal = errno != EADDRINUSE;
ae8d13
-      g_warning ("failed to bind to @%s: %m", addr.sun_path + 1);
ae8d13
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                   "Failed to bind to @%s: %s",
ae8d13
+                   addr.sun_path + 1, g_strerror (errno));
ae8d13
       close (fd);
ae8d13
       return -1;
ae8d13
     }
ae8d13
 
ae8d13
   if (listen (fd, 1) < 0)
ae8d13
     {
ae8d13
-      *fatal = errno != EADDRINUSE;
ae8d13
-      g_warning ("Failed to listen on abstract socket @%s: %m",
ae8d13
-                 addr.sun_path + 1);
ae8d13
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                   "Failed to listen on abstract socket @%s: %s",
ae8d13
+                   addr.sun_path + 1, g_strerror (errno));
ae8d13
       close (fd);
ae8d13
       return -1;
ae8d13
     }
ae8d13
@@ -304,7 +354,8 @@ bind_to_abstract_socket (int       display,
ae8d13
 }
ae8d13
 
ae8d13
 static int
ae8d13
-bind_to_unix_socket (int display)
ae8d13
+bind_to_unix_socket (int      display,
ae8d13
+                     GError **error)
ae8d13
 {
ae8d13
   struct sockaddr_un addr;
ae8d13
   socklen_t size, name_size;
ae8d13
@@ -321,13 +372,18 @@ bind_to_unix_socket (int display)
ae8d13
   unlink (addr.sun_path);
ae8d13
   if (bind (fd, (struct sockaddr *) &addr, size) < 0)
ae8d13
     {
ae8d13
-      g_warning ("failed to bind to %s: %m\n", addr.sun_path);
ae8d13
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                   "Failed to bind to %s: %s",
ae8d13
+                   addr.sun_path, g_strerror (errno));
ae8d13
       close (fd);
ae8d13
       return -1;
ae8d13
     }
ae8d13
 
ae8d13
   if (listen (fd, 1) < 0)
ae8d13
     {
ae8d13
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
ae8d13
+                   "Failed to listen on %s: %s",
ae8d13
+                   addr.sun_path, g_strerror (errno));
ae8d13
       unlink (addr.sun_path);
ae8d13
       close (fd);
ae8d13
       return -1;
ae8d13
@@ -385,7 +441,6 @@ choose_xdisplay (MetaXWaylandManager *manager)
ae8d13
 {
ae8d13
   int display = 0;
ae8d13
   char *lock_file = NULL;
ae8d13
-  gboolean fatal = FALSE;
ae8d13
 
ae8d13
   if (display_number_override != -1)
ae8d13
     display = display_number_override;
ae8d13
@@ -394,33 +449,37 @@ choose_xdisplay (MetaXWaylandManager *manager)
ae8d13
 
ae8d13
   do
ae8d13
     {
ae8d13
-      lock_file = create_lock_file (display, &display);
ae8d13
+      g_autoptr (GError) error = NULL;
ae8d13
+
ae8d13
+      lock_file = create_lock_file (display, &display, &error);
ae8d13
       if (!lock_file)
ae8d13
         {
ae8d13
-          g_warning ("Failed to create an X lock file");
ae8d13
+          g_warning ("Failed to create an X lock file: %s", error->message);
ae8d13
           return FALSE;
ae8d13
         }
ae8d13
 
ae8d13
-      manager->abstract_fd = bind_to_abstract_socket (display, &fatal);
ae8d13
+      manager->abstract_fd = bind_to_abstract_socket (display, &error);
ae8d13
       if (manager->abstract_fd < 0)
ae8d13
         {
ae8d13
           unlink (lock_file);
ae8d13
 
ae8d13
-          if (!fatal)
ae8d13
+          if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE))
ae8d13
             {
ae8d13
+              meta_verbose ("Failed to bind abstract socket: %s\n", error->message);
ae8d13
               display++;
ae8d13
               continue;
ae8d13
             }
ae8d13
           else
ae8d13
             {
ae8d13
-              g_warning ("Failed to bind abstract socket");
ae8d13
+              g_warning ("Failed to bind abstract socket: %s", error->message);
ae8d13
               return FALSE;
ae8d13
             }
ae8d13
         }
ae8d13
 
ae8d13
-      manager->unix_fd = bind_to_unix_socket (display);
ae8d13
+      manager->unix_fd = bind_to_unix_socket (display, &error);
ae8d13
       if (manager->unix_fd < 0)
ae8d13
         {
ae8d13
+          meta_verbose ("Failed to bind unix socket: %s\n", error->message);
ae8d13
           unlink (lock_file);
ae8d13
           close (manager->abstract_fd);
ae8d13
           display++;
ae8d13
@@ -435,6 +494,8 @@ choose_xdisplay (MetaXWaylandManager *manager)
ae8d13
   manager->display_name = g_strdup_printf (":%d", manager->display_index);
ae8d13
   manager->lock_file = lock_file;
ae8d13
 
ae8d13
+  g_message ("Using X11 display %s for Xwayland", manager->display_name);
ae8d13
+
ae8d13
   return TRUE;
ae8d13
 }
ae8d13
 
ae8d13
-- 
1443a4
2.29.2
ae8d13