<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>code on Mister Muffin Blog</title>
    <link>http://blog.mister-muffin.de/tags/code/</link>
    <description>Recent content in code on Mister Muffin Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Sun, 25 Oct 2015 18:44:00 +0000</lastBuildDate><atom:link href="http://blog.mister-muffin.de/tags/code/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>unshare without superuser privileges</title>
      <link>http://blog.mister-muffin.de/2015/10/25/unshare-without-superuser-privileges/</link>
      <pubDate>Sun, 25 Oct 2015 18:44:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2015/10/25/unshare-without-superuser-privileges/</guid>
      <description>&lt;p&gt;TLDR: With the help of Helmut Grohne I finally figured out most of the bits
necessary to unshare everything without becoming root (though one might say
that this is still cheated because the suid root tools &lt;code&gt;newuidmap&lt;/code&gt; and &lt;code&gt;newgidmap&lt;/code&gt;
are used). I wrote a Perl script which documents how this is done in practice.
This script is nearly equivalent to using the existing commands &lt;code&gt;lxc-usernsexec [opts] -- unshare [opts] -- COMMAND&lt;/code&gt; except that these two together cannot be
used to mount a new proc. Apart from this problem, this Perl script might also
be useful by itself because it is architecture independent and easily
inspectable for the curious mind without resorting to sources.debian.net (it is
heavily documented at nearly 2 lines of comments per line of code on average).
It can be retrieved here at
&lt;a href=&#34;https://gitlab.mister-muffin.de/josch/user-unshare/blob/master/user-unshare&#34;&gt;https://gitlab.mister-muffin.de/josch/user-unshare/blob/master/user-unshare&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Long story: Nearly two years after my last &lt;a href=&#34;http://blog.mister-muffin.de/2014/01/11/why-do-i-need-superuser-privileges-when-i-just-want-to-write-to-a-regular-file/&#34;&gt;last rant about everything needing
superuser privileges in
Linux&lt;/a&gt;,
I&amp;rsquo;m still interested in techniques that let me do more things without becoming
root. Helmut Grohne had told me for a while about unshare(), or user namespaces
as the right way to have things like chroot without root. There are also
reports of LXC containers working without root privileges but they are hard to
come by. A couple of days ago I had some time again, so Helmut helped me to get
through the major blockers that were so far stopping me from using unshare in a
meaningful way without executing everything with &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;My main motivation at that point was to let &lt;code&gt;dpkg-buildpackage&lt;/code&gt; when executed
by &lt;code&gt;sbuild&lt;/code&gt; be run with an unshared network namespace and thus without network
access (except for the loopback interface) because like pbuilder I wanted
sbuild to enforce the rule not to access any remote resources during the build.
After several evenings of investigating and doctoring at the Perl script I
mentioned initially, I came to the conclusion that the only place that can
unshare the network namespace without disrupting anything is schroot itself.
This is because unsharing &lt;em&gt;inside&lt;/em&gt; the chroot will fail because
dpkg-buildpackage is run with non-root privileges and thus the user namespace
has to be unshared. But this then will destroy all ownership information. But
even if that wasn&amp;rsquo;t the case, the chroot itself is unlikely to have (and also
should not) tools like &lt;code&gt;ip&lt;/code&gt; or &lt;code&gt;newuidmap&lt;/code&gt; and &lt;code&gt;newgidmap&lt;/code&gt; installed. Unsharing
the schroot call itself also will not work. Again we first need to unshare the
user namespace and then schroot will complain about wrong ownership of its
configuration file &lt;code&gt;/etc/schroot/schroot.conf&lt;/code&gt;. Luckily, when contacting Roger
Leigh about this wishlist feature in
&lt;a href=&#34;http://bugs.debian.org/802849&#34;&gt;bug#802849&lt;/a&gt; I was told that this was already
implemented in its git master \o/. So this particular problem seems to be taken
care of and once the next schroot release happens, sbuild will make use of it
and have &lt;code&gt;unshare --net&lt;/code&gt; capabilities just like &lt;code&gt;pbuilder&lt;/code&gt; already had since
last year.&lt;/p&gt;
&lt;p&gt;With the sbuild case taken care of, the rest of this post will introduce &lt;a href=&#34;https://gitlab.mister-muffin.de/josch/user-unshare/blob/master/user-unshare&#34;&gt;the
Perl script I wrote&lt;/a&gt;.
The name &lt;code&gt;user-unshare&lt;/code&gt; is really arbitrary. I just needed some identifier for
the git repository and a filename.&lt;/p&gt;
&lt;p&gt;The most important discovery I made was, that Debian disables unprivileged user
namespaces by default with the patch
&lt;code&gt;add-sysctl-to-disallow-unprivileged-CLONE_NEWUSER-by-default.patch&lt;/code&gt; to the
Linux kernel. To enable it, one has to first either do&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;echo 1 | sudo tee /proc/sys/kernel/unprivileged_userns_clone &amp;gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sudo sysctl -w kernel.unprivileged_userns_clone=1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The tool tries to be like unshare(1) but with the power of lxc-usernsexec(1) to
map more than one id into the new user namespace by using the programs
&lt;code&gt;newgidmap&lt;/code&gt; and &lt;code&gt;newuidmap&lt;/code&gt;. Or in other words: This tool tries to be like
lxc-usernsexec(1) but with the power of unshare(1) to unshare more than just
the user and mount namespaces. It is nearly equal to calling:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;lxc-usernsexec [opts] -- unshare [opts] -- COMMAND
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Its main reason of existence are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;as a project for me to learn how unprivileged namespaces work&lt;/li&gt;
&lt;li&gt;written in Perl which means:
&lt;ul&gt;
&lt;li&gt;architecture independent (same executable on any architecture)&lt;/li&gt;
&lt;li&gt;easily inspectable by other curious minds&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;tons of code comments to let others understand how things work&lt;/li&gt;
&lt;li&gt;no need to install the lxc package in a minimal environment (perl itself
might not be called minimal either but is present in every Debian
installation)&lt;/li&gt;
&lt;li&gt;not suffering from being unable to mount proc&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I hoped that &lt;code&gt;systemd-nspawn&lt;/code&gt; could do what I wanted but it seems that its
requirement for being run as root will &lt;a href=&#34;http://lists.freedesktop.org/archives/systemd-devel/2015-February/028139.html&#34;&gt;not change any time
soon&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Another tool in Debian that offers to do chroot without superuser privileges is
&lt;code&gt;linux-user-chroot&lt;/code&gt; but that one cheats by being suid root.&lt;/p&gt;
&lt;p&gt;Had I found &lt;code&gt;lxc-usernsexec&lt;/code&gt; earlier I would&amp;rsquo;ve probably not written this. But
after I found it I happily used it to get an even better understanding of the
matter and further improve the comments in my code. I started writing my own
tool in Perl because that&amp;rsquo;s the language sbuild was written in and as mentioned
initially, I intended to use this script with sbuild. Now that the sbuild
problem is taken care of, this is not so important anymore but I like if I can
read the code of simple programs I run directly from /usr/bin without having to
retrieve the source code first or use sources.debian.net.&lt;/p&gt;
&lt;p&gt;The only thing I wasn&amp;rsquo;t able to figure out is how to properly mount proc into
my new mount namespace. I found a workaround that works by first mounting a new
proc to &lt;code&gt;/proc&lt;/code&gt; and then bind-mounting &lt;code&gt;/proc&lt;/code&gt; to whatever new location for
proc is requested. I didn&amp;rsquo;t figure out how to do this without mounting to
&lt;code&gt;/proc&lt;/code&gt; first partly also because this doesn&amp;rsquo;t work at all when using
&lt;code&gt;lxc-usernsexec&lt;/code&gt; and &lt;code&gt;unshare&lt;/code&gt; together. In this respect, this perl script is a
bit more powerful than those two tools together. I suppose that the reason is
that &lt;code&gt;unshare&lt;/code&gt; wasn&amp;rsquo;t written with having being called without superuser
privileges in mind. If you have an idea what could be wrong, the code has a big
&lt;code&gt;FIXME&lt;/code&gt; about this issue.&lt;/p&gt;
&lt;p&gt;Finally, here a demonstration of what my script can do. Because of the &lt;code&gt;/proc&lt;/code&gt;
bug, &lt;code&gt;lxc-usernsexec&lt;/code&gt; and &lt;code&gt;unshare&lt;/code&gt; together are not able to do this but it
might also be that I&amp;rsquo;m just not using these tools in the right way. The
following will give you an interactive shell in an environment created from one
of my sbuild chroot tarballs:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ mkdir -p /tmp/buildroot/proc
$ ./user-unshare --mount-proc=/tmp/buildroot/proc --ipc --pid --net \
	--uts --mount --fork -- sh -c &#39;ip link set lo up &amp;amp;&amp;amp; ip addr &amp;amp;&amp;amp; \
	hostname hoothoot-chroot &amp;amp;&amp;amp; \
	tar -C /tmp/buildroot -xf /srv/chroot/unstable-amd64.tar.gz; \
	/usr/sbin/chroot /tmp/buildroot /sbin/runuser -s /bin/bash - josch &amp;amp;&amp;amp; \
	umount /tmp/buildroot/proc &amp;amp;&amp;amp; rm -rf /tmp/buildroot&#39;
