Hack BSD
Hack BSD
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.
By default, only the superuser can start xconsole. A regular user will instead receive a Couldn't
open console message. This is a safety precaution on multiuser systems, preventing regular
users from viewing system messages. If you're the only user who uses your system, remove the
comment (#) from this line in /etc/fbtab:
If you spend a lot of your time at an X session, consider adding xconsole to your ~/.xinitrc file so
it will start automatically (see [Hack #9]).
For example, suppose I want to know when the script from [Hack #80] finishes. I could execute that
script as follows:
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:
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:
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.
Taking Screenshots
Have you ever needed to send a user a screenshot? There are ports available for this purpose, but the
built-in X command xwd will suffice. Creating a screenshot is a simple matter of:
The command will appear to hang as it waits for you to click your mouse on the portion of the
screen you'd like to capture. Use the -root switch to capture the entire screen and save yourself a
click.
You can view and manipulate the resulting file with most third-party image editors, including xv
and gimp. For quick viewing, though, nothing beats the built-in xwud:
Your results won't seem that impressive if you use xwud immediately, as your screen still probably
looks like your screenshot. When you're finished viewing the screenshot, press Ctrl-c.
See also:
man xconsole
man xclipboard
man xauth
man xwd
man xwud