8

After reading this question about the X clipboard getting cleared when vim is exited I learned that the X window clipboard only exists while the program - from which the selection was obtained - remains open.
It is because of this behaviour that programs like "glipper" and "parcellite" exist.

If the X clipboard is cleared every time a program is exited, how do programs like xclip and xsel work?
And what are the security implications of using programs like this? For example, if a password was copied to the clipboard, could this password be saved into some temp file that could be accessed by programs or users?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

12

Unless there's a clipboard application like xclipboard, clipit... that steals the selections from them, xsel/xclip will fork a background process to handle the future selection requests as long as they own the selection.

$ printf test | xclip
$ ps -C xclip
  PID TTY          TIME CMD
14115 pts/10   00:00:00 xclip

That xclip process is handling requests for the selection (here PRIMARY selection). But, if you select something in another application (or use xsel or xclip again to store something else), then that xclip process will concede the selection to that other application and terminate.

$ printf test | xsel
$ ps -C xclip
  PID TTY          TIME CMD
$ ps -C xsel
  PID TTY          TIME CMD
14212 ?        00:00:00 xsel

Above, xsel took over the selection from xclip.

You can find out who owns a given selection with:

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
int main(int argc, char* argv[])
{
    Display *d = XOpenDisplay(NULL);
    Window w = XGetSelectionOwner(d, XInternAtom (d, argv[1], False));
    printf("0x%08x\n", w);
    return 0;
}

Then:

$ make xgo LDFLAGS=-lX11
$ ./xgo PRIMARY
0x07000001

That will give you the window ID. You can use xprop -id or xwininfo -id on that id, but in the case of xclip/xsel, you won't get much information.

On GNU/Linux based systems, ltrace is useful to see what's happening at the X library API level.

See also Capture the X11 protocol's traffic to see what's happening at the X11 protocol level.

  • 1
    thanks that explains it. yes a command like ps -efl --forest will show xclip disowned from its parent and re-attached higher up in the process tree similar to a daemon. Note to future readers who want to run this c code on ubuntu 16.04, I needed to sudo apt-get install libx11-dev, and compile with gcc -o clip clip.c -lX11 to avoid make errors. – the_velour_fog Jun 03 '17 at 00:11