(unstable-amd64-sbuild)josch@hoothoot-chroot:/$ whoami
josch
(unstable-amd64-sbuild)josch@hoothoot-chroot:/$ hostname
hoothoot-chroot
(unstable-amd64-sbuild)josch@hoothoot-chroot:/$ ls -lha /proc | head
total 0
dr-xr-xr-x 218 nobody nogroup    0 Oct 25 19:06 .
drwxr-xr-x  22 root   root     440 Oct  1 08:42 ..
dr-xr-xr-x   9 root   root       0 Oct 25 19:06 1
dr-xr-xr-x   9 josch  josch      0 Oct 25 19:06 15
dr-xr-xr-x   9 josch  josch      0 Oct 25 19:06 16
dr-xr-xr-x   9 root   root       0 Oct 25 19:06 7
dr-xr-xr-x   9 josch  josch      0 Oct 25 19:06 8
dr-xr-xr-x   4 nobody nogroup    0 Oct 25 19:06 acpi
dr-xr-xr-x   6 nobody nogroup    0 Oct 25 19:06 asound
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course instead of running this long command we can also instead write a
small shell script and execute that instead. The following does the same things
as the long command above but adds some comments for further explanation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;set -exu
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# I&amp;#39;m using /tmp because I have it mounted as a tmpfs&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rootdir&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/tmp/buildroot&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# bring the loopback interface up&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ip link set lo up
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# show that the loopback interface is really up&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ip addr
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# make use of the UTS namespace being unshared&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;hostname hoothoot-chroot
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# extract the chroot tarball. This must be done inside the user namespace for&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# the file permissions to be correct.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# tar will fail to call mknod and to change the permissions of /proc but we are&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# ignoring that&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tar -C &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;$rootdir&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; -xf /srv/chroot/unstable-amd64.tar.gz &lt;span style=&#34;color:#f92672&#34;&gt;||&lt;/span&gt; true
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# run chroot and inside, immediately drop permissions to the user &amp;#34;josch&amp;#34; and&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# start an interactive shell&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;/usr/sbin/chroot &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;$rootdir&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; /sbin/runuser -s /bin/bash - josch
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# unmount /proc and remove the temporary directory&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;umount &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;$rootdir&lt;span style=&#34;color:#e6db74&#34;&gt;/proc&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;rm -rf &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;$rootdir&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ mkdir -p /tmp/buildroot/proc
$ ./user-unshare --mount-proc=/tmp/buildroot/proc --ipc --pid --net --uts --mount --fork -- ./chroot.sh
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As mentioned in the beginning, the tool is nearly equivalent to calling
&lt;code&gt;lxc-usernsexec [opts] -- unshare [opts] -- COMMAND&lt;/code&gt; but because of the problem
with mounting proc (mentioned earlier), &lt;code&gt;lxc-usernsexec&lt;/code&gt; and &lt;code&gt;unshare&lt;/code&gt; cannot
be used with above example. If one tries anyways one will only get:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ lxc-usernsexec -m b:0:1000:1 -m b:1:558752:1 -- unshare --mount-proc=/tmp/buildroot/proc --ipc --pid --net --uts --mount --fork -- ./chroot.sh
unshare: mount /tmp/buildroot/proc failed: Invalid argument
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;rsquo;d be interested in finding out why that is and how to fix it.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Reliable IMAP synchronization with IDLE support</title>
      <link>http://blog.mister-muffin.de/2013/06/05/reliable-imap-synchronization-with-idle-support/</link>
      <pubDate>Wed, 05 Jun 2013 06:47:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2013/06/05/reliable-imap-synchronization-with-idle-support/</guid>
      <description>&lt;p&gt;The task: reliably synchronize my local MailDir with several remote IMAP
