Cogs and Levers A blog full of technical stuff

Can't connect to X11 window server when using docker

On some host system configurations, I’ve found that Docker containers that use the host’s X11 session to present a user interface, terminate with a dreaded:

Can't connect to X11 window server using 'unix:0.0' as the value of the DISPLAY variable

Seems that on these systems, access needs to be granted to users making connections to the X11 host. This is done using the xhost command, like so.

$ sudo xhost +

Your docker containers shouldn’t have any issues now.

Viewing shared dependencies with ldd

Today’s post is about the system utility ldd. ldd will show you the shared object dependencies on the program that you supply as input.

$ ldd program

I ran ldd against an executable that I’d made from a previous post on libxml2 and was given the following results:

linux-vdso.so.1 =>  (0x00007ffc6e987000)
libxml2.so.2 => /usr/lib/x86_64-linux-gnu/libxml2.so.2 (0x00007fbbfcca7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbbfc8e2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fbbfc6de000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fbbfc4c5000)
liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007fbbfc2a3000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbbfbf9d000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbbfd00d000)

A word of warning is given in the man page, when using this tool however:

In the usual case, ldd invokes the standard dynamic linker (see ld.so(8)) with the LD_TRACE_LOADED_OBJECTS environment variable set to 1, which causes the linker to display the library dependencies. Be aware, however, that n some circumstances, some versions of ldd may attempt to obtain the dependency information by directly executing the program. Thus, you should never employ ldd on an untrusted executable, since this may result in the execution of arbitrary code.

The suggestion from this manual page is to use the objdump command, as this won’t invoke anything extra to find the dependencies:

$ objdump -p program | grep NEEDED

For the same program, this gives me the following output:

NEEDED      libxml2.so.2
NEEDED      libc.so.6

Console control with readline

The GNU Readline Library is a really useful library for creating single-line input style programs at the console. From the introduction:

The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in.

There’s some really nice stuff added on top though. Being able to use emacs or vi editing modes are just some of them. This allows you to provide your users with consistent key bindings to what they use in their text editor.

Modules

Taking a look on your file system, you can find readline’s header files under /usr/include/readline/.

chardefs.h  keymaps.h   rlconf.h  rltypedefs.h
history.h   readline.h  rlstdc.h  tilde.h

Taking a really brief tour of these headers, history.h provides the library with the ability to recount previously entered text blocks. This sort of behavior can be observed when using bash; pressing the up and down buttons allow you to scroll through previous commands. keymaps.h provides the library with the mapping abstraction to make your readline-based application behave like vi or emacs or any custom implemented map you wish.

Reading lines

The library’s purpose is to read lines of text. The function that you’ll use to do this is called readline. A simple example of this would be:

#include <stdlib.h>
#include <stdio.h>
#include <readline/readline.h>

int main() {
  char *name;

  name = readline("What is your name? ");

  printf("Hello %s!\n", name);
  free(name);

  return 0;
}

Note! The string that you’re given back from readline is allocated using malloc, so it’s your responsibility to ensure that the memory is freed using free.

Resulting with the following execution:

$ ./hello
What is your name? Joe
Hello Joe!

Historical and auto complete

You can quickly give your readline program a historical account of previously entered lines with the following sort of structure:

while (running) {
  filename = readline("Enter a filename: ");

  // add the data to the input history
  add_history(filename);

  // perform some work on "filename"

  free(filename);
}

The add_history method is available from the readline/history.h header.

Allowing your line edit to include file path auto completion is pretty easy also. You can bind this behavior to any key that you’d like; we’re all pretty used to the tab character doing this work for us though:

rl_bind_key('\t', rl_complete);

Using libxml2

libxml2 is a C library that provides XML handling capabilities to your applications. It supports an extensive range of standards and specifications common in the XML community. It’s also a highly ported library between platforms and architectures making it a good choice when your application needs to move.

For a deeper-dive into the libxml2 library, I suggest a read through on their tutorial.

Building applications

The xml2-config program allows your Makefiles to remain relatively noise-free. To build any applications against libxml2, you just need the --cflags and --libs switches to compile and link respectively:

$ gcc `xml2-config --cflags --libs` prog.c -o prog

Writing programs

Rather than re-produce them all, I’ve just got a link to the set of examples on the libxml2 site.

File encodings and iconv

There are a handful of really useful tools for dealing with character encoding. In today’s post, I’ll take you through identifying this characteristic and changing it.

What is Character Encoding?

Wikipedia has the most comprehensive breakdown on the topic. The simplest way to look at it though is that a character encoding assigns a code to each character in an alphabet.

Code Encoding Character
65 ASCII A
U+2776 UNICODE
0xd8 LATIN4 Ø

The unicode and latin4 characters don’t exist within the ASCII character space, therefore those characters simple don’t translate and can’t be encoded by ASCII.

Querying files

To determine what encoding is being used with a file, you can use the file unix utility.

$ echo "Here is some text" > a-test-file
$ file a-test-file
a-test-file: ASCII text

Using the -i switch, we can turn the ASCII text output into a mime string which can yield some more information:

$ file -i a-test-file
a-test-file: text/plain; charset=us-ascii

To make this encoding representation change a little clearer, below I’ve pasted in the output of hexdump on that test file that we’d created earlier.

0000000 6548 6572 6920 2073 6f73 656d 7420 7865
0000010 0a74                                   
0000012

Remember, these bytes are not only in hex; they’re also flipped according to how the string is written. Let’s take the first two bytes 6548:

0x65 = e
0x48 = H

We’re using an 8-bit encoding, our string has 17 characters plus a newline (18). Easy.

Changing the encoding of a file

We can use iconv to transition our text file from one encoding to another. We specify its current encoding with the -f switch and the encoding that we want to convert it to using the -t switch.

$ iconv -f ascii -t unicode a-test-file > a-test-file.unicode

This is changing our test file into an encoding that uses more data-space per character. Taking a look at the type of file we’ve just created:

$ file a-test-file.unicode a-test-file.unicode: Little-endian UTF-16 Unicode text, with no line terminators

If we take a look at the hexdump of this file, you can see that every byte is now padded with an extra zeroed-out byte.

0000000 feff 0048 0065 0072 0065 0020 0069 0073
0000010 0020 0073 006f 006d 0065 0020 0074 0065
0000020 0078 0074 000a                         
0000026

The file also starts with a BOM of feff which was unseen in the ASCII counterpart.

What encodings are supported

You can list the known coded character sets with iconv as well with the --list switch. This will dump a massive list of encodings (and aliases) that you can use.

More!

A really good article was written about the The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). It’s certainly well written article that’ll give you some schooling in encodings, quick smart.