How do command line clipboard tools like "xclip" and "xsel" persist the clipboard - in a X windows environment that doesn't?

the_velour_fog

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?

Stéphane Chazelas

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Backgroundworker doesn't like Clipboard

From Dev

Image copied to clipboard doesn't persist on Linux

From Dev

Operate X clipboard from command line

From Dev

Command Line Clipboard Access

From Dev

How to use xclip to concatenate clipboard contents to file?

From Dev

How to pipe text from command line to the clipboard

From Dev

How to Send the Contents of a Text File to the Clipboard from the Command Line in OS X?

From Dev

How to avoid xsel sending text to clipboard with space/Enter at the end

From Dev

windows like clipboard manager for linux?

From Dev

Copy to clipboard from php command line script in Windows 7

From Dev

Windows utility/tool command line to move clipboard to file

From Dev

in MySQL Command Line, how do I paste from the clipboard using only the keyboard?

From Dev

How secure is the Windows clipboard?

From Dev

How do I pipe the last command in my command history to clipboard?

From Dev

How to copy an entire command line to my clipboard, without the mouse?

From Dev

How to copy an image to the clipboard from a file using command line?

From Dev

How to copy text from command line to clipboard without using the mouse?

From Dev

How to send rich text to the clipboard from command line?

From Dev

How to copy an image to the clipboard from a file using command line?

From Dev

How to copy an entire command line to my clipboard, without the mouse?

From Dev

How to copy a picture to clipboard from command line in linux?

From Dev

Copying files from command line to clipboard

From Dev

A command-line clipboard copy and paste utility?

From Dev

Copying files from command line to clipboard

From Dev

A command-line clipboard copy and paste utility?

From Dev

What is the command line equivalent of copying a file to clipboard?

From Dev

Clipboard for copying and pasting files in command line?

From Dev

Using command line to copy html file to clipboard

From Dev

How do I view my clipboard history on OS X?

Related Related

  1. 1

    Backgroundworker doesn't like Clipboard

  2. 2

    Image copied to clipboard doesn't persist on Linux

  3. 3

    Operate X clipboard from command line

  4. 4

    Command Line Clipboard Access

  5. 5

    How to use xclip to concatenate clipboard contents to file?

  6. 6

    How to pipe text from command line to the clipboard

  7. 7

    How to Send the Contents of a Text File to the Clipboard from the Command Line in OS X?

  8. 8

    How to avoid xsel sending text to clipboard with space/Enter at the end

  9. 9

    windows like clipboard manager for linux?

  10. 10

    Copy to clipboard from php command line script in Windows 7

  11. 11

    Windows utility/tool command line to move clipboard to file

  12. 12

    in MySQL Command Line, how do I paste from the clipboard using only the keyboard?

  13. 13

    How secure is the Windows clipboard?

  14. 14

    How do I pipe the last command in my command history to clipboard?

  15. 15

    How to copy an entire command line to my clipboard, without the mouse?

  16. 16

    How to copy an image to the clipboard from a file using command line?

  17. 17

    How to copy text from command line to clipboard without using the mouse?

  18. 18

    How to send rich text to the clipboard from command line?

  19. 19

    How to copy an image to the clipboard from a file using command line?

  20. 20

    How to copy an entire command line to my clipboard, without the mouse?

  21. 21

    How to copy a picture to clipboard from command line in linux?

  22. 22

    Copying files from command line to clipboard

  23. 23

    A command-line clipboard copy and paste utility?

  24. 24

    Copying files from command line to clipboard

  25. 25

    A command-line clipboard copy and paste utility?

  26. 26

    What is the command line equivalent of copying a file to clipboard?

  27. 27

    Clipboard for copying and pasting files in command line?

  28. 28

    Using command line to copy html file to clipboard

  29. 29

    How do I view my clipboard history on OS X?

HotTag

Archive