mailboxes with IDLE support so that there is no need to poll with small time
intervals to get new email immediately.&lt;/p&gt;
&lt;p&gt;Most graphical mail clients like icedove/thunderbird or evolution have IDLE
support which means that their users get notified about new email as soon as it
arrives. I prefer to handle email fetching/reading/sending using separate
programs so after having used icedove for a long time, I switched to a
&lt;a href=&#34;http://offlineimap.org/&#34;&gt;offlineimap&lt;/a&gt;/mutt based setup a few years ago. Some
while after that I discovered sup and later
&lt;a href=&#34;http://notmuchmail.org/&#34;&gt;notmuch&lt;/a&gt;/&lt;a href=&#34;https://github.com/pazz/alot&#34;&gt;alot&lt;/a&gt; which
made me switch again. Now my only remaining problem is the synchronization
between my local email and several remote IMAP servers.&lt;/p&gt;
&lt;p&gt;Using offlineimap worked fine in the beginning but soon I discovered some of
its shortcomings. It would for example sometimes lock up or simply crash when I
switched between different wireless networks or switched to ethernet or UMTS.
Crashing was not a big problem as I just put it into a script which re-executed
it every time it crashed. The problem was it locking up for one of its
synchronizing email accounts while the others were kept in sync as usual. This
once let to me missing five days of email because offlineimap was not crashing
and I believed everything was fine (new messages from the other accounts were
scrolling by as usual) while people were sending me worried emails whether I
was okay or if something bad had happened. I nearly missed a paper submission
deadline and another administrative deadline due to this. This was insanely
annoying. It turned out that other mail synchronization programs suffered from
the same lockup problem so I stuck with offlineimap and instead executed it as:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; true; &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    timeout --signal&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;KILL 1m offlineimap
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    notmuch new
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sleep 5m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;done&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which would synchronize my email every five minutes and kill offlineimap if a
synchronization took more than one minute. While I would&amp;rsquo;ve liked an email
synchronizer which would not need this measure, this worked fine over the
months.&lt;/p&gt;
&lt;p&gt;After some while it bugged me that icedove (which my girlfriend is using) was
receiving email nearly instantaneously after they arrived and I couldnt have
this feature with my setup. This instantaneous arrival of email works by using
the IMAP IDLE command which allows the server to notify the client once new
email arrives instead of the client having to poll for new email in small
intervals.  Unfortunately offlineimap (and any other email synchronizer I
found) would not support the IDLE command. There is a fork of it which supports
IDLE by using a newer python imap library but this is of little use to me as
there is no possibility of a hook which executes when new email arrives so that
I can execute &lt;code&gt;notmuch new&lt;/code&gt; on the new email. At this point I could&amp;rsquo;ve used
inotify to execute &lt;code&gt;notmuch new&lt;/code&gt; upon arrival of new email but I went another
way.&lt;/p&gt;
&lt;p&gt;Here is a short python script &lt;code&gt;idle.py&lt;/code&gt; which connects to my IMAP servers and
sends the IDLE command:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; imaplib
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; select
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; sys
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;mysock&lt;/span&gt;():
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; __init__(self, name, server, user, passwd, directory):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;name &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; name
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;directory &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; directory
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; imaplib&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;IMAP4(server)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;login(user, passwd)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;select(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;directory)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;send(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; IDLE&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\r\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt;(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;_new_tag()))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;readline()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;startswith(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;+&amp;#39;&lt;/span&gt;): &lt;span style=&#34;color:#75715e&#34;&gt;# expect continuation response&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	    exit(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fileno&lt;/span&gt;(self):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;socket()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fileno()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; __name__ &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sockets &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	    mysock(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Name1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;imap.foo.bar&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;user1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;pass1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;folder1&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	    mysock(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Name1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;imap.foo.bar&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;user1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;pass1&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;folder2&amp;#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	    mysock(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Name2&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;imap.blub.bla&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;user2&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;pass2&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;folder3&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    readable, _, _ &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; select&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;select(sockets, [], [], &lt;span style=&#34;color:#ae81ff&#34;&gt;1980&lt;/span&gt;) &lt;span style=&#34;color:#75715e&#34;&gt;# 33 mins timeout&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    found &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;False&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; sock &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; readable:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; sock&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;M&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;readline()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;startswith(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;* BYE &amp;#39;&lt;/span&gt;): &lt;span style=&#34;color:#66d9ef&#34;&gt;continue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        print &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;-u basic -o -q -f &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; -a &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt;(sock&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;directory, sock&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;name)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        found &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; found:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        print &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;-u basic -o&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It would create a connection to every directory I want to watch on every email
account I want to synchronize. The select call will expire after 33 minutes as
most email servers would drop the connection if nothing happens at around 30
minutes. If something happened within that time though, the script would output
the arguments to offlineimap to do a quick check on just that mailbox on that
account. If nothing happened, the script would output the arguments to
offlineimap to do a full check on all my mailboxes.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; true; &lt;span style=&#34;color:#66d9ef&#34;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	args&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;timeout --signal&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;KILL 35m python idle.py&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;||&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		echo &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;idle timed out&amp;#34;&lt;/span&gt; &amp;gt;&amp;amp;2;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		args&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;-u basic -o&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#f92672&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	echo &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;call offlineimap with: &lt;/span&gt;$args&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt; &amp;gt;&amp;amp;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	timeout --signal&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;KILL 1m offlineimap $args &lt;span style=&#34;color:#f92672&#34;&gt;||&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		echo &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;offlineimap timed out&amp;#34;&lt;/span&gt; &amp;gt;&amp;amp;2;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#66d9ef&#34;&gt;continue&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#f92672&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	notmuch new
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;done&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Every call which interacts with the network is wrapped in a &lt;code&gt;timeout&lt;/code&gt; command
to avoid any funny effects. Should the python script timeout, a full
synchronization with offlineimap is triggered. Should offlineimap timeout, an
error message is written to stderr and the script continues. The above
naturally has the disadvantage of not immediately responding to new email which
arrives during the time that &lt;code&gt;idle.py&lt;/code&gt; is not running. But as this email will
be fetched once the next message arrives on the same account, there is no much
waiting time and so far, this problem didnt bite me.&lt;/p&gt;
&lt;p&gt;Is there a better way to synchronize my email and at a same time make use of
IDLE? I&amp;rsquo;m surprised I didnt find software which would offer this feature.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>enumerating elementary circuits of a directed_graph</title>
      <link>http://blog.mister-muffin.de/2012/07/04/enumerating-elementary-circuits-of-a-directed_graph/</link>
      <pubDate>Wed, 04 Jul 2012 14:57:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2012/07/04/enumerating-elementary-circuits-of-a-directed_graph/</guid>
      <description>&lt;p&gt;For my GSoC project this year I need to be able to enumerate all elementary
circuits of a directed graph. My code is written in Ocaml but neither the
&lt;a href=&#34;http://ocamlgraph.lri.fr/&#34;&gt;ocamlgraph library&lt;/a&gt; nor graph libraries for other
languages seem to implement a well tested algorithm for this task.&lt;/p&gt;
&lt;p&gt;In lack of such a well tested solution to the problem, I decided to implement a
couple of different algorithms. Since it is unlikely that different algorithms
yield the same wrong result, I can be certain enough that each individual
algorithm is working correctly in case they all agree on a single solution.&lt;/p&gt;
&lt;p&gt;As a result I wrote a testsuite, containing an unholy mixture of Python, Ocaml,
D and Java code which implements algorithms by D. B. Johnson, R.  Tarjan, K. A.
Hawick and H. A. James.&lt;/p&gt;
&lt;h2 id=&#34;algorithm-by-r-tarjan&#34;&gt;Algorithm by R. Tarjan&lt;/h2&gt;
&lt;p&gt;The earliest algorithm that I included was published by R. Tarjan in 1973.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Enumeration of the elementary circuits of a directed graph
R. Tarjan, SIAM Journal on Computing, 2 (1973), pp. 211-216
http://dx.doi.org/10.1137/0202017
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I implemented the pseudocode given in the paper using Python. The git
repository can be found here:
&lt;a href=&#34;https://github.com/josch/cycles_tarjan&#34;&gt;https://github.com/josch/cycles_tarjan&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;algorithm-by-d-b-johnson&#34;&gt;Algorithm by D. B. Johnson&lt;/h2&gt;
&lt;p&gt;The algorithm by D. B. Johnson from 1975 improves on Tarjan&amp;rsquo;s algorithm by its
complexity.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Finding all the elementary circuits of a directed graph.
D. B. Johnson, SIAM Journal on Computing 4, no. 1, 77-84, 1975.
http://dx.doi.org/10.1137/0204007
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the worst case, Tarjan&amp;rsquo;s algorithm has a time complexity of O(n⋅e(c+1))
whereas Johnson&amp;rsquo;s algorithm supposedly manages to stay in O((n+e)(c+1)) where n
is the number of vertices, e is the number of edges and c is the number of
cycles in the graph.&lt;/p&gt;
&lt;p&gt;I found two implementations of Johnson&amp;rsquo;s algorithm. One was done by &lt;a href=&#34;http://normalisiert.de/&#34;&gt;Frank
Meyer&lt;/a&gt; and can be downloaded as a &lt;a href=&#34;http://normalisiert.de/code/java/elementaryCycles.zip&#34;&gt;zip
archive&lt;/a&gt;. The other was
done by Pietro Abate and the code can be found in a &lt;a href=&#34;http://mancoosi.org/~abate/finding-all-elementary-circuits-directed-graph&#34;&gt;blog
entry&lt;/a&gt;
which also points to a git repository.&lt;/p&gt;
&lt;p&gt;The implementation by Frank Meyer seemed to work flawlessly. I only had to add
code so that a graph could be given via commandline. The git repository of my
additions can be found here:
&lt;a href=&#34;https://github.com/josch/cycles_johnson_meyer&#34;&gt;https://github.com/josch/cycles_johnson_meyer&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pietro Abate implemented an iterative and a functional version of Johnson&amp;rsquo;s
algorithm.  It turned out that both yielded incorrect results as some cycles
were missing from the output. A fixed version can be found in this git
repository:
&lt;a href=&#34;https://github.com/josch/cycles_johnson_abate&#34;&gt;https://github.com/josch/cycles_johnson_abate&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;algorithm-by-k-a-hawick-and-h-a-james&#34;&gt;Algorithm by K. A. Hawick and H. A. James&lt;/h2&gt;
&lt;p&gt;The algorithm by K. A. Hawick and H. A. James from 2008 improves further on
Johnson&amp;rsquo;s algorithm and does away with its limitations.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs.
Hawick and H.A. James, In Proceedings of FCS. 2008, 14-20
www.massey.ac.nz/~kahawick/cstn/013/cstn-013.pdf
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In contrast to Johnson&amp;rsquo;s algorithm, the algorithm by K. A. Hawick and H. A.
James is able to handle graphs containing edges that start and end at the same
vertex as well as multiple edges connecting the same two vertices. I do not
need this functionality but add the code as additional verification.&lt;/p&gt;
&lt;p&gt;The paper posts extensive code snippets written in the D programming language.
A full, working version with all pieces connected together can be found here:
&lt;a href=&#34;https://github.com/josch/cycles_hawick_james&#34;&gt;https://github.com/josch/cycles_hawick_james&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The algorithm was verified using example output given in the paper. The project
README states how to reproduce it.&lt;/p&gt;
&lt;h2 id=&#34;input-format&#34;&gt;Input format&lt;/h2&gt;
&lt;p&gt;All four codebases have been modified to produce executables that take the same
commandline arguments which describes the graphs to investigate.&lt;/p&gt;
&lt;p&gt;The first argument is the number of vertices of the graph. Subsequent arguments
are ordered pairs of comma separated vertices that make up the directed edges
of the graph.&lt;/p&gt;
&lt;p&gt;Lets look at the following graph as an example:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://blog.mister-muffin.de/images/cycle_example.dot.png&#34;&gt;&lt;img src=&#34;http://blog.mister-muffin.de/thumbs/cycle_example.dot.png&#34; alt=&#34;cycle example&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The DOT source for this graph is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;digraph G {
  0;
  1;
  2;
  0 -&amp;gt; 1;
  0 -&amp;gt; 2;
  1 -&amp;gt; 0;
  2 -&amp;gt; 0;
  2 -&amp;gt; 1;
  }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To generate the list of elementary circuits using Tarjan&amp;rsquo;s algorithm for the
