jump to navigation

flock vs fcntl 2014 November 18 12:46

Posted by diamond in : Tech , add a comment

Both flock(2) and fcntl(2) can create advisory locks on unix. What i hadn’t realised, until it bit me this morning, is that the semantics are subtly different. flock() creates a lock at the open file table level. fcntl() creates a lock at the file region level. This means that if you open a file twice, flock() will treat both FDs independently, whereas fcntl() treats them as being the same thing. Here’s a demonstration using python.

First, fcntl() (note that fcntl.lockf uses fcntl() underneath):


Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fcntl
>>> a=open("/tmp/testy", "w")
>>> b=open("/tmp/testy", "w")
>>> fcntl.lockf(a, fcntl.LOCK_EX | fcntl.LOCK_NB)
>>> fcntl.lockf(b, fcntl.LOCK_EX | fcntl.LOCK_NB)
>>>

Second, flock():

Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fcntl
>>> a=open("/tmp/testy", "w")
>>> b=open("/tmp/testy", "w")
>>> fcntl.flock(a, fcntl.LOCK_EX | fcntl.LOCK_NB)
>>> fcntl.flock(b, fcntl.LOCK_EX | fcntl.LOCK_NB)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 11] Resource temporarily unavailable
>>>

The reason i came across this was i was trying to write unittests around some locking code, and couldn’t figure out why opening the same file twice and locking both FDs never failed. Eventually i spotted that flock() was working while fcntl() was not, and careful reading of the manpages indicated why :)

Mark irssi window as unread 2013 September 24 16:00

Posted by diamond in : Tech , add a comment

Someone messages me on irc/im, i change to the irssi window, see that it’s something i want to leave for later, then promptly forget about it. This has been happening for many years. Today, it finally ends. I present unread.pl, a tiny irssi script that allows me to mark windows as unread:


$ cat unread.pl
#!/usr/bin/perl -w
 
sub cmd_unread {
my ($data, $server, $witem) = @_;
my $base = Irssi::window_find_refnum(1);
my $target;
 
if ($data eq "") {
$target = Irssi::active_win();
$base->set_active();
} else {
$target = Irssi::window_find_refnum($data);
}
 
$target->activity(4);
}
 
Irssi::command_bind("unread", "cmd_unread");

If given no argument, it marks the current window as unread and switches to window 1. Otherwise it just marks the specified window as unread.

Google chrome per-workspace profiles

Posted by diamond in : Tech , add a comment

Do you use chrome and find it irritating that when you click on a link outside of the browser, it’ll just use whatever chrome window you last had active? I sure do. Here’s my fix for gnome classic:

1) A custom launcher script that detects which workspace i’m on, and decides which browser profile to send the request to:

$ cat bin/google-chrome-launcher
#!/bin/bash
workspace=$(xprop -root -notype _NET_CURRENT_DESKTOP | sed 's/^.* = //')
profiledir=""
 
case "$workspace" in
0) profiledir="Default";;
1) profiledir="Profile 1";;
esac
 
if [ -n "$profiledir" ]; then
exec google-chrome --profile-directory="$profiledir" "$@"
else
exec google-chrome "$@"
fi

2) A custom .desktop to invoke my custom script:

$ cat .local/share/applications/google-chrome-custom.desktop
[Desktop Entry]
Version=1.0
Name=Google Chrome Custom
Exec=google-chrome-launcher %U
Terminal=false
Icon=google-chrome
Type=Application
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml_xml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;

3) Make this the default. Gnome Classic/Fallback being the horrible hybrid broken beast that it is, i could find no gui way of doing this, so i simply edited ~/.local/share/applications/mimeapps.list and changed every occurrence of google-chrome.desktop to google-chrome-custom.desktop, and that seemed to do the trick.

I’ve found a new bio one-liner 2011 May 9 18:41

Posted by diamond in : Random , add a comment

A friend of mine, Tobias, when asked to describe me in one line came out up with this gem:

“He’s the reason babies cry when they’re born.”

This is hands-down the best thing that’s ever been said about me.

It is done 2011 May 4 20:04

Posted by diamond in : Random , 2 comments

Yesterday myself and noirin got divorced. It took 8 months of separation before we finally saw a divorce lawyer, but only 3 weeks to actually get a court date and all the paperwork in order. As for the court hearing itself, it went as well as it could. By the end i was monosyllabic.

During the last hour as my wife, noirin made me look at pictures of shoes, and promise to never be in charge of a power plant. It was a glimpse into what we’d shared.

The marriage is over. This is not a cause for celebration, nor is it a cause for regret. It is simply necessary, it is what it is.

It is what it is.