Myon's Blog
afu books bridge debian geocaching irc life notes unix
Cool Unix Features: date -d @

I've always been annoyed about how hard it is to convert seconds-since-epoch to strings. I've always been using "date -d '1970-01-01 + 1234 sec'", but as it turned out, that's wrong because it uses the wrong timezone. Luckily, there's a slick replacement:

$ date -d '@1234'
Do 1. Jan 01:20:34 CET 1970

The right version of the "long" version is:

$ date -d '1970-01-01 UTC + 1234 sec'
Do 1. Jan 01:20:34 CET 1970
Posted Tue Jul 13 15:04:25 2010 Tags:
Cool Unix Features: nproc

Since coreutils 8.1 (in Squeeze, not Lenny), there is a command that simply prints out how many processors (cores, processing units) are available:

$ nproc
2

The use case is obvious:

$ make -j $(nproc)

On a side note, this is gnulib's nproc module wrapped into a C program. If you didn't know gnulib before (it had slipped my attention until recently), it is a library of portability functions and other useful things. Adding it to a project is simply done by calling gnulib-tool and tweaking a few lines in the automake/whatever build scripts.

PS: do not use nproc unconditionally in debian/rules. Parse DEB_BUILD_OPTIONS for "parallel" instead.

Posted Wed Jun 23 09:58:49 2010 Tags:
Cool Unix Features: flock

Lockfiles are usually hard to get right, especially in sh scripts. The best bet so far was "lockfile" included with procmail, but its semantics are pretty weird. (Try to understand what exit code you want, with or without "-!".) Not to mention that failing to clean up the lockfile will stop the next cron run, etc.

Since Lenny, util-linux ships "flock". Now you simply say

$ flock /path/to/lockfile /path/to/command

and are done. If you want it non-blocking, add "-n":

$ flock -n /path/to/lockfile /path/to/command

I should probably migrate all my cronjobs to use this.

Posted Fri Jun 18 16:20:43 2010 Tags:
Cool Unix Features: stat

"stat" is "date +format" for files:

$ stat -c %s ~/me.jpg  # size
520073
$ stat -c %U ~/me.jpg  # owner
cbe

No more parsing of "ls" output or similar hacks.

It also shows detailed information about files.

$ stat ~/me.jpg
  File: „/home/cbe/me.jpg“
  Size: 520073          Blocks: 1024       IO Block: 4096   reguläre Datei
Device: fe03h/65027d    Inode: 12427268    Links: 1
Access: (0600/-rw-------)  Uid: ( 2062/     cbe)   Gid: ( 2062/     cbe)
Access: 2010-06-06 12:58:07.000000000 +0200
Modify: 2010-04-09 22:38:46.000000000 +0200
Change: 2010-04-26 14:18:00.000000000 +0200

It supports similar features for stat'ing filesystems.

Posted Fri Jun 18 00:26:58 2010 Tags:
Cool Unix Features: /dev/full

I've always thought about collecting random bits of useful/interesting/cool Unix features and the like. Before I let that rot indefinitely in a text file in my $HOME, I'll post it in a series of blog posts. So here's bit #1:

Everyone knows /dev/null, and most will know /dev/zero. But /dev/full was unknown to me until some time ago. This device will respond to any write request with ENOSPC, No space left on device. Handy if you want to test if your program catches "disk full" - just let it write there:

$ echo foo > /dev/full
bash: echo: write error: No space left on device
Posted Wed Jun 16 23:47:38 2010 Tags:
New feeds

I post rarely myself to Planet Bridge, but that shouldn't stop anyone from suggesting new feed additions. I've just added Catchall and Phillip Martin aka the Gargoyle Chronicles. Welcome!

Posted Sat May 8 23:20:13 2010 Tags:
tenace 0.10 released

It has been a while since the last update for tenace, my bridge hand viewer. The highlight in version 0.10 is version 2.0 of the double dummy engine dds which has been updated to support parallel computation in multiple threads. The parscore computation in tenace now uses all available CPU cores. Even my notebook has two CPUs :).

More on the technical side, the GUI has been switched to use GtkBuilder which comes with Gtk so there is no external library needed anymore (previously libglade). The looks are pretty much the same as before, though.

The previous version 0.9 had added windows support via mingw. I would still appreciate if people could test it and tell me which bits I need to improve.

Posted Sat May 8 23:09:27 2010 Tags:
Clamav segfaults

