SANE USB permissions

Today I had a permission problem with SANE on Linux. SANE stands for “Scanner Access Now Easy”, it provides standardized access to scanner hardware (http://www.sane-project.org) and this is the most commonly used scanning tool on UNIX/Linux.

In my case the USB scanner was not recognized when issuing scanimage -L from my user account although it worked correctly under root and my user is in the scanner group. What more is sane-find-scanner reported permissions errors while running the command as user. The owner and group for the device (in my case it was /dev/bus/usb/002/004) were root:root. At this point we already know that something weird is happening and I expected something like root:scanner instead.

Looking into /lib/udev/rules.d/60-libsane.rules, the line in charge of changing the permissions for each scanner device matched by SANE:

ENV{libsane_matched}=="yes", RUN+="/bin/setfacl -m g:scanner:rw $env{DEVNAME}"

This is nice but I do not use ACL and they are disabled in kernel,  so this command is useless. So I replaced this line with:

ENV{libsane_matched}=="yes", RUN+="/bin/setfacl -m g:scanner:rw $env{DEVNAME}", MODE="0664", GROUP="scanner"

Now the owner and group are correctly set to root:scanner and I can use my scanner as a regular user.

Note that on my system the libsane, sane-utils and xsane are the only packages depending on the acl package. According to what I’ve seen in the ChangeLog they do so in order to cope with MFP which I presume should be accessible as a scanner and printer device at the same time. What I would have done instead would be to create special group for MFP devices and use this instead. IMO still less of a mess than enabling ACL on the whole system for a single package.

GTalk browser plugin on Debian (testing)

So you installed the GTalk browser plugin on Debian testing and it doesn’t work. However GTalk is listed correctly when you list the plugins in your browser. So what now?

Well you can try to remove libudev0. It seems that the plugin has some problems when both libudev1 and libudev0 are present on the system.

Get rid of SIGINT

Yesterday I had some problems trying to get rid of the SIGINT signal (when you press C-c on your terminal). Imagine you have the following script :

# a.sh

b &
sleep

Let’s suppose that b is a sleep or another simple command or a shell script. In that case a SIGINT on the terminal will kill a.sh but b will be rattached to init. This is strange because:

  1. b effectively receives the signal (seen with strace)
  2. the default action should terminate the process
The reason behind this is that the shell will (roughly) do the following sequence of operations before starting the process in the background:
if(!fork()) {
  /* child */
  signal(SIGINT, SIG_IGN);
  signal(SIGQUIT, SIG_IGN);

  execve(...cmd...);
}

 

The execve manpage has this to say about signals:

*  POSIX.1-2001 specifies that the dispositions of any signals that are
ignored or set to the default are  left  unchanged.

i.e. the process will inherit SIG_IGN signals and set all the others to default. So with our shell the SIGINT is ignored for the background processes. However if the process defines its own handler and catch the signal, it will override the inherited SIG_IGN value. This is the case for example with tcpdump which will catch this signal to terminate properly. This is also the case for the following code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

void sigint(int n)
{
  printf("child int; exitn");
  exit(0);
}

int main()
{
  signal(SIGINT, sigint);
  usleep(-1);
}

In this case the process will catch SIGINT and terminate. So if we cannot prevent the process to catch the signal the question now is: How can we prevent b to receive the signal?

When this signal is generated, it is sent to the foreground process group on the session associated to the tty. If you have a shell open on your tty, it will be the currently running command if any. In this case the current command is a non-interactive shell script. In this non-interactive shell, all commands even those sent to background with “&” are attached to the same process group. So here the background commands of the shell script are associated to the foreground process group.  This is why they still receive SIGINT even when the parent process die and they are rattached to init.

The solution is to create the processes which we want to make unaware of SIGINT in a new process group, different from the foreground process group and therefore a background process group. However there is no command to start something in a new process group. Instead we have a command which can start something in a new session and therefore a new process group too. Better still, your process is detached from your tty which makes it (nearly) a real daemon. So the solution is:

# a.sh

setsid b &
sleep

Another solution forces the shell to turn on job control in non-interactive mode with set -m. This way each background process will be created in a new process group. This could be embarrassing though because you cannot background processes in the same process group anymore. Basically we want processes in the same process group to make sure that a SIGINT will terminate the entire script, background commands included. Otherwise you have to save the pid, trap the SIGINT signal and take care of terminating everything by yourself.

Moreover using setsid is a part of the REAL answer to this question:

“How do we fully detach a process from its tty?”

For which we almost see:

“Use nohup…”

Which is wrong! According to man nohup justs: “run a command immune to hangups, with output to a non-tty”. That is, it will just adjust input/output, ignore SIGHUP and exec your command. However if you do something like signal(SIGHUP, SIG_DFL); then nohup has no effect anymore and the process will be terminated when the terminal hang up.

Graph editor on Linux

Imagine you just want to create a graph to represent some network topology so you can parse it and generate the configuration files accordingly. You probably want to do that in an editor because you want to quickly check different topologies. So no fancy diagram editor just nodes, edges, attributes, point and click interface which exports in a simple and clear dot format that’s it.

I’m afraid this doesn’t seem to exist. Most people will redirect you to yEd, which is a somewhat cumbersome java diagram editor (it takes more than ten seconds to start the thing). It cannot even export in the dot format, instead it saves in an overly verbose and messy GraphML format. Well, there are some other diagram editors which can export to the dot format but these tools are used to produce good looking visual content and the resulting dot file will be crowded with useless attributes and sometime invisible nodes.

It’s crazy one cannot find a such simple graph editor. Does anyone know one? Does anyone want to create one?  I see… a nice and clean GUI in Python/GtkBuilder which saves in dot and GraphML and can export the thing in various format with the “dot” command.

XScreenSaver slow to lock

I already posted an article about the impact of a slow XScreenSaver when you suspend your machine. However if you wait for the screen to be effectively locked then a slow XScreenSaver will annoyingly delay going into sleep. The same apply if you often use your keyboard to lock your screen. And when I say slow I mean you can wait up to 6 entire seconds or more before you screen is effectively locked.

The reason behind this may be that some other program is grabbing the mouse or keyboard. If you want to investigate you may turn on XScreenSaver’s verbose mode and start it manually. Modify these two lines in ~/.xscreensaver :

verbose: False
captureStderr: False
And now start xscreensaver in a terminal and issue a lock with xscreensaver-command. You may see something like this :

xscreensaver: 20:10:22: LOCK ClientMessage received; activating and locking.
xscreensaver: 20:10:22: 0: locked mode switching.
xscreensaver: 20:10:22: user is idle (ClientMessage)
xscreensaver: 20:10:22: blanking screen at Mon Jul 29 20:10:22 2013.
xscreensaver: 20:10:22: 0: grabbing keyboard on 0x81... GrabSuccess.
xscreensaver: 20:10:22: 0: grabbing mouse on 0x81... AlreadyGrabbed.
xscreensaver: 20:10:23: 0: grabbing mouse on 0x81... AlreadyGrabbed.
xscreensaver: 20:10:24: 0: grabbing mouse on 0x81... AlreadyGrabbed.
xscreensaver: 20:10:25: 0: grabbing mouse on 0x81... AlreadyGrabbed.
xscreensaver: 20:10:26: couldn't grab pointer!  (AlreadyGrabbed)

As you can see, XScreenSaver desperately tries to grab the mouse and refuses to lock until it finally gives up four seconds later. In my case it was unclutter which was grabbing it. Moreover I was using the -grab option and I guess this was causing the problem so you may just start the command this way instead :

unclutter -idle 5 -root &

 

Sound problem with zsnes on Debian amd64

You may have some sound problems with zsnes on Debian amd64 (jessie) especially if you want to use pulseaudio with SDL. There are a lot of old posts about these kind of sound problems with zsnes though none of the proposed solutions will work anymore. The last zsnes version (1.510+bz2-5) comes with only the SDL audio output. I configured the SDL audiodriver to pulseaudio but it had to be configured to pulse for zsnes to work (well don’t know why). Anyway all I had to do was this :

export SDL_AUDIODRIVER=pulse
zsnes

And now it works. So you may just add an alias or wrap this into a script to do that automatically.

Attach a patch to an issue on GitHub

You can’t attach a patch on an issue on GitHub. Instead, it seems you have to use this :

https://gist.github.com

Well… Copy-pasting ain’t a way to issue a patch. So I made a little script to upload files on my server. You can download it here:

https://raw.github.com/gawen947/scripts/master/bin/upload.sh

It uses some other commands from the SUnix repository. Though it should be easy to adapt it without them.

http://github.com/gawen947/sunix