graph above, use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ python cycles.py 3 0,1 0,2 1,0 2,0 2,1
0 1
0 2
0 2 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The commandline arguments are the exact same for the other three methods and
yield the same result in the same order.&lt;/p&gt;
&lt;p&gt;If the DOT graph is in a format as simple as above, the following sed construct
can be used to generate the commandline argument that represents the graph:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ echo `sed -n -e &#39;/^\s*[0-9]\+;$/p&#39; graph.dot | wc -l` `sed -n -e &#39;s/^\s*\([0-9]\) -&amp;gt; \([0-9]\);$/\1,\2/p&#39; graph.dot`
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;testsuite&#34;&gt;Testsuite&lt;/h2&gt;
&lt;p&gt;As all four codebases take the same input format and have the same output
format, it is now trivial to write a testsuite that compares the individual
output of each algorithm for the same input and checks for differences.&lt;/p&gt;
&lt;p&gt;The code of the testsuite is available via this git repository:
&lt;a href=&#34;https://github.com/josch/cycle_test&#34;&gt;https://github.com/josch/cycle_test&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The other four repositories exist as submodules of the testsuite repository.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/josch/cycle_test.git
$ cd cycle_test
$ git submodule update --init
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A testrun is done via calling:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ ./test.sh 11
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The argument to the shell script is an integer denoting the maximum number &lt;code&gt;N&lt;/code&gt;
of vertices for which graphs will be generated.&lt;/p&gt;
&lt;p&gt;The script will compile the Ocaml, Java and D sourcecode of the submodules as
well as an ocaml script called &lt;code&gt;rand_graph.ml&lt;/code&gt; which generates random graphs
from &lt;code&gt;v = 1..N&lt;/code&gt; vertices where &lt;code&gt;N&lt;/code&gt; is given as a commandline argument. For each
number of vertices &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;e = 1..M&lt;/code&gt; number of edges are chosen where &lt;code&gt;M&lt;/code&gt; is
maximum number of edges given the number of vertices. For every combination of
number of vertices &lt;code&gt;v&lt;/code&gt; and number of edges &lt;code&gt;e&lt;/code&gt;, a graph is randomly generated
using &lt;code&gt;Pack.Digraph.Rand.graph&lt;/code&gt; from the ocamlgraph library. Each of those
generated graphs is checked for cycles and written to a file &lt;code&gt;graph-v-e.dot&lt;/code&gt; if
the graph contains a cycle.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;test.sh&lt;/code&gt; then loops over all generated dot files. It uses the above sed
expression to convert each dot file to a commandline argument for each of the
algorithms.&lt;/p&gt;
&lt;p&gt;The outputs of each algorithm are then compared with each other and only if
they do not differ, does the loop continue. Otherwise the script returns with
an exit code.&lt;/p&gt;
&lt;p&gt;The tested algorithms are the Python implementation of Tarjan&amp;rsquo;s algorithm, the
iterative and functional Ocaml implementation as well as the Java
implementation of Johnson&amp;rsquo;s algorithm and the D implementation of the algorithm
by Hawick and James.&lt;/p&gt;
&lt;h2 id=&#34;future-developments&#34;&gt;Future developments&lt;/h2&gt;
&lt;p&gt;Running the testsuite with a maximum of 12 vertices takes about 33 minutes on a
2.53GHz Core2Duo and produces graphs with as much as 1.1 million cycles.  It
seems that all five implementations agree on the output for all 504 generated
graphs that were used as input.&lt;/p&gt;
&lt;p&gt;If there should be another implementation of an algorithm that enumerates all
elementary circuits of a directed graph, I would like to add it.&lt;/p&gt;
&lt;p&gt;There are some more papers that I would like to read but I lack access to
epubs.siam.org and ieeexplore.ieee.org and would have to buy them.&lt;/p&gt;
&lt;p&gt;Benchmarks seem a bit pointless as not only the algorithms are very different
from each other (and there are many ways to tweak each of them) but also the
programming languages differ. Though for the curious kind, it follows the time
each algorithm takes to enumerate all cycles for all generated graphs up to 11
vertices.&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;algorithm&lt;/th&gt;&lt;th&gt;time (s)&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Johnson, Abate, Ocaml, iterative&lt;/td&gt;&lt;td&gt;137&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Johnson, Abate, Ocaml, functional&lt;/td&gt;&lt;td&gt;139&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Tarjan, Python&lt;/td&gt;&lt;td&gt;153&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Hawick, D&lt;/td&gt;&lt;td&gt;175&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Johnson, Meyer, Java&lt;/td&gt;&lt;td&gt;357&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;The iterative Ocaml code performs as well as the functional one. In practice,
the iterative code should probably be preferred as the functional code is not
tail recursive. On the other hand it is also unlikely that cycles ever grow big
enough to make a difference in the used stack space.&lt;/p&gt;
&lt;p&gt;The Python implementation executes surprisingly fast, given that Tarjan&amp;rsquo;s
algorithm is supposedly inferior to Johnson&amp;rsquo;s and given that Python is
interpreted but the Python implementation is also the most simple one with the
least amount of required datastructures.&lt;/p&gt;
&lt;p&gt;The D code potentially suffers from the bigger datastructures and other
bookkeeping that is required to support multi and self arcs.&lt;/p&gt;
&lt;p&gt;The java code implements a whole graph library which might explain some of its
slowness.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>network file transfer to marvell kirkwood</title>
      <link>http://blog.mister-muffin.de/2012/05/19/network-file-transfer-to-marvell-kirkwood/</link>
      <pubDate>Sat, 19 May 2012 01:06:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2012/05/19/network-file-transfer-to-marvell-kirkwood/</guid>
      <description>&lt;p&gt;I have a Seagate GoFlex Net with two 2TB harddrives attached to it via SATA.
