Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get client pid to x11 surface #1670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PapyElGringo
Copy link
Contributor

In order to be able to match a Surface to the .desktop file that started it retrieving the proper surface PID is mandatory.

Currently the X11Surface.pid() return the underlying _NET_WM_PID artifact which is not robust enough as the Signal Flatpak App return "2" for exemple while the new X11Surface.get_client_pid return the proper PID.

I took inspiration of the following mutter code

static pid_t
meta_window_x11_get_client_pid (MetaWindow *window)
{
  MetaX11Display *x11_display = window->display->x11_display;
  xcb_connection_t *xcb = XGetXCBConnection (x11_display->xdisplay);
  xcb_res_client_id_spec_t spec = { 0 };
  xcb_res_query_client_ids_cookie_t cookie;
  xcb_res_query_client_ids_reply_t *reply = NULL;

  spec.client = meta_window_x11_get_xwindow (window);
  spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;

  cookie = xcb_res_query_client_ids (xcb, 1, &spec);
  reply = xcb_res_query_client_ids_reply (xcb, cookie, NULL);

  if (reply == NULL)
    return 0;

  uint32_t pid = 0, *value;
  xcb_res_client_id_value_iterator_t it;
  for (it = xcb_res_query_client_ids_ids_iterator (reply);
       it.rem;
       xcb_res_client_id_value_next (&it))
    {
      spec = it.data->spec;
      if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID)
        {
          value = xcb_res_client_id_value_value (it.data);
          pid = *value;
          break;
        }
    }

  free (reply);
  return (pid_t) pid;
}

The code use the X11 Res protocol to query the LOCAL_CLIENT_PID for a X11 window.

@Drakulix
Copy link
Member

Drakulix commented Mar 9, 2025

The code use the X11 Res protocol to query the LOCAL_CLIENT_PID for a X11 window.

Did you find some SPEC that lists what exactly the LOCAL_CLIENT_PID referrs to?

I assume the LOCAL-part is concerned with x11 over network, which we do not have to worry about. However if the PID is queried and returned by the client, this smells like it could go wrong and many ways with process namespaces.

@PapyElGringo
Copy link
Contributor Author

Here is the protocol description https://www.x.org/releases/X11R7.7/doc/resourceproto/resproto.txt but I don't know if it holds the informations you are looking for.

But this protocol is used by Mutter to address the same need.

What's could go wrong exactly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants