<?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>The Daily Build &#187; linux</title>
	<atom:link href="http://blog.bstpierre.org/tag/linux/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bstpierre.org</link>
	<description>Software Development, version 3.0</description>
	<lastBuildDate>Wed, 26 May 2010 16:08:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An Interesting pid File Race</title>
		<link>http://blog.bstpierre.org/pid-file-race</link>
		<comments>http://blog.bstpierre.org/pid-file-race#comments</comments>
		<pubDate>Wed, 26 May 2010 16:08:50 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[software-engineering]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=305</guid>
		<description><![CDATA[ISC&#8217;s dhcpd uses this code to check for an already-running daemon:
/* Read previous pid file. */
if ((i = open (path_dhcpd_pid, O_RDONLY)) &#62;= 0) {
    status = read (i, pbuf, (sizeof pbuf) - 1);
    close (i);
    if (status &#62; 0) {
       [...]


Related posts:<ol><li><a href='http://blog.bstpierre.org/who-else-wants-better-short-term-memory' rel='bookmark' title='Permanent Link: Who Else Wants Better Short Term Memory?'>Who Else Wants Better Short Term Memory?</a> <small> In &#8220;Talent is Overrated&#8221;, Geoff...</small></li><li><a href='http://blog.bstpierre.org/local-ssh-forwarding' rel='bookmark' title='Permanent Link: Use Local SSH Forwarding to Reduce the Number of Manual Hops'>Use Local SSH Forwarding to Reduce the Number of Manual Hops</a> <small>Local port forwarding is the same...</small></li><li><a href='http://blog.bstpierre.org/must-have-tools-software-teams' rel='bookmark' title='Permanent Link: 9 &#8220;Must-Have&#8221; Tools for Software Teams'>9 &#8220;Must-Have&#8221; Tools for Software Teams</a> <small> The items below are useful...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>ISC&#8217;s dhcpd uses <a href="http://google.com/codesearch/p?hl=en#5KTrgOW2hXs/pub/nslu2/sources/dhcp-3.0.4.tar.gz|9nqObdv7Xcs/dhcp-3.0.4/server/dhcpd.c&amp;q=_PATH_DHCPD_CONF&amp;d=3&amp;l=539">this code</a> to check for an already-running daemon:</p>
<pre>/* Read previous pid file. */
if ((i = open (path_dhcpd_pid, O_RDONLY)) &gt;= 0) {
    status = read (i, pbuf, (sizeof pbuf) - 1);
    close (i);
    if (status &gt; 0) {
        pbuf [status] = 0;
        pid = atoi (pbuf);

        /* If the previous server process is not still running,
           write a new pid file immediately. */
        if (pid &amp;&amp; (pid == getpid() || kill (pid, 0) &lt; 0)) {
            unlink (path_dhcpd_pid);
            if ((i = open (path_dhcpd_pid,
                           O_WRONLY | O_CREAT, 0644)) &gt;= 0) {
                sprintf (pbuf, "%d\n", (int)getpid ());
                write (i, pbuf, strlen (pbuf));
                close (i);
                pidfilewritten = 1;
            }
        } else
            log_fatal ("There's already a DHCP server running.");
    }
}
</pre>
<p>The problem with this strategy is that, if the box dies, there&#8217;s a stale pid file left in /var/run/dhcpd.pid. This wouldn&#8217;t be so bad &#8212; the code above checks [using <code>kill(pid, 0)</code>] to see if there&#8217;s a process running with that pid. But when the box is restarting, there will be a bunch of processes all starting in similar sequence each time. So on one boot, you might see dhcpd with a pid of 1001 and ntpd with a pid of 1002. If the box dies violently (e.g. power cut), the dhcpd pid file will contain 1001. On the second boot, assume ntpd starts first and gets a pid of 1001 and dhcpd is 1002. Now, the <code>kill(pid, 0)</code> will succeed, making it appear that dhcpd is already running, and dhcpd will exit.</p>
<p>How to fix this?</p>
<ol>
<li>Explicitly put the pid file under /tmp. Getting this right is fussy &#8212; make sure you avoid the race conditions associated with creating temp files. Use dhcpd&#8217;s &#8220;-pf&#8221; flag to tell it where to use the pid file. This avoids spurious &#8220;already running&#8221; messages, because dhcpd will never read a pid from an existing pid file. [You could also just remove the /var/run/dhcpd.pid file, but I'd rather explicitly provide the path in my startup script in case some dim bulb decides to change the compiled-in default.]</li>
<li>Be careful in your restart code to kill any existing dhcpd (assuming you really want a new dhcpd), or avoid trying to start a new one (assuming you want to use an already running dhcpd). <code>pgrep(1)</code> and <code>pkill(1)</code> will be useful here.</li>
</ol>
<p>In researching this, I saw this <a href="http://openbsd.monkey.org/misc/200601/msg00735.html">bit of wisdom from Henning Brauer</a>: &#8220;pid files are useless.&#8221;.</p>
<p>I heartily agree&#8230;</p>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/who-else-wants-better-short-term-memory' rel='bookmark' title='Permanent Link: Who Else Wants Better Short Term Memory?'>Who Else Wants Better Short Term Memory?</a> <small> In &#8220;Talent is Overrated&#8221;, Geoff...</small></li><li><a href='http://blog.bstpierre.org/local-ssh-forwarding' rel='bookmark' title='Permanent Link: Use Local SSH Forwarding to Reduce the Number of Manual Hops'>Use Local SSH Forwarding to Reduce the Number of Manual Hops</a> <small>Local port forwarding is the same...</small></li><li><a href='http://blog.bstpierre.org/must-have-tools-software-teams' rel='bookmark' title='Permanent Link: 9 &#8220;Must-Have&#8221; Tools for Software Teams'>9 &#8220;Must-Have&#8221; Tools for Software Teams</a> <small> The items below are useful...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/pid-file-race/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hassle Free Way to Kill Sudo&#8217;d Jobs</title>
		<link>http://blog.bstpierre.org/hassle-free-way-to-kill-sudod-jobs</link>
		<comments>http://blog.bstpierre.org/hassle-free-way-to-kill-sudod-jobs#comments</comments>
		<pubDate>Thu, 13 Aug 2009 00:45:28 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=189</guid>
		<description><![CDATA[Every now and then I have to run a foreground job under sudo that doesn&#8217;t want to die when I hit ^C. Then it&#8217;s a hassle to ^Z, get the pid of the sudo job, and sudo kill that pid.
So I wrote a little script (or a template for scripts) that runs the sudo job [...]


Related posts:<ol><li><a href='http://blog.bstpierre.org/lenny-jaunty-packages' rel='bookmark' title='Permanent Link: Using Lenny to Build Jaunty Packages'>Using Lenny to Build Jaunty Packages</a> <small> &lt;shameless_plug&gt; I&#8217;m in the middle...</small></li><li><a href='http://blog.bstpierre.org/pid-file-race' rel='bookmark' title='Permanent Link: An Interesting pid File Race'>An Interesting pid File Race</a> <small> ISC&#8217;s dhcpd uses this code...</small></li><li><a href='http://blog.bstpierre.org/moving-to-zsh' rel='bookmark' title='Permanent Link: Moving to zsh'>Moving to zsh</a> <small> To get started: sudo aptitude...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Every now and then I have to run a foreground job under sudo that doesn&#8217;t want to die when I hit ^C. Then it&#8217;s a hassle to ^Z, get the pid of the sudo job, and sudo kill that pid.</p>
<p>So I wrote a little script (or a template for scripts) that runs the sudo job in the background (but preserves stdout/stderr) and relies on bash to clean up the job when you ^C the script. Only gotcha with this is that you may have to retype your sudo password when you ^C if your authentication has timed out by the time you get around to killing it.</p>
<pre>#!/bin/bash

function cleanup()
{
    sudo kill $job_pid
    wait $job_pid
    exit 0
}

trap cleanup SIGTERM
trap cleanup SIGINT

sudo long_running_foreground_process &amp;
job_pid=$!
wait</pre>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/lenny-jaunty-packages' rel='bookmark' title='Permanent Link: Using Lenny to Build Jaunty Packages'>Using Lenny to Build Jaunty Packages</a> <small> &lt;shameless_plug&gt; I&#8217;m in the middle...</small></li><li><a href='http://blog.bstpierre.org/pid-file-race' rel='bookmark' title='Permanent Link: An Interesting pid File Race'>An Interesting pid File Race</a> <small> ISC&#8217;s dhcpd uses this code...</small></li><li><a href='http://blog.bstpierre.org/moving-to-zsh' rel='bookmark' title='Permanent Link: Moving to zsh'>Moving to zsh</a> <small> To get started: sudo aptitude...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/hassle-free-way-to-kill-sudod-jobs/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Python&#8217;s ctypes to Call Into C Libraries</title>
		<link>http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls</link>
		<comments>http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls#comments</comments>
		<pubDate>Wed, 05 Aug 2009 10:35:55 +0000</pubDate>
		<dc:creator>Brian St. Pierre</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.bstpierre.org/?p=185</guid>
		<description><![CDATA[The ctypes module makes loading and calling into a dynamic library incredibly easy.


Related posts:<ol><li><a href='http://blog.bstpierre.org/pid-file-race' rel='bookmark' title='Permanent Link: An Interesting pid File Race'>An Interesting pid File Race</a> <small> ISC&#8217;s dhcpd uses this code...</small></li><li><a href='http://blog.bstpierre.org/local-ssh-forwarding' rel='bookmark' title='Permanent Link: Use Local SSH Forwarding to Reduce the Number of Manual Hops'>Use Local SSH Forwarding to Reduce the Number of Manual Hops</a> <small>Local port forwarding is the same...</small></li><li><a href='http://blog.bstpierre.org/configure-ssh-username' rel='bookmark' title='Permanent Link: How to Tell SSH Who You Are'>How to Tell SSH Who You Are</a> <small>Do you log in to several...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The ctypes module makes loading and calling into a dynamic library incredibly easy:</p>
<pre>&gt;&gt;&gt; from ctypes import CDLL
&gt;&gt;&gt; libc = CDLL('libc.so.6')
&gt;&gt;&gt; print libc.strlen('abcde')
5</pre>
<p>As with everything else in python, it gets even better when you scratch the surface. In the example above, CDLL returns an object that represents the dynamic library. You can access the functions in that library by attribute access (&#8220;libc.strlen&#8221;) or item access (&#8220;libc['strlen']&#8220;). Both access mechanisms return a callable object.</p>
<p>This callable object has an &#8220;errcheck&#8221; attribute that can be assigned a callable. We can use this for error-checking our calls into the library. Let&#8217;s write a simple version of the &#8220;kill&#8221; command that uses the kill(2) system call.</p>
<pre>import sys
from ctypes import *

# Load the library.
libc = CDLL('libc.so.6')

# Our error checking function. This will receive the
# return value of the library function, the function that
# was called, and the arguments passed to the function as a
# tuple.
def kill_errcheck(retval, func, funcargs):
    '''Check for error -- retval == -1.'''
    if retval &lt; 0:
        raise Exception('kill%s failed' % (funcargs, ))
    return True

# Get the kill function from the standard library.
kill = libc.kill

# Set the error checker for kill().
kill.errcheck = kill_errcheck

# Pass the command line argument as a pid to kill, with
# SIGSEGV (11).
pid = int(sys.argv[1])
kill(pid, 11)</pre>
<p>Save this as kill.py. Then, in your shell, try something like this:</p>
<pre># Notice that the 3401 is the pid of the process
# we're putting into the background. Yours will
# be different.
bash$ sleep 120&amp;
[1] 3401
bash$ python kill.py 3401
[1]+ Segmentation Fault         sleep 120
bash$ python kill.py 3401
Traceback (most recent call last):
  File "kill.py", line 17, in
    kill(pid, 11)
  File "kill.py", line 10, in kill_errcheck
    raise Exception('kill%s failed' % (funcargs, ))
Exception: kill(3401, 11) failed</pre>
<p>At line 4 of the output we run sleep in the background. At line 5 we learn the pid of this process. At line 6 we run our kill program, giving it the pid we just spawned, and we see the notification from bash that the process was killed (with signal 11, segmentation fault). At line 8 we run our kill program again on pid 3401, but it doesn&#8217;t exist, the kill system call returns -1, and our error checker raises an exception when it detects the system call failure.</p>
<p>But wait, there&#8217;s more&#8230; I&#8217;m working on a follow up post that combines ctypes.Structure with calls into a linux system call.</p>


<p>Related posts:<ol><li><a href='http://blog.bstpierre.org/pid-file-race' rel='bookmark' title='Permanent Link: An Interesting pid File Race'>An Interesting pid File Race</a> <small> ISC&#8217;s dhcpd uses this code...</small></li><li><a href='http://blog.bstpierre.org/local-ssh-forwarding' rel='bookmark' title='Permanent Link: Use Local SSH Forwarding to Reduce the Number of Manual Hops'>Use Local SSH Forwarding to Reduce the Number of Manual Hops</a> <small>Local port forwarding is the same...</small></li><li><a href='http://blog.bstpierre.org/configure-ssh-username' rel='bookmark' title='Permanent Link: How to Tell SSH Who You Are'>How to Tell SSH Who You Are</a> <small>Do you log in to several...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