The device itself is connected to my PC via its Gigabit Ethernet connection. It
houses a Marvell Kirkwood at 1.2GHz and 128MB. I am booting Debian from a USB
stick connected to its USB 2.0 port.&lt;/p&gt;
&lt;p&gt;The specs are pretty neat so I planned it as my NAS with 4TB of storage being
attached to it. The most common use case is the transfer of big files (1-10 GB)
between my laptop and the device.&lt;/p&gt;
&lt;p&gt;Now what are the common ways to achieve this?&lt;/p&gt;
&lt;p&gt;scp:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;scp /local/path user@goflex:/remote/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;rsync:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rsync -Ph /local/path user@goflex:/remote/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;sshfs:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sshfs -o user@goflex:/remote/path /mnt
cp /local/path /mnt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ssh:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;cat &amp;gt; /remote/path&amp;quot; &amp;lt; /local/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I then did some benchmarks to see how they perform:&lt;/p&gt;
&lt;p&gt;scp: 5.90 MB/s&lt;/p&gt;
&lt;p&gt;rsync: 5.16 MB/s&lt;/p&gt;
&lt;p&gt;sshfs: 5.05 MB/s&lt;/p&gt;
&lt;p&gt;ssh: 5.42 MB/s&lt;/p&gt;
&lt;p&gt;Since they all use ssh for transmission, the similarity of the result does not
come as a surprise and 5.90 MB/s are also not too shabby for a plain scp. It
means that I can transfer 1 GB in a bit under three minutes. I could live with
that.  Even for 10 GB files I would only have to wait for half an hour which is
mostly okay since it is mostly known well in advance that a file is needed.&lt;/p&gt;
&lt;p&gt;As adam points out in the comments, I can also use try using another cipher
than AES for ssh. So I tried to use arcfour and blowfish-cbc.&lt;/p&gt;
&lt;p&gt;scp+arcfour: 12.4MB/s&lt;/p&gt;
&lt;p&gt;scp+blowfish: 9.0MB/s&lt;/p&gt;
&lt;p&gt;But lets see if we can somehow get faster than this. Lets analyze where the
bottleneck is.&lt;/p&gt;
&lt;p&gt;Lets have a look at the effective TCP transfer rate with netcat:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;netcat -l -p 8000 &amp;gt; /dev/null&amp;quot;
dd if=/dev/zero bs=10M count=1000 | netcat goflex 8000
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;79.3 MB/s wow! Can we get more? Lets try increasing the buffer size on both
ends. This can be done using nc6 with the -x argument on both sides.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;netcat -x -l -p 8000 &amp;gt; /dev/null&amp;quot;
dd if=/dev/zero bs=10M count=1000 | netcat -x gloflex 8000
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;103 MB/s okay this is definitely NOT the bottleneck here.&lt;/p&gt;
&lt;p&gt;Lets see how fast I can read from my harddrive:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;hdparm -tT /dev/sda
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;114.86 MB/s.. hmm&amp;hellip; and writing to it?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;time sh -c &#39;dd if=/dev/zero of=/remote/path bs=10M count=100; sync&#39;&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;42.93 MB/s&lt;/p&gt;
&lt;p&gt;Those values are far faster than my puny 5.90 MB/s I get with scp. A look at
the CPU usage during transfer shows, that the ssh process is at 100% CPU usage
the whole time. It seems the bottleneck was found to be ssh and the
encryption/decryption involved.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m transferring directly from my laptop to the device. Not even a switch is in
the middle so encryption seems to be quite pointless here. Even authentication
doesnt seem to be necessary in this setup. So how to make the transfer
unencrypted?&lt;/p&gt;
&lt;p&gt;The ssh protocol specifies a null cipher for not-encrypted connections. OpenSSH
doesnt support this. Supposedly, adding&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{ &amp;quot;none&amp;quot;, SSH_CIPHER_NONE, 8, 0, 0, EVP_enc_null }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to cipher.c adds a null cipher but I didnt want to patch around in my
installation.&lt;/p&gt;
&lt;p&gt;So lets see how a plain netcat performs.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;netcat -l -p 8000 &amp;gt; /remote/path&amp;quot;
netcat goflex 8000 &amp;lt; /local/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;32.9 MB/s This is far better! Lets try a bigger buffer:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;netcat -x -l -p 8000 &amp;gt; /remote/path&amp;quot;
netcat -x goflex 8000 &amp;lt; /local/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;37.8 MB/s now this is far better! My Gigabyte will now take under half a minute
and my 10 GB file under five minutes.&lt;/p&gt;
&lt;p&gt;But it is tedious to copy multiple files or even a whole directory structure
with netcat. There are far better tools for this.&lt;/p&gt;
&lt;p&gt;An obvious candidate that doesnt encrypt is rsync when being used with the
rsync protocol.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rsync -Ph /local/path user@goflex::module/remote/path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;30.96 MB/s which is already much better!&lt;/p&gt;
&lt;p&gt;I used the following line to have the rsync daemon being started by inetd:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rsync stream tcp nowait root /usr/bin/rsync rsyncd --daemon
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it is slower than pure netcat.&lt;/p&gt;
&lt;p&gt;If we want directory trees, then how about netcatting a tarball?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ssh user@goflex &amp;quot;netcat -x -l -p 8000 | tar -C /remote/path -x&amp;quot;
tar -c /local/path | netcat goflex 8000
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;26.2 MB/s so tar seems to add quite the overhead.&lt;/p&gt;
&lt;p&gt;How about ftp then? For this test I installed vsftpd and achieved a speed of
30.13 MB/s. This compares well with rsync.&lt;/p&gt;
&lt;p&gt;I also tried out nfs. Not surprisingly, its transfer rate is up in par with
rsync and ftp at 31.5 MB/s.&lt;/p&gt;
&lt;p&gt;So what did I learn? Lets make a table:&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;method&lt;/th&gt;&lt;th&gt;speed in MB/s&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;scp&lt;/td&gt;&lt;td&gt;5.90&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;rsync+ssh&lt;/td&gt;&lt;td&gt;5.16&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;sshfs&lt;/td&gt;&lt;td&gt;5.05&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;ssh&lt;/td&gt;&lt;td&gt;5.42&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;scp+arcfour&lt;/td&gt;&lt;td&gt;12.4&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;scp+blowfish&lt;/td&gt;&lt;td&gt;9.0&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;netcat&lt;/td&gt;&lt;td&gt;32.9&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;netcat -x&lt;/td&gt;&lt;td&gt;37.8&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;netcat -x | tar&lt;/td&gt;&lt;td&gt;26.2&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;rsync&lt;/td&gt;&lt;td&gt;30.96&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;ftp&lt;/td&gt;&lt;td&gt;30.13&lt;/td&gt;&lt;/dr&gt;
&lt;tr&gt;&lt;td&gt;nfs&lt;/td&gt;&lt;td&gt;31.5&lt;/td&gt;&lt;/dr&gt;
&lt;/table&gt;
&lt;p&gt;For transfer of a directory structure or many small files, unencrypted rsync
seems the way to go. It outperforms a copy over ssh more than five-fold.&lt;/p&gt;
&lt;p&gt;When the convenience of having the remote data mounted locally is needed, nfs
outperforms sshfs at speeds similar to rsync and ftp.&lt;/p&gt;
&lt;p&gt;As rsync and nfs already provide good performance, I didnt look into a more
convenient solution using ftp.&lt;/p&gt;
&lt;p&gt;My policy will now be to use rsync for partial file transfers and mount my
remote files with nfs.&lt;/p&gt;
&lt;p&gt;For transfer of one huge file, netcat is faster. Especially with increased
buffer sizes it is a quarter faster than without.&lt;/p&gt;
&lt;p&gt;But copying a file with netcat is tedious and hence I wrote a script that
simplifies the whole remote-login, listen, send process to one command. First
argument is the local file, second argument is the remote name and path just as
in scp.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/sh -e

