Periapt Technologies - fresh clean websites
Bookmark and Share

Struggling with Duende

Reprise

Previously I have discussed how fixing the Debian init script integration issues using only shell script, proved relatively successful but complex and unsatisfying. The duende approach seems more elegant and is of course the approach recommneded by upstream. However it has hitherto hit the fundamental issue that we need a pid file for the duende process. Apparently other people had had the same issue and provided a patch that provides an optional pid file argument. However this was not enough to complete the integration. Furthermore it occurred to me that duende could be of wider interest.

What is a Duende?

You must be wondering what a duende actually is and where the word comes from. According to wikipedia a duende is a "fairy- or goblin-like mythological creature from Iberian, Latin American and Filipino folklore". The word is a shortening of "duen de casa", meaning the "the owner of the house". I find these definitions hard to relate to the carnival pictures on the left. However Sam Trenholme says that the word simply means "daemon" in Spanish, which makes a lot more sense of those pictures and does also fit with what the executable does.

In local traditions the word often takes on extra colour. In the Phillipines:

They are known to be either good or mischievous, depending on how homeowners treat them. They usually come out at 12 noon for an hour and during the night. Filipinos always mutter words ("tabi-tabi po" or "bari-bari apo ma ka ilabas kami apo") asking them to excuse themselves for bothering the Duwendes. Filipinos would leave food on the floor, so that the duwende residing (or guarding) the house would not be angry with them. They also take your things, and laugh at you when you try to find it. They give it back when they feel like it, or when you tell them to please give it back.

In the Hispanic folklore of Central America and the American Southwest:

Duendes are known as gnome like creatures who live inside the walls of homes, especially in the bedroom walls of young children. They attempt to clip the toenails of unkempt children, often leading to the mistaken removal of entire toes.[2] They are also known for taking items from young children. They have also been able to barter with the mother of young children so that they can take the child and have them to eat. They appear at night when children are at play with a ball, and watch the children and later make their appearance and confront the children.

The concept of duende has also become a term in Spanish, meaning something like the spirit that transforms art from being merely skillful to being sublime. In 1933, the Spanish poet and playwright, Federico García Lorca, delivered a lecture in which he described the role of duende in art:

La Niña de Los Peines had to tear apart her voice, because she knew experts were listening, who demanded not form but the marrow of form, pure music with a body lean enough to float on air. She had to rob herself of skill and safety: that is to say, banish her Muse, and be helpless, so her duende might come, and deign to struggle with her at close quarters. And how she sang! Her voice no longer at play, her voice a jet of blood, worthy of her pain and her sincerity, opened like a ten-fingered hand as in the feet, nailed there but storm-filled, of a Christ by Juan de Juni.

What does the Duende process do?

The duende process starts a child process and a log helper process that captures the output from the child process and passes it onto syslog:

  1. Duende starts child process in a protected chroot environment.
  2. Duende sets up a pipe between the log helper process and what will be the child process.
  3. Duende starts log helper process in a protected chroot environment.
Copyright 2010, Nicholas Bamber 2010, pastel at flickr 2009, theilr at flickr 2008, megavas at flickr 2010, ajourneyroundmyskull at flickr

Our modifications to Duende

In order to better integrate duende into Debian and to baptise it as a useful program in its own right, I have added the following arguments to the duende code base:

short formlong formdescription
-c--chroot=DIRDirectory log helper should change and chroot to
-g--gid=GIDGroupid log helper should change down to
-i--ident=STRHow log helper should be identified in syslog
-p--pid=FILEFile used to store pid of duende process
-r--restart_on=INTExit status on which to restart child process
-u--uid=UIDUserid log helper should change down to

These arguments are parsed with the argp library and so the --usage, --help and -? arguments are supported. There are a couple of other issues. It seemed that the log helper did not actually call chroot.

Also when collecting the exited children there seemed to be a race condition. As the duende code stands when the main child process exits, the log helper process will be killed and vice versa. The current code always tries to reap the main child process first and the log helper process second. I found that in some cases the log helper process gets missed and so a zombie process gets created. This Debian version reaps all exited child processes and takes the appropriate action according to whether this was a main child, the log helper or something that got missed from a previous cycle.

A toy example of Duende used generically

In this release I have provided an example Perl daemon script that can be used with this version of Duende. The script responds to various stimuli as follows:

StimulusResponse to stdoutOther repsonse
Every 61 secondsAnyone there?n/a
USR1 signalOuch!n/a
USR2 signalStop that or I will blow your computer up!n/a
HUP signaln/aexit 42
HTTP access on port 8888Message from [host]:[port]One line web page

The example init script uses this version of duende to drop the user and group of the script to daemon, change the working directory and chroot and prepares to restart the main child when it exits with code 42. All of the steps required to daemonize a process are handled betweem the start-stop-daemon pocess, the init script or the Duende process apart from:

  1. Change directory to chroot directory
  2. Chroot to chosen directory
  3. Call setpgrp
  4. Set group
  5. Set user id

The example script cheats slightly because it does these things after all the Perl modules have been loaded. In some real world examples it might be necessary to have a chroot environment with something actually in it.

Nicholas Bamber, 13th July 2011
 
Behind the magic there has to be technology:
Valid XHTML 1.0 Transitional Valid Robots.txt Ethical Junction Member 2010 Sitemap ©2009-11 Periapt Technologies Ltd