<?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>Valentin's Lab &#187; vaab</title>
	<atom:link href="https://vaab.blog.kal.fr/author/vaab/feed/" rel="self" type="application/rss+xml" />
	<link>https://vaab.blog.kal.fr</link>
	<description>Ratiocination of an opensource techie</description>
	<lastBuildDate>Thu, 15 Nov 2018 08:04:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F&amp;language=en_US&amp;category=text&amp;title=Valentin%27s+Lab&amp;description=Ratiocination+of+an+opensource+techie&amp;tags=blog" type="text/html" />
	<item>
		<title>Recompiling fastboot to integrate reboot-edl command</title>
		<link>https://vaab.blog.kal.fr/2017/10/04/recompiling-fastboot-to-integrate-reboot-edl-command/</link>
		<comments>https://vaab.blog.kal.fr/2017/10/04/recompiling-fastboot-to-integrate-reboot-edl-command/#comments</comments>
		<pubDate>Wed, 04 Oct 2017 17:12:07 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=605</guid>
		<description><![CDATA[Context The fastboot command line is an android tool that allows to interact with an android device when in fastboot mode. I needed to switch from &#34;fastboot mode&#34; toward &#34;EDL mode&#34; (EDL stands for &#34;Emergency Download&#34;). If this is usually &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2017/10/04/recompiling-fastboot-to-integrate-reboot-edl-command/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<div class="section" id="context">
<h3>Context</h3>
<p>The <tt class="docutils literal">fastboot</tt> command line is an android tool that allows to interact
with an android device when in fastboot mode. I needed to switch from &quot;fastboot mode&quot;
toward &quot;EDL mode&quot; (<tt class="docutils literal">EDL</tt> stands for &quot;Emergency Download&quot;).</p>
<p>If this is usually done with the command <tt class="docutils literal">fastboot oem edl</tt>, but on the <cite>Xiaomi RedMi note 3 Pro</cite>, for some reasons, it uses a specific non-standard root command <tt class="docutils literal"><span class="pre">reboot-edl</span></tt>. This is a new occasion for showing why open-source matters, and to recompile <tt class="docutils literal">fastboot</tt> and add the missing command.</p>
</div>
<div class="section" id="compiling">
<h3>Compiling</h3>
<p>The following is for debian related systems. In a previous blog post I explained in detail each command
for a similar hack in <a class="reference external" href="http://vaab.blog.kal.fr/2013/04/04/fixing-empathy-in-ubuntu-12-10/">empathy source code</a>.</p>
<p>This time, I'll give the full transcript without much explanation:</p>
<pre class="literal-block">
mkdir /tmp/fastboot &amp;&amp;
cd /tmp/fastboot &amp;&amp;
apt-get source android-tools-fastboot &lt;/dev/null &amp;&amp;
cd android-tools-*
cat &lt;&lt;EOF | patch -p1 &amp;&amp;
--- a/core/fastboot/fastboot.c
+++ b/core/fastboot/fastboot.c
&#64;&#64; -284,6 +284,7 &#64;&#64; void usage(void)
             &quot;  continue                                 continue with autoboot\n&quot;
             &quot;  reboot                                   reboot device normally\n&quot;
             &quot;  reboot-bootloader                        reboot device into bootloader\n&quot;
+            &quot;  reboot-edl                               reboot device into EDL (RedMi Note 3 Pro)\n&quot;
             &quot;  help                                     show this help message\n&quot;
             &quot;\n&quot;
             &quot;options:\n&quot;
&#64;&#64; -803,6 +804,7 &#64;&#64; int main(int argc, char **argv)
     int wants_wipe = 0;
     int wants_reboot = 0;
     int wants_reboot_bootloader = 0;
+    int wants_reboot_edl = 0;
     int erase_first = 1;
     void *data;
     unsigned sz;
&#64;&#64; -929,6 +931,9 &#64;&#64; int main(int argc, char **argv)
         } else if(!strcmp(*argv, &quot;reboot-bootloader&quot;)) {
             wants_reboot_bootloader = 1;
             skip(1);
+        } else if(!strcmp(*argv, &quot;reboot-edl&quot;)) {
+            wants_reboot_edl = 1;
+            skip(1);
         } else if (!strcmp(*argv, &quot;continue&quot;)) {
             fb_queue_command(&quot;continue&quot;, &quot;resuming boot&quot;);
             skip(1);
&#64;&#64; -1009,6 +1014,8 &#64;&#64; int main(int argc, char **argv)
         fb_queue_reboot();
     } else if (wants_reboot_bootloader) {
         fb_queue_command(&quot;reboot-bootloader&quot;, &quot;rebooting into bootloader&quot;);
+    } else if (wants_reboot_edl) {
+        fb_queue_command(&quot;reboot-edl&quot;, &quot;rebooting into EDL mode&quot;);
     }

     if (fb_queue_is_empty())
EOF

apt-get build-dep android-tools-fastboot -y &lt;/dev/null &amp;&amp;  ## prefix with `sudo` if needed
dpkg-buildpackage -rfakeroot -b &lt;/dev/null &amp;&amp;
cd .. &amp;&amp;
dpkg -i android-tools-fastboot*.deb  ## prefix with `sudo` if needed
</pre>
<p>You should now have a new <tt class="docutils literal">fastboot</tt> command with the <tt class="docutils literal"><span class="pre">reboot-edl</span></tt> command.</p>
<p>Once your <cite>Redmi Note 3 Pro</cite> is in fastboot mode (power it up with <cite>Power</cite> and <cite>Volume-Down</cite> button pressed), you are ready to fire:</p>
<pre class="literal-block">
# fastboot reboot-edl
rebooting into EDL mode...
OKAY [  0.001s]
finished. total time: 0.001s
</pre>
<p>Checking that your device is now in EDL mode could be done with:</p>
<pre class="literal-block">
# lsusb | grep Qualcomm
Bus 001 Device 086: ID 05c6:9008 Qualcomm, Inc. Gobi Wireless Modem (QDL mode)
</pre>
<p>Happy hacking !</p>
</div>
<div class="section" id="more-info">
<h3>More info</h3>
<ul class="simple">
<li><tt class="docutils literal">fastboot</tt> <a class="reference external" href="https://forum.xda-developers.com/redmi-note-3/how-to/guide-reboot-to-edl-mode-fastboot-test-t3398718">pre-compiled for windows</a> on <cite>xda</cite>.</li>
</ul>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=605&amp;md5=117fc76eda61d18190a15caafb5559d0" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2017/10/04/recompiling-fastboot-to-integrate-reboot-edl-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2017%2F10%2F04%2Frecompiling-fastboot-to-integrate-reboot-edl-command%2F&amp;language=en_GB&amp;category=text&amp;title=Recompiling+fastboot+to+integrate+reboot-edl+command&amp;description=Context+The+fastboot+command+line+is+an+android+tool+that+allows+to+interact+with+an+android+device+when+in+fastboot+mode.+I+needed+to+switch+from+%26quot%3Bfastboot+mode%26quot%3B+toward+%26quot%3BEDL...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Fixing Windows Python 2.7 unicode issue with subprocess&#8217;s Popen.</title>
		<link>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/</link>
		<comments>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/#comments</comments>
		<pubDate>Thu, 16 Mar 2017 10:22:49 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=591</guid>
		<description><![CDATA[TL;DR Fixing Windows Python 2.7 unicode issue with subprocess.Popen being unable to properly send command lines to the system. Here is the python code fixing unicode issue Details What Python 2.7 is plagued by bugs that won't be fixed. One &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<div class="section" id="tl-dr">
<h3>TL;DR</h3>
<p>Fixing <a class="reference external" href="http://bugs.python.org/issue19264">Windows Python 2.7 unicode issue</a> with <tt class="docutils literal">subprocess.Popen</tt> being unable to properly
send command lines to the system.</p>
<p>Here is <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a></p>
</div>
<div class="section" id="details">
<h3>Details</h3>
<div class="section" id="what">
<h4>What</h4>
<p>Python 2.7 is plagued by bugs that won't be fixed.</p>
<p>One of them is that <strong>you can't use unicode chars on
sub process call command line</strong> via <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt>
in windows platform.</p>
<p>This code fixes <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> in python 2.7 under windows. This allows your
code simply work on python 2.7 with little changes.</p>
</div>
<div class="section" id="how">
<h4>How</h4>
<p>The following code leverage <tt class="docutils literal">ctypes</tt> to call the <tt class="docutils literal"><span class="pre">CreateProcessW(..)</span></tt>
function of the windows C API. This function should have been used
in cPython 2.7 but wasn't. This is the core reason why Python 2.7
on windows does not support unicode command line.</p>
<p>This is how Python 3.0+ works.</p>
</div>
<div class="section" id="the-code">
<h4>The code</h4>
<p><strong>This code was not thoroughly tested</strong> and I'm posting it on a gist so I
can update it and people can comment if they run into any issues.</p>
<p><strong>So here is</strong> <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a>.</p>
</div>
<div class="section" id="a-test">
<h4>A test ?</h4>
<p>As a test, we'll create a small <tt class="docutils literal">test.py</tt> python file
that you can call with the new <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt> provided
in the Gist.</p>
<p>To be complete, we'll need another fix of python 2.7 unicode support: <a class="reference external" href="http://code.activestate.com/recipes/572200/">recipe to read the arguments in unicode</a>.</p>
<p>Here's the full code for the testing:</p>
<ul class="simple">
<li>with <tt class="docutils literal">subprocess_fix</tt> module being the code in the Gist. Correctly
sending the full unicode command line to the system. This is thus used on the calling side.</li>
<li>And <tt class="docutils literal">commandline_fix</tt> module being the recipe to
decode the current programs unicode command line with a final
added <tt class="docutils literal">sys.argv = win32_unicode_argv()</tt> statement. This is thus used on the called side.</li>
</ul>
<p>The caller side (named <tt class="docutils literal">test.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

from subprocess import PIPE
from subprocess_fix import Popen

def indent(text, chars=&quot; &quot;, first=None):
    if first:
        first_line = text.split(&quot;\n&quot;)[0]
        rest = '\n'.join(text.split(&quot;\n&quot;)[1:])
        return '\n'.join([(first + first_line).rstrip(),
                          indent(rest, chars=chars)])
    return '\n'.join([(chars + line).rstrip()
                      for line in text.split('\n')])

p = Popen(u&quot;python reading.py ć&quot;, shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()

if p.returncode != 0:
    print(&quot;errlvl: %s&quot; % p.returncode)

if err:
    print(&quot;stderr:\n%s&quot; % indent(err, &quot; | &quot;))
if out:
    print(&quot;stdout:\n%s&quot; % indent(out, &quot; | &quot;))
</pre>
<p>The called side (named <tt class="docutils literal">reading.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

import sys

import commandline_fix

print &quot;command line received (repr): %r&quot; % (sys.argv, )
print &quot;command line received (str): %s&quot; % (u&quot; &quot;.join(sys.argv), )
</pre>
<p><tt class="docutils literal">test.py</tt> uses the fixed <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> to call <tt class="docutils literal">reading.py</tt> in a subprocess
and it specifies a unicode character in the command line arguments. <tt class="docutils literal">reading.py</tt> will
simply read the unicode command line and print it to the console.</p>
<p>To run the test:</p>
<pre class="literal-block">
python test.py
</pre>
<p>Notice that depending on your active charset (you can check with <tt class="docutils literal">chcp</tt>),
the console might fail to display the character correctly. You might need
then to do a <tt class="docutils literal">chcp 65001</tt>.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=591&amp;md5=77d3033e88bd60e262aba556bd682af8" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2017%2F03%2F16%2Ffixing-windows-python-2-7-unicode-issue-with-subprocesss-popen%2F&amp;language=en_GB&amp;category=text&amp;title=Fixing+Windows+Python+2.7+unicode+issue+with+subprocess%26%238217%3Bs+Popen.&amp;description=TL%3BDR+Fixing+Windows+Python+2.7+unicode+issue+with+subprocess.Popen+being+unable+to+properly+send+command+lines+to+the+system.+Here+is+the+python+code+fixing+unicode+issue+Details+What+Python...&amp;tags=ctypes%2Cpython%2Cwindows%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Keyboard rate and delay</title>
		<link>https://vaab.blog.kal.fr/2016/10/09/keyboard-rate-and-delay/</link>
		<comments>https://vaab.blog.kal.fr/2016/10/09/keyboard-rate-and-delay/#comments</comments>
		<pubDate>Sun, 09 Oct 2016 10:02:08 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=566</guid>
		<description><![CDATA[Here's how to change the rate and delay of your linux system, in direct console or in X, thanks to the command line. TL;DR You are using X Set: Command: xset r rate DELAY [RATE] DELAY is in milliseconds, and &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2016/10/09/keyboard-rate-and-delay/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Here's how to change the rate and delay of your linux system,
in direct console or in X, thanks to the command line.</p>
<div class="section" id="tl-dr">
<h3>TL;DR</h3>
<div class="section" id="you-are-using-x">
<h4>You are using X</h4>
<ul>
<li><p class="first">Set:</p>
<ul>
<li><p class="first">Command:</p>
<pre class="literal-block">
xset r rate DELAY [RATE]
</pre>
<p>DELAY is in milliseconds, and RATE is in character per seconds.</p>
<ul class="simple">
<li>This <strong>has no effect on direct console</strong>.</li>
</ul>
</li>
<li><p class="first">Example:</p>
<pre class="literal-block">
xset r rate 190 38
</pre>
</li>
<li><p class="first">Reset defaults:</p>
<pre class="literal-block">
xset r rate
</pre>
<p>Default values are a delay of 660ms and a rate of 25 characters per sec.</p>
</li>
</ul>
</li>
<li><p class="first">Query:</p>
<pre class="literal-block">
xset q
</pre>
</li>
</ul>
</div>
<div class="section" id="you-are-in-console">
<h4>You are in console</h4>
<ul>
<li><p class="first">Set:</p>
<ul>
<li><p class="first">Command:</p>
<pre class="literal-block">
kbdrate [-d DELAY] [-r RATE]
</pre>
<p>DELAY is in milliseconds, and RATE is in character per seconds.</p>
<ul class="simple">
<li>You need permission on <tt class="docutils literal">/dev/port</tt>.</li>
<li>This <strong>has no effect on X sessions</strong>.</li>
<li><strong>It affects all direct consoles</strong> (the one you can get thanks to Ctrl-Alt-F1).</li>
<li>It might not work as expected on exotic keyboards.</li>
</ul>
</li>
<li><p class="first">Example:</p>
<pre class="literal-block">
kbdrate -d 190 -r 38
</pre>
</li>
<li><p class="first">Reset defaults:</p>
<pre class="literal-block">
kbdrate
</pre>
<p>Default values are a delay of 250ms and a rate of 11 characters per sec.</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="more-info">
<h3>More info</h3>
<div class="section" id="definitions">
<h4>Definitions</h4>
<dl class="docutils">
<dt>keyboard press delay</dt>
<dd>is the time waited while the key is pressed before
the keyboard issues more character than one.</dd>
<dt>keyboard character rate</dt>
<dd>is the number of character per second once while
you keep the key pressed down and only after you've waited the previous delay.</dd>
</dl>
</div>
<div class="section" id="context">
<h4>Context</h4>
<p>There are roughly two different UI context where you might want to fiddle with
the keyboard's rate and delay: you are in a X session, or in direct console.</p>
<p>If you are in a graphical X session, then it is X. X is listening for scancodes that are
roughly events about keys going up, or keys going downs. So X can implement its own
algorithm to produce characters and can choose its own delay and rate.</p>
<p>If you are in a direct console (Ctrl-Alt-F1 for instance), then it is your BIOS
that is handling the rate through <a class="reference external" href="(https://en.wikipedia.org/wiki/INT_16H)">interrupt 16h</a>. <tt class="docutils literal">kbdrate</tt> uses an <tt class="docutils literal">ioctl</tt> call to change the settings and
this might not work if you have some exotic keyboard. You most note that this will affect
all the consoles of the computer, but this won't have any effect on your X sessions.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=566&amp;md5=6619c4d238375768ee07e6c6ee8f8afe" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2016/10/09/keyboard-rate-and-delay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2016%2F10%2F09%2Fkeyboard-rate-and-delay%2F&amp;language=en_GB&amp;category=text&amp;title=Keyboard+rate+and+delay&amp;description=Here%27s+how+to+change+the+rate+and+delay+of+your+linux+system%2C+in+direct+console+or+in+X%2C+thanks+to+the+command+line.+TL%3BDR+You+are+using+X+Set%3A+Command%3A...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>trapping SIGINT, SIGQUIT in asynchronous tasks</title>
		<link>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/</link>
		<comments>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/#comments</comments>
		<pubDate>Sat, 30 Jul 2016 10:36:31 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[bash signals]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=562</guid>
		<description><![CDATA[Scattered in bash documentation lies the truth about SIGINT and SIGQUIT signals being mysteriously ignored and non-trappable in background processes. This has puzzled many, and will probably continue to do so. Traps Here's my try to unveil the terrible truth. &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Scattered in bash documentation lies the truth about <tt class="docutils literal">SIGINT</tt>
and <tt class="docutils literal">SIGQUIT</tt> signals being mysteriously ignored and
non-trappable in background processes.</p>
<p>This has puzzled many, and will probably continue to do so.</p>
<div class="section" id="traps">
<h3>Traps</h3>
<p>Here's my try to unveil the terrible truth.</p>
<p>Let this script separate all cases and enlighten us:</p>
<pre class="literal-block">
cat &lt;&lt;'EOF' &gt; /tmp/test.sh
#!/bin/bash
traps_show_and_test() {
    local label=&quot;$1&quot;
    echo; echo &quot;TRAPS from $label:&quot;; trap
    trap &quot;echo modified by $label&quot; SIGINT  ## &lt;-- MODIFICATION
    echo &quot;TRAPS from $label (after modification attempt):&quot;; trap
}
export -f traps_show_and_test

traps_show_and_test main
{ traps_show_and_test subshell; }
( traps_show_and_test subparen )
bash -c 'traps_show_and_test bash_subprocess'

traps_show_and_test job &amp; wait
{ bash -c 'traps_show_and_test subshell_job'; } &amp; wait
( bash -c 'traps_show_and_test subparen_job' ) &amp; wait
bash -c 'traps_show_and_test bash_job' &amp; wait

echo
echo FINAL main traps:
trap

EOF
chmod +x /tmp/test.sh
/tmp/test.sh
</pre>
<p>Running the previous script in bash (version is
<tt class="docutils literal"><span class="pre">4.3.46(1)-release</span></tt>) gives the following:</p>
<pre class="literal-block">
TRAPS from main:
TRAPS from main (after modification attempt):
trap -- 'echo modified by main' SIGINT

TRAPS from subshell:
trap -- 'echo modified by main' SIGINT
TRAPS from subshell (after modification attempt):
trap -- 'echo modified by subshell' SIGINT

TRAPS from subparen:
trap -- 'echo modified by subshell' SIGINT
TRAPS from subparen (after modification attempt):
trap -- 'echo modified by subparen' SIGINT

TRAPS from bash_subprocess:
TRAPS from bash_subprocess (after modification attempt):
trap -- 'echo modified by bash_subprocess' SIGINT

TRAPS from job:
trap -- 'echo modified by subshell' SIGINT
TRAPS from job (after modification attempt):
trap -- 'echo modified by job' SIGINT

TRAPS from subshell_job:
TRAPS from subshell_job (after modification attempt):
trap -- 'echo modified by subshell_job' SIGINT

TRAPS from subparen_job:
TRAPS from subparen_job (after modification attempt):
trap -- 'echo modified by subparen_job' SIGINT

TRAPS from bash_job:
trap -- '' SIGINT
trap -- '' SIGQUIT
TRAPS from bash_job (after modification attempt):
trap -- '' SIGINT
trap -- '' SIGQUIT

FINAL main traps:
trap -- 'echo modified by subshell' SIGINT
</pre>
<p>Let's highlight important points:</p>
<ul class="simple">
<li>Foreground Jobs<ul>
<li>subshells from <tt class="docutils literal"><span class="pre">{..}</span></tt> and <tt class="docutils literal"><span class="pre">(..)</span></tt> inherit their traps from the main scope.</li>
<li>only <tt class="docutils literal"><span class="pre">{..}</span></tt> can modify the main scope.</li>
<li>subprocesses do not inherit its trap from its parent process</li>
<li>they all can modify at least locally their traps</li>
</ul>
</li>
<li>Background Jobs<ul>
<li>bash function inherits its traps from main scope but can't modify it (like <tt class="docutils literal"><span class="pre">(..)</span></tt>)</li>
<li>processes in <tt class="docutils literal"><span class="pre">{..}</span> &amp;</tt> and <tt class="docutils literal"><span class="pre">(..)</span> &amp;</tt> do not inherit traps</li>
<li>direct background subprocess have default signed traps that are umodifiable</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="bash-documentation">
<h3>Bash documentation</h3>
<p>As a reference, here are the documentation from bash related to these behaviors:</p>
<p>Process group id effect on background process (in <em>Job Control</em> section of doc):</p>
<blockquote>
[...] processes whose process group ID is equal to the current terminal
process group ID [..] receive keyboard-generated signals such as
SIGINT.  These processes are said  to  be  in the  foreground.
<strong>Background processes</strong> are those whose process group ID  differs from
the terminal's; such processes <strong>are immune to keyboard-generated
signals</strong>.</blockquote>
<p>Default handler for <tt class="docutils literal">SIGINT</tt> and <tt class="docutils literal">SIGQUIT</tt> (in <em>Signals</em> section of doc):</p>
<blockquote>
Non-builtin commands run by bash have signal handlers set to
the values inherited by the shell from its parent.  When job
control is not in effect, <strong>asynchronous commands ignore SIGINT
and SIGQUIT</strong> in addition to these inherited handlers.</blockquote>
<p>and about modification of traps (in <tt class="docutils literal">trap</tt> builtin doc):</p>
<blockquote>
<strong>Signals  ignored upon entry to the shell cannot be trapped or reset</strong>.</blockquote>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=562&amp;md5=880d404889c09219cc780ffed2083da5" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2016/07/30/trapping-sigint-sigquit-in-asynchronous-tasks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2016%2F07%2F30%2Ftrapping-sigint-sigquit-in-asynchronous-tasks%2F&amp;language=en_GB&amp;category=text&amp;title=trapping+SIGINT%2C+SIGQUIT+in+asynchronous+tasks&amp;description=Scattered+in+bash+documentation+lies+the+truth+about+SIGINT+and+SIGQUIT+signals+being+mysteriously+ignored+and+non-trappable+in+background+processes.+This+has+puzzled+many%2C+and+will+probably+continue+to+do...&amp;tags=bash+signals%2Cblog" type="text/html" />
	</item>
		<item>
		<title>ecryptfs made straightforward</title>
		<link>https://vaab.blog.kal.fr/2016/01/30/ecryptfs-made-straightforward/</link>
		<comments>https://vaab.blog.kal.fr/2016/01/30/ecryptfs-made-straightforward/#comments</comments>
		<pubDate>Sat, 30 Jan 2016 06:59:41 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=558</guid>
		<description><![CDATA[Using ecryptfs outside of Ubuntu's very controlled environment can be tedious. I wanted to have a fully automated script that could simply mount an ecryptfs partition given a single key (roughly along these lines): ecryptfs-mount PATH KEY Here are the &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2016/01/30/ecryptfs-made-straightforward/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Using <tt class="docutils literal">ecryptfs</tt> outside of Ubuntu's very controlled environment can
be tedious.</p>
<p>I wanted to have a fully automated script that could simply mount an
<tt class="docutils literal">ecryptfs</tt> partition given a single key (roughly along these lines):</p>
<pre class="literal-block">
ecryptfs-mount PATH KEY
</pre>
<p>Here are the main objective:</p>
<ul class="simple">
<li>non-interactive</li>
<li>mount the encrypted partition on any host</li>
</ul>
<p>An important point, is that we don't try to protect against
compromission of the main host's root access. We just try to produce
an encrypted filesystems that we can send externally (with <tt class="docutils literal">rsync</tt>
for example) safely. The host will have the key accessible by root
anyway.</p>
<p>As I didn't have much time, I might have missed a simpler alternative
so feel free to comment on this please. My knowledge on <tt class="docutils literal">ecryptfs</tt>
and security in general is very limited.</p>
<p>You'll find a breakdown of the process first and then a
<a class="reference external" href="https://github.com/0k/ecryptfs-mount">ecryptfs-mount script</a> that support copy paste and will make all
this easier.</p>
<div class="section" id="requirements">
<h3>Requirements</h3>
<p>You'll need to install <tt class="docutils literal"><span class="pre">ecryptfs-utils</span></tt> which provides a few scripts
that can be alleviated to achieve our goals.</p>
</div>
<div class="section" id="overview">
<h3>Overview</h3>
<p>As <tt class="docutils literal">ecryptfs</tt> seems to require the usage of kernel key rings, we'll
convolutedly insert the key in the kernel keyring prior the mounting.</p>
</div>
<div class="section" id="breakdown">
<h3>Breakdown</h3>
<p>Say your key is stored in the <tt class="docutils literal">$KEY</tt> environment variable:</p>
<pre class="literal-block">
$ tmp_wrapped_file=/tmp/wrapped-file
$ salt=ANYTHING
$ echo -en &quot;$KEY\n$salt&quot; | ecryptfs-wrap-passphrase &quot;$tmp_wrapped_file&quot;
</pre>
<p>We have to create this 'wrapped' passphrase file before insertion in keyring:</p>
<pre class="literal-block">
$ echo -n &quot;$salt&quot; | ecryptfs-insert-wrapped-passphrase-into-keyring &quot;$tmp_wrapped_file&quot;
</pre>
<p>This last commands outputs your key signature (here 9f3193d42b4df62):</p>
<pre class="literal-block">
Inserted auth tok with sig [9f3193d42b4df62] into the user session keyring
</pre>
<p>Let's say that you store this one in <tt class="docutils literal">$SIG</tt> and that
<tt class="docutils literal">$PATH_TO_CRYPT</tt> the path to the folder you want to mount with
<tt class="docutils literal">ecryptfs</tt>:</p>
<pre class="literal-block">
mount -i -t ecryptfs $PATH_TO_CRYPT $PATH_TO_CRYPT \
      -o ecryptfs_enable_filename_crypto=y,ecryptfs_passthrough=n,ecryptfs_key_bytes=16,ecryptfs_cipher=aes,ecryptfs_sig=$SIG,ecryptfs_fnek_sig=$SIG,ecryptfs_unlink_sigs=y
</pre>
<p>In the given options, you have:</p>
<ul class="simple">
<li>the four first are to be chosen to your convenience. Go get some
general information to get the options you'd like. (These are
<tt class="docutils literal">ecryptfs_enable_filename_crypto</tt>, <tt class="docutils literal">ecryptfs_passthrough</tt>,
<tt class="docutils literal">ecryptfs_key_bytes</tt>, <tt class="docutils literal">ecryptfs_cipher</tt>)</li>
<li>the three remaining are part of the circumvolution needed to make
this work:<ul>
<li>Both <tt class="docutils literal">ecryptfs_sig</tt>, <tt class="docutils literal">ecryptfs_fnek_sig</tt> are the ids of the
key in the kernel keyring.</li>
<li>At last the <tt class="docutils literal">ecryptfs_unlink_sigs=y</tt> will unregister the key
in the kernel.</li>
</ul>
</li>
</ul>
<p>And notice the <tt class="docutils literal"><span class="pre">-i</span></tt> option to mount that bypass the &quot;helpers&quot; of
<tt class="docutils literal"><span class="pre">ecryptfs-utils</span></tt>.</p>
</div>
<div class="section" id="actual-script">
<h3>Actual script</h3>
<p>I'm providing a <a class="reference external" href="https://github.com/0k/ecryptfs-mount">ecryptfs-mount script</a> on github. It's straight forward
to use:</p>
<pre class="literal-block">
ecryptfs-mount PATH KEY
</pre>
<p>Don't forget to <tt class="docutils literal">umount</tt> if finished.</p>
</div>
<div class="section" id="links">
<h3>Links</h3>
<ul class="simple">
<li><a class="reference external" href="https://wiki.archlinux.org/index.php/ECryptfs#Manual_setup">https://wiki.archlinux.org/index.php/ECryptfs#Manual_setup</a></li>
<li><a class="reference external" href="https://www.kernel.org/doc/Documentation/security/keys-ecryptfs.txt">https://www.kernel.org/doc/Documentation/security/keys-ecryptfs.txt</a></li>
</ul>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=558&amp;md5=069e4ea17137b99d3ada7f25424095ce" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2016/01/30/ecryptfs-made-straightforward/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2016%2F01%2F30%2Fecryptfs-made-straightforward%2F&amp;language=en_GB&amp;category=text&amp;title=ecryptfs+made+straightforward&amp;description=Using+ecryptfs+outside+of+Ubuntu%27s+very+controlled+environment+can+be+tedious.+I+wanted+to+have+a+fully+automated+script+that+could+simply+mount+an+ecryptfs+partition+given+a+single+key...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>docker update, or how to incrementally build images without Dockerfile.</title>
		<link>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/</link>
		<comments>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/#comments</comments>
		<pubDate>Wed, 28 Jan 2015 02:20:56 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[admin]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[incremental]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=546</guid>
		<description><![CDATA[This handy little script covers an important missing feature in current docker's tool set. I needed to build incrementally my images: From a given image, I want full script control (un-tamed shell scripts) and access to hosts files to do &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>This handy little script covers an important missing feature in
current docker's tool set.</p>
<p>I needed to build incrementally my images:</p>
<p>From a given image, I want full script control (un-tamed shell
scripts) and access to hosts files to do whatever I want to
do. At the end, I need to commit the filesystem (and only the filesystem).</p>
<p><tt class="docutils literal"><span class="pre">docker-update</span></tt> manage all this:</p>
<pre class="literal-block">
docker-update my-docker-image -v /srv/files:/mnt/files &lt;&lt;EOF

## Full fledged shell script, will run in docker container
## have access to /mnt/files

cp /mnt/files/data /opt/myapp/data

...

EOF
</pre>
<p>A last thing about the <tt class="docutils literal"><span class="pre">docker-update</span></tt> script, is that it
tries to be clever (I know that's often the beginning of hell):
if you apply the same code to the same image ID, it won't execute
it, but will use the previous result instead.</p>
<p>You can disable the cache by inserting <tt class="docutils literal"># docker: ALWAYS</tt> in
your bash code.</p>
<p>You can look at the <a class="reference external" href="https://gist.github.com/vaab/89d710f452b1d4fc7912">source code of docker-update</a>, or directly
<a class="reference external" href="https://gist.githubusercontent.com/vaab/89d710f452b1d4fc7912/raw/7385874aba683250e873d56e4912b940f9c41be4/docker-update">download docker-update</a>.</p>
<div class="section" id="how-does-it-work">
<h3>How does it work</h3>
<p>It use <tt class="docutils literal">docker run <span class="pre">--entrypoint</span> /bin/bash IMAGE</tt> on the bash
script, then <tt class="docutils literal">docker commit</tt>.</p>
<p>Well that's the general
outline. In the detail, <tt class="docutils literal">docker</tt> <a class="reference external" href="https://github.com/docker/docker/issues/4362">current shortcomings</a> are making this idea much more
complex than it should.</p>
<p>The first <tt class="docutils literal">docker run</tt> will modify ENTRYPOINT and CMD of image,
so when you commit, you break your previous values. So
<tt class="docutils literal"><span class="pre">docker-update</span></tt> needs to save them, and push another commit to
set them back.</p>
</div>
<div class="section" id="docker-update-shortcomings">
<h3>docker-update shortcomings</h3>
<p>As a quick and dirty solution, this script has some shortcomings
and sharp corners.  Some of these could easily repaired, and
additions are welcome.</p>
<div class="section" id="nul-characters-in-your-code">
<h4>NUL characters in your code</h4>
<p>Any <tt class="docutils literal">NUL</tt> character from your bash script will be removed prior to be run.
That's a direct consequence of running <tt class="docutils literal">bash <span class="pre">-c</span> &quot;$code&quot;</tt>. There are probably
better ways to do this.</p>
</div>
<div class="section" id="complex-cmd-and-entrypoint">
<h4>Complex CMD and ENTRYPOINT</h4>
<p>I rely on the fact that the output format of <tt class="docutils literal">{{json .Config.Cmd}}</tt> templating system
is directly evaluable in the Dockerfile as arguments to CMD and ENTRYPOINT.</p>
<p>I'm pretty sure that this is bad. But it works in simple cases.</p>
</div>
<div class="section" id="caching">
<h4>Caching</h4>
<p>Remember that by default, your code will be checked wether it was already
run on the same image (identified by it's ID). If you don't want that to
happen, include <tt class="docutils literal"># docker: ALWAYS</tt>.</p>
<p>For instance:</p>
<pre class="literal-block">
docker-update MYIMAGE &lt;&lt;EOF
# docker: always
apt-get update &amp;&amp; apt-get upgrade -y
EOF
</pre>
</div>
<div class="section" id="docker-update-options">
<h4>docker-update options</h4>
<p>They are sent directly to <tt class="docutils literal">docker run</tt> command, and you can easily break
the command. It's primarily meant to be used with the <tt class="docutils literal"><span class="pre">-v</span></tt> option.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=546&amp;md5=c2d6b5482df2e9d72fed4fb6d079f837" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2015/01/28/docker-update-or-how-to-incrementally-build-images-without-dockerfile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2015%2F01%2F28%2Fdocker-update-or-how-to-incrementally-build-images-without-dockerfile%2F&amp;language=en_GB&amp;category=text&amp;title=docker+update%2C+or+how+to+incrementally+build+images+without+Dockerfile.&amp;description=This+handy+little+script+covers+an+important+missing+feature+in+current+docker%27s+tool+set.+I+needed+to+build+incrementally+my+images%3A+From+a+given+image%2C+I+want+full+script+control...&amp;tags=docker%2Cimage%2Cincremental%2Cblog" type="text/html" />
	</item>
		<item>
		<title>bash lore: how to properly parse NUL separated fields</title>
		<link>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/</link>
		<comments>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/#comments</comments>
		<pubDate>Sat, 03 Jan 2015 09:26:28 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[nul]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=513</guid>
		<description><![CDATA[As a lot of other part of bash, this is black magic. Lets suppose a friendly command that spits out NUL separated fields (as find -print0, shyaml get-values-0, ...). Which - may I insist - is the recommended way to &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>As a lot of other part of bash, this is black magic.</p>
<p>Lets suppose a friendly command that spits out <tt class="docutils literal">NUL</tt> separated fields (as <tt class="docutils literal">find <span class="pre">-print0</span></tt>, <tt class="docutils literal">shyaml <span class="pre">get-values-0</span></tt>, ...). Which - may I insist - is the recommended way to communicate wild binary data in a solid way in bash.</p>
<p>How would you parse adequately each individual records by paquets ?</p>
<p>For the purpose of demonstration, lets use the fixed content of a simple <tt class="docutils literal">data.bin</tt> file
as our <tt class="docutils literal">NUL</tt>-separated input:</p>
<pre class="literal-block">
cat &lt;&lt;EOF | tr : &quot;\000&quot; &gt; /tmp/data.bin
a:1:b:2 3:c:4
  5:d:6\n7:e::f:9
EOF
</pre>
<p>Let's verify that we have our <tt class="docutils literal">NUL</tt> bytes:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | hexdump -v -e '/1 &quot;%02X &quot;'
61 00 31 00 62 00 32 20 33 00 63 00 34 0A 20 20 35 00 64 00 36 5C 6E 37 00 65 00 00 66 00 39 0A
</pre>
<p>You have noticed that we have some values containing:</p>
<blockquote>
<ul class="simple">
<li>spaces (hex: <tt class="docutils literal">20</tt>),</li>
<li>line breaks (hex: <tt class="docutils literal">0A</tt>),</li>
<li>a <tt class="docutils literal">\</tt> followed by a <tt class="docutils literal">n</tt>.</li>
<li>a 0 sized value</li>
<li>a final value <tt class="docutils literal">9</tt> ending with a <tt class="docutils literal">0a</tt> and no final <tt class="docutils literal">00</tt>.</li>
</ul>
</blockquote>
<p>If using NUL separated fields is recommended, it's to support this kind of data.</p>
<p>I want the implementation of a function <tt class="docutils literal"><span class="pre">read-0</span></tt> that would allow this type of interaction:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while read-0 f1 f2; do
    echo &quot;f1: '$f1', f2: '$f2'&quot;
  done
f1: 'a', f2: '1'
f1: 'b', f2: '2 3'
f1: 'c', f2: '4
  5'
f1: 'd', f2: '6\n7'
f1: 'e', f2: ''
f1: 'f', f2: '9
'
</pre>
<div class="section" id="first-try">
<h3>First try</h3>
<p>Let's be naive, and we'll use <tt class="docutils literal">read f1 f2</tt>:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while read f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
f1: 'a1b2', f2: '3c4'
f1: '5d6n7ef9', f2: ''
</pre>
<p>You can notice that:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal">NUL</tt> char where ignored for field separation</li>
<li>fields where separated upon <strong>consecutive</strong> space or return, it uses value stored in <tt class="docutils literal">IFS</tt> environment variable.</li>
<li>their are only 2 lines because the <tt class="docutils literal">\n</tt> was used to separate each record. We should use <tt class="docutils literal"><span class="pre">-d</span></tt> to specify the line delimiter.</li>
<li>Note that the <tt class="docutils literal">NUL</tt> chars are also extracted out of the data as variables don't support the NUL char.</li>
<li>The <tt class="docutils literal">\</tt> was eaten, because <tt class="docutils literal">read</tt> builtin parse and give it special meaning. We should use <tt class="docutils literal"><span class="pre">-r</span></tt> to avoid that.</li>
</ul>
</blockquote>
<p>But how should we provide the <tt class="docutils literal">NUL</tt> delimiter to the read builtin ? knowing that you can't put <tt class="docutils literal">NUL</tt> chars on the command line ? Hopefully I stumbled onto this blog post: <a class="reference external" href="http://transnum.blogspot.sg/2008/11/bashs-read-built-in-supports-0-as.html">http://transnum.blogspot.sg/2008/11/bashs-read-built-in-supports-0-as.html</a></p>
<p>Conclusion is that <tt class="docutils literal"><span class="pre">-d</span> ''</tt> should be understood magically by bash <tt class="docutils literal">read</tt> builtin to delimit lines with <tt class="docutils literal">NUL</tt>
characters.</p>
</div>
<div class="section" id="better-try">
<h3>Better try</h3>
<p>Let's apply our new acquired knowledge by trying <tt class="docutils literal"><span class="pre">IFS=$'\0'</span> read <span class="pre">-d</span> '' <span class="pre">-r</span> f1 f2</tt>:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | while IFS=$'\0' read -d '' -r f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
f1: 'a', f2: ''
f1: '1', f2: ''
f1: 'b', f2: ''
f1: '2 3', f2: ''
f1: 'c', f2: ''
f1: '4
  5', f2: ''
f1: 'd', f2: ''
f1: '6\n7', f2: ''
f1: 'e', f2: ''
f1: '', f2: ''
f1: 'f', f2: ''
</pre>
<p>That's much better. But notice that:</p>
<blockquote>
<ul class="simple">
<li>we didn't get anything in <tt class="docutils literal">$f2</tt>, that's normal: by specifying <tt class="docutils literal">NUL</tt> as line delimiter (with <tt class="docutils literal"><span class="pre">-d</span> ''</tt>) and having NUL as field delimiter (<tt class="docutils literal">IFS</tt>) we will be doomed to have one field per record. We will need to manage the repacking in a <tt class="docutils literal">while</tt> loop. This doesn't sound too difficult.</li>
<li>where's the final field <tt class="docutils literal">0A</tt> ? Hum, as there is no <tt class="docutils literal">NUL</tt> final character in the data, <tt class="docutils literal">read</tt> returned errlvl 1 on this last field but filled correctly the variable. A simple <tt class="docutils literal">echo $f1</tt> prints <tt class="docutils literal">9</tt> (if you use this form: <tt class="docutils literal">while <span class="pre">IFS=''</span> read <span class="pre">-d</span> '' <span class="pre">-r</span> f1 f2; do echo &quot;f1: '$f1', f2: <span class="pre">'$f2'&quot;;</span> done &lt; /tmp/data.txt</tt> to access variables of the <tt class="docutils literal">while</tt>).</li>
</ul>
</blockquote>
</div>
<div class="section" id="final-implementation">
<h3>Final Implementation ?</h3>
<p>So knowing this, here is the final implemetation of <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
read-0() {
    local eof
    eof=
    while [ &quot;$1&quot; -a -z &quot;$eof&quot; ]; do
        IFS='' read -r -d '' &quot;$1&quot; || eof=true
        shift
    done
    test &quot;$eof&quot; != true -o -z &quot;$1&quot;
}
</pre>
<p>Final ? It surely properly works for our specification test. But what happens if <tt class="docutils literal">EOF</tt> happens before
we have fed all the variables ?:</p>
<pre class="literal-block">
$ echo -n &quot;a&quot; | while read-0 f1 f2; do echo &quot;f1: '$f1', f2: '$f2'&quot;; done
$
</pre>
<p>Nothing is spit out, despite the fact that we have sent a character.</p>
<p>This is now a specification issue: Do we want <tt class="docutils literal"><span class="pre">read-0</span></tt> to return errorlevel 0 when it hits <tt class="docutils literal">EOF</tt>
while filing the variables ? Okay, but we said 0-sized string was a possible value... <tt class="docutils literal"><span class="pre">read-0</span></tt> in the
current specification knows it hit <tt class="docutils literal">EOF</tt> while filling variables as your first variable can be the
0-sized string. We could make a special case, but I want to be able to distinguish a last empty element from an element.</p>
<p>That <tt class="docutils literal"><span class="pre">read-0</span></tt>, in the actual specification, can't do it. But we can offer a slight change in the way you build your while loop to allow that parsing.</p>
</div>
<div class="section" id="correct-implementation">
<h3>Correct Implementation</h3>
<p>To fill partial records, will need another specification change as current implementation will fail whenever it encounters EOF. This is an incompatible specification issue. Aside from this, we need also to take care to actually set the value of the remaining fields to the empty string. This will require to use another version of <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
read-0() {
    local eof
    eof=
    while [ &quot;$1&quot; ]; do
        IFS='' read -r -d '' -- &quot;$1&quot; || eof=true
        shift
    done
    test &quot;$eof&quot; != true
}
</pre>
<p>So this would work with <tt class="docutils literal"><span class="pre">read-0</span></tt>:</p>
<pre class="literal-block">
$ echo -n a | tr :  '\000' | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: ''

$ echo -n a: | tr :  '\000' | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: ''
</pre>
<p>Basically, this construct allows a last round in the loop after detecting EOF... and achieve the starting spec:</p>
<pre class="literal-block">
$ cat /tmp/data.bin | {  eof= ; while [ -z $eof ]; do read-0 f1 f2 || eof=true; echo &quot;f1: '$f1', f2: '$f2'&quot;; done  }
f1: 'a', f2: '1'
f1: 'b', f2: '2 3'
f1: 'c', f2: '4
  5'
f1: 'd', f2: '6\n7'
f1: 'e', f2: ''
f1: 'f', f2: '9
'
</pre>
<p>Trivial ?</p>
<p>Happy hacking.</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=513&amp;md5=57730888592c27e128ffadf2e707ab79" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2015/01/03/bash-lore-how-to-properly-parse-nul-separated-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2015%2F01%2F03%2Fbash-lore-how-to-properly-parse-nul-separated-fields%2F&amp;language=en_GB&amp;category=text&amp;title=bash+lore%3A+how+to+properly+parse+NUL+separated+fields&amp;description=As+a+lot+of+other+part+of+bash%2C+this+is+black+magic.+Lets+suppose+a+friendly+command+that+spits+out+NUL+separated+fields+%28as+find+-print0%2C+shyaml+get-values-0%2C+...%29.+Which...&amp;tags=bash%2Cnul%2Cblog" type="text/html" />
	</item>
		<item>
		<title>bash lore: command substitution and final new lines</title>
		<link>https://vaab.blog.kal.fr/2014/05/07/bash-lore-command-substitution-and-final-new-lines/</link>
		<comments>https://vaab.blog.kal.fr/2014/05/07/bash-lore-command-substitution-and-final-new-lines/#comments</comments>
		<pubDate>Wed, 07 May 2014 12:38:43 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[tip]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=500</guid>
		<description><![CDATA[Be wary that command substitution will remove all final new lines. Command substitution are $(command ..) construct or older `command ..` one that will be substitued by the standard output of the given command. But that's incorrect: a proper definition &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2014/05/07/bash-lore-command-substitution-and-final-new-lines/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Be wary that command substitution will remove all final new lines.</p>
<p>Command substitution are <tt class="docutils literal">$(command ..)</tt> construct or older <tt class="docutils literal">`command ..`</tt> one that will be substitued by
the standard output of the given command.</p>
<p>But that's incorrect: a proper definition would have been: It will be substitued by
the standard output <strong>minus any final new lines</strong>. For example:</p>
<pre class="literal-block">
$ value_w_final_dot=&quot;$(echo -en &quot;hello\n\n\n.&quot;)&quot;
$ value_wo_final_dot=&quot;$(echo -en &quot;hello\n\n\n&quot;)&quot;
$ echo &quot;My first value: &lt;&lt;$value_w_final_dot&gt;&gt;&quot;
My first value: &lt;&lt;hello


.&gt;&gt;
$ echo &quot;My second value: &lt;&lt;$value_wo_final_dot&gt;&gt;&quot;
My second value: &lt;&lt;hello&gt;&gt;
</pre>
<p>When you are bitten by these types of &quot;features&quot;, don't you value more
the python motto &quot;explicit is better than implicit&quot; ?.</p>
<p>This chopping occurs at command substitution time. You can definitively store
ending new lines in a bash variable, in command parameters, and send them of course
over a pipe, just avoid command substitution:</p>
<pre class="literal-block">
$ value_wo_final_dot=&quot;hello&quot;$'\n\n\n'
$ echo &quot;My corrected value: &lt;&lt;$value_wo_final_dot&gt;&gt;&quot;
My first value: &lt;&lt;hello


&gt;&gt;
$ /bin/echo &quot;As system process argument: &quot; &quot;$value_wo_final_dot&quot;
As system process argument: hello



$
</pre>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=500&amp;md5=90e9a0a394f4fc7896f82befc435741c" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2014/05/07/bash-lore-command-substitution-and-final-new-lines/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2014%2F05%2F07%2Fbash-lore-command-substitution-and-final-new-lines%2F&amp;language=en_GB&amp;category=text&amp;title=bash+lore%3A+command+substitution+and+final+new+lines&amp;description=Be+wary+that+command+substitution+will+remove+all+final+new+lines.+Command+substitution+are+%24%28command+..%29+construct+or+older+%60command+..%60+one+that+will+be+substitued+by+the+standard+output...&amp;tags=bash%2Cblog" type="text/html" />
	</item>
		<item>
		<title>bash lore: NUL character and variables</title>
		<link>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/</link>
		<comments>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/#comments</comments>
		<pubDate>Sat, 03 May 2014 10:29:27 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=489</guid>
		<description><![CDATA[Bash variables are often thought as being able to store any binary content. Please bear in mind that it can't store NUL character, and only this one. Here you are: $ ascii_table() { echo -en &#34;$(echo '\'0{0..3}{0..7}{0..7} &#124; tr -d &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Bash variables are often thought as being able to store any binary content.</p>
<p>Please bear in mind that it can't store <tt class="docutils literal">NUL</tt> character, and only this one.</p>
<p>Here you are:</p>
<pre class="literal-block">
$ ascii_table() { echo -en &quot;$(echo '\'0{0..3}{0..7}{0..7} | tr -d &quot; &quot;)&quot;; }
$ ascii_table | hd
00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010  10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f  |................|
00000020  20 21 22 23 24 25 26 27  28 29 2a 2b 2c 2d 2e 2f  | !&quot;#$%&amp;'()*+,-./|
00000030  30 31 32 33 34 35 36 37  38 39 3a 3b 3c 3d 3e 3f  |0123456789:;&lt;=&gt;?|
00000040  40 41 42 43 44 45 46 47  48 49 4a 4b 4c 4d 4e 4f  |&#64;ABCDEFGHIJKLMNO|
00000050  50 51 52 53 54 55 56 57  58 59 5a 5b 5c 5d 5e 5f  |PQRSTUVWXYZ[\]^_|
00000060  60 61 62 63 64 65 66 67  68 69 6a 6b 6c 6d 6e 6f  |`abcdefghijklmno|
00000070  70 71 72 73 74 75 76 77  78 79 7a 7b 7c 7d 7e 7f  |pqrstuvwxyz{|}~.|
00000080  80 81 82 83 84 85 86 87  88 89 8a 8b 8c 8d 8e 8f  |................|
00000090  90 91 92 93 94 95 96 97  98 99 9a 9b 9c 9d 9e 9f  |................|
000000a0  a0 a1 a2 a3 a4 a5 a6 a7  a8 a9 aa ab ac ad ae af  |................|
000000b0  b0 b1 b2 b3 b4 b5 b6 b7  b8 b9 ba bb bc bd be bf  |................|
000000c0  c0 c1 c2 c3 c4 c5 c6 c7  c8 c9 ca cb cc cd ce cf  |................|
000000d0  d0 d1 d2 d3 d4 d5 d6 d7  d8 d9 da db dc dd de df  |................|
000000e0  e0 e1 e2 e3 e4 e5 e6 e7  e8 e9 ea eb ec ed ee ef  |................|
000000f0  f0 f1 f2 f3 f4 f5 f6 f7  f8 f9 fa fb fc fd fe ff  |................|
00000100
</pre>
<p>But:</p>
<pre class="literal-block">
$ echo -n &quot;$(ascii_table)&quot; | hd
00000000  01 02 03 04 05 06 07 08  09 0a 0b 0c 0d 0e 0f 10  |................|
00000010  11 12 13 14 15 16 17 18  19 1a 1b 1c 1d 1e 1f 20  |............... |
00000020  21 22 23 24 25 26 27 28  29 2a 2b 2c 2d 2e 2f 30  |!&quot;#$%&amp;'()*+,-./0|
00000030  31 32 33 34 35 36 37 38  39 3a 3b 3c 3d 3e 3f 40  |123456789:;&lt;=&gt;?&#64;|
00000040  41 42 43 44 45 46 47 48  49 4a 4b 4c 4d 4e 4f 50  |ABCDEFGHIJKLMNOP|
00000050  51 52 53 54 55 56 57 58  59 5a 5b 5c 5d 5e 5f 60  |QRSTUVWXYZ[\]^_`|
00000060  61 62 63 64 65 66 67 68  69 6a 6b 6c 6d 6e 6f 70  |abcdefghijklmnop|
00000070  71 72 73 74 75 76 77 78  79 7a 7b 7c 7d 7e 7f 80  |qrstuvwxyz{|}~..|
00000080  81 82 83 84 85 86 87 88  89 8a 8b 8c 8d 8e 8f 90  |................|
00000090  91 92 93 94 95 96 97 98  99 9a 9b 9c 9d 9e 9f a0  |................|
000000a0  a1 a2 a3 a4 a5 a6 a7 a8  a9 aa ab ac ad ae af b0  |................|
000000b0  b1 b2 b3 b4 b5 b6 b7 b8  b9 ba bb bc bd be bf c0  |................|
000000c0  c1 c2 c3 c4 c5 c6 c7 c8  c9 ca cb cc cd ce cf d0  |................|
000000d0  d1 d2 d3 d4 d5 d6 d7 d8  d9 da db dc dd de df e0  |................|
000000e0  e1 e2 e3 e4 e5 e6 e7 e8  e9 ea eb ec ed ee ef f0  |................|
000000f0  f1 f2 f3 f4 f5 f6 f7 f8  f9 fa fb fc fd fe ff     |...............|
000000ff
</pre>
<p>So bash variables (and <tt class="docutils literal"><span class="pre">$(...)</span></tt> idiom) are reasonably strong: if you know your content hasn't any <tt class="docutils literal">NUL</tt> characters you can safely use them. All this is probably
linked to the fact that command line arguments cannot hold <tt class="docutils literal">NUL</tt> characters neither, a common string termination in C programs.</p>
<p>If you really need to store full binary content in a bash variable, you should think about encoding it (base64, <tt class="docutils literal">xxd</tt>, or any format of yours).</p>
<p>Please understand that <tt class="docutils literal">bash</tt> might also be able to do what you want without using variables but only pipes (stdin and stdout). They won't suffer from this limitation of course.</p>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=489&amp;md5=1083440b003f7df80df9ef35d8b40717" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2014/05/03/bash-lore-nul-character-and-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2014%2F05%2F03%2Fbash-lore-nul-character-and-variables%2F&amp;language=en_GB&amp;category=text&amp;title=bash+lore%3A+NUL+character+and+variables&amp;description=Bash+variables+are+often+thought+as+being+able+to+store+any+binary+content.+Please+bear+in+mind+that+it+can%27t+store+NUL+character%2C+and+only+this+one.+Here+you+are%3A...&amp;tags=bash%2Clinux%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Fixing empathy in ubuntu 12.10, 13.04, 13.10, and 14.04</title>
		<link>https://vaab.blog.kal.fr/2013/04/04/fixing-empathy-in-ubuntu-12-10/</link>
		<comments>https://vaab.blog.kal.fr/2013/04/04/fixing-empathy-in-ubuntu-12-10/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 20:41:47 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=431</guid>
		<description><![CDATA[I recently decided I'll upgrade my Ubuntu 12.04 to Quantal 12.10. There wasn't a lot of new feature, and I was really disappointed by the changes in empathy. Empathy was not usable anymore. I installed pidgin and recalled that it &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2013/04/04/fixing-empathy-in-ubuntu-12-10/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>I recently decided I'll upgrade my Ubuntu 12.04 to Quantal 12.10.</p>
<p>There wasn't a lot of new feature, and I was really disappointed by the changes in <a class="reference external" href="https://live.gnome.org/Empathy">empathy</a>.</p>
<p>Empathy was not usable anymore. I installed pidgin and recalled that it wasn't correctly integrated with SIP technology which was a no-go for me.</p>
<p>I felt trapped with this upgrade: the version of empathy shipped with <tt class="docutils literal">12.04</tt> was way better than what I was confronted in <tt class="docutils literal">12.10</tt> (and <tt class="docutils literal">13.04</tt>, and <tt class="docutils literal">13.10</tt>, and <tt class="docutils literal">14.04</tt>)... Hopefully I managed to fix all this to my needs. Here's what I did.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">And when I upgraded to 13.04 (then 13.10, and 14.04), I could make the same complaints, and the same fixes.</p>
</div>
<div class="section" id="the-griefs">
<h3>The Griefs</h3>
<p>Bugs were reported:</p>
<ul>
<li><p class="first">the <strong>contact list has HUGE avatars</strong> and you can't choose to have smaller ones: what happened exactly is that the <a class="reference external" href="http://blog.desmottes.be/post/2012/06/15/New-Empathy-contact-list">contact list skin drastically changed</a> in a very un-wise move: each avatar took 48 pixel height, leading to un-manageably long, multi-screen tall contact list.
(<a class="reference external" href="https://bugs.launchpad.net/ubuntu/+source/empathy/+bug/1068668">30 people are awaiting on this bug resolution</a>). Worse: the <a class="reference external" href="https://git.gnome.org/browse/empathy/commit/?id=f58d68ddddf15dfe4e0d49af86e2add3c606316e">option to get compact list was removed</a> and more than <a class="reference external" href="https://bugs.launchpad.net/ubuntu/+source/empathy/+bug/1068668">96 people are awaiting for this option to come back</a>. A bug
report was also <a class="reference external" href="https://bugzilla.gnome.org/show_bug.cgi?id=693488">reported on bugzilla</a> at least <a class="reference external" href="https://bugzilla.gnome.org/show_bug.cgi?id=684199">twice</a>.</p>
<blockquote>
<ul class="simple">
<li><strong>I'm providing a fix</strong> to set avatar icon size.</li>
</ul>
</blockquote>
</li>
<li><p class="first">The <strong>contact list is not anymore sorted by status</strong>: yes, this means that <cite>connected</cite> and <cite>away</cite> contacts are mixed, if you have more than 10 contacts using google
talk for instance, they probably be growing your contact list as <cite>away</cite>. You'll have to painfully filter by eye the ones that are really available and active.</p>
<blockquote>
<ul class="simple">
<li><strong>I'm providing a quick fix</strong> to this also.</li>
</ul>
</blockquote>
</li>
<li><p class="first">You <strong>can't phone using your SIP account</strong> to your address book phone numbers.</p>
<blockquote>
<ul class="simple">
<li><strong>Hit only 12.10</strong>, on which <strong>I've provided a fix</strong> by cherry-picking the upstream bug resolution</li>
</ul>
</blockquote>
</li>
<li><p class="first"><strong>No more notification of new message</strong> (<a class="reference external" href="https://bugs.launchpad.net/ubuntu/+source/indicator-messages/+bug/1060469">launchpad report</a>).
What a failure ! I could hear message alert sound whenever I received a new message but <strong>could not get to see the incoming messages</strong> as there is no way to make the messaging window appear without the messaging indicator. Additionaly for the same reason, I could not answer to standard events as standard requests from new contacts
to share our visibility status despite a pretty notification message displayed on the top right corner of my screen telling me that the request was received (and that
the contact awaited an answer).</p>
<blockquote>
<ul class="simple">
<li><strong>This one was Ubuntu's fault</strong>, and can be fixed by installing <cite>telepathy-indicator</cite>, more info following</li>
</ul>
</blockquote>
</li>
</ul>
<p>Of course, I'm not the only one to have expressed deep concerns on theses <a class="reference external" href="http://ubuntuforums.org/showthread.php?t=2079243">empathy regressions</a>.</p>
</div>
<div class="section" id="the-cure">
<h3>The Cure</h3>
<div class="section" id="tl-dr">
<h4>TL;DR</h4>
<p>Provided you are on 64bit architecture, here are the new DEB files:</p>
<blockquote>
<ul class="simple">
<li><strong>To get compact contact list</strong>, and <strong>contact sorted by status</strong> get <a class="reference external" href="http://vaab.blog.kal.fr/files/2013/04/empathy_3.6.0.3-0ubuntu1_amd64.deb">this new fixed empathy deb for 12.10</a> (same for <a class="reference external" href="http://vaab.blog.kal.fr/files/2014/02/empathy_3.8.4-1ubuntu2_amd64.deb">13.10</a>, or for <a class="reference external" href="http://vaab.blog.kal.fr/files/2013/04/empathy_3.8.6-0ubuntu9_amd64.deb">14.04</a>) and install it.</li>
<li><strong>To re-enable calling landlines phone</strong> with your SIP account (only needed for 12.10), get <a class="reference external" href="http://vaab.blog.kal.fr/files/2013/04/account-plugin-sip_3.6.0.3-0ubuntu1_amd64.deb">this new account-plugin-sip deb for 12.10</a> and install it.</li>
<li><strong>To get instant message back in notification area</strong> ensure that you have <cite>telepathy-indicator</cite> installed and running.</li>
</ul>
</blockquote>
</div>
<div class="section" id="how-you-could-patch-it-yourself-the-next-time">
<h4>How you could patch it yourself the next time</h4>
<p>Open-source matters. Let me show you why:</p>
<p>Let's get the source code:</p>
<pre class="literal-block">
cd /tmp
apt-get source empathy
</pre>
<p>Note that this last command will get the source package of empathy, and apply ubuntu specific patches.</p>
<p>Now, we'll proceed with getting all build dependencies required for compilation of empathy sources:</p>
<pre class="literal-block">
apt-get build-dep empathy
</pre>
<p>With Ubuntu 12.10 (13.04, 13.10, and 14.04), the version of empathy is 3.6.0.3 (3.6.4, 3.8.4, and 3.8.6 respectively). We will now access to the source and make some modifications:</p>
<pre class="literal-block">
cd empathy-*
sed -ri 's/#define AVATAR_SIZE 48/#define AVATAR_SIZE 24/g' \
    libempathy-gtk/empathy-roster-contact.c
</pre>
<p>I'll let you tweak the size in this line, it's expressed in pixel. New value is <tt class="docutils literal">24</tt> here, instead of <tt class="docutils literal">48</tt>.</p>
<p>Optionally, I found that the <a class="reference external" href="https://bugzilla.gnome.org/show_bug.cgi?id=691195">SIP bug that prevented to call landlines phones</a> was corrected in empathy <tt class="docutils literal">3.7.4</tt>. And that it wasn't included in <tt class="docutils literal">12.10</tt> but was in <tt class="docutils literal">13.04</tt> and newer.</p>
<p>If using <tt class="docutils literal">12.10</tt> and interested by this feature, you should cherry-pick the patch:</p>
<pre class="literal-block">
wget -O - &quot;https://bug691195.bugzilla-attachments.gnome.org/attachment.cgi?id=232954&amp;action=diff&amp;collapsed=&amp;context=patch&amp;format=raw&amp;headers=1&quot; | patch -p 1
</pre>
<p>Optionally, I patched the sort algorithm to sort by contact status before sorting by alphabetical order, if you want to get this patch:</p>
<pre class="literal-block">
cat &lt;&lt;EOF | patch -p 1
diff --git a/libempathy-gtk/empathy-roster-view.c b/libempathy-gtk/empathy-roster-view.c
index c3ae66e..77d76d6 100644
--- a/libempathy-gtk/empathy-roster-view.c
+++ b/libempathy-gtk/empathy-roster-view.c
&#64;&#64; -188,6 +188,10 &#64;&#64; add_roster_contact (EmpathyRosterView *self,
   g_signal_connect (contact, &quot;notify::alias&quot;,
       G_CALLBACK (roster_contact_changed_cb), self);

+  /* Need to resort if presence is changed */
+  g_signal_connect (contact, &quot;notify::presence-type&quot;,
+      G_CALLBACK (roster_contact_changed_cb), self);
+
   gtk_widget_show (contact);
   gtk_container_add (GTK_CONTAINER (self), contact);

&#64;&#64; -633,19 +637,76 &#64;&#64; compare_roster_contacts_by_alias (EmpathyRosterContact *a,
 }

 static gint
+presence_sort_key (FolksPresenceType presence) {
+
+  switch (presence)
+    {
+      case FOLKS_PRESENCE_TYPE_UNSET:
+      case FOLKS_PRESENCE_TYPE_OFFLINE:
+      case FOLKS_PRESENCE_TYPE_UNKNOWN:
+      case FOLKS_PRESENCE_TYPE_ERROR:
+        return 0;
+
+      case FOLKS_PRESENCE_TYPE_HIDDEN:
+      case FOLKS_PRESENCE_TYPE_EXTENDED_AWAY:
+        return 1;
+
+      case FOLKS_PRESENCE_TYPE_AWAY:
+        return 2;
+
+      case FOLKS_PRESENCE_TYPE_BUSY:
+      case FOLKS_PRESENCE_TYPE_AVAILABLE:
+        return 3;
+
+      default:
+        g_warning (&quot;Unknown FolksPresenceType: %d&quot;, presence);
+        return 0;
+  }
+
+}
+
+static gint
+compare_roster_contacts_by_presence (EmpathyRosterContact *a,
+    EmpathyRosterContact *b)
+{
+  FolksPresenceType presence_a, presence_b;
+  FolksIndividual *ind_a, *ind_b;
+  gint key_a, key_b;
+
+  ind_a = empathy_roster_contact_get_individual (a);
+  ind_b = empathy_roster_contact_get_individual (b);
+
+  presence_a = folks_presence_details_get_presence_type (
+      FOLKS_PRESENCE_DETAILS (ind_a));
+  presence_b = folks_presence_details_get_presence_type (
+      FOLKS_PRESENCE_DETAILS (ind_b));
+
+  key_a = presence_sort_key(presence_a);
+  key_b = presence_sort_key(presence_b);
+
+  return key_b - key_a;
+}
+
+static gint
 compare_roster_contacts_no_group (EmpathyRosterView *self,
     EmpathyRosterContact *a,
     EmpathyRosterContact *b)
 {
   gboolean top_a, top_b;
+  gint presence_cmp;

   top_a = contact_in_top (self, a);
   top_b = contact_in_top (self, b);

   if (top_a == top_b)
-    /* Both contacts are in the top of the roster (or not). Sort them
-     * alphabetically */
-    return compare_roster_contacts_by_alias (a, b);
+    {
+      /* Both contacts are in the top of the roster (or not). Sort them
+       * by status, then alphabetically */
+      presence_cmp = compare_roster_contacts_by_presence(a, b);
+      if (presence_cmp == 0)
+        return compare_roster_contacts_by_alias (a, b);
+      return presence_cmp;
+    }
   else if (top_a)
     return -1;
   else
&#64;&#64; -676,14 +737,18 &#64;&#64; compare_roster_contacts_with_groups (EmpathyRosterView *self,
     EmpathyRosterContact *b)
 {
   const gchar *group_a, *group_b;
+  gint presence_cmp;

   group_a = empathy_roster_contact_get_group (a);
   group_b = empathy_roster_contact_get_group (b);

-  if (!tp_strdiff (group_a, group_b))
+  if (!tp_strdiff (group_a, group_b)) {
     /* Same group, compare the contacts */
-    return compare_roster_contacts_by_alias (a, b);
-
+    presence_cmp = compare_roster_contacts_by_presence(a, b);
+    if (presence_cmp == 0)
+      return compare_roster_contacts_by_alias (a, b);
+    return presence_cmp;
+  }
   /* Sort by group */
   return compare_group_names (group_a, group_b);
 }
EOF
</pre>
<p>And proceeded with the recompilation of the debian package:</p>
<pre class="literal-block">
dpkg-buildpackage -rfakeroot -b
</pre>
<p>After some time waiting for the compilation to finish, you should have some new 'deb' packages in <tt class="docutils literal">/tmp</tt>:</p>
<pre class="literal-block">
cd ..
ls -al *.deb
</pre>
<p>Before installing these packages, you can make sure to remove any empathy/telepathy process:</p>
<pre class="literal-block">
killall telepathy-gabble empathy-auth-client empathy-chat \
        telepathy-idle telepathy-logger telepathy-rakia  \
        telepathy-salut telepathy-haze mission-control-5 \
        empathy
</pre>
<p>And then install the new empathy:</p>
<pre class="literal-block">
sudo dpkg -i empathy_*.deb
</pre>
<p>If you applied the SIP patch:</p>
<pre class="literal-block">
sudo dpkg -i account-plugin-sip_*.deb
</pre>
<p>Then run empathy by any usual way you could think of.</p>
</div>
</div>
<div class="section" id="more-info">
<h3>More info</h3>
<ul class="simple">
<li>Many thanks to this how-to on <a class="reference external" href="http://www.cyberciti.biz/faq/rebuilding-ubuntu-debian-linux-binary-package/">rebuilding deb packages</a></li>
</ul>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=431&amp;md5=2cb31d00d49ab29bf1faf0f4d49e21cc" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2013/04/04/fixing-empathy-in-ubuntu-12-10/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2013%2F04%2F04%2Ffixing-empathy-in-ubuntu-12-10%2F&amp;language=en_GB&amp;category=text&amp;title=Fixing+empathy+in+ubuntu+12.10%2C+13.04%2C+13.10%2C+and+14.04&amp;description=I+recently+decided+I%27ll+upgrade+my+Ubuntu+12.04+to+Quantal+12.10.+There+wasn%27t+a+lot+of+new+feature%2C+and+I+was+really+disappointed+by+the+changes+in+empathy.+Empathy+was...&amp;tags=blog" type="text/html" />
	</item>
	</channel>
</rss>