HOST=${2%%:*}
USER=${HOST%%@*}
if [ &amp;quot;$HOST&amp;quot; = &amp;quot;$2&amp;quot; -o &amp;quot;$USER&amp;quot; = &amp;quot;$HOST&amp;quot; ]; then
        echo &amp;quot;second argument is not of form user@host:path&amp;quot; &amp;gt;&amp;amp;2
        exit 1
fi
HOST=${HOST#*@}
LPATH=$1
LNAME=`basename &amp;quot;$1&amp;quot;`
RPATH=`printf %q ${2#*:}/$LNAME`

ssh &amp;quot;$USER@$HOST&amp;quot; &amp;quot;nc6 -x -l -p 8000 &amp;gt; $RPATH&amp;quot; &amp;amp;
sleep 1.5
pv &amp;quot;$LPATH&amp;quot; | nc6 -x &amp;quot;$HOST&amp;quot; 8000

wait $!

ssh &amp;quot;$USER@$HOST&amp;quot; &amp;quot;md5sum $RPATH&amp;quot; &amp;amp;
md5sum &amp;quot;$LPATH&amp;quot;

wait $!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I use pv to get a status of the transfer on my local machine and ssh to login
to the remote machine and start netcat in listening mode. After the transfer I
check the md5sum to be sure that everything went fine. This step can of course
be left out but during testing it was useful. Escaping of the arguments is done
with &lt;code&gt;printf %q&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Problems with the above are the sleep, which can not be avoided but must be
there to give the remote some time to start netcat and listen. This is unclean.
A next problem with the above is, that one has to specify a username. Another
is, that in scp, one has to double-escape the argument while above this is not
necessary. The host that it netcats to is the same as the host it ssh&amp;rsquo;s to.
This is not necessarily the case as one can specify an alias in ~/.ssh/config.
Last but not least this only transfers from the local machine to the remote
host. Doing it the other way round is of course possible in the same manner but
then one must be able to tell how the local machine is reachable for the remote
host.&lt;/p&gt;
&lt;p&gt;Due to all those inconveniences I decided not to expand on the above script.&lt;/p&gt;
&lt;p&gt;Plus, rsync and nfs seem to perform well enough for day to day use.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>a periodic counter</title>
      <link>http://blog.mister-muffin.de/2012/05/18/a-periodic-counter/</link>
      <pubDate>Fri, 18 May 2012 20:36:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2012/05/18/a-periodic-counter/</guid>
      <description>&lt;p&gt;tldr: &lt;a href=&#34;https://github.com/josch/periodic&#34;&gt;counting without cumulative timing errors&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Sometimes I want just a small counter, incrementing an integer each second
running somewhere in a terminal. Maybe it is because my wristwatch is in the
bathroom or because I want to do more rewarding things than counting seconds
manually. Maybe I want not only to know how long something takes but also for
how long it already ran in the middle of its execution? There are many reason
why I would want some script that does nothing else than simply counting upward
or downward with some specific frequency.&lt;/p&gt;
&lt;p&gt;Some bonuses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the period should be possible to give as a floating point number and
especially periods of a fraction of a second would be nice&lt;/li&gt;
&lt;li&gt;it should be able to execute an arbitrary program after each period&lt;/li&gt;
&lt;li&gt;it should not matter how long the execution of this program takes for the
overall counting&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now this can not be hard, right? One would probably write this line and be done
with it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;while sleep 1; do echo $i; i=$((i+1)); done
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or to count for a certain number of steps:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for i in `seq 1 100`; do echo $i; sleep 1; done
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would roughly do the job but in each iteration some small offset would be
added and though small, this offset would quickly accumulate.&lt;/p&gt;
&lt;p&gt;Sure that cumulative error is tiny but given that this task seems to be so damn
trivial I couldn&amp;rsquo;t bear anymore with running any of the above but started
looking into a solution.&lt;/p&gt;
&lt;p&gt;Sure I could just quickly hack a small C script that would check
gettimeofday(2) at each iteration and would adjust the time to usleep(3)
accordinly but there HAD to be people before me with the same problem who
already came up with a solution.&lt;/p&gt;
&lt;p&gt;And there was! The solution is the sleepenh(1) program which, when given the
timestamp of its last invocation and the sleep time in floating point seconds,
will sleep for just the right amount to keep the overall frequency stable.&lt;/p&gt;
&lt;p&gt;The author suggests, that sleepenh is to be used in shell scripts that need to
repeat an action in a regular time interval and that is just what I did.&lt;/p&gt;
&lt;p&gt;The result is trivial and simple but does just what I want:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the interval will stay the same on average and the counter will not &amp;ldquo;fall behind&amp;rdquo;&lt;/li&gt;
&lt;li&gt;count upward or downward&lt;/li&gt;
&lt;li&gt;specify interval length as a floating point number of seconds including fractions of one second&lt;/li&gt;
&lt;li&gt;begin to count at given integer and count for a specific number of times or until infinity&lt;/li&gt;
&lt;li&gt;execute a program at every step, optionally by forking it from the script for programs possibly running longer than the given interval&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can check it out and read how to use and what to do with it on github:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/josch/periodic&#34;&gt;https://github.com/josch/periodic&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now lets compare the periodic script with the second example from above:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ time sh -c &#39;for i in `seq 1 1000`; do echo $i; sleep 1; done&#39;
0.08s user 0.12s system 0% cpu 16:41.55 total
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So after only 1000 iterations, the counter is already off by 1.55 seconds. This
means that instead of having run with a frequency of 1.0 Hz, the actual
frequency was 1.00155 Hz. Is it too much to not want this 0.155% of error?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ time ./periodic -c 1000
0.32s user 0.00s system 0% cpu 16:40.00 total
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;1000 iterations took exactly 1000 seconds. Cool.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>announcing img2pdf</title>
      <link>http://blog.mister-muffin.de/2012/03/29/announcing-img2pdf/</link>
      <pubDate>Thu, 29 Mar 2012 11:57:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2012/03/29/announcing-img2pdf/</guid>
      <description>&lt;p&gt;tldr; lossless conversion of JPEG and JPEG2000 files to PDF without significant
increase of filesize: &lt;a href=&#34;https://github.com/josch/img2pdf&#34;&gt;https://github.com/josch/img2pdf&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Since she knew I was able to do all sorts of fancy stuff with pdf on my
computer (inkscape, pdftk, pdflatex, cairo, ghostscript and others), a friend
of mine asked me to convert a JPEG of a scanned document into a PDF for her.
From a homework assignment that I solved as an undergrad I got a fairly good
understanding about the PDF file format and I knew that PDF just uses embedded
JPEG data for images. So I thought it would be easy enough to just wrap her
JPEG with the PDF file structure and be done with it. Surprisingly it turned
out that no tool I knew of (or was able to find on the internets) was able to
do exactly that. Surely tools were able to convert my JPEG to a PDF of equal
size but they were re-encoding the JPEG and hence lead to quality loss. Others
did a lossless conversion but achieved lossless encoding by compressing an RGB
representation of the image using zip/flate encoding (the right way to store
images lossless in pdf) which increased the filesize manyfold. I knew that it
was technically possible to inject her JPEG into a PDF without any byte of the
JPEG changing so I was refusing to accept to either loose quality by
re-encoding her JPEG or to increase the filesize of the 2.8MB JPEG to a 14MB
PDF file. On top of that, imagemagick would take a whopping 27 seconds to
convert just a single JPEG to PDF (using lossless zip compression). This is
completely unacceptable for bigger conversion tasks. Searching on the internet
revealed other people having the same problems and there were some claiming to
sit on huge JPEG2000 scan archive they wanted to lossless convert to PDF so I
decided to support JPEG2000 as well.&lt;/p&gt;
&lt;p&gt;Out came a tool that does exactly what I wanted. It takes image filenames as
commandline arguments. If the image is a JPEG or JPEG2000 file, it will dump
its content into the pdf structure as it is without changing a single byte. If
any other format is found, the normal zip/flate encoding will be used. Thus, in
both cases, the encoding is done lossless while in case of JPEG and JPEG2000
images also much smaller than it would&amp;rsquo;ve been by forcing re-ecoding as
zip/flate. Giving multiple images on the commandline will produce a multipage
pdf.&lt;/p&gt;
&lt;p&gt;More information is available in the README.md&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;git clone git://github.com/josch/img2pdf.git
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Patches are welcome as usual.&lt;/p&gt;
&lt;p&gt;Also tell me if I should&amp;rsquo;ve missed another application that can do the same.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>announcing PyFeRea</title>
      <link>http://blog.mister-muffin.de/2012/03/29/announcing-pyferea/</link>
      <pubDate>Thu, 29 Mar 2012 10:03:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2012/03/29/announcing-pyferea/</guid>
      <description>&lt;p&gt;tldr; An RSS reader without Gnome/KDE dependencies using Python, Gtk, Webkit:
&lt;a href=&#34;https://github.com/josch/pyferea&#34;&gt;https://github.com/josch/pyferea&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;About a year ago I wrote the following
&lt;a href=&#34;http://lists.debian.org/debian-user/2011/07/msg01362.html&#34;&gt;email&lt;/a&gt; to the
debian-user list:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Subject: is there no sane, minimal, graphical RSS feed reader in existance?

Hi,

I&#39;ve been looking for a good RSS feed reader for years now but I still seem not
to be able to find a sane, minimal graphical RSS reader.

What I&#39;m using now is liferea which is okay but could be more minimal and
mainly, is way too slow to enjoy using it (search for the fsync issue).

So what is left?

- There is a bunch of web based readers but I treasure having my stuff offline
  as well.

- There are readers for the gnome or KDE environment. Since I use neither it
  would mean to get 100s of MB (literally) of dependencies

- There are firefox extensions but why would I have to install a web browser to
  read my RSS feeds?

- There are thunderbird, evolution and opera but same argument: why would I
  want to install an email-client/browser for my feeds?

- There are clients like blam that are written in .NET/mono and would also
  require dozens of dependencies (not talking about .NET evilness)

- There are readers for the terminal but I have several feeds with images and I
  dont want to open another window of my browser each time.

I can&#39;t imagine there are no others who do not use Gnome/KDE (having a more
minimal setup) but would want to have a graphical RSS reader?

What I&#39;m looking for is not much: it would just depend on either
gtk/qt/efl/whatever for its UI, would have one list of the feeds, another list
for recent feed items and another frame with a gecko or webkit plugin for
presenting the item. Why this feature/dependency bloat everywhere?

Why is there no simple reader with minimal dependencies? Am I just overlooking
one? Are my requirements too weird? I&#39;m not afraid to compile from source
either, should it not be in Debian. Should I like it I would also package it
for Debian.

As I said, liferea is close (just had to bear with the gconf2 dependency) but
slow as hell (and no, I refuse to use the &amp;quot;fsync workaround&amp;quot;).

Are there others that share my need? If there is really no such thing as a real
minimal graphical RSS reader, I&#39;m close to writing one myself.

Since I&#39;m not subscribed, please dont forget to CC me. Thanks!

cheers, josch
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I pasted this email because it best describes my issue and I would&amp;rsquo;ve just
repeated its content anyways.&lt;/p&gt;
&lt;p&gt;Well apparently I was close enough to writing one myself so that I did it,
thereby announcing PyFeRea, a minimal RSS reader without Gnome or KDE
dependencies, written in Python, using GtK and WebKit and coming without the
LiFeRea slowness.&lt;/p&gt;
&lt;p&gt;I suck at naming things and hence I am still left with my initial &amp;lsquo;codename&amp;rsquo;
for it: PyFeRea as I wanted something that looked just as LiFeRea but would not
suffer from its slowness.&lt;/p&gt;
&lt;p&gt;More can be found in the projects README.md&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;git clone git://github.com/josch/pyferea.git
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Patches are welcome as usual.&lt;/p&gt;
&lt;p&gt;Also tell me if I should&amp;rsquo;ve missed an RSS reader with my requirements in mind.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>OMG WTF PDF</title>
      <link>http://blog.mister-muffin.de/2011/07/15/omg-wtf-pdf/</link>
      <pubDate>Fri, 15 Jul 2011 23:07:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2011/07/15/omg-wtf-pdf/</guid>
      <description>&lt;p&gt;This is the title of a &lt;a href=&#34;http://events.ccc.de/congress/2010/Fahrplan/events/4221.en.html&#34;&gt;great
talk&lt;/a&gt; at the
last chaos communication congress in berlin (27c3).&lt;/p&gt;
&lt;p&gt;When writing my own pdf parser for a homework assignment that I put way too
much ambition into, I encountered all of what is mentioned in that talk and was
also able to realize how bad the situation really is. I just deleted three
paragraphs of this post where I started to rant about how frickin bad the pdf
format is. How unworkable it is and how literally impossible to perfectly
implement. But instead, just &lt;a href=&#34;http://mirror.fem-net.de/CCC/27C3/mp4-h264-HQ/27c3-4221-en-omg_wtf_pdf.mp4&#34;&gt;watch the
video&lt;/a&gt;
of the talk and make sure to remember that it is even worse than Julia Wolf is
able to make clear in 1h she was given.&lt;/p&gt;
&lt;p&gt;So after I stopped myself from spreading another wave of my pdf hate over the
internets lets look at the issue at hand:&lt;/p&gt;
&lt;p&gt;I wanted to sign up online for a new contract with my bank. One of the
requirements was, that I entered a number code that was supposedly only
accessible to me once I printed out a pdf document. You heard me right - the
pdf document only contained a gray box where the number was supposed to be and
only upon printing it, it should reveal itself. I still have no clue how this
is supposed to work, but assume it is some weird javascript (yes pdf can
contain javascript) or proprietary forms extension. Or maybe even flash (yes,
the acrobat reader contains an implementation of flash). Or it might just be
native bytecode (yes, you can put native, platform specific bytecode into a pdf
that the reader will then execute for you - isnt it great?). Needless to say
that no pdf renderer I had at hand (I tried poppler based programs and mupdf)
was able to give me the number - even when trying to print it where the magic
was supposed to happen. So when I was already down to setting up a qemu
instance to install windows xp so that I could install acrobat reader to
finally open the document and print it to another pdf so that I could see that
number, I thought again and wrote some additional code to my pdf parser that
allowed me to investigate that pdf more thoroughly. And indeed, just by chance,
I spotted a number in the annotation area of the document which looked just
like the six digit number I needed. Tried it and voila it worked.&lt;/p&gt;
&lt;p&gt;This is the snippet I uncompressed from the pdf to (just by chance) find the
number I was looking for. The 000000 piece was actually containing the number I
needed.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;6 0 obj
&amp;lt;&amp;lt;
  /DA (/Arial 14 Tf 0 g)
  /Rect [ 243.249 176.784 382.489 210.297 ]
  /FT /Tx
  /MK &amp;lt;&amp;lt;
    /BG [ 0.75 0.75 0.75 ]
  &amp;gt;&amp;gt;
  /Q 1
  /P 4 0 R
  /AP &amp;lt;&amp;lt;
    /N 7 0 R
  &amp;gt;&amp;gt;
  /V (000000)
  /T (Angebotskennnummer)
  /Subtype /Widget
  /Type /Annot
  /F 36
  /Ff 1
&amp;gt;&amp;gt;
endobj
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So let me say: WTF? My bank not only requires me to resort to one specific pdf
implementation (namely the acrobat reader by adobe) but also requires me to pay
to a US based company first to have an operating system that reader software
works on? Or am I really supposed to go through the raw pdf source by hand??
Bleh&amp;hellip;&lt;/p&gt;
&lt;p&gt;Also, dont ask for my code - it&amp;rsquo;s super dirty and unreadable. Instead look at
the mupdf project. It supplies a renderer which is massively superior to
poppler in terms of speed (even suitable for embedded devices) and comes with a
program called pdfclean which does the same thing my program did so that I was
able to get the number I needed.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>timestamp counter</title>
      <link>http://blog.mister-muffin.de/2011/07/15/timestamp-counter/</link>
      <pubDate>Fri, 15 Jul 2011 14:08:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2011/07/15/timestamp-counter/</guid>
      <description>&lt;p&gt;I recently discovered the &lt;a href=&#34;http://en.wikipedia.org/wiki/Time_Stamp_Counter&#34;&gt;timestamp
counter&lt;/a&gt; instruction which
solved a problem where I had to accurately benchmark a very small piece of code
while putting it in a loop made gcc optimize it away with -O3.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; __inline__ &lt;span style=&#34;color:#66d9ef&#34;&gt;unsigned&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;getticks&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#66d9ef&#34;&gt;unsigned&lt;/span&gt; a, d;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#66d9ef&#34;&gt;asm&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;volatile&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;rdtsc&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;=a&amp;#34;&lt;/span&gt; (a), &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;=d&amp;#34;&lt;/span&gt; (d));
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;     &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; ((&lt;span style=&#34;color:#66d9ef&#34;&gt;unsigned&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt;)a) &lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; (((&lt;span style=&#34;color:#66d9ef&#34;&gt;unsigned&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;long&lt;/span&gt;)d) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;32&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;More code for other architectures as well can be found
&lt;a href=&#34;http://www.fftw.org/cycle.h&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When using that piece one has to take care that the code stays on the same
processor, the processor doesnt change its clock speed and the system is not
hibernated/suspended inbetween.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>scriptreplay in javascript</title>
      <link>http://blog.mister-muffin.de/2011/07/05/scriptreplay-in-javascript/</link>
      <pubDate>Tue, 05 Jul 2011 09:19:00 +0000</pubDate>
      
      <guid>http://blog.mister-muffin.de/2011/07/05/scriptreplay-in-javascript/</guid>
      <description>&lt;p&gt;TL;DR:
&lt;a href=&#34;http://mister-muffin.de/scriptreplay/&#34;&gt;http://mister-muffin.de/scriptreplay/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Using terminal applications instead of GUI applications has the definitive
speed advantage of not having to spend some time on moving a cursor to a 2D
coordinate on the screen but instead just doing a 2mm downward motion with one
or two fingers. I wonder if something other than terminal applications will
allow me to interact with my computer faster until the invention of direct
neural interfaces.&lt;/p&gt;
&lt;p&gt;I really like terminal applications - not only because of the speed advantage
but also because they offer much more real-estate in terms of usable screen
space as they dont waste space on stuff like buttons, menu bars or all those
pixels wasted on separators and the space between UI elements. Starting to use
the pentadactyl firefox extension was not only a huge browsing speed
improvement for me but I could also finally use my whole frickin 1920x1080
pixels for viewing the website (well except for a 19 pixels status bar at the
bottom).&lt;/p&gt;
&lt;p&gt;Now lets finally come to the topic of this post: scriptreplay in javascript.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;script(1)&lt;/code&gt; and &lt;code&gt;scriptreplay(1)&lt;/code&gt;, being part of the bsdutils package in
debian/ubuntu and the util-linux package in rpm based distribution will
probably also already be installed on your system and is one of those really
handy tools you did not know of before even though they were always there.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;script&lt;/code&gt; is a program that you can use to capture a terminal session or console
application output whereas &lt;code&gt;scriptreplay&lt;/code&gt; is able to replay that session by
using a timing file that &lt;code&gt;script&lt;/code&gt; is able to output on stderr. Without the
timings file, the typescript will include all terminal interaction and is
readily readable with a text editor or printable or easily uploadable to a
pastebin (no more selecting parts of your terminal window with your mouse and
copy-pasting that into the browser). Without the timing file it is useful to
document a process - for example if you want to show others how a bug happened
on your system or for a homework submission you have to hand in.&lt;/p&gt;
&lt;p&gt;A very powerful feature is the mentioned timingfile you can capture by
redirecting the &lt;code&gt;stderr&lt;/code&gt; output of &lt;code&gt;script&lt;/code&gt; into a file. With the use of
&lt;code&gt;scriptreplay&lt;/code&gt; you can then watch your terminal interaction in real time.&lt;/p&gt;
&lt;p&gt;While it would be kinda tedious to share your typescript and timingfile over a
pastebin so that a party would have to download those manually and use
&lt;code&gt;scriptreplay&lt;/code&gt; to watch them, I imagined a kind of youtube for terminal
sessions. This would also solve the problem of all those youtube videos that
are screen captures of terminal windows - needlessly encoding text as moving
images and by that not only destroying the ability to copy&amp;amp;paste things but
also needlessly increasing the filesize.&lt;/p&gt;
&lt;p&gt;I remembered having seen such a website years ago but dont manage to find it
again. To show a proof of concept I prepared the following website:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://mister-muffin.de/scriptreplay/&#34;&gt;http://mister-muffin.de/scriptreplay/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I will probably never have the time to make a real webservice out of it but
maybe something of this is useful for others.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
