Dienstag, 9. September 2014

nonblocking input - kbhit() example

This is a useful utility, when being in an endless loop and waiting for some user input without blocking the loop.

int kbhit()
    struct timeval tv;
    fd_set fds;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    return FD_ISSET(STDIN_FILENO, &fds);


This function performs a non-blocking check on the standard input (stdin) without timeout 0, tv.tv_sec and tv.tv_usec are both set to zero. "select" is usually used in the case where multiple I/O's need to process, or performing a check at the same time. But in this case, I'm only interested in standard input, therefore only one FD_SET(STDIN_FILENO, &fds) is the trigger. For details of the select parameters, please check out the manual. As we are only interested in input, we place out fd set at second parameter of select(), the 3rd is for output and 4th is for the exception.

Important part, after select if user input is trigger, FD_ISSET will return non zero value, else return 0. So, we now can use it like this:

      //do certain operation..
//user hits enter.

(c) by Neil Matthew und Richard Stones in their book "Beginning Linux Programming (Programmer to Programmer)", Wiley, 3rd edition (30.12.2003), ISBN-10: 0764544977

Donnerstag, 12. Januar 2012

Mittwoch, 30. November 2011

Accessing the host network from a KVM guest

KVM guests can be created and installed very easy by the use of a NAT network. However, with that you cannot access the network on your host as both networks are working in a different network.
The solution is to install the bridge-utils and configure a tun/tap network, which will allow the network access to the host system.

Here an example for Ubuntu 12.04:

  • installing the needed packages
sudo apt-get install bridge-utils
sudo apt-get install uml-utilities

  • make a backup of your original network configuration file
sudo cp /etc/network/interfaces /etc/network/interfaces.backup

  • edit the network configuration file similar to this

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto tap0
iface tap0 inet static
pre-up tunctl -u $USER -t $IFACE
post-down tunctl -d $IFACE

auto br0
iface br0 inet static
bridge_ports eth0 tap0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
  •   restart the network
 sudo /etc/init.d/networking restart

  • start the KVM guest for example with a similar shell script like this:
/usr/bin/kvm  -soundhw es1370 -k de -vga vmware \

-enable-kvm -m 3072 -localtime \
-hda "/vm/vm-images/ubuntu1004_32b"  \
-boot once=c,menu=off \
-net nic,vlan=0,macaddr=52:54:00:7b:95:22 \
-net tap,vlan=0,ifname=tap0,script=no \
-name "Ubuntu 10.04 32B" $*

 voilá :-)

Donnerstag, 6. Oktober 2011

terminating telnet

Terminating telnet is quite easy. Just follow, what telnet itself writes on screen:

Escape character is '^]'.

But, hey, what does that mean? This is the defined ASCII character for "Escape Sequence", which is in value 0x1B or 27 in decimal. You might think that terminating telnet would be possible by just using the "ESC" key on your keyboard, which is exactly the keyvalue. But this key-press will not be recognised by the telnet session.

The solution is to generate the escape-sequence with the following keys ("+" means using the keys simultaneously):


Note: This is how it works on a german keyboard. It might be different (in case of ALT GR) on other language maps.

Mittwoch, 16. Februar 2011

sudo doesn't export LD_LIBRARY_PATH

Sometimes it is desired that you put your own shared libraries into an own directory to keep your system's libraries paths clean. There's nothing to say against it. For this reason, an environment variable can be defined, which points to your library path: LD_LIBRARY_PATH.
Normally you define your path for example in /etc/profile and it is then available for every user.
However, you can find yourself surprised, that calling your application as user root by using the 'sudo' command will fail. The reason is quite simple: The sudo command doesn't export your LD_LIBRARY_PATH!

This can be proven by doing a 'sudo printenv'. Also doing a sudo -V as superuser will show you, that every environment variable beginning with 'LD_*' will not be forwarded.

The only workaround I found for this is the use of the sudo command with an alias.

In your ~/.bashrc file add following alias:

# because sudo doesn't export LD_LIBRARY_PATH for our apps
alias sudo='sudo env LD_LIBRARY_PATH=/usr/local/lib/your_path'

Save this file and source it. Afterwards your sudo command can be used conveniently.

Montag, 15. November 2010

unpacking of source rpm's

Sometimes I have the problem to install a source rpm package on a non-rpm based linux distribution (in my case gentoo linux). In opposite to a source tarball, a source rpm can cause a bit of a headache to get the sourcecode. If you know the way, it's easy though... so here's the solution:

get the source-rpm into your test directory in home, e.g. with fedora kernel sources:

cd {HOME}/test
wget http://mirrors.kernel.org/fedora/releases/11/Fedora/source/SRPMS/kernel-

transfer the source-rpm into a cpio file:

rpm2cpio kernel- > kernelsource.cpio

Now it's easy to unpack the cpio package:
cpio --extract --make-directories < kernelsource.cpio

Voilà, we have now the source code in our test directory.

Donnerstag, 12. November 2009

Read-Only Root-Filesystem on Embedded Linux Systems

Almost every embedded Linux device being in development must finally be mounted "read-only" to avoid clobbering the file system when powering off immediately without doing a save shutdown. The problem in this case are the processes which want to create and write into files (log files, configuration files, temporary files).

An interesting way for making an embedded Linux "poweroff-save" is the use of a RAM disk and the mount option "bind". In an embedded Linux it is not too difficult to find all the processes, who want to write into files. Most times these files can be found in the directories /etc, /var and /tmp.


1) Creating a RAM-Disk

2) Mounting the RAM-Disk

mount -t $TMPFS rwfs /mnt/rwfs -o size=$TMPFS_SIZE

3) copying the affected directories into the RAM-Disk

RAMDIRS="$RAMDIRS /tmp /etc /var"
for i in $RAMDIRS
if [ ! -e /mnt/rwfs/$i ] ; then
cp -a $i /mnt/rwfs/

4) Doing a binding mount of the new directory in the RAM-Disk.

for i in $RAMDIRS
if [ ! -e /mnt/rwfs/$i ] ; then
mount -o bind /mnt/rwfs/$i $i

Doing this very early (e.g. with a startscript) would be very good to make the system not whining about unlucky tries to write.