<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>64k</title>
	<atom:link href="http://www.64k-tec.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.64k-tec.de</link>
	<description>Software development and more ...</description>
	<lastBuildDate>Wed, 29 Feb 2012 14:16:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>TT: Uni-directional file synchronization between different hosts with Unison</title>
		<link>http://www.64k-tec.de/2011/11/tt-uni-directional-file-synchronization-between-different-hosts-with-unison/</link>
		<comments>http://www.64k-tec.de/2011/11/tt-uni-directional-file-synchronization-between-different-hosts-with-unison/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 21:19:25 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Root]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[directory management]]></category>
		<category><![CDATA[file synchronization]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[remote access]]></category>
		<category><![CDATA[Rsync]]></category>
		<category><![CDATA[secure communication]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[synchronization]]></category>
		<category><![CDATA[Synchronizer]]></category>
		<category><![CDATA[tooltips]]></category>
		<category><![CDATA[TT]]></category>
		<category><![CDATA[Unison]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[X11]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2561</guid>
		<description><![CDATA[When you work with at least two computers on the same project on a daily basis you might have a problem. You need to get changed files from host A to host B and vice versa. The problem getting bigger when you work in addition on different operation systems or use more than two hosts. [...]]]></description>
			<content:encoded><![CDATA[<p>When you work with at least two computers on the same project on a daily basis you might have a problem. You need to get changed files from host A to host B and vice versa. The problem getting bigger when you work in addition on different operation systems or use more than two hosts. On UNIX/Linux the preferred tool for such a task is <a title="Rsync - fast incremental file transfer" href="http://rsync.samba.org/" target="_blank">Rsync</a>. Unfortunately Rsync synchronize only in one direction, it doesn't work very well when more than two hosts are involved (and it isn't really comfortable to set up on Windows) and can't use a secure communication channel. Another approach is to check-in changed source files into a <a title="Wikipedia: Revision Control Systems" href="http://en.wikipedia.org/wiki/Revision_control" target="_blank">version control system</a>, like <a title="Wikipedia: Concurrent Versions System" href="http://en.wikipedia.org/wiki/Concurrent_Versions_System" target="_blank">CVS</a>. On host A you check it in and on host B you check it out afterwards. But this means you always need a more or less stable variant of your code, so that other developer can, at least compile, or much better use it. That is not always the case (especially when you leave the office at 11:00 p.m.) and it also doesn't cover files which aren't handled by a version control system. Luckily there is a solution for all the problems mentioned which is called <a title="Unison - A File Synchronizer" href="http://www.cis.upenn.edu/~bcpierce/unison/" target="_blank">Unison</a>. So here comes the second post in the <a title="ToolTips - Useful applications for the daily business" href="/tag/tt/">ToolTips</a> series, which covers an easy and portable way for file synchronization.</p>
<h2>Installing Unison</h2>
<p>Most modern Linux distributions include Unison in their package manage system. On Mac OS X you can use <a title="The MacPorts Project Official Homepage" href="http://www.macports.org/" target="_blank">MacPorts</a>. Alternatively you could download a binary version for Mac OS X or Windows <a title="Binary versions of Unison" href="http://alan.petitepomme.net/unison/index.html" target="_blank">here</a>. To prevent surprises and unnecessary trouble it might be a good idea to make sure that every involved system use the same version of Unison. At least on Linux and Mac OS X it is relatively easy to compile Unison from the <a title="Unison sources" href="http://www.cis.upenn.edu/~bcpierce/unison/download.html" target="_blank">sources</a>.</p>
<h2>Setting up public/private key authentication for ssh</h2>
<p>One advantage of Unison over Rsync is that you can use different communication channels for the file transfers. One is ssh. As I always prefer/demand encrypted communication this is a big plus of course. In the default setup you can just use ssh. But for a little bit more comfort I suggest to create a public/private key pair for the authentication.<br />
<span class="sb_alert">The following creates public/private keys without a password. Although this is much more easier to use, it should be only used on hosts which are trusted. If you are in doubt, use the normal password approach or even better create a public/private key pair with a password.</span> Create a new public/private key pair with the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>host-a ~ $ <span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa</pre></div></div>

<p>When you are asked for a password just hit Enter twice. The command creates the private key in <code class="path">~/.ssh/id_rsa</code> and the public key in <code class="path">~/.ssh/id_rsa.pub</code>. Now copy the public key to host B:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>host-a ~ $ <span style="color: #c20cb9; font-weight: bold;">scp</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub host_B:.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></div></div>

<p>If you already have some public keys on host B, make sure you append the new key and not overwrite the file by the above command. Make the file accessible by the user only with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>host-b ~ $ <span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">600</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></div></div>

<p>Now you should be able to connect to host B without any interaction needed.</p>
<h2>Configuring Unison</h2>
<p>Like in the long UNIX tradition, Unison is configured using text files. The files are located in the <code class="path">~/.unison</code> directory. You can configure more than one synchronization target by choosing a meaningful name. There exists one default target which is configured in the file <code class="path">default.prf</code>. Because I have more than one target I prefer to split the configuration into several files. You can include other project files with the <code>include</code> statement as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># directory on host a (this is where Unison will be executed)</span>
root       = <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>projects
<span style="color: #666666; font-style: italic;"># directory on host b (this is the remote host)</span>
root       = <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>host-b<span style="color: #000000; font-weight: bold;">//</span>mnt<span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>projects
<span style="color: #666666; font-style: italic;"># which directories to sync?</span>
include projects_files.prf
<span style="color: #666666; font-style: italic;"># options</span>
include options.prf
ignorecase = <span style="color: #c20cb9; font-weight: bold;">false</span>
<span style="color: #666666; font-style: italic;"># unison executable on the server</span>
servercmd  = <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>unison</pre></div></div>

<p>We setup the root directories on both machines, including the configuration file for the project target and some generic option file. We also overwriting the default unison location, because this is a self compiled version. The file <code class="path">options.prf</code> looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># No staled nfs and mac store files</span>
ignore  = Name .nfs<span style="color: #000000; font-weight: bold;">*</span>
ignore  = Name .DS_Store
<span style="color: #666666; font-style: italic;"># options</span>
log     = <span style="color: #c20cb9; font-weight: bold;">true</span>
rsrc    = <span style="color: #c20cb9; font-weight: bold;">true</span>
auto    = <span style="color: #c20cb9; font-weight: bold;">true</span>
<span style="color: #666666; font-style: italic;">#debug   = verbose</span>
<span style="color: #666666; font-style: italic;">#logfile = ~/.unison/unison.log</span></pre></div></div>

<p>This just set some generic options which are valid for all my targets. For the specific target <strong>projects</strong> the file <code class="path">projects_files.prf</code> contains mainly the directories and files which should be ignored:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># No ISOs</span>
ignore    = Path vms<span style="color: #000000; font-weight: bold;">/</span>ISO
<span style="color: #666666; font-style: italic;"># Ignore VBox branches</span>
ignore    = Path vbox-<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #666666; font-style: italic;"># No binary output from the other platforms</span>
ignore    = Path vbox<span style="color: #000000; font-weight: bold;">*/</span>out<span style="color: #000000; font-weight: bold;">/*</span>
<span style="color: #666666; font-style: italic;"># One exception:</span>
ignorenot = Path vbox<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>linux.amd64.additions
<span style="color: #666666; font-style: italic;"># No wine stuff</span>
ignore    = Path vbox<span style="color: #000000; font-weight: bold;">*/</span>wine.<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #666666; font-style: italic;"># Tools</span>
ignore    = Name vbox<span style="color: #000000; font-weight: bold;">*/</span>tools<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>FetchDir,freebsd<span style="color: #000000; font-weight: bold;">*</span>,os2<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>So in general, you configure the directory to synchronize and later define directories or files which should be ignored. As you see, you can include or exclude paths as you like. Even simple bash wildcards are possible. As shown in this example I exclude all binary files of a VirtualBox build, because they are useless on another platform. Understanding how Unison decide which directories or files should be synchronized is sometimes difficult. So I suggest to carefully read the <a title="Unison - Documentation" href="http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html" target="_blank">documentation</a> and just use the "try and failure" approach <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Another reason for splitting up the configuration files is you can synchronize these files as well. I have another target which synchronize several configuration files, e.g. <code class="path">.bashrc</code>, <code class="path">.profile</code>, <code class="path">.vim*</code> and the sub-project files of Unison like the <code class="path">projects_files.prf</code>. You can't synchronize e.g. <code class="path">default.prf</code>, cause the root directories are different from host to host, but the general configuration is always the same. My <strong>home</strong> target looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Which directories/files to sync?</span>
path = .bashrc
path = .ion3
path = .gdbinit
path = .cgdb
path = .valgrind-vbox.supp
path = .vim
path = .vimrc
path = .gvimrc
path = .Xdefaults
path = .gnupg
path = .unison<span style="color: #000000; font-weight: bold;">/</span>options.prf
path = .unison<span style="color: #000000; font-weight: bold;">/</span>home_files.prf
path = .unison<span style="color: #000000; font-weight: bold;">/</span>projects_files.prf
<span style="color: #666666; font-style: italic;"># Do not sync:</span>
ignore = Path .vim<span style="color: #000000; font-weight: bold;">/</span>.netrwhist
ignore = Path .ion3<span style="color: #000000; font-weight: bold;">/</span>default-session<span style="color: #000000; font-weight: bold;">*</span>
ignore = Path .cgdb<span style="color: #000000; font-weight: bold;">/</span>readline_history.txt</pre></div></div>

<p>One of the strengths over other synchronization tools is, you can do this for others host as well. So if you synchronize between host a and host b you can also synchronize between host c and host b. However, a little bit of discipline is necessary. There should be one host which all other host synchronize again.<br />
If you now execute <code class="cmd">unison</code> the project target will be used. If you execute <code class="cmd">unison home</code> the files of the home target will be synchronized.</p>
<h2>Conclusion</h2>
<p>Unison is a very powerful tool. You can synchronize between more than two hosts (OS independent), in a secure way and uni-directional. Currently there is no better tool and I use it on a daily basis.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/11/tt-uni-directional-file-synchronization-between-different-hosts-with-unison/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FRITZ!Box tuning part 4: Cross-building and installing additional applications</title>
		<link>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/</link>
		<comments>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 20:14:36 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Root]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[Buildroot]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[cross-building]]></category>
		<category><![CDATA[cross-compiling]]></category>
		<category><![CDATA[dropbear]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[FritzBox]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MIPS]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[overlay]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tmpfs]]></category>
		<category><![CDATA[Toolchain]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2338</guid>
		<description><![CDATA[The articles I wrote about the FRITZ!Box are pretty popular. They are creating the most traffic on my website. I understand this, cause the FRITZ!Box is a really great piece of hardware and AVM is also a company which knows how to make their users happy by serving regular updates to the firmware. Although I [...]]]></description>
			<content:encoded><![CDATA[<p>The articles I wrote about the <a href="/tag/fritzbox/" target="_blank">FRITZ!Box</a> are pretty popular. They are creating the most traffic on my website. I understand this, cause the FRITZ!Box is a really great piece of hardware and AVM is also a company which knows how to make their users happy by serving regular updates to the firmware. Although I didn't tuned my FRITZ!Box any further, I updated it with the latest <a href="http://www.avm.de/de/Service/Service-Portale/Labor/index.php" target="_blank">Labor</a> firmware version regularly. At some point the sshd setup (with dropbear) doesn't worked anymore and I decided it is the time to update my software as well. Beside that it didn't work anymore it is always a good idea to update software which allow access to a host from everywhere very regularly. Anyway, it turned out this isn't as simple as I initial thought. Therefore here is the next post in the FRITZ!Box tuning series, which shows how to cross-build software for the MIPS32 architecture used in the FRITZ!Box and in particular get the sshd software to life again. I use a FRITZ!Box Fon WLAN 7270 v2 and the firmware is 54.05.05. Please make sure you read the other <a href="/tag/fritzbox/" target="_blank">FRITZ!Box articles</a> as well, cause some of the information given there still applies.</p>
<span class="sb_alert">As already mentioned in the other articles, there is no warranty that this will work on your side and I am not responsible for any damage which may happen. You have been warned!</span>
<h2>Cross-building for the MIPS32 architecture</h2>
<p>The FRITZ!Box itself doesn't include any build tools. Why should it! It's an end-consumer product which is usually never changed, beside the user is updating it with an official firmware update. So we have to do it ourself. Creating a build chain for another architecture is usually <a href="http://www.landley.net/writing/docs/cross-compiling.html" target="_blank">hard</a>. There are several things you have to consider. This includes problems with the configuration, endianess differences between the host and the target, bit depths and depending shared libraries which have to be available in the target format. Fortunately this is a common problem, especially for embedded systems, which means there is a proper solution called <a href="http://buildroot.uclibc.org/" target="_blank">Buildroot</a> (look at the first line in <code class="cmd">dmsg</code>, when the FRITZ!Box has booted and you know what I mean). Buildroot is a build system which allows, similar to the Linux kernel, configuring and generating complete embedded Linux systems. Regardless how configured, Buildroot builds a complete Linux system, always. This is of course a little bit overhead for us, but we just pick out the relevant files. First we need to know which architecture we target. You have to login into the FRITZ!Box either using <a href="http://www.64k-tec.de/2010/01/fritzbox-tuning-part-1-enable-remote-access-over-ssh/" target="_blank">telnet or a working ssh daemon</a>. To get some information about the hardware used in your FRITZ!Box execute <code class="cmd">cat /proc/cpuinfo</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># cat /proc/cpuinfo
system type             : TI UR8 (7270)
processor               : 0
cpu model               : MIPS 4KEc V6.8
BogoMIPS                : 359.62
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 16
extra interrupt vector  : yes
hardware watchpoint     : no
ASEs implemented        :
shadow register sets    : 1
core                    : 0
VCED exceptions         : not available
VCEI exceptions         : not available</pre></div></div>

<p>Here we learn that the FRITZ!Box is using a <a href="http://www.mips.com/products/cores/hard-ip-cores/4kec-hard-ip-cores/" target="_blank">MIPS</a> processor. In this case it uses the <a href="http://en.wikipedia.org/wiki/Endianness" target="_blank">little-endian</a> format. Next you need some information about the system C library. On normal Linux (desktop) systems the GNU C Library is used. This library is heavyweight and therefore the <a href="http://uclibc.org/" target="_blank">uClibc</a> was invented for embedded systems. By executing <code class="cmd">ls /lib/libuClibc*</code> you get the version installed. In my case it is 0.9.31. Now we have all information needed to configure Buildroot. <a href="http://buildroot.uclibc.org/download.html" target="_blank">Download</a> and extract the source of Buildroot.</p>
<span class="sb_tip">If you are working on a Mac like me (where Buildroot doesn't work), <a href="http://www.virtualbox.org/" target="_blank">VirtualBox</a> with a Linux system as a guest is a good alternative <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</span>
<p>By executing <code class="cmd">make menuconfig</code> in the root directory of Buildroot you are able to configure it. The most important options are <strong>Target Architecture</strong>, set this to <code>mipsel</code> and <strong>Target Architecture Variant</strong>, set this to <code>mips 32r2</code>. In <strong>Toolchain -&gt; uClibc C library Version</strong> select a version which is close to the one on your FRITZ!Box. Next you have to select which packages should be build in <strong>Package Selection for the target</strong>. Choose whatever you want but for the beginning <strong>Networking applications -&gt; openssh</strong> might be enough. If you prefer a more lightweight ssh implementation <strong>dropbear</strong> may also an option. In the following we use OpenSSH. You can also tweak the build options, like the <code class="cmd">gcc</code> version to use or set the gcc optimization level to <code>-O3</code> in favor of size. After saving the configuration and executing <code class="cmd">make</code>, it is time for a coffee. When the build has been finished all relevant files are in <code class="path">output/target/</code>. We only pack the directories and files which are necessary. For example the <code>libuClibc</code> was built by Buildroot, but we will use the one from the FRITZ!Box. The following create a zip archive of the files for OpenSSH.<a name="zip"></a></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">zip -r ../../local.zip &quot;lib/libutil* lib/libnsl* lib/libresolv* \
 lib/libcrypt* usr/lib usr/share usr/sbin usr/bin etc/ssh_config \
 etc/sshd_config&quot;</pre></div></div>

<h2>Creating overlay directories on the root filesystem</h2>
<p>The following is based on ideas invented by the guys at <a href="http://www.spblinux.de/fbox.new/" target="_blank">http://www.spblinux.de/</a>. Our aim is it to have as little persistent changes on the FRITZ!Box as possible. This allows us to go back to the original state with a simple reboot. Therefore all writeable directories will be on a <a href="http://en.wikipedia.org/wiki/Tmpfs" target="_blank">tmpfs</a> filesystem. tmpfs filesystems are created in RAM and the content will be removed when the filesystem is unmounted. Because the FRITZ!Box has only limited RAM, we will not copy our applications into the tmpfs filesystem, but just create symbolic links to the files on the USB stick (where usually much more space is available). Because we want the applications from the FRITZ!Box and our own one be accessible from the root filesystem, we need to merge both into the tmpfs directory. Therefore first symbolic links into the original filesystem and second symbolic links to the USB stick are created. The second step is only performed when there isn't a file already. This makes sure the original files are always preferred over our own one. The last step is binding the directories to the root filesystem. Summarized the following will be done:</p>
<ol>
<li>remount <code class="path">/</code> readonly</li>
<li>mount <code class="path">/</code> into <code class="path">/var/_ro_</code></li>
<li>create tmpfs in <code class="path">/var/_overlay_</code></li>
<li>symlink the files from <code class="path">/var/_ro_</code> into <code class="path">/var/_overlay_</code></li>
<li>symlink the files from the USB stick into <code class="path">/var/_overlay_</code></li>
<li>bind the directories in <code class="path">/var/_overlay_</code> to <code class="path">/</code></li>
</ol>
<p>This script does the previous steps. It uses helper methods which are defined in <code class="path">common.sh</code>. You will find all the necessary files in the package you can download at the end of this article.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
. common.sh
&nbsp;
<span style="color: #007800;">MNT</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>_overlay_
<span style="color: #007800;">BASE_RO</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>_ro_
<span style="color: #007800;">BASE_OVL</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/local&quot;</span>
&nbsp;
<span style="color: #007800;">DIRS</span>=<span style="color: #ff0000;">&quot;lib etc usr/bin usr/sbin usr/lib usr/share&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remount root readonly</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">${MOUNT}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${GREP}</span>&quot;</span> <span style="color: #ff0000;">&quot;/dev/root&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${GREP}</span>&quot;</span> <span style="color: #660033;">-q</span> ro
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _lmt <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>root <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">&quot;-o remount -r&quot;</span>
<span style="color: #666666; font-style: italic;"># bind root into var</span>
_lchkmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  _lmkdir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span>
  _lmnt <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span> <span style="color: #ff0000;">&quot;-o bind&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create our overlay dir</span>
_lchkmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  _lmkdir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
  _lmnt tmpfs <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span> <span style="color: #ff0000;">&quot;-t tmpfs&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># link/copy the base stuff</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lsymlnk_dir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># link/copy our own stuff</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lsymlnk_dir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_OVL}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># now bind all overlay dirs to root</span>
<span style="color: #666666; font-style: italic;"># (this is critical don't interupt)</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>/<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;/<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;-o bind&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>Now copy the zip archive you have created <a href="#zip">above</a> to the FRITZ!Box and unpack it on the USB stick in <code class="path">addons/local/</code>. The scripts have to be placed into <code class="path">addons/</code>. You need to configure the following in <code class="path">common.sh</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># the base of the usb stick goes here:
BASE=&quot;/var/media/ftp/FLASH-DISK-01/addons&quot;
# add the encrypted root password here:
PASS=&quot;&quot;</pre></div></div>

<p>Point to the base directory within the USB stick in <code>BASE</code>. Also add your encrypted root password into <code>PASS</code>. This will create a root user in <code class="path">/etc/passwd</code> automatically.</p>
<p>If you believe all is correct, you can start the installation by executing <code class="cmd">./install.sh</code>. If no errors are shown, you can try ssh. <code class="cmd">ssh -v</code> should output something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># ssh -v
OpenSSH_5.8p2, OpenSSL 1.0.0d 8 Feb 2011
...</pre></div></div>

<p>Additional to the standard filesystems mounted, you should see the following when executing <code class="cmd">mount</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># mount
...
/dev/root on /var/_ro_ type squashfs (ro,relatime)
tmpfs on /var/_overlay_ type tmpfs (rw,relatime)
tmpfs on /lib type tmpfs (rw,relatime)
tmpfs on /etc type tmpfs (rw,relatime)
tmpfs on /usr/bin type tmpfs (rw,relatime)
tmpfs on /usr/sbin type tmpfs (rw,relatime)
tmpfs on /usr/lib type tmpfs (rw,relatime)
tmpfs on /usr/share type tmpfs (rw,relatime)</pre></div></div>

<h2>Configure and start the OpenSSH daemon</h2>
<p>Now that we have our brand new OpenSSH on the FRITZ!Box working, we need some last steps to let the daemon running correctly. First create a private/public RSA host key pair for the server process by executing:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">ssh-keygen -t rsa -f addons/config/ssh_host_rsa_key</pre></div></div>

<p><code class="path">addons/config/</code> have to be an existing directory below the USB stick base. Next create a file <code class="path">addons/config/sshd_config</code> in the same directory with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">Protocol               2
UsePrivilegeSeparation no
Subsystem              sftp /usr/lib/sftp-server</pre></div></div>

<p>The following script could be use to start the <code class="cmd">sshd</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
. common.sh
&nbsp;
<span style="color: #007800;">SSHD</span>=<span style="color: #ff0000;">&quot;/usr/sbin/sshd&quot;</span>
<span style="color: #007800;">SSH_SPKEY</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/config/ssh_host_rsa_key&quot;</span>
<span style="color: #007800;">SSH_CFG</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/config/sshd_config&quot;</span>
<span style="color: #007800;">SSH_TPKEY</span>=<span style="color: #ff0000;">&quot;/var/tmp/ssh_host_rsa_key&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># sanity check</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSHD}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _lce <span style="color: #007800;">$?</span> <span style="color: #ff0000;">&quot;sshd not installed&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># start in tmp</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp   
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #660033;">-o</span> remount devpts <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>pts <span style="color: #660033;">-t</span> devpts
&nbsp;
<span style="color: #666666; font-style: italic;"># cp key and make them user readable only</span>
_lcp_file <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_SPKEY}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span>
_lchmod <span style="color: #000000;">400</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span>             
&nbsp;
<span style="color: #666666; font-style: italic;"># start sshd</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSHD}</span>&quot;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_CFG}</span>&quot;</span> <span style="color: #660033;">-h</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span></pre></div></div>

<p>Please note that you have to set up the root user first and maybe adjust your port forwarding rules to allow access from the Internet as described in this <a href="http://www.64k-tec.de/2010/01/fritzbox-tuning-part-1-enable-remote-access-over-ssh/" target="_blank">post</a>. The package will contain a script <code class="cmd">startup.sh</code> which will add the root user automatically.</p>
<p>Here is the obligatory screenshot which shows <code class="cmd">htop</code> with color terminal support. You see OpenSSH and OpenVPN in the list of the running processes.</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/htop-e1311594537898.png"><img class="aligncenter size-medium wp-image-2427" title="htop" src="/wordpress/wp-content/uploads/htop-e1311594537898-300x176.png" alt="" width="300" height="176" /></a></p>
<h2>Conclusion</h2>
<p>Thanks to Buildroot it is pretty easy to cross-build for embedded machines these days. With some tricky directory rebinding new applications could be injected into the FRITZ!Box root filesystem without overwriting the existing firmware. Now you should be able to build any software already bundled with Buildroot or even <a href="http://buildroot.uclibc.org/buildroot.html#add_packages" target="_blank">add new one to the build process</a>. Happy cross-building!</p>
<span class="sb_download">You can download the scripts and binaries discussed in this post:<br />
<a href="/downloads/FRITZBox/FRITZBox_OpenSSH_5.8p2.tar.gz">FRITZBox_OpenSSH_5.8p2.tar.gz</a> (3.8MB, <a href="/downloads/FRITZBox/FRITZBox_OpenSSH_5.8p2.tar.gz-sha1.txt">SHA1</a>)</span>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>TT: Console navigation the easy way with Apparix</title>
		<link>http://www.64k-tec.de/2011/05/tt-console-navigation-the-easy-way-with-apparix/</link>
		<comments>http://www.64k-tec.de/2011/05/tt-console-navigation-the-easy-way-with-apparix/#comments</comments>
		<pubDate>Sat, 14 May 2011 19:43:04 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Root]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[Apparix]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[bookmark]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[directory management]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[TT]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[work environment]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2237</guid>
		<description><![CDATA[Today I will start a new series where I present small tools which I use on a daily basis and considered very useful. These tools haven't to be killer applications, but doing the task they are written for, very well. Therefor also the name of this series: TT, which stands for ToolTips. Most of this [...]]]></description>
			<content:encoded><![CDATA[<p>Today I will start a new series where I present small tools which I use on a daily basis and considered very useful. These tools haven't to be killer applications, but doing the task they are written for, very well. Therefor also the name of this series: <a href="/tag/TT/">TT</a>, which stands for ToolTips. Most of this applications are open source, so I will take the opportunity to say "Thank you" to all the people out there, which create such cool stuff in there free time.</p>
<p>We start with a tool called <a href="http://www.micans.org/apparix/" target="_blank">Apparix</a>.</p>
<h2>Console?</h2>
<p>As you may know, I'm a keyboard addict. My preferred OS is Linux, I use <a href="http://tuomov.iki.fi/software/" target="_blank">Ion3</a> as the window manager, <a href="http://software.schmorp.de/pkg/rxvt-unicode.html" target="_blank">rxvt-unicode</a> as terminal application and <a href="http://www.vim.org/" target="_blank">(g)Vim</a> as the preferred editor. On the other side I have a MacBook Pro as laptop and love the possibilities of Mac OS X. How does this come together? Well, all these tools are available on Mac OS X as well. The X11 port of Mac OS X is getting better with each release and using it in fullscreen is good enough. Mac OS X is based on BSD and together with <a href="http://www.macports.org/" target="_blank">MacPorts</a>, there is nothing I missed until today. As I have to work really platform independent, there is Windows. Vim is available, but working console based (read: <code>command.com</code>) isn't really much fun. There is of course <a href="http://www.mingw.org/" target="_blank">mingw</a>, but I never managed it to create a work environment like on Linux/UNIX. Anyway, I work not that much on Windows, so I can live with other tools there as well (a really good free editor is <a href="http://notepad-plus-plus.org/" target="_blank">Notepad++</a> and if you hate Explorer have a look at <a href="http://www.altap.cz/salam_en/index.html" target="_blank">Altap Salamander</a>). So the question remains: Console? I have the feeling using the keyboard within a console application is much faster than clicking around with the mouse. This doesn't mean I like to type every single character when starting an application or changing the current working directory. Many modern command interpreters, like the <a href="http://tiswww.case.edu/php/chet/bash/bashtop.html" target="_blank">Bash</a>, have built in word expansion. This allows you to use the tabulator key for fast expansion of typed characters for a given set of commands. Of course this makes the console life much more easier. But there is more!</p>
<h2>Bookmarking important directories</h2>
<p>Usually, I'm working on projects. One project is VirtualBox. Other might be some private stuff, like personal letters or other development I do. As the regular user organize his projects in directory trees it make sense to bookmark them. Most people believe bookmarking is something Internet specific, but this isn't true. Bookmarking means getting access to an important information very fast, without remembering the exact location of this information. This is exactly what Apparix does. It bookmarks directories and make these bookmarks easy accessible on the console. So I have bookmarks for sub-projects within VirtualBox, like the <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Frontends/VirtualBox" target="_blank">GUI</a> or <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Main" target="_blank">Main</a>. Getting there is as easy as writing <code class="cmd">to vgui</code>. It's the same as writing <code class="cmd">cd /bin</code> and it even provide word expansion of the available bookmarks. In this case the bookmark is <code>vgui</code> and it expand to the GUI directory within the VirtualBox source tree.</p>
<h2>How does this work?</h2>
<p>Installing Apparix is pretty simple. You will find all the information on the <a href="http://www.micans.org/apparix/" target="_blank">website</a>. The important part is the integration within your shell. You need to extent your <code class="path">.bashrc</code>. One way is to add the functions you need into <code class="path">.bashrc</code>. Alternatively you can download the file <a href="http://www.micans.org/apparix/bash_apparix" target="_blank"><code class="path">.bash_apparix</code></a> and source it in your <code class="path">.bashrc</code>. When you have done this and start a new shell, you should navigate to a directory you like to bookmark. There you type <code class="cmd">bm</code> to create your first bookmark. Afterwards, regardless what your current working directory is, you could execute <code class="cmd">to DIR_NAME</code> to get there. Of course you can name the bookmark other than the current directory name, by using <code class="cmd">bm MY_NAME</code>. By executing <code class="cmd">als</code> all bookmarks are shown. There is much more possible of course. See the documentation for more information. Anyway, this is the main use case for Apparix, so there is not much more to say. Really?</p>
<h2>Using the Apparix database for other applications</h2>
<p>The cool thing on many Linux applications is, they are saving their data in simple text files. Also Apparix. The bookmark "database" is saved in a file called <code class="path">.apparixrc</code>. It's a simple text file, which is easy to parse. It looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">j,vbox,/Users/poetzsch/projects/vbox
j,VirtualBox,/Users/poetzsch/projects/vbox/src/VBox/Frontends/VirtualBox
j,Main,/Users/poetzsch/projects/vbox/src/VBox/Main
j,Runtime,/Users/poetzsch/projects/vbox/src/VBox/Runtime
j,Installer,/Users/poetzsch/projects/vbox/src/VBox/Installer/darwin
j,cv,/Users/poetzsch/projects/private/office/latex/cv
j,dow,/Users/poetzsch/Downloads</pre></div></div>

<p>This allows the usage of this information in other contexts as well. One example is Vim. As I mentioned earlier, this is my main editor. So it would be cool to use the bookmarks, I already have, there as well. To easy change the current directory in Vim, add the following to your <code class="path">.vimrc</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #668080;">command</span><span style="color: #000000;">!</span> <span style="color: #000000;">-</span><span style="color: #25BB4D;">complete</span>=custom,BmCe <span style="color: #668080;">-nargs</span>=<span style="color: #000000; font-weight:bold;">1</span> To <span style="color: #000000;">:</span><span style="color: #804040;">call</span> BmTo<span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'&lt;args&gt;'</span><span style="color: #000000;">&#41;</span>
<span style="color: #804040;">fun</span> BmCe<span style="color: #000000;">&#40;</span>A,L,P<span style="color: #000000;">&#41;</span>
 <span style="color: #804040;">return</span> <span style="color: #25BB4D;">system</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">&quot;awk 'BEGIN{FS=<span style="">\&quot;</span>,<span style="">\&quot;</span>}/j,/{print $2}' ~/.apparixrc&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #668080;">endfun</span>
<span style="color: #804040;">fun</span> BmLs<span style="color: #000000;">&#40;</span><span style="color: #668080;">dir</span><span style="color: #000000;">&#41;</span>
 <span style="color: #804040;">return</span> <span style="color: #25BB4D;">system</span><span style="color: #000000;">&#40;</span><span style="color: #C5A22D;">'apparix '</span><span style="color: #000000;">.</span>a<span style="color: #000000;">:</span><span style="color: #668080;">dir</span><span style="color: #000000;">.</span><span style="color: #C5A22D;">' | tr -d &quot;<span style="">\n</span>&quot;'</span><span style="color: #000000;">&#41;</span>
<span style="color: #668080;">endfun</span>
<span style="color: #804040;">fun</span> BmTo<span style="color: #000000;">&#40;</span><span style="color: #668080;">dir</span><span style="color: #000000;">&#41;</span>
 <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">':cd '</span><span style="color: #000000;">.</span>BmLs<span style="color: #000000;">&#40;</span>a<span style="color: #000000;">:</span><span style="color: #668080;">dir</span><span style="color: #000000;">&#41;</span>
<span style="color: #668080;">endfun</span></pre></div></div>

<p>After restarting Vim, it is possible to navigate with <code class="cmd">:To MY_BOOKMARK</code> to any directory known by Apparix.</p>
<h2>Conclusion</h2>
<p>One simple task, getting to a directory where you are often have to be, with one simple solution. Thats all to say about Apparix. It does this in a very simple way, which is fully integrated into the Bash and the saved information could be used by other applications as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/05/tt-console-navigation-the-easy-way-with-apparix/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Test, 1, 2, 3, test, test</title>
		<link>http://www.64k-tec.de/2011/03/test-1-2-3-test-test/</link>
		<comments>http://www.64k-tec.de/2011/03/test-1-2-3-test-test/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 20:51:27 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Callgrind]]></category>
		<category><![CDATA[compile time tests]]></category>
		<category><![CDATA[dynamic analyzer]]></category>
		<category><![CDATA[dynamic tests]]></category>
		<category><![CDATA[fortify]]></category>
		<category><![CDATA[GUI tests]]></category>
		<category><![CDATA[Instruments]]></category>
		<category><![CDATA[leak check]]></category>
		<category><![CDATA[panic]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[static tests]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tinderbox]]></category>
		<category><![CDATA[unit tests]]></category>
		<category><![CDATA[Valgrind]]></category>
		<category><![CDATA[VirtualBox]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=1631</guid>
		<description><![CDATA[Everybody know these words. You say it when you have to check a microphone which were plugged into some sound system. It's just test if the microphone is working well and the spoken words are reflected by the amplifier. It's a simple test to check the functionality of the microphone or the whole sound system. [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody know these words. You say it when you have to check a microphone which were plugged into some sound system. It's just test if the microphone is working well and the spoken words are reflected by the amplifier. It's a simple test to check the functionality of the microphone or the whole sound system. Why should I write about sound systems? No, I don't try to learn playing a guitar or any other instrument, I like to share some thoughts about software testing. After writing a post about <a href="http://www.64k-tec.de/2010/10/using-suppression-files-with-valgrind/">Valgrind</a>, I had the feeling that I should try to show some ways of testing software. In the following post I will analyze what can be tested, what is useful and which tools are available. I don't aim to make a full overview of software testing itself, I just like to highlight some tools (or thoughts) which are useful and every developer should know of.</p>
<h2>Why should I test my software</h2>
<p>This is easy to answer. Humans write software and humans tend to make mistakes. Even if something is working <a href="http://en.wikipedia.org/wiki/TeX#Development" target="_blank">perfect</a>, a bug fix or new feature can break it. Testing software will not prevent you from bugs, but it can boost your user satisfaction a lot, by removing errors which are common and avoidable.</p>
<h2>What can be tested</h2>
<p>First of all, you should think about what should be tested at all. The following list tries to categorizes different tests from a technical perspective:</p>
<ol>
<li>Static tests</li>
<li>Compile time tests</li>
<li>Runtime test</li>
</ol>
<p>Also we have to differ between automatic tests and manual tests done by an operator. In the following I will prioritize the automatic tests, simply because once they are setup, they are easier to handle. Manuel tests means, setting up a test plan, making someone responsible for checking the test plan and forcing the test to be done before a release. On the other side automatic tests could be done by a machine at day and night with every software level your application is currently in.</p>
<h3>Static tests</h3>
<p>Static tests analyze the code base and try to predict error paths. There are several applications on the market, but most of them are not free. First you need a product which speaks your language. If you develop in C, C++, Java, C# or whatever makes a huge difference here. There are many static analyzer out there which can analyze C, but not many can successfully parse any of the widely used higher programming languages. One of them, which can analyze C++, is <a href="https://www.fortify.com/" target="_blank">Fortify</a>. Fortify uses rules to find different errors a developer can do. There are of course default rules, but a developer can define rules himself. This product is specialized in detecting errors which may end in security holes. One such error in terms of security considerations is file input handling. When one is reading a string directly out of a file, some assumptions are done. Strings in the C/C++ world are zero terminated. All string handling/manipulation function, which are available e.g. from the glibc, are working over the single characters as long as they didn't find the termination byte. Now, if a developer is reading a string out of a file and doesn't check if the terminating zero is there, he open a security hole. If an attacker is using this knowledge he is able to provoke a buffer overflow. Fortify can detect such blindly faith to input data. Of course it can detect much more. On the down side, as most software projects are really complex, static code analyzer tend to produce many false/positive. Working through this false/positive and defining filters for the own software project needs much time. On the other side these tools help in finding error paths, which even runtime tests doesn't show, cause they are so unusual. But exactly these errors are used by attackers to abuse your software for bad things.</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/fortify.png"><img class="aligncenter size-medium wp-image-1662" title="Fortify" src="/wordpress/wp-content/uploads/fortify-300x203.png" alt="" width="300" height="203" /></a></p>
<h3>Compile time tests</h3>
<p>Compile tests make sure your application compiles on different architectures, bit depth, or with other resources like external dependencies (e.g. depending third-party libraries). They are important when you develop for and/or if your team is using different platforms. They make sure a software is at least compiling on every important system. This doesn't mean the current status works well, it just makes sure every developer involved in the progress of the product is able to build an actual version, even if he is working on a different part of the software itself. Such a situation happens often in open source products, like Mozilla, which invented <a href="http://en.wikipedia.org/wiki/Tinderbox_%28software%29" target="_blank">tinderbox</a> for making these tests. It allows you to set up different build environments for your software and watch the result of every check-in you made in terms of compatibility to this environment. The following shows a screenshot of the <a href="http://tinderbox.mozilla.org/showbuilds.cgi?tree=Firefox-Ports" target="_blank">Firefox-Ports</a> tinderbox with one broken build box. As everybody know, <span style="color: #ff0000;">red</span> is bad:</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/tinderbox.png"><img class="aligncenter size-medium wp-image-1661" title="Tinderbox" src="/wordpress/wp-content/uploads/tinderbox-300x165.png" alt="" width="300" height="165" /></a></p>
<p style="text-align: left;">Red means a burning tree. It shows you that your last check-in doesn't compile on a specific system. The cool thing is that you can look at the log and spot the error with ease. You know why it doesn't compile and can fix it. This will prevent you from being blamed by other team members because they can't work with the current <a href="http://en.wikipedia.org/wiki/Trunk_%28software%29" target="_blank">trunk</a>.</p>
<h3 style="text-align: left;">Runtime tests</h3>
<p>Runtime tests are of course the biggest part of testing. They try to make sure the application does the job it was written for. This also means these tests are the hardest one. It may help to try to separate them as well. I use the following separation:</p>
<ol>
<li>Unit tests</li>
<li>Dynamic analyzer</li>
<li>GUI tests</li>
</ol>
<h4>Unit tests</h4>
<p>Unit tests check that the specialized functionality a unit provide is working correctly. A well-known example is string handling. String handling is necessary in almost all software projects out there. May it printing the help section of a command line tool or the information presentation in an GUI application. A unit test tries to test anything a user of this unit may do with it. This could be adding a string to another, replacing parts of a string, converting a string to other formats (like to another encoding or to an integer) or much more complex operations like searching within a string with a regular expression. All these operations could be relatively easy tested with unit tests. There is no general usable software out there which could be used for unit testing, but most software frameworks offer some help to write unit tests. Qt has a <a href="http://doc.trolltech.com/latest/qtestlib-manual.html" target="_blank">unit test framework</a> and VirtualBox has a <a href="http://www.virtualbox.org/browser/trunk/include/iprt/test.h" target="_blank">integrated system</a>, as well. Here is a screenshot from a just <a href="http://labs.qt.nokia.com/2010/10/29/making-auto-testing-easier/" target="_blank">announced</a> graphical tool by Nokia, which makes spotting errors in Qt unit tests much more easier. The second screenshot shows the output of a VirtualBox unit test.</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/qt-autotester12.png"><img class="alignnone size-medium wp-image-1592" style="margin-left: 10px; margin-right: 10px;" title="Qt Autotester" src="/wordpress/wp-content/uploads/qt-autotester12-300x220.png" alt="" width="190" height="140" /></a><a href="/wordpress/wp-content/uploads/tstRTStrFormat1.png"><img class="alignnone size-medium wp-image-1663" style="margin-left: 10px; margin-right: 10px;" title="tstRTStrFormat" src="/wordpress/wp-content/uploads/tstRTStrFormat1-300x162.png" alt="" width="259" height="140" /></a></p>
<p>Please also note that unit testing not just means testing functionality. It could also mean testing performance regression. If some function needed an amount of time in an existing version, it should be at least equal or faster in means of execution time in a new version. Unit tests are evolutionary like the software itself. It is illusionary to believe a unit test is finished sometime. The test will/should grow as long as users will report errors. Often a unit test is called by the bugtracker id, which showed some specific problem the first time.</p>
<h4>Dynamic analyzer</h4>
<p>This field of testing is a little bit different from the other one, cause the developer needs to interact here. Although it would be possible to automate some of these tests, they are more designed to be done by the developer from time to time (usually after a feature is completed) to check for some basic errors. The most common dynamic analyzer in the open source world is the Valgrind instrumentation framework. It has a tool for checking memory management errors, like I showed in this <a href="http://www.64k-tec.de/2010/10/using-suppression-files-with-valgrind/" target="_blank">post</a>. As already written there, memory leak errors can happen to everyone, so having a tool for finding and eliminating them, is very useful. On the other side there is a tool called Callgrind, which <a href="http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29" target="_blank">profiles</a> a given software in detail. After profiling some software, a developer is able to see execution times relatively to each other. This makes it possible to find the places an algorithm spend the most time. With this information a developer has the chance to find bottlenecks, which maybe could be eliminated. Beside that, is the knowledge of the <a href="http://en.wikipedia.org/wiki/Call_graph" target="_blank">call graph</a> sometimes from great information, especially if you develop graphical applications, like in Qt. Having events, slots or asynchrony window systems, like in X11, make it sometimes hard to predict how the code will <a href="http://en.wikipedia.org/wiki/Control_flow" target="_blank">flow</a>. It may also be helpful in deciding if some subroutine is a candidate for parallel execution. The following screenshot shows the result of analyzing some parts of VirtualBox. To visualize the output of Callgrind (and to interactively work with this data), <a href="http://kcachegrind.sourceforge.net/html/Home.html" target="_blank">KCachegrind</a> is the number one tool. The second screenshot shows some similar profiling with <a href="http://developer.apple.com/technologies/tools/" target="_blank">Instruments</a> from Xcode.</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/kcachegrind.png"><img class="size-medium wp-image-1672 alignnone" style="margin: 5px 10px;" title="KCachegrind" src="/wordpress/wp-content/uploads/kcachegrind-300x190.png" alt="" width="221" height="140" /></a><a href="/wordpress/wp-content/uploads/instruments.png"><img class="alignnone size-medium wp-image-1677" style="margin: 5px 10px;" title="Instruments" src="/wordpress/wp-content/uploads/instruments-300x182.png" alt="" width="230" height="140" /></a></p>
<h4>GUI tests</h4>
<p>GUI's have the advantage to do several tasks in an independent order and users tend to do tasks in a way a developer don't expect. Developer tends to do tasks in the way they have programmed it. On the other side GUI testing tools do only things they are programmed for. To be honest, we don't do any automatic GUI testing in VirtualBox. It's such a wide field and doing it right is hard. Anyway, there are products out there which can be helpful. Qt itself have possibilities for GUI testing build in. Another product is from <a href="http://www.froglogic.com/" target="_blank">froglogic</a>. Squish is specialized in GUI testing, especially for Qt applications. But of course there are other tools as well. Just have a look at your preferred build environment.</p>
<h2>Conclusion</h2>
<p>Testing produces work. It is also something developers don't like. On the other side it helps to prevent further work by bypassing errors which are preventable. Everybody should ask himself how much time he will sacrifice in setting up test machines or unit tests to make a product much more stable. Errors in freshly released products which don't happen in an <a href="http://en.wikipedia.org/wiki/Software_regression" target="_blank">older release</a> are always bad marketing. If there is a chance to prevent them by just adding some lines of code, everyone should use this chance. This post doesn't show all available tools for testing. For example, the big players like Apple and Microsoft have also testing and analyzing software build into there IDE's. I just want to give a brief overview what is possible, so that everybody could check the own environment for possibilities of doing testing.</p>
<p>As a last word which touches my personal work environment: Please be assured that we are trying hard to make VirtualBox as stable and save as much as possible <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/03/test-1-2-3-test-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kernel driver code signing with the VeriSign Class 3 Primary CA &#8211; G5 certificate</title>
		<link>http://www.64k-tec.de/2011/02/kernel-driver-code-signing-with-the-verisign-class-3-primary-ca-g5-certificate/</link>
		<comments>http://www.64k-tec.de/2011/02/kernel-driver-code-signing-with-the-verisign-class-3-primary-ca-g5-certificate/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 20:58:11 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[Applications]]></category>
		<category><![CDATA[CA]]></category>
		<category><![CDATA[Certificate]]></category>
		<category><![CDATA[Certificate Authority]]></category>
		<category><![CDATA[chain of trust]]></category>
		<category><![CDATA[code signing]]></category>
		<category><![CDATA[cross certificate]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[installer]]></category>
		<category><![CDATA[intermediate certificate]]></category>
		<category><![CDATA[kernel driver module]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Signature]]></category>
		<category><![CDATA[signtool]]></category>
		<category><![CDATA[trustworthy]]></category>
		<category><![CDATA[VeriSign]]></category>
		<category><![CDATA[VirtualBox]]></category>
		<category><![CDATA[WDK]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2101</guid>
		<description><![CDATA[Since the first 64-bit version of Windows Vista it is necessary to digital sign any kernel mode driver. Without a proper code signing the driver isn't loaded by the system. Although it is also possible to sign drivers and applications for the 32-bit versions of Windows (as far as I know starting with Windows XP) [...]]]></description>
			<content:encoded><![CDATA[<p>Since the first <a href="http://msdn.microsoft.com/en-us/library/bb530195.aspx" target="_blank">64-bit version of Windows Vista</a> it is necessary to digital sign any kernel mode driver. Without a proper <a href="http://en.wikipedia.org/wiki/Code_signing" target="_blank">code signing</a> the driver isn't loaded by the system. Although it is also possible to sign drivers and applications for the 32-bit versions of Windows (as far as I know starting with Windows XP) it became mandatory in the 64-bit versions for any kernel mode driver. A serious software provider always sign its own software to make sure the user can rely on the authenticity of the package he e.g. downloaded from the Internet. It also prevent a question about installing a driver from an untrusted source which could be denied by the user and therefore makes the own software unusable. In any case the user has to confirm an installation of a driver, even if this driver is correctly signed, if the driver isn't <em><a href="http://en.wikipedia.org/wiki/WHQL_Testing" target="_blank">Windows Hardware Quality Labs</a></em> (WHQL) certificated. In the following post I will not explain the basics of how to sign Windows drivers, there are many articles out there like the one from Microsoft itself, but I will look at changes which have to be made to correctly code sign drivers with a certificate signed by the <a href="http://www.verisign.com/support/roots.html" target="_blank">VeriSign Class 3 Primary CA - G5</a> root certificate, which is in use by the end of 2010.</p>
<h2>Chain of trust</h2>
<p>To ensure the validity of every component in a computer system (hardware or software) a <a href="http://en.wikipedia.org/wiki/Chain_of_trust" target="_blank">chain of trust</a> is build. This basically means there is some root institution (in computer cryptography this is called <em><a href="http://en.wikipedia.org/wiki/Certificate_Authority" target="_blank">Certificate Authority</a></em> (CA)) which is trusted per se. Any following part in this hierarchy is signed by the parent authority. This allows a flexible mechanism where only the connected parties have to make sure they trust each other to make the full chain trustworthy. This concept is also used for code signing, cause it allows to be trustworthy in the eyes of Microsoft without ever being in touch with them. As Microsoft don't trust (for code signing) every root CA they have in their certificate store, they explicit allow only a handful of root CA's to be in this trust of chain. They archive this by cross signing root CA's with their own CA. The full concept is described in this <a href="http://www.microsoft.com/whdc/driver/install/drvsign/crosscert.mspx" target="_blank">article</a>. It basically means the software maker certificate has to be trusted by an official CA included in every Windows version and this root CA has to be cross signed by Microsoft for code signing. This is the point where the problems start with the new VeriSign Primary CA.</p>
<h2>Finding the little differences</h2>
<p>The old code signing CA is the VeriSign Class 3 Public Primary CA, available since 1996. This certificate uses an 1024 bit key, which isn't considered save anymore in the future. Therefor VeriSign decided to replace this root CA with a stronger one, which uses an 2048 bit key. If you simply replace an old software maker certificate (signed with the old root CA) with one which is signed by the new CA you get a surprise. Installing a kernel mode driver signed with the new certificate ends up with a message like this:</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/unsigned_driver.png"><img class="aligncenter size-medium wp-image-2126" title="Error, because an unsigned driver." src="/wordpress/wp-content/uploads/unsigned_driver-300x284.png" alt="" width="201" height="190" /></a></p>
<p>A look into the security log of the event viewer shows this error message:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Code integrity determined that the image hash of a file is not valid. The
file could be corrupt due to unauthorized modification or the invalid hash
could indicate a potential disk device error.</pre></div></div>

<p>To be honest: This information doesn't contain any useful hint. If you compare the old signed driver with the new signed driver you will not see any difference. Both could be successful verified as shown next:</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/certs_both.png"><img class="aligncenter size-medium wp-image-2142" title="Old and new driver verification." src="/wordpress/wp-content/uploads/certs_both-300x172.png" alt="" width="300" height="172" /></a></p>
<p>Even if you compare all the sub-dialogs side by side you will not find any difference, beside the different root CA of course. So whats the difference? Well, you can't rely on the graphical representation of this trust of chain. When you invoke the <code>signtool</code> of the <a href="http://en.wikipedia.org/wiki/Windows_Driver_Kit" target="_blank"><em>Windows Driver Kit</em></a> (WDK) with the <code>verify</code> option, you will see the difference:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\Program Files\Oracle\VirtualBox_Sun&gt;signtool.exe verify /v /kp drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Verifying: drivers/vboxdrv/VBoxDrv.sys
SHA1 hash of file: 9E58611C764D5AE04140E4CC7782B3229D1BCB8A
Signing Certificate Chain:
    Issued to: Microsoft Code Verification Root
    Issued by: Microsoft Code Verification Root
    Expires:   01.11.2025 14:54:03
    SHA1 hash: 8FBE4D070EF8AB1BCCAF2A9D5CCAE7282A2C66B3
&nbsp;
        Issued to: Class 3 Public Primary Certification Authority
        Issued by: Microsoft Code Verification Root
        Expires:   23.05.2016 18:11:29
        SHA1 hash: 58455389CF1D0CD6A08E3CE216F65ADFF7A86408
&nbsp;
            Issued to: VeriSign Class 3 Code Signing 2004 CA
            Issued by: Class 3 Public Primary Certification Authority
            Expires:   16.07.2014 00:59:59
            SHA1 hash: 197A4AEBDB25F0170079BB8C73CB2D655E0018A4
&nbsp;
                Issued to: Sun Microsystems, Inc.
                Issued by: VeriSign Class 3 Code Signing 2004 CA
                Expires:   12.06.2011 00:59:59
                SHA1 hash: 1D4458051589B47A06260125F6EC6BBB6C24472E
&nbsp;
The signature is timestamped: 08.02.2011 00:23:54
Timestamp Verified by:
    Issued to: Thawte Timestamping CA
    Issued by: Thawte Timestamping CA
    Expires:   01.01.2021 00:59:59
    SHA1 hash: BE36A4562FB2EE05DBB3D32323ADF445084ED656
&nbsp;
        Issued to: VeriSign Time Stamping Services CA
        Issued by: Thawte Timestamping CA
        Expires:   04.12.2013 00:59:59
        SHA1 hash: F46AC0C6EFBB8C6A14F55F09E2D37DF4C0DE012D
&nbsp;
            Issued to: VeriSign Time Stamping Services Signer - G2
            Issued by: VeriSign Time Stamping Services CA
            Expires:   15.06.2012 00:59:59
            SHA1 hash: ADA8AAA643FF7DC38DD40FA4C97AD559FF4846DE
&nbsp;
Successfully verified: drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Number of files successfully Verified: 1
Number of warnings: 0
Number of errors: 0
&nbsp;
C:\Program Files\Oracle\VirtualBox_Sun&gt;</pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\Program Files\Oracle\VirtualBox_Oracle_Wrong\&gt;signtool.exe verify /v /kp drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Verifying: drivers/vboxdrv/VBoxDrv.sys
SHA1 hash of file: F398B7124B0A8C32DBFB262343AC1180807505D0
Signing Certificate Chain:
    Issued to: VeriSign Class 3 Public Primary Certification Authority - G5
    Issued by: VeriSign Class 3 Public Primary Certification Authority - G5
    Expires:   17.07.2036 00:59:59
    SHA1 hash: 4EB6D578499B1CCF5F581EAD56BE3D9B6744A5E5
&nbsp;
        Issued to: VeriSign Class 3 Code Signing 2010 CA
        Issued by: VeriSign Class 3 Public Primary Certification Authority - G5
        Expires:   08.02.2020 00:59:59
        SHA1 hash: 495847A93187CFB8C71F840CB7B41497AD95C64F
&nbsp;
            Issued to: Oracle Corporation
            Issued by: VeriSign Class 3 Code Signing 2010 CA
            Expires:   08.02.2014 00:59:59
            SHA1 hash: A88FD9BDAA06BC0F3C491BA51E231BE35F8D1AD5
&nbsp;
The signature is timestamped: 10.02.2011 09:30:08
Timestamp Verified by:
    Issued to: Thawte Timestamping CA
    Issued by: Thawte Timestamping CA
    Expires:   01.01.2021 00:59:59
    SHA1 hash: BE36A4562FB2EE05DBB3D32323ADF445084ED656
&nbsp;
        Issued to: VeriSign Time Stamping Services CA
        Issued by: Thawte Timestamping CA
        Expires:   04.12.2013 00:59:59
        SHA1 hash: F46AC0C6EFBB8C6A14F55F09E2D37DF4C0DE012D
&nbsp;
            Issued to: VeriSign Time Stamping Services Signer - G2
            Issued by: VeriSign Time Stamping Services CA
            Expires:   15.06.2012 00:59:59
            SHA1 hash: ADA8AAA643FF7DC38DD40FA4C97AD559FF4846DE
&nbsp;
Successfully verified: drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Number of files successfully Verified: 1
Number of warnings: 0
Number of errors: 0
&nbsp;
C:\Program Files\Oracle\VirtualBox_Oracle_Wrong&gt;</pre></div></div>

<p>As you can see, the chain of trust of the old certificate contains the Microsoft Code Verification Root, the new signed driver not. As Microsoft released the cross certificate somewhere in 2006, it makes sense that the new VeriSign certificate isn't signed by them. So first of all, we have to blame the <code>signtool</code> for <strong>silently</strong> ignoring a cross certificate which, obviously, doesn't trust the root certificate. When you have this information you can search for additional information and probably find an <a href="https://knowledge.verisign.com/support/code-signing-support/index?page=content&amp;actp=CROSSLINK&amp;id=AD220" target="_blank">advisory of VeriSign</a>. There you learn you need <a href="http://www.verisign.com/support/verisign-intermediate-ca/code-signing-intermediate/index.html" target="_blank">intermediate certificates</a> for the new root CA.</p>
<h2>Installing the right certificates on the build machine</h2>
<p>The rest is easy. The certificate store of any Windows installation contains the new VeriSign Root CA as shown here:</p>
<p style="text-align: left;"><a href="/wordpress/wp-content/uploads/g5_root_ca.png"><img class="aligncenter size-medium wp-image-2168" title="VeriSign Class 3 Primary CA - G5 in the Windows certificate store." src="/wordpress/wp-content/uploads/g5_root_ca-300x164.png" alt="" width="300" height="164" /></a>Delete this root CA and replace it by the intermediate certificates you fetched from the website shown above. Just place the certificate in a text file, add the extension <code>.der</code> and double-click to install it. Make sure to replace really all versions of this certificate, even the one in the global store. When you now sign your driver with your new certificate the Microsoft Code Verification Root is in the trust of chain, as shown in the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\Program Files\Oracle\VirtualBox_Oracle_Correct&gt;signtool.exe verify /v /kp drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Verifying: drivers/vboxdrv/VBoxDrv.sys
SHA1 hash of file: 201B7F97473D7F015A104D7841371C5AE4F22FF2
Signing Certificate Chain:
    Issued to: Microsoft Code Verification Root
    Issued by: Microsoft Code Verification Root
    Expires:   01.11.2025 14:54:03
    SHA1 hash: 8FBE4D070EF8AB1BCCAF2A9D5CCAE7282A2C66B3
&nbsp;
        Issued to: Class 3 Public Primary Certification Authority
        Issued by: Microsoft Code Verification Root
        Expires:   23.05.2016 18:11:29
        SHA1 hash: 58455389CF1D0CD6A08E3CE216F65ADFF7A86408
&nbsp;
            Issued to: VeriSign Class 3 Public Primary Certification Authority - G5
            Issued by: Class 3 Public Primary Certification Authority
            Expires:   08.11.2021 00:59:59
            SHA1 hash: 32F30882622B87CF8856C63DB873DF0853B4DD27
&nbsp;
                Issued to: VeriSign Class 3 Code Signing 2010 CA
                Issued by: VeriSign Class 3 Public Primary Certification Authority - G5
                Expires:   08.02.2020 00:59:59
                SHA1 hash: 495847A93187CFB8C71F840CB7B41497AD95C64F
&nbsp;
                    Issued to: Oracle Corporation
                    Issued by: VeriSign Class 3 Code Signing 2010 CA
                    Expires:   08.02.2014 00:59:59
                    SHA1 hash: A88FD9BDAA06BC0F3C491BA51E231BE35F8D1AD5
&nbsp;
The signature is timestamped: 10.02.2011 15:03:30
Timestamp Verified by:
    Issued to: Thawte Timestamping CA
    Issued by: Thawte Timestamping CA
    Expires:   01.01.2021 00:59:59
    SHA1 hash: BE36A4562FB2EE05DBB3D32323ADF445084ED656
&nbsp;
        Issued to: VeriSign Time Stamping Services CA
        Issued by: Thawte Timestamping CA
        Expires:   04.12.2013 00:59:59
        SHA1 hash: F46AC0C6EFBB8C6A14F55F09E2D37DF4C0DE012D
&nbsp;
            Issued to: VeriSign Time Stamping Services Signer - G2
            Issued by: VeriSign Time Stamping Services CA
            Expires:   15.06.2012 00:59:59
            SHA1 hash: ADA8AAA643FF7DC38DD40FA4C97AD559FF4846DE
&nbsp;
Successfully verified: drivers/vboxdrv/VBoxDrv.sys
&nbsp;
Number of files successfully Verified: 1
Number of warnings: 0
Number of errors: 0
&nbsp;
C:\Program Files\Oracle\VirtualBox_Oracle_Correct&gt;</pre></div></div>

<p>You see the old certificate (which is trusted by Microsoft) is the parent of the new certificate, which completes the trust of chain again. I guess this is only some temporary solution as long as Microsoft doesn't release a new cross certificate (that's why it is called intermediate). Luckily, you only need the intermediate certificates on the build machine. For your end users nothing has to be changed.</p>
<h2>Conclusion</h2>
<p>This article shows how a service provider make an easy task hard to do. Signing a kernel mode driver with the new certificate isn't hard, but finding the right information is. Although there is an advisory from VeriSign, it doesn't really explain what to do. As I believe in the future many other people will be in the same situation, I hope this article will save them from some sleepless nights.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/02/kernel-driver-code-signing-with-the-verisign-class-3-primary-ca-g5-certificate/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Adding a badge to the unified toolbar on Mac OS X</title>
		<link>http://www.64k-tec.de/2011/01/adding-a-badge-to-the-unified-toolbar-on-mac-os-x/</link>
		<comments>http://www.64k-tec.de/2011/01/adding-a-badge-to-the-unified-toolbar-on-mac-os-x/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 21:41:56 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[bagde]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[NSResponder]]></category>
		<category><![CDATA[NSView]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[toolbar]]></category>
		<category><![CDATA[unified titlebar]]></category>
		<category><![CDATA[VirtualBox]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2013</guid>
		<description><![CDATA[Mac OS X and the applications running on it are known for being sometimes unusual in the look and feel. For some reason developer on Mac OS X seems to be more creative than on other platforms. For an application developer which deals with user input it is important to make any user interaction as [...]]]></description>
			<content:encoded><![CDATA[<p>Mac OS X and the applications running on it are known for being sometimes unusual in the look and feel. For some reason developer on Mac OS X seems to be more creative than on other platforms. For an application developer which deals with user input it is important to make any user interaction as useful as possible and present this information in a way which is on the one side as much less annoying as possible but also attractive and informative on the other side. Lastly I was thinking about how to show the users they are using a beta version, which should remind them that this is not production ready software, but on the same time make it easy to respond to bugs they find in this pre-release. The solution, I came up with, was a badge in the toolbar which shows this is a release which is definitely in a testing phase, but also allows the user to double-click to give a respond to this particular version. In the following post I will show how to do this.</p>
<h2>Getting the superview</h2>
<p>In Cocoa all user displayed content is a <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" target="_blank">NSView</a> (most of the time; forget about the Dock). This is really nice, cause you could manipulate them. Although there are no public methods for getting the NSView of the toolbar or even the titlebar, they exist as an NSView. The NSView of the toolbar could be accessed by a private method. As always in Objective C you could ask for a particular method by using the <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/respondsToSelector:" target="_blank"><code>respondsToSelector</code></a> statement like in the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSToolbar</span> <span style="color: #002200;">*</span>tb <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pWindow toolbar<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>tb respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>_toolbarView<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
 <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>tbv <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tb performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>_toolbarView<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>tbv<span style="color: #002200;">&#41;</span>
 <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">/* do something with tbv */</span>
 <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This return the NSView of the toolbar at least until 10.6. But be warned this is a private method and of course this could be changed in a future version of Mac OS X. At least the shown method will correctly fail in the case Apple change his mind which will result in doing nothing. Anyway this will not include the area of the titlebar. To get the NSView which covers both the title bar and the toolbar you need another trick. The titlebar usual has a close, minimize or maximize button. These buttons are NSButton's and added by the system depending of the window type. As a NSButton is also a NSView it also has a parent. This dependency allows us to access the view which is responsible for displaying the unified titlebar and the toolbar. The following code shows how to get the responsible view and how to add an additional <a href="http://developer.apple.com/library/mac/#documentation/cocoa/reference/ApplicationKit/Classes/NSImageView_Class/Reference/Reference.html" target="_blank">NSImageView</a> to it:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>wv <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>pWindow standardWindowButton<span style="color: #002200;">:</span>NSWindowCloseButton<span style="color: #002200;">&#93;</span> superview<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>wv<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
 <span style="color: #11740a; font-style: italic;">/* We have to calculate the size of the title bar for the center case. */</span>
 NSSize s <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pImage size<span style="color: #002200;">&#93;</span>;
 NSSize s1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>wv frame<span style="color: #002200;">&#93;</span>.size;
 NSSize s2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>pWindow contentView<span style="color: #002200;">&#93;</span> frame<span style="color: #002200;">&#93;</span>.size;
 <span style="color: #11740a; font-style: italic;">/* Correctly position the label. */</span>
 <span style="color: #400080;">NSImageView</span> <span style="color: #002200;">*</span>iv <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSImageView</span> alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>
        NSMakeRect<span style="color: #002200;">&#40;</span>s1.width <span style="color: #002200;">-</span> s.width <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>fCenter ? <span style="color: #2400d9;">10</span> <span style="color: #002200;">:</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>,
                   fCenter ? s2.height <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>s1.height <span style="color: #002200;">-</span> s2.height <span style="color: #002200;">-</span> s.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span> <span style="color: #002200;">:</span> s1.height <span style="color: #002200;">-</span> s.height <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span>,
  	           s.width, s.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #11740a; font-style: italic;">/* Configure the NSImageView for auto moving. */</span>
 <span style="color: #002200;">&#91;</span>iv setImage<span style="color: #002200;">:</span>pImage<span style="color: #002200;">&#93;</span>;
 <span style="color: #002200;">&#91;</span>iv setAutoresizesSubviews<span style="color: #002200;">:</span><span style="color: #a61390;">true</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #002200;">&#91;</span>iv setAutoresizingMask<span style="color: #002200;">:</span>NSViewMinXMargin | NSViewMinYMargin<span style="color: #002200;">&#93;</span>;
 <span style="color: #11740a; font-style: italic;">/* Add it to the parent of the close button. */</span>
 <span style="color: #002200;">&#91;</span>wv addSubview<span style="color: #002200;">:</span>iv positioned<span style="color: #002200;">:</span>NSWindowBelow relativeTo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This code needs a NSWindow, a NSImage as label and the flag fCenter for deciding if the image is vertically centered within the titlebar. Also the badge is pinned on the right side by setting the <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/setAutoresizingMask:" target="_blank"><code>AutoresizingMask</code></a>. The following shows how this could be look like:</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/badge.png"><img class="size-medium wp-image-2056 aligncenter" style="vertical-align: middle;" title="badge" src="/wordpress/wp-content/uploads/badge-300x281.png" alt="" width="300" height="281" /></a></p>
<h2>Adding functionality to the badge</h2>
<p>First we should add some information about this release by using the <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/occ/instm/NSView/setToolTip:" target="_blank"><code>setToolTip</code></a> method of an NSView. Simply add this call to the code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #002200;">&#91;</span>iv setToolTip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Some info about the beta version.&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>To make this beta hint more useful, we should make it clickable. Getting mouse down events in Cocoa is only possible by handling the <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/mouseDown:" target="_blank"><code>mouseDown</code></a> event of the <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html" target="_blank">NSResponder</a> sub-class. Also the view must accept first responder events. How to do this is shown in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> BetaImageView<span style="color: #002200;">:</span> <span style="color: #400080;">NSImageView</span>
<span style="color: #002200;">&#123;</span><span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>acceptsFirstResponder;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mouseDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEvent</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>pEvent;
<span style="color: #a61390;">@end</span>
<span style="color: #a61390;">@implementation</span> BetaImageView
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>acceptsFirstResponder
<span style="color: #002200;">&#123;</span>
 <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mouseDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEvent</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>pEvent
<span style="color: #002200;">&#123;</span>
 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>pEvent clickCount<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSWorkspace</span> sharedWorkspace<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span>
        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.virtualbox.org/&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
 <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#91;</span>super mouseDown<span style="color: #002200;">:</span>pEvent<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Replace NSImageView with BetaViewImage in the allocation call in listing 2 and you are done. Change the URL to one where the user can respond to the beta questions, like a bug tracker or forum.</p>
<h2>Conclusion</h2>
<p>This post showed how to add an nice looking badge to the unified toolbar on Mac OS X. The user is reminded he is using some pre-release software all the time, but at the same time has an easy way to report problems with this release. Of course could such a badge used for anything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/01/adding-a-badge-to-the-unified-toolbar-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating file shortcuts on three different operation systems</title>
		<link>http://www.64k-tec.de/2010/11/creating-file-shortcuts-on-three-different-operation-systems/</link>
		<comments>http://www.64k-tec.de/2010/11/creating-file-shortcuts-on-three-different-operation-systems/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 13:44:50 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[AESend]]></category>
		<category><![CDATA[Alias]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[desktop]]></category>
		<category><![CDATA[Desktop Entry Specification]]></category>
		<category><![CDATA[file shortcuts]]></category>
		<category><![CDATA[freeedesktop.org]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[hard link]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[IPersistFile]]></category>
		<category><![CDATA[IShellLink]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[platform]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[VirtualBox]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[X11]]></category>
		<category><![CDATA[Xorg]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=1838</guid>
		<description><![CDATA[As you may know, developing for multiple platforms is one of my strengths. Strictly speaking, it's a basic requirement if you are involved in such a product like VirtualBox, which runs on every major (and several minor) platform available today. Beside the GUI, which uses Qt and therewith is portable without any additional cost (which [...]]]></description>
			<content:encoded><![CDATA[<p>As you may know, developing for multiple platforms is one of my strengths. Strictly speaking, it's a basic requirement if you are involved in such a product like VirtualBox, which runs on every major (and several minor) platform available today. Beside the GUI, which uses <a href="http://qt.nokia.com/" target="_blank">Qt</a> and therewith is portable without any additional cost (which isn't fully true if you want <a href="http://www.64k-tec.de/2009/12/integrating-native-cocoa-controls-into-qt/" target="_blank">real native look and feel on every platform, especially on Mac OS X</a>), all the rest of VirtualBox is written in a portable way. This is done by using only C/C++ and Assembler when necessary. Everything which needs a different approach, because of the design of the OS (and the API's which are available there), is implemented in a platform dependent way. In the history of VirtualBox, several <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Runtime" target="_blank">modules</a> are created and grown by the time, which makes it really easy to deal with this differences. For stuff like file handling, paths, strings, semaphores or any other basic functionality, you can just use the modules which are available. On the other side it might be necessary, for a new feature we implement, to write it from the ground. In the following post I will show how to create a <a href="http://en.wikipedia.org/wiki/File_shortcut" target="_blank">file shortcut</a> for the three major operation systems available today.</p>
<h2>Why do you want to use file shortcuts</h2>
<p>On the classical UNIX systems you have <a href="http://en.wikipedia.org/wiki/Hard_link" target="_blank">hard</a> and <a href="http://en.wikipedia.org/wiki/Soft_link" target="_blank">soft</a> links. These are implemented by the filesystem and make it possible to link to another file or folder without any trouble. Most of the time soft links are used, but it really depends on the use case. Unfortunately these kind of links are not available on Windows (yes, I know there are also hard links and junctions on NTFS, but they are not common and difficult to handle), these links doesn't allow any additional attributes. For example one like to add a different icon to the link or provide more information through a comment field. Beside on Mac OS X, shortcuts can also be work as an application launcher, where the link contain the information what application should be started and how. In contrast to filesystem links which are handled by the operation system, these shortcuts are handled by the window system (or shell) running on the host (which doesn't mean there is no filesystem support for it). On Windows this is the Explorer, on Mac OS X the Finder and on Linux a <a href="http://www.freedesktop.org/" target="_blank">freedesktop.org</a> conforming file manager.</p>
<h2>Creating a Desktop file on Linux</h2>
<p>Desktop files on Linux (or any other UNIX system which conforms to freedesktop.org) is easy. It's a simple text file which implement the <a href="http://standards.freedesktop.org/desktop-entry-spec/latest/" target="_blank">Desktop Entry Specification</a>. In version 1.0 there are 18 possible entries, where not all of them are mandatory. In the following example I use Qt to write these files, but it should be no problem to use any other toolkit or plain C.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #0057AE;">bool</span> createShortcut<span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span> <span style="color: #006E28;">&amp;</span>strSrcFile<span style="color: #006E28;">,</span>
                    <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span> <span style="color: #006E28;">&amp;</span>strDstPath<span style="color: #006E28;">,</span>
                    <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span> <span style="color: #006E28;">&amp;</span>strName<span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>
 <span style="color: #22aadd;">QFile</span> link<span style="color: #006E28;">&#40;</span>strDstPath <span style="color: #006E28;">+</span> <span style="color: #22aadd;">QDir</span><span style="color: #006E28;">::</span><span style="color: #2B74C7;">separator</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">+</span> strName <span style="color: #006E28;">+</span> <span style="color: #BF0303;">&quot;.desktop&quot;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
 <span style="color: #000000; font-weight:bold;">if</span> <span style="color: #006E28;">&#40;</span>link.<span style="color: #2B74C7;">open</span><span style="color: #006E28;">&#40;</span><span style="color: #22aadd;">QFile</span><span style="color: #006E28;">::</span><span style="color: #2B74C7;">WriteOnly</span> <span style="color: #006E28;">|</span> <span style="color: #22aadd;">QFile</span><span style="color: #006E28;">::</span><span style="color: #2B74C7;">Truncate</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span>
 <span style="color: #006E28;">&#123;</span>
  <span style="color: #22aadd;">QTextStream</span> out<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&amp;</span>link<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  out.<span style="color: #2B74C7;">setCodec</span><span style="color: #006E28;">&#40;</span><span style="color: #BF0303;">&quot;UTF-8&quot;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
  out <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;[Desktop Entry]&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;Encoding=UTF-8&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;Version=1.0&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;Type=Link&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;Name=&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> strName <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;URL=&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> strSrcFile <span style="color: #006E28;">&lt;&lt;</span> endl
      <span style="color: #006E28;">&lt;&lt;</span> <span style="color: #BF0303;">&quot;Icon=icon-name&quot;</span> <span style="color: #006E28;">&lt;&lt;</span> endl<span style="color: #006E28;">;</span>
  <span style="color: #000000; font-weight:bold;">return</span> true<span style="color: #006E28;">;</span>
 <span style="color: #006E28;">&#125;</span>
 <span style="color: #000000; font-weight:bold;">return</span> false<span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span></pre></div></div>

<p>Replace <code>icon-name</code> by a registered icon on the system and you are done.</p>
<h2>Creating a Shell link on Windows</h2>
<p>Windows provides an interface for <a href="http://msdn.microsoft.com/en-us/library/bb774950%28VS.85%29.aspx" target="_blank"><code>IShellLink</code></a> since Windows XP. The following example shows how to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> createShortcut<span style="color: #008000;">&#40;</span>LPCSTR lpszSrcFile,
                    LPCSTR lpszDstPath,
                    LPCSTR lpszName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
 IShellLink <span style="color: #000040;">*</span>pShl <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
 IPersistFile <span style="color: #000040;">*</span>pPPF <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
 HRESULT rc <span style="color: #000080;">=</span> CoCreateInstance<span style="color: #008000;">&#40;</span>CLSID_ShellLink,
                               <span style="color: #0000ff;">NULL</span>,
                               CLSCTX_INPROC_SERVER,
                               IID_IShellLink,
                               <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>pShl<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>FAILED<span style="color: #008000;">&#40;</span>rc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
 <span style="color: #0000ff;">do</span>
 <span style="color: #008000;">&#123;</span>
  rc <span style="color: #000080;">=</span> pShl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetPath<span style="color: #008000;">&#40;</span>lpszSrcFile<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>FAILED<span style="color: #008000;">&#40;</span>rc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
  rc <span style="color: #000080;">=</span> pShl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>QueryInterface<span style="color: #008000;">&#40;</span>IID_IPersistFile, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>pPPF<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>FAILED<span style="color: #008000;">&#40;</span>rc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
  WORD wsz<span style="color: #008000;">&#91;</span>MAX_PATH<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  TCHAR path<span style="color: #008000;">&#91;</span>MAX_PATH<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  lstrcat<span style="color: #008000;">&#40;</span>path, lpszDstPath<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  lstrcat<span style="color: #008000;">&#40;</span>path, <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  lstrcat<span style="color: #008000;">&#40;</span>path, lpszName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  lstrcat<span style="color: #008000;">&#40;</span>path, <span style="color: #FF0000;">&quot;.lnk&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  MultiByteToWideChar<span style="color: #008000;">&#40;</span>CP_ACP, <span style="color: #0000dd;">0</span>, buf, <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span>, wsz, MAX_PATH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  rc <span style="color: #000080;">=</span> pPPF<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Save<span style="color: #008000;">&#40;</span>wsz, TRUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pPPF<span style="color: #008000;">&#41;</span>
  pPPF<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pShl<span style="color: #008000;">&#41;</span>
  pShl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #0000ff;">return</span> SUCCEEDED<span style="color: #008000;">&#40;</span>rc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you may noticed this uses <a href="http://en.wikipedia.org/wiki/Component_Object_Model" target="_blank">COM</a>. Many API's on Windows using the COM interface to communicate between processes. If you don't use COM in your application you have to initialize it first. This is achieved by adding the following call to the front of the function:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>FAILED<span style="color: #008000;">&#40;</span>CoInitialize<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span></pre></div></div>

<p>Depending on your application it might be worth to unitialize COM after usage by appending the following to the function:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> CoUninitialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The function itself isn't any magic. It gets a COM interface to the <code>IShellLink</code> interface and then work with it, by setting the source path and adding a target path by using the <a href="http://msdn.microsoft.com/en-us/library/ms687223%28VS.85%29.aspx" target="_blank"><code>IPersistFile</code></a> interface. As I wrote before you could do much more. Providing a path to a specific application or adding your own parameters is no problem. Have a look at the documentation.</p>
<h2>Creating an Alias file on Mac OS X</h2>
<p>Shortcut files on Mac OS X are a little bit different. At first, they aren't one. There are the classical filesystem links and <a href="http://en.wikipedia.org/wiki/Alias_%28Mac_OS%29" target="_blank">Alias</a> files. Alias files are links which targeting a specific file, but they haven't all the possibilities of shortcuts like on Windows or Linux. As the name suggest they are really only an alias for another file or directory. So specifying an application to start or things like that aren't possible. Anyway they allow changing the icon and they are more persistent than on Window or Linux cause they are working with several attributes of the target file. Even if you rename or move the target, an Alias file will resolve the target correctly (if it is possible). On the other side, being such special means also being hard to create. In principle there are two possibilities. The first one is, creating a file which is no file at all, but has several <a href="http://en.wikipedia.org/wiki/Resource_fork" target="_blank">resources forks</a> attached. Therefor you need to know exactly how Alias files are built of and make sure with every release of Mac OS X you are following the development. There is a free project which does exactly that: <a href="https://github.com/nathanday/ndalias" target="_blank">NDAlias</a>. If you are like me and a little bit more lazy, you ask someone who should know how to create Alias files. This is Finder. Although writing the files itself isn't easy, asking the Finder to do the job is not really easier, cause the information about doing exactly that are really rare. The following code shows how to achieve it:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">bool createShortcut<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>pstrSrcFile,
                    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>pstrDstPath,
                    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>pstrName<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
 <span style="color: #11740a; font-style: italic;">/* First of all we need to figure out which process Id the Finder
  * currently has. */</span>
 <span style="color: #400080;">NSWorkspace</span> <span style="color: #002200;">*</span>pWS <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSWorkspace</span> sharedWorkspace<span style="color: #002200;">&#93;</span>;
 <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pApps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pWS launchedApplications<span style="color: #002200;">&#93;</span>;
 bool fFFound <span style="color: #002200;">=</span> <span style="color: #a61390;">false</span>;
 ProcessSerialNumber psn;
 <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>pDict <span style="color: #a61390;">in</span> pApps<span style="color: #002200;">&#41;</span>
 <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>pDict valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSApplicationBundleIdentifier&quot;</span><span style="color: #002200;">&#93;</span>
         isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;com.apple.finder&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
   psn.highLongOfPSN <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>pDict
                          valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSApplicationProcessSerialNumberHigh&quot;</span><span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span>;
   psn.lowLongOfPSN  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>pDict
                          valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSApplicationProcessSerialNumberLow&quot;</span><span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span>;
   fFFound <span style="color: #002200;">=</span> <span style="color: #a61390;">true</span>;
   <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#125;</span>
 <span style="color: #002200;">&#125;</span>
 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>fFFound<span style="color: #002200;">&#41;</span>
  <span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
 <span style="color: #11740a; font-style: italic;">/* Now the event fun begins. */</span>
 OSErr err <span style="color: #002200;">=</span> noErr;
 AliasHandle hSrcAlias <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
 AliasHandle hDstAlias <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
 <span style="color: #a61390;">do</span>
 <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">/* Create a descriptor which contains the target psn. */</span>
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>finderPSNDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span>
                                            descriptorWithDescriptorType<span style="color: #002200;">:</span>typeProcessSerialNumber
                                            bytes<span style="color: #002200;">:&amp;</span>psn
                                            length<span style="color: #002200;">:</span><span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>psn<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>finderPSNDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #11740a; font-style: italic;">/* Create the Apple event descriptor which points to the Finder
   * target already. */</span>
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>finderEventDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span>
                                              appleEventWithEventClass<span style="color: #002200;">:</span>kAECoreSuite
                                              eventID<span style="color: #002200;">:</span>kAECreateElement
                                              argetDescriptor<span style="color: #002200;">:</span>finderPSNDesc
                                              returnID<span style="color: #002200;">:</span>kAutoGenerateReturnID
                                              transactionID<span style="color: #002200;">:</span>kAnyTransactionID<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>finderEventDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #11740a; font-style: italic;">/* Create and add an event type descriptor: Alias */</span>
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>osTypeDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span> descriptorWithTypeCode<span style="color: #002200;">:</span>typeAlias<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>osTypeDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#91;</span>finderEventDesc setParamDescriptor<span style="color: #002200;">:</span>osTypeDesc forKeyword<span style="color: #002200;">:</span>keyAEObjectClass<span style="color: #002200;">&#93;</span>;
  <span style="color: #11740a; font-style: italic;">/* Now create the source Alias, which will be attached to the event. */</span>
  err <span style="color: #002200;">=</span> FSNewAliasFromPath<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span>, <span style="color: #002200;">&#91;</span>pstrSrcFile fileSystemRepresentation<span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&amp;</span>hSrcAlias, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>err <span style="color: #002200;">!=</span> noErr<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #a61390;">char</span> handleState;
  handleState <span style="color: #002200;">=</span> HGetState<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hSrcAlias<span style="color: #002200;">&#41;</span>;
  HLock<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hSrcAlias<span style="color: #002200;">&#41;</span>;
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>srcAliasDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span>
                                           descriptorWithDescriptorType<span style="color: #002200;">:</span>typeAlias
                                           bytes<span style="color: #002200;">:*</span>hSrcAlias
                                           length<span style="color: #002200;">:</span>GetAliasSize<span style="color: #002200;">&#40;</span>hSrcAlias<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>srcAliasDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#91;</span>finderEventDesc setParamDescriptor<span style="color: #002200;">:</span>srcAliasDesc
    forKeyword<span style="color: #002200;">:</span>keyASPrepositionTo<span style="color: #002200;">&#93;</span>;
  HSetState<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hSrcAlias, handleState<span style="color: #002200;">&#41;</span>;
  <span style="color: #11740a; font-style: italic;">/* Next create the target Alias and attach it to the event. */</span>
  err <span style="color: #002200;">=</span> FSNewAliasFromPath<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span>, <span style="color: #002200;">&#91;</span>pstrDstPath fileSystemRepresentation<span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&amp;</span>hDstAlias, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>err <span style="color: #002200;">!=</span> noErr<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  handleState <span style="color: #002200;">=</span> HGetState<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hDstAlias<span style="color: #002200;">&#41;</span>;
  HLock<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hDstAlias<span style="color: #002200;">&#41;</span>;
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>dstAliasDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span>
                                           descriptorWithDescriptorType<span style="color: #002200;">:</span>t ypeAlias
                                           bytes<span style="color: #002200;">:*</span>hDstAlias
                                           length<span style="color: #002200;">:</span>GetAliasSize<span style="color: #002200;">&#40;</span>hDstAlias<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>dstAliasDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#91;</span>finderEventDesc setParamDescriptor<span style="color: #002200;">:</span>dstAliasDesc
    forKeyword<span style="color: #002200;">:</span>keyAEInsertHere<span style="color: #002200;">&#93;</span>;
  HSetState<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hDstAlias, handleState<span style="color: #002200;">&#41;</span>;
  <span style="color: #11740a; font-style: italic;">/* Finally a property descriptor containing the target
   * Alias name. */</span>
  <span style="color: #400080;">NSAppleEventDescriptor</span> <span style="color: #002200;">*</span>finderPropDesc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span> recordDescriptor<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>finderPropDesc<span style="color: #002200;">&#41;</span>
   <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#91;</span>finderPropDesc setDescriptor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAppleEventDescriptor</span> descriptorWithString<span style="color: #002200;">:</span>pstrName<span style="color: #002200;">&#93;</span>
    forKeyword<span style="color: #002200;">:</span>keyAEName<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>finderEventDesc setParamDescriptor<span style="color: #002200;">:</span>finderPropDesc forKeyword<span style="color: #002200;">:</span>keyAEPropData<span style="color: #002200;">&#93;</span>;
  <span style="color: #11740a; font-style: italic;">/* Now send the event to the Finder. */</span>
  err <span style="color: #002200;">=</span> AESend<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>finderEventDesc aeDesc<span style="color: #002200;">&#93;</span>,
               <span style="color: #a61390;">NULL</span>,
               kAENoReply,
               kAENormalPriority,
               kNoTimeOut,
               <span style="color: #2400d9;">0</span>,
               <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;
 <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
 <span style="color: #11740a; font-style: italic;">/* Cleanup */</span>
 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>hSrcAlias<span style="color: #002200;">&#41;</span>
  DisposeHandle<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hSrcAlias<span style="color: #002200;">&#41;</span>;
 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>hDstAlias<span style="color: #002200;">&#41;</span>
  DisposeHandle<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>Handle<span style="color: #002200;">&#41;</span>hDstAlias<span style="color: #002200;">&#41;</span>;
 <span style="color: #a61390;">return</span> err <span style="color: #002200;">==</span> noErr ? <span style="color: #a61390;">true</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Although the code above looks a little bit scary, it does not much. It fetch the process serial number of the current Finder process, creates an Application event for creating an Alias file and send this event to the Finder.</p>
<h2>Conclusion</h2>
<p>Beside showing how to create file shortcuts on different platforms, this article also shows which work is necessary to create platform independent code. It's a simple example. But it also makes clear that one simple solution for platform one, not necessarily mean it's such simple on platform two.</p>
<p>Making this easy accessible to any developer is the next step. I will leave this exercise to the reader, but have a look at the <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Frontends/VirtualBox/src/platform" target="_blank">platform code of the VirtualBox GUI</a> and the corresponding <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk" target="_blank">Makefile</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2010/11/creating-file-shortcuts-on-three-different-operation-systems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding some of the mysteries of launchd</title>
		<link>http://www.64k-tec.de/2010/11/understanding-some-of-the-mysteries-of-launchd/</link>
		<comments>http://www.64k-tec.de/2010/11/understanding-some-of-the-mysteries-of-launchd/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 00:11:55 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Applications]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[file types]]></category>
		<category><![CDATA[Info.plist]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[installer]]></category>
		<category><![CDATA[launchd]]></category>
		<category><![CDATA[LaunchServices]]></category>
		<category><![CDATA[lsregister]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[plist]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=1716</guid>
		<description><![CDATA[Have you ever wondered how Mac OS X knows which file type belongs to which application? On Windows there is the registry. An installer writes the necessary info into it. Most applications on Mac OS X doesn't come with an installer, they are just moved from the downloaded DMG file to the /Applications folder. So [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered how Mac OS X knows which file type belongs to which application? On Windows there is the registry. An installer writes the necessary info into <a href="http://msdn.microsoft.com/en-us/library/ee872121%28v=VS.85%29.aspx" target="_blank">it</a>. Most applications on Mac OS X doesn't come with an installer, they are just moved from the downloaded <a href="http://en.wikipedia.org/wiki/Apple_Disk_Image" target="_blank">DMG</a> file to the <code class="path">/Applications</code> folder. So a developer doesn't have the ability to take action when the user "install" the application. Anyway there is no need to provide an installer for just this task, cause Mac OS X register file type associations on the first start of the application. In the following post, I will show how to do this, but furthermore I will show where this information is stored and how it could be reseted.</p>
<h2>Providing the necessary information to Mac OS X</h2>
<p>Applications on Mac OS X need some defined structure. They are so-called <a href="http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html" target="_blank">bundles</a>, which means on the filesystem layer they are directories. You can prove this by checking the Applications directory within the Terminal.app. You could also right-click on an application and select "Show Package Contents". The content of the bundle directory is usually hidden from the user when he works with the Finder or any other high level function of Mac OS X (like the open dialog). Additional to this layout on the filesystem, an application provide information about itself to the system with a plist file. This file has to be named <code class="path">Application.app/Content/Info.plist</code>. The following shows exemplary the content of the <a href="http://code.google.com/p/tunnelblick/" target="_blank">Tunnelblick</a> application:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleDevelopmentRegion<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>English<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleExecutable<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tunnelblick<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIconFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tunnelblick.icns<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIdentifier<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.openvpn.tunnelblick<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleInfoDictionaryVersion<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundlePackageType<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>APPL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleShortVersionString<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.1beta20 (build 2132)<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleSignature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OVPN<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleVersion<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2132<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NSMainNibFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MainMenu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NSPrincipalClass<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NSApplication<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NSUIElement<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SUEnableSystemProfiling<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SUFeedURL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://tunnelblick.net/appcast.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SUPublicDSAKeyFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dsa_pub.pem<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The keys are mostly self explaining, you can find a full list <a href="http://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009247" target="_blank">here</a>.</p>
<p>To register a file type association you have to add an array of the type <code>CFBundleDocumentTypes</code>. Again, here is an extraction of the Tunnelblick application, which shows the registration of the <code class="path">tblk</code> extension:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleDocumentTypes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleTypeExtensions<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tblk<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleTypeIconFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tunnelblick_package.icns<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleTypeName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tunnelblick VPN Configuration<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleTypeRole<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Editor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>LSTypeIsPackage<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NSPersistentStoreTypeKey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Binary<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You have to provide a file extension or mime-type, can add an icon and give a hint what your application can do with this type of file (viewer, editor or nothing).</p>
<p>Beside this passive way of announcing this information, there is also an active way (e.g. for use in an installer). See <a href="http://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/doc/uid/TP30000998-CH1g-LSRegisterFSRef" target="_blank">here</a> for further information.</p>
<h2>Where is this information stored</h2>
<p>Although Mac OS X hasn't a registry like windows, some information are stored in global databases, too. Applications are registered at the <code class="cmd">launchd</code>. The launchd is the central place for starting all kind of programs, from a background service to any common application, like Thunderbird. E.g. background services can register itself and even made their start depending on different events. VirtualBox has an example configuration included, which let launchd start the <a href="http://www.virtualbox.org/browser/trunk/src/VBox/Installer/darwin/VirtualBox/org.virtualbox.vboxwebsrv.plist" target="_blank">vbox webservice</a> on activity on a certain port. Although this isn't used by many users out there (and therefore disabled by default), it shows some generic usage of this functionality.</p>
<p>If you look around in Mac OS X you have the tool <code class="cmd">launchctl</code>, which allows you to start jobs or register background services with launchd. But it seems there is no tool which is able to get some information about the knowledge of launchd. Well, there is one. It's a little bit hidden in <code class="cmd">/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister</code>. To be more comfortable with it, we should include it into the user path by linking it to a known path, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>CoreServices.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span>A<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>LaunchServices.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span>A<span style="color: #000000; font-weight: bold;">/</span>Support<span style="color: #000000; font-weight: bold;">/</span>lsregister <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lsregister</pre></div></div>

<p>With this tiny tool we are able to dump the launchd database. <code class="cmd">lsregister -dump | grep -n10 Tunnelblick</code> displays something like this for Tunnelblick:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">10823</span>---------------------------------------------------------------------------------
<span style="color: #000000;">10824</span>-bundle    <span style="color: #c20cb9; font-weight: bold;">id</span>:            <span style="color: #000000;">6984</span>
<span style="color: #000000;">10825</span>:  path:          <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>Tunnelblick.app
<span style="color: #000000;">10826</span>-  name:
<span style="color: #000000;">10827</span>-  identifier:    com.openvpn.tunnelblick <span style="color: #7a0874; font-weight: bold;">&#40;</span>0x800097b8<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">10828</span>-  version:       <span style="color: #000000;">2132</span>
<span style="color: #000000;">10829</span>-  mod <span style="color: #c20cb9; font-weight: bold;">date</span>:      <span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">31</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2010</span> <span style="color: #000000;">12</span>:<span style="color: #000000;">37</span>:08
<span style="color: #000000;">10830</span>-  reg <span style="color: #c20cb9; font-weight: bold;">date</span>:      <span style="color: #000000;">11</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">19</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2010</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">13</span>:<span style="color: #000000;">39</span>
<span style="color: #000000;">10831</span>-  <span style="color: #7a0874; font-weight: bold;">type</span> code:     <span style="color: #ff0000;">'APPL'</span>
<span style="color: #000000;">10832</span>-  creator code:  <span style="color: #ff0000;">'OVPN'</span>
<span style="color: #000000;">10833</span>-  sys version:   <span style="color: #000000;">0</span>
<span style="color: #000000;">10834</span>-  flags:         relative-icon-path  ui-element
<span style="color: #000000;">10835</span>-  item flags:    container  package  application  extension-hidden  native-app  ppc  i386
<span style="color: #000000;">10836</span>-  icon:          Contents<span style="color: #000000; font-weight: bold;">/</span>Resources<span style="color: #000000; font-weight: bold;">/</span>tunnelblick.icns
<span style="color: #000000;">10837</span>:  executable:    Contents<span style="color: #000000; font-weight: bold;">/</span>MacOS<span style="color: #000000; font-weight: bold;">/</span>Tunnelblick
<span style="color: #000000;">10838</span>-  inode:         <span style="color: #000000;">3800129</span>
<span style="color: #000000;">10839</span>-  <span style="color: #7a0874; font-weight: bold;">exec</span> inode:    <span style="color: #000000;">3800213</span>
<span style="color: #000000;">10840</span>-  container <span style="color: #c20cb9; font-weight: bold;">id</span>:  <span style="color: #000000;">32</span>
<span style="color: #000000;">10841</span>-  library:
<span style="color: #000000;">10842</span>-  library items:
<span style="color: #000000;">10843</span>-  <span style="color: #660033;">--------------------------------------------------------</span>
<span style="color: #000000;">10844</span>-  claim   <span style="color: #c20cb9; font-weight: bold;">id</span>:            <span style="color: #000000;">17864</span>
<span style="color: #000000;">10845</span>:          name:          Tunnelblick VPN Configuration
<span style="color: #000000;">10846</span>-          rank:          Default
<span style="color: #000000;">10847</span>-          roles:         Editor
<span style="color: #000000;">10848</span>-          flags:         relative-icon-path  package
<span style="color: #000000;">10849</span>-          icon:          Contents<span style="color: #000000; font-weight: bold;">/</span>Resources<span style="color: #000000; font-weight: bold;">/</span>tunnelblick_package.icns
<span style="color: #000000;">10850</span>-          bindings:      .tblk
<span style="color: #000000;">10851</span>---------------------------------------------------------------------------------</pre></div></div>

<p>As you see, all the information provided by the plist file is registered. Beside other information, it also has the registration and modification times stored. To let launchd reread the plist file, usual its only necessary to change the modification time of the application. This could be done by simply executing <code class="cmd">touch /Applications/Application.app</code>. Anyway, it's sometimes necessary to reset the content of this database for the own application. This could be done by executing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">lsregister <span style="color: #660033;">-u</span> <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>Application.app</pre></div></div>

<p>To register the application again, just remove the <code>-u</code> parameter. You should also check if your application isn't registered more than once, with different paths, by using the <code>-dump</code> parameter. To fully reset the launchd database, you could use <code>-kill</code>. This will remove any file type association and registered application. You have been <span style="color: #ff0000;">warned</span>.</p>
<h2>Conclusion</h2>
<p>This article shows how Mac OS X handle informations about installed applications. With this knowledge a developer is able to register the own application into the launchd ecosystem and see how launchd interpret this information. Furthermore, the usage of lsregister allows a developer to analyze the content of the launchd database and make changes to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2010/11/understanding-some-of-the-mysteries-of-launchd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the backtrace from a kernel panic</title>
		<link>http://www.64k-tec.de/2010/10/getting-the-backtrace-from-a-kernel-panic/</link>
		<comments>http://www.64k-tec.de/2010/10/getting-the-backtrace-from-a-kernel-panic/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 17:56:31 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[backtrace]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[graphical modes]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[kernel mode settings]]></category>
		<category><![CDATA[KMS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[netconsole]]></category>
		<category><![CDATA[panic]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[remote access]]></category>
		<category><![CDATA[X11]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=1423</guid>
		<description><![CDATA[You may know the following situation. You arrive in the morning in the office, do what you always do and check out the latest changes of the software you are working on. After a little bit of compile time and the first coffee you start the just build application. Bumm, kernel panic. After rebooting and [...]]]></description>
			<content:encoded><![CDATA[<p>You may know the following situation. You arrive in the morning in the office, do what you always do and check out the latest changes of the software you are working on. After a little bit of compile time and the first coffee you start the just build application. Bumm, <a href="http://en.wikipedia.org/wiki/Kernel_panic" target="_blank"><strong>kernel panic</strong></a>. After rebooting and locking through the changes you may have an idea what the reason for this could be. A colleague of you is working on a fancy new feature which needed changes to a kernel module. As you almost know nothing about this code you seek for help and, as it of course not happen on his computer, he is asking for a <a href="http://en.wikipedia.org/wiki/Stack_trace" target="_blank">backtrace</a> of this panic. You have two problems now. First you need to see the panic yourself and second it would be nice to get a copy of the backtrace for sharing this info within a bugtracker. In the following post I will show how both aims could be easily archived.</p>
<h2>Let the kernel manage the graphical modes</h2>
<p>As most people are working under X11 they don't see the output of an kernel panic. When a kernel panic happens the kernel prints the reason for the panic and a kernel backtrace to the console window and stops immediately its own execution. It is not written into a log file or somewhere else. In consequence you don't have the ability to look into the panic text, cause the graphical mode is still on. Historically the mode settings are done by the graphic driver of the X11 system. So the kernel has no idea that or which graphic mode is currently in use. Fortunately the kernel hackers invented a new infrastructure which let the kernel do the mode switch. This subsystem is called <a href="https://fedoraproject.org/wiki/Features/KernelModesetting" target="_blank">Kernel-Mode-Settings (KMS)</a>. As the kernel do the mode settings, he can switch back to the console on a panic, regardless which graphical mode is currently configured. Beside this, KMS has other improvements like Fast User Switching or a flicker free switch between text and graphic mode. On the other side is this highly hardware dependent and even if it was introduced with version 2.6.28, not all today available hardware can make use of it. If you are an owner of an Intel graphic card you are in good shape. Radeon and NVidia cards have limited support through the in kernel drivers <a href="http://wiki.x.org/wiki/radeonhd" target="_blank">radeonhd</a> and <a href="http://nouveau.freedesktop.org/wiki/" target="_blank">nouveau</a>. For an Intel i915 card you need to enable the following kernel options:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">CONFIG_DRM_I915</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">y</span>
Location:
-&gt; Device Drivers
-&gt; Graphics support
-&gt; Direct Rendering Manager <span style="">&#40;</span>XFree86 4.1.0 and higher DRI support<span style="">&#41;</span> <span style="">&#40;</span>DRM <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span>
-&gt; Intel 830M, 845G, 852GM, 855GM, 865G <span style="">&#40;</span> <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span>
&nbsp;
<span style="color: #000099;">CONFIG_DRM_I915_KMS</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">y</span>
Location:
-&gt; Device Drivers
-&gt; Graphics support
-&gt; Direct Rendering Manager <span style="">&#40;</span>XFree86 4.1.0 and higher DRI support<span style="">&#41;</span> <span style="">&#40;</span>DRM <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span>
-&gt; Intel 830M, 845G, 852GM, 855GM, 865G <span style="">&#40;</span> <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span>
-&gt; i915 driver <span style="">&#40;</span>DRM_I915 <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span></pre></div></div>

<p>The kernel line in your favorite boot loader needs the following additional parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">i915.modeset=1</pre></div></div>

<p>X11 should have this minimal configuration for the device section:</p>

<div class="wp_syntax"><div class="code"><pre class="xorg_conf" style="font-family:monospace;"><span style="color: #b1b100;">Section</span> <span style="color: #0000ff;">&quot;Device&quot;</span>
 <span style="color: #990000;">Identifier</span>    <span style="color: #0000ff;">&quot;i915&quot;</span>
 <span style="color: #990000;">Driver</span>        <span style="color: #0000ff;">&quot;intel&quot;</span>
 <span style="color: #990000;">Option</span>        <span style="color: #0000ff;">&quot;DRI&quot;</span>   <span style="color: #0000ff;">&quot;true&quot;</span>
<span style="color: #b1b100;">EndSection</span></pre></div></div>

<p>Please note that you need of course some recent kernel, X11 version and Intel X11 driver to make this work. After a compile, install and boot of the new kernel, KMS should be in use. You will notice it, cause the boot messages will be printed in a much higher graphical resolution, than the usual text mode provide. The next time a kernel panic occurs, the kernel will switch back to the console before the panic is printed. This allows you to see the info printed and maybe you get a useful hint for the reason of the panic.</p>
<h2>Post the panic</h2>
<p>If you can't use KMS or don't want transcribe the panic text by hand into the bugtracker, it would be nice if the text could be made available on another computer. Kernel hackers usual use the serial port for that. Unfortunately most modern computers doesn't have such a serial port anymore. Also you need two hosts with a serial port and the setup is complex (you have to know about <a href="http://en.wikipedia.org/wiki/Serial_port#Settings" target="_blank">baud-rates, parity</a> and stuff like this). But there is a simpler solution: <a href="http://www.mjmwired.net/kernel/Documentation/networking/netconsole.txt" target="_blank">netconsole</a>. Netconsole is a kernel module, which sends kernel messages anywhere to the net using UDP. The setup is really simple. In the kernel configuration you need the following setting:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">CONFIG_NETCONSOLE</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">m</span>
Location:
-&gt; Device Drivers
-&gt; Network device support <span style="">&#40;</span>NETDEVICES <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span><span style="color: #000066; font-weight:bold;">=</span>y<span style="">&#93;</span></span><span style="">&#41;</span></pre></div></div>

<p>I prefer to compile it as module, which allows me to turn it on only when I need it. Load it with the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">modprobe netconsole <span style="color: #007800;">netconsole</span>=<span style="color: #000000; font-weight: bold;">@/</span>,<span style="color: #000000; font-weight: bold;">@</span>192.168.220.10<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>The ip has to be replaced by the one of your target computer. You can of course tune it much more, like setting source and target ports or even let netconsole send the text to more than one host. On your client you need a network tool which can read from a socket and print the read text to stdout. Netcat or nc are two tools which are able to do just that. The call for nc looks like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">nc <span style="color: #660033;">-l</span> <span style="color: #660033;">-u</span> <span style="color: #000000;">6666</span></pre></div></div>

<p>Now if a kernel panic will happen you will see an output like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [] rb_erase+0x15c/0x320
PGD 6942f067 PUD a1e4067 PMD 0
Oops: 0000 [#1] SMP
last sysfs file: /sys/devices/virtual/block/md1/dev
CPU 3
Modules linked in: vboxnetadp vboxnetflt vboxdrv netconsole ...
&nbsp;
Pid: 18887, comm: VirtualBox Tainted: G        W   2.6.36-gentoo #4 DG33TL/
RIP: 0010:[]  [] rb_erase+0x15c/0x320
RSP: 0018:ffff8800b430db58  EFLAGS: 00010046
RAX: 0000000000000000 RBX: ffff880069557a68 RCX: 0000000000000001
RDX: ffff880069557a68 RSI: ffff880001d8ed58 RDI: 0000000000000000
RBP: ffff8800b430db68 R08: 0000000000000001 R09: 000000008edcb5d6
R10: 0000000000000000 R11: 0000000000000202 R12: ffff880001d8ed58
R13: 0000000000000000 R14: 000000000000ed00 R15: 0000000000000002
FS:  00007fffde457710(0000) GS:ffff880001d80000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000000000000 CR3: 00000000064f9000 CR4: 00000000000026e0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
Process VirtualBox (pid: 18887, threadinfo ffff8800b430c000, task ffff880091e227f0)
Stack:
 ffff88000a03ba18 ffff880001d8ed48 ffff8800b430dba8 ffffffff8105bf06
&lt;0&gt; ffff8800b430dba8 ffffffff8105c97c ffff8800b430dbc8 ffff88000a03ba18
&lt;0&gt; 00004ff8a86ba455 ffff880001d8ed48 ffff8800b430dc48 ffffffff8105ce77
Call Trace:
 [] __remove_hrtimer+0x36/0xb0
 [] ? lock_hrtimer_base+0x2c/0x60
 [] __hrtimer_start_range_ns+0x2b7/0x3c0
 [] ? rtR0SemEventMultiLnxWait+0x250/0x3d0 [vboxdrv]
 [] ? RTLogLoggerExV+0x12f/0x180 [vboxdrv]
 [] hrtimer_start+0x13/0x20
 [] rtTimerLnxStartSubTimer+0x60/0x120 [vboxdrv]
 [] rtTimerLnxStartOnSpecificCpu+0x21/0x30 [vboxdrv]
 [] rtmpLinuxWrapper+0x23/0x30 [vboxdrv]
 [] RTMpOnSpecific+0x99/0xa0 [vboxdrv]
 [] ? rtTimerLnxStartOnSpecificCpu+0x0/0x30 [vboxdrv]
 [] RTTimerStart+0x2a6/0x2e0 [vboxdrv]
 [] ? g_abExecMemory+0x33665/0x180000 [vboxdrv]
 [] g_abExecMemory+0xc678/0x180000 [vboxdrv]
 [] g_abExecMemory+0x328d7/0x180000 [vboxdrv]
 [] supdrvIOCtlFast+0x6a/0x70 [vboxdrv]
 [] VBoxDrvLinuxIOCtl+0x47/0x1e0 [vboxdrv]
 [] ? pick_next_task_fair+0xde/0x150
 [] do_vfs_ioctl+0xa1/0x590
 [] ? sys_futex+0x76/0x170
 [] sys_ioctl+0x4a/0x80
 [] system_call_fastpath+0x16/0x1b
Code: 07 a8 01 75 9d eb 81 0f 1f 84 00 00 00 00 00 48 3b 78 10 0f 84 ...
RIP  [] rb_erase+0x15c/0x320
 RSP
CR2: 0000000000000000
---[ end trace 4eaa2a86a8e2da24 ]---</pre></div></div>

<p>Normally only kernel panics are sent to the console. You can increase the verbosity level by executing <code>dmesg -n 8</code> as root.</p>
<h2>Conclusion</h2>
<p>To continue with the story from the beginning: With the shown methods you can hope your colleague get enough information to find the reason for the kernel panic. To be more helpful, the next step would be to try to debug the problem yourself. Even if the <a href="https://kgdb.wiki.kernel.org/index.php/Main_Page" target="_blank">KGDB</a> was merged into the kernel in version 2.6.35, it is not really usable for me. The reason is that it seems kernel hackers usually have really old hardware which either has a serial port, a PS/2 keyboard or both. Otherwise I can't find a reason why USB keyboards don't work. I <a href="http://sourceforge.net/mailarchive/forum.php?thread_name=4CC99477.8010801%40oracle.com&amp;forum_name=kgdb-bugreport" target="_blank">asked</a> on the mailing list of KGDB about the status of USB keyboard support and I can only hope support will be integrated soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2010/10/getting-the-backtrace-from-a-kernel-panic/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using suppression files with Valgrind</title>
		<link>http://www.64k-tec.de/2010/10/using-suppression-files-with-valgrind/</link>
		<comments>http://www.64k-tec.de/2010/10/using-suppression-files-with-valgrind/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 19:10:22 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[leak check]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[memory checker]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[suppression lists]]></category>
		<category><![CDATA[symbol names]]></category>
		<category><![CDATA[Valgrind]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=1195</guid>
		<description><![CDATA[Valgrind is one of the great tools in the long list of freely available applications for development. Beside several profiling tools it also contains a memory checker. Leaking memory is one of the more common errors a programmer could step into. Basically it means to forget freeing memory (or in a more general sense: any [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://valgrind.org/" target="_blank">Valgrind</a> is one of the great tools in the long list of freely available applications for development. Beside several profiling tools it also contains a memory checker. <a href="http://en.wikipedia.org/wiki/Memory_leak" target="_blank">Leaking memory</a> is one of the more common errors a programmer could step into. Basically it means to forget freeing memory (or in a more general sense: any resource) a program has acquired. If you are a perfect developer, this will never happen to you. If you are a good developer it may happen and that's where Valgrind will save you some trouble. As most of the developers out there are more or less good developers, their programs produce memory leaks, too <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . The right solution for this, is of course to write a bug report. But there are times where this isn't possible or you are in hurry and don't want to see all the errors of a third-party library you link against.</p>
<p>In the following post, I will show how to suppress such unwanted error messages to make it much more easier to analyze the output of Valgrind for your own application.</p>
<h2>Installing Valgrind</h2>
<p>On Mac OS X you can use MacPorts to install Valgrind. You have to use valgrind-devel if you are on Snow Leopard, because Snow Leopard is supported in the current development version only. It's as simply as typing <code>sudo port install valgrind-devel</code>.</p>
<p>On Gentoo it can become a bit harder. The current stable version is 3.5 (like in MacPorts). If you try this version (at least on an unstable Gentoo like mine) with <code>valgrind ls</code>, you will get the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">valgrind:  Fatal error at startup: a function redirection
valgrind:  which is mandatory for this platform-tool combination
valgrind:  cannot be set up.  Details of the redirection are:
valgrind:
valgrind:  A must-be-redirected function
valgrind:  whose name matches the pattern:      strlen
valgrind:  in an object with soname matching:   ld-linux-x86-64.so.2
valgrind:  was not found whilst processing
valgrind:  symbols from the object with soname: ld-linux-x86-64.so.2
valgrind:
valgrind:  Possible fixes: (1, short term): install glibc's debuginfo
valgrind:  package on this machine.  (2, longer term): ask the packagers
valgrind:  for your Linux distribution to please in future ship a non-
valgrind:  stripped ld.so (or whatever the dynamic linker .so is called)
valgrind:  that exports the above-named function using the standard
valgrind:  calling conventions for this platform.
valgrind:
valgrind:  Cannot continue -- exiting now.  Sorry.</pre></div></div>

<p>The reason is a striped glibc. To work properly, Valgrind needs to overwrite some of the system functions the glibc provide. It does this by getting the symbols by name out of this library. This is of course not possible if all the symbol names are removed. You can prove this by executing <code>nm /lib/ld-linux-x86-64.so.2</code>. Gentoo provides a <em>FEATURE=splitdebug</em>, which adds debug libraries to the installation. Unfortunately setting this feature in <code class="path">/etc/make.conf</code>, means setting it global. Gentoo is known as being configurable as no other distribution out there and of course we can set a feature for one program only. To do so, create a file called <code class="path">glibc</code> in <code class="path">/etc/portage/env/sys-libs/</code> and add the following content to it.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">FEATURES</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #933;">&quot;splitdebug&quot;</span></pre></div></div>

<p>After a rebuild of glibc by executing <code>emerge --oneshot glibc</code>, we have a working Valgrind.</p>
<p>As all programs, Valgrind isn't perfect. Version 3.5 shows many false/positive hits on my system, but fortunately the development goes on. Currently there is no newer version available in the Gentoo tree. Anyway it is not necessary to build one yourself, to get a more recent version. Using <strong>layman</strong> and the overlay tree of <a href="http://gentoo-overlays.zugaina.org/flameeyes-overlay/dev-util.html.en" target="_blank">Flameeyes</a> will let you integrate the development version of Valgrind seamlessly into your system. For a general How-to of layman check out this <a href="http://www.gentoo.org/proj/en/overlays/userguide.xml" target="_blank">Users' guide</a>. In short, something like the following should be sufficient:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">layman <span style="color: #660033;">-a</span> flameeyes-overlay
layman <span style="color: #660033;">-s</span> flameeyes-overlay
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;=dev-util/valgrind-9999 **&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>portage<span style="color: #000000; font-weight: bold;">/</span>package.keywords
emerge <span style="color: #c20cb9; font-weight: bold;">valgrind</span></pre></div></div>

<p>Installing the development version of Valgrind is optional of course.</p>
<h2>Know your tools</h2>
<p>One usage of Valgrind could be look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">valgrind</span> <span style="color: #660033;">--leak-check</span>=full <span style="color: #660033;">--leak-resolution</span>=high .<span style="color: #000000; font-weight: bold;">/</span>VirtualBox</pre></div></div>

<p>Beside other errors it also shows this error message on my system:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">==27174==    at 0x4C26C09: memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==27174==    by 0x4C26CB9: posix_memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==27174==    by 0xBA8967F: ??? (in /usr/lib64/libglib-2.0.so.0.2400.2)
==27174==    by 0xBA89E9D: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.2400.2)
==27174==    by 0xBA89F86: g_slice_alloc0 (in /usr/lib64/libglib-2.0.so.0.2400.2)
==27174==    by 0xC204847: g_type_create_instance (in /usr/lib64/libgobject-2.0.so.0.2400.2)
==27174==    by 0xC1EB8A5: ??? (in /usr/lib64/libgobject-2.0.so.0.2400.2)
==27174==    by 0xC1ECE5D: g_object_newv (in /usr/lib64/libgobject-2.0.so.0.2400.2)
==27174==    by 0xC1ED494: g_object_new (in /usr/lib64/libgobject-2.0.so.0.2400.2)
==27174==    by 0x72A495F: ??? (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x72A0D4F: ??? (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x7289264: QGtkStyle::QGtkStyle() (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x7215DB6: QStyleFactory::create(QString const&amp;amp;) (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x6F5B7FC: QApplication::style() (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x6F61DFF: QApplicationPrivate::initialize() (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x6F61E88: QApplicationPrivate::construct(_XDisplay*, unsigned long, unsigned long) (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x6F61FF3: QApplication::QApplication(_XDisplay*, int&amp;amp;, char**, unsigned long, unsigned long, int) (in /usr/lib64/qt4/libQtGui.so.4.6.3)
==27174==    by 0x44BC38: TrustedMain (main.cpp:371)
==27174==    by 0x44C649: main (main.cpp:651)</pre></div></div>

<p>If you analyze the backtrace, you see that something in <em>libQtGui</em> is leaking memory. I don't want to blame someone for it or make a statement if this is right or wrong, I just want to get rid of it, to be able to easily spot errors VirtualBox itself produce. To do so, add <code>--gen-suppressions=all</code> to the Valgrind call. This will produce something similar like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{
 Memcheck:Leak
 fun:memalign
 fun:posix_memalign
 obj:/usr/lib64/libglib-2.0.so.0.2400.2
 fun:g_slice_alloc
 fun:g_slice_alloc0
 fun:g_type_create_instance
 obj:/usr/lib64/libgobject-2.0.so.0.2400.2
 fun:g_object_newv
 fun:g_object_new
 obj:/usr/lib64/qt4/libQtGui.so.4.6.3
 obj:/usr/lib64/qt4/libQtGui.so.4.6.3
 fun:_ZN9QGtkStyleC1Ev
 fun:_ZN13QStyleFactory6createERK7QString
 fun:_ZN12QApplication5styleEv
 fun:_ZN19QApplicationPrivate10initializeEv
 fun:_ZN19QApplicationPrivate9constructEP9_XDisplaymm
 fun:_ZN12QApplicationC1EP9_XDisplayRiPPcmmi
 fun:TrustedMain
 fun:main
}</pre></div></div>

<p>To let Valgrind ignore this error in the future, copy the text into a file vbox.supp and start Valgrind with <code>--suppressions=vbox.supp</code>. Viola, this specific error isn't shown anymore. The format used there is easy to understand and you can of course tweak this much more. E.g. you could replace some of the <code>fun:</code> entries by "<code>...</code>". This is a placeholder for one or more functions calls with any name. Beside making suppression rules more general you can of course add as much as you like. Adding a name at the top make it easy to identify the different rules. For all the possibilities have a look at the <a href="http://valgrind.org/docs/manual/mc-manual.html#mc-manual.suppfiles" target="_blank">documentation</a>. Just for the curious, Valgrind is using such a file itself. Have a look at /usr/lib/valgrind/default.supp. You may also have noted that the function names in the normal error message differ  from the one in the suppression list. The former is in the <a href="http://en.wikipedia.org/wiki/Name_mangling" target="_blank">demangled</a> form  and the later in the saved form. You could force Valgrind to print  mangled function names by adding the <code>--demangle=no</code> parameter to the call. This becomes handy if you manually create suppression lists.</p>
<h2>Conclusion</h2>
<p>By using suppression rules for the own application, unimportant errors could be eliminated in the output of Valgrind. With this is in mind there is no excuse anymore for memory leaks in the self developed applications. Beside memory leaks, Valgrind also finds places where uninitialized variables are in use or where memory is used which isn't allocated by the application. Also these tests could be filtered out by suppression rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2010/10/using-suppression-files-with-valgrind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