If you are still using clamav on etch, you might want to upgrade now:

# /etc/init.d/clamav-daemon start  
Starting ClamAV daemon: clamd LibClamAV Warning: ***********************************************************
LibClamAV Warning: ***  This version of the ClamAV engine is outdated.     ***
LibClamAV Warning: *** DON'T PANIC! Read http://www.clamav.net/support/faq ***
LibClamAV Warning: ***********************************************************
/etc/init.d/clamav-daemon: line 240:  5221 Segmentation fault      start-stop-daemon --start -o -c $User --exec $DAEMON
 failed!

Rolling back to yesterday's daily.cld fixes the issue, at least for the segfault.

Posted Fri May 7 13:36:01 2010 Tags:
Too much magic in Gentoo

Five years after the release of patch 2.5.9, patch 2.6 was released two weeks ago. The most interesting feature is that it will now produce reject files in unified format when the input is in that format. Lucky for those of us whose brain hurts when trying to read context diffs.

Of course there are more changes, and one of them is affecting Gentoo in particular: Gentoo service announcement: keep clear of GNU patch-2.6. The linked Gentoo bug #293570 has attachments to reproduce the bug:

benz:~/tmp/tripwire-2.3.1-2 $ patch -p0 -F3 < ../tripwire-friend-classes.patch
patching file tripwire-2.3.1-2-p1/src/fco/fconame.h
Hunk #1 succeeded at 48 with fuzz 3 (offset -1 lines).
patching file tripwire-2.3.1-2-p1/src/fco/fcosetimpl.h
Hunk #1 succeeded at 45 with fuzz 3 (offset -1 lines).
patching file tripwire-2.3.1-2-p1/src/tw/fcoreport.h
Hunk #1 succeeded at 84 with fuzz 3 (offset -1 lines).

benz:~/tmp/tripwire-2.3.1-2 $ ls -l
drwxr-xr-x 4 cbe credativ 4096 18. Nov 05:03 src/
drwxr-xr-x 3 cbe credativ 4096  2. Dez 12:23 tripwire-2.3.1-2-p1/

Note that it created a new tripwire-2.3.1-2-p1 directory inside the existing one, which is consistent with -p0. The newly created file contain just the "+" lines from the patch. (There are no "-" lines.) patch 2.5.9 would have refused the patch because it couldn't find the file to patch.

This is arguably a surprising behavior, but what really happens here is that -F3 (fuzz 3) will explicitly tell patch to ignore 3 context lines if it cannot apply the patch otherwise. diff's default behavior is to create patches with 3 lines of context. patch's default fuzz factor is one less than that, 2. Now, 3 - 3 is zero, which doesn't leave any context left.

Apparently lots of Gentoo ebuilds use -F3, and try to guess the correct number of directories to strip, starting with -p0, -p1, ... No wonder that things break badly for them now, as patches will suddenly succeed with -p0.

I'm not sure I like the new patch behavior, but in Gentoo's case I'd say they are using way too much magic in their build system, and now get bitten by options they shouldn't have put there in the first place.

Posted Wed Dec 2 12:42:57 2009 Tags:
6to4

Setting up a 6to4 tunnel on freewrt:

#!/bin/sh
# http://tldp.org/HOWTO/Linux+IPv6-HOWTO/configuring-ipv6to4-tunnels.html

ip -6 route flush dev tun6to4
ip link set dev tun6to4 down
ip tunnel del tun6to4

sleep 2

ttldefault=15
ipv4=$(ip a show ppp0 | sed -ne 's/.*inet \([^ ]*\).*/\1/p')
local6to4prefix=$(printf "2002:%02x%02x:%02x%02x" `echo $ipv4 | tr "." " "`)
local6to4address="$local6to4prefix::1"

ip tunnel add tun6to4 mode sit ttl $ttldefault remote any local $ipv4
ip link set dev tun6to4 up
ip -6 addr add $local6to4address/16 dev tun6to4
ip -6 route add 2000::/3 via ::192.88.99.1 dev tun6to4

ip -6 addr add $local6to4address/64 dev eth0.0
ip -6 route add $local6to4address/64 dev eth0.0

sed -i -e "s/prefix.*/prefix $local6to4prefix::\\/64/" /etc/radvd.conf
/etc/init.d/S51radvd restart
Posted Sun Mar 8 19:58:37 2009 Tags:

This blog is powered by ikiwiki.