O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
BSD Hacks
By Dru Lavigne
May 2004
More Info

HACK
#100
Fun with X
Use the utilities that come with the core X distribution.
[Discuss (0) | Link to this hack]

There are so many GUI utilities, available either as part of your favorite Window Manager or as a separate installation, that you can forget that the core X distribution also provides several useful and lightweight programs. Do you need to monitor console messages, manage your clipboard, send pop-up messages, or create and view screenshots? Before you hit the ports collection, give the built-in utilities a try.

Sending Pop-up Messages

Do you find yourself starting a command that takes a while to execute, continuing your work in an X session, then returning periodically to the original terminal or xterm to see how that command is perking along? Wouldn't it be easier to send yourself a pop-up message once the command completes?

For example, suppose I want to know when the script from [Hack #80] finishes. I could execute that script as follows:

#~/bin/mycustomupgrade.sh && xmessage -nearmouse cvsup is complete.

When the upgrade completes, a pop-up message with the text cvsup is complete. will appear in my X session near my mouse. That message will disappear once I click on the Okay button.

If you're in the habit of using su -l to provide a new login when you become the superuser, you'll find that the preceding command will fail to send you a pop-up menu. (I'm assuming you're logged in as a regular user when you start your X session. You should be!) Instead, you'll receive this error message:

Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified
Error: Can't open display: :0.0

This has to do with the X authorization process. If I start my X session as the user dru and use su to execute a command, I'm still logged in as dru, so I'm allowed to send a message to my display. However, if I use su -l to execute the command, I'm no longer logged in as dru but as root. The X server refuses to let another user interfere with my display, which is a good thing.

A quick workaround is to not use su -l when sending pop-up messages to your regular user account. An alternative is to understand the X authorization process. You can then use this knowledge to enable the superuser to send a message to any user on any display.

Understanding X authorization

Your X server uses a token known as an MIT magic cookie to provide authorization. When you start your X session, the server creates and stores this unique cookie in ~/.Xauthority. You can view it at any time using this command:

% xauth list
genisis/unix:0  MIT-MAGIC-COOKIE-1  7e7bc20f9413469a7376e2e5c91aa6f1

Take note that you're the only user with access to this file:

% ls -l ~/.Xauthority
-rw-------  1  dru  wheel   101  Feb 18 13:28 .Xauthority

Always keep in the back of your mind, though, that file ownership does not matter to the superuser. For example, if I need to send an important message to the user dru, I can ssh into the system she's working on and become the superuser. Then:

# cp ~dru/.Xauthority .

I now have a copy of dru's magic cookie. However, before I can use it, I'll first have to change my display. Since I sshed into a terminal, I currently don't have one:

# echo $DISPLAY
DISPLAY: Undefined variable.

I don't want just any display, I want the display dru is currently using. I can find the name of her display by reading her magic cookie:

# xauth list
genisis/unix:0  MIT-MAGIC-COOKIE-1  7e7bc20f9413469a7376e2e5c91aa6f1 

The name of her display is genisis/unix:0, where genisis represents the hostname of the system. I'll now attach to that display and send my message:

# setenv DISPLAY genisis/unix:0
# xmessage -nearmouse Time to go home, Dru...
(prompt hangs until dru responds by pressing the "Okay" button)

This cheat works on any system to which you have superuser access. Technically, you can execute any command X understands in a user's X session once you have his cookie and display. Do remember to use your superuser powers for good, though.

See also:


  • man xconsole

  • man xclipboard

  • man xauth

  • man xwd

  • man xwud


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.