Content

Observations from a West Coast family

Bookmarks for 1 July through 19 July

Monday 19 July 2010 - Filed under Bookmarks

Comments Off  ::  Share or discuss  ::  2010-07-19  ::  Stephen

Bookmarks for 8 June through 29 June

Tuesday 29 June 2010 - Filed under Bookmarks

Comments Off  ::  Share or discuss  ::  2010-06-29  ::  Stephen

adirent.[ch]: Adding d_type to struct dirent on OpenSolaris

Sunday 13 June 2010 - Filed under Software

An occasional porting problem you may encounter when compiling programs for OpenSolaris is the absence of d_type in the directory entry structure returned by readdir(3C). I hit this issue when experimenting with mu as a search solution for my accumulated email.

A trivial example of the failure you might see would be caused by the following program:

#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
        DIR *d;
        struct dirent *e;

        if ((d = opendir("/")) == NULL)
                err(1, "opendir failed");

        for (e = readdir(d); e != NULL; e = readdir(d)) {
                    if (e->d_type != DT_UNKNOWN)
                        (void) printf("recognized filetype for '%s'\n",
                            e->d_name);
        }

        (void) closedir(d);

        return (0);
 }

When we attempt to compile this program with gcc, we get something like

$ gcc a.c
a.c: In function `main':
a.c:16: error: structure has no member named `d_type'
a.c:16: error: `DT_UNKNOWN' undeclared (first use in this function)
a.c:16: error: (Each undeclared identifier is reported only once
a.c:16: error: for each function it appears in.)

Studio cc will give similar output:

$ /opt/SunStudioExpress/bin/cc a.c
"a.c", line 16: undefined struct/union member: d_type
"a.c", line 16: undefined symbol: DT_UNKNOWN
cc: acomp failed for a.c

The addition of d_type to struct dirent came first for the BSD Unixes and was later added to Linux. Because it’s not easy to add members to well-known structures and preserve binary compatibility, OpenSolaris and Solaris lack this field, as well as the DT_* constant definitions. (If d_type were to become part of the Unix standards, Solaris would likely have to introduce a second family of opendir()/readdir()/closedir() functions and a second version of the structure, similar to how large files were introduced for 32-bit programs.)

Because we fail at compilation time, our workaround has to modify either the program’s source code or its build environment. (Preloading is too late.) It’s probably possible to combine a few definitions and a shared object that we include via LD_PRELOAD but it seems easier to just provide a C wrapper around readdir(3C) and an alternate struct dirent. We develop this approach in the next section.

DIRENT and READDIR

The approach we take is

  1. Introduce DIRENT and READDIR via adirent.h.
  2. Change the source program such that each call to readdir() is replaced by READDIR() and each use of struct dirent is replaced by DIRENT. In each file so modified, add a #include <adirent.h>.
  3. Compile adirent.c via gcc -I. -O2 -c adirent.c or equivalent.
  4. Add adirent.o to the link line for each binary that includes one of the files modified in step 2.

If we apply these steps to our example above, we get

#include <sys/types.h>
#include <adirent.h>
#include <err.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
        DIR *d;
        DIRENT *e;

        if ((d = opendir("/")) == NULL)
                err(1, "opendir failed");

        for (e = READDIR(d); e != NULL; e = READDIR(d)) {
                    if (e->d_type != DT_UNKNOWN)
                        (void) printf("recognized filetype for '%s'\n",
                            e->d_name);
        }

        (void) closedir(d);

        return (0);
 }

with the result that compilation and execution now work

$ gcc -O2 -I. -c adirent.c
$ gcc -I. a.c adirent.o
$ ./a.out

This shim function and definitions should be sufficient for most ports around this incompatibility, but there are some additional comments worth making.

Performance. Because many programs expect d_type to be one of DT_REG or DT_DIR to save on a stat(2) call, this shim will force those programs into an alleged “slow” path. The actual impact of returning DT_UNKNOWN on every call will be program- and situation-dependent; it didn’t seem to affect my mail indexing.

Multithreaded programs. The current implementation does not protect the static structure defined in adirent.c. Programs with multiple threads performing readdir(3C) calls through READDIR() will get unexpected results. It should be relatively straightforward to dynamically allocate one struct adirent for each thread coming through READDIR() for the first time.

Downloads

I suppose these should be in a repository on Bitbucket or GitHub. For now, they’re just simple downloads:

Acknowledgments

I discussed this problem with Dan, who in particular noted that DT_UNKNOWN was always a legal return value for d_type. Bart looked over my shoulder and spied at least one error during the debugging phase.

Tagged:

Comments Off  ::  Share or discuss  ::  2010-06-13  ::  Stephen

A cruise for World Oceans Day

Saturday 12 June 2010 - Filed under Family + Peninsula + Photo


We went on the Marine Science Institute‘s cruise today, which was in part to celebrate World Oceans Day 2010. Part of the cruise involved sampling the bay floor and a short trawl with a net. (All animals caught are returned to the bay later in the day.) Ben got to hold a juvenile leopard shark.

Comments Off  ::  Share or discuss  ::  2010-06-12  ::  Stephen

Bookmarks for 18 May through 2 June

Wednesday 2 June 2010 - Filed under Bookmarks

Comments Off  ::  Share or discuss  ::  2010-06-02  ::  Stephen

Pipes arrive

Monday 17 May 2010 - Filed under Observations + Peninsula + Photo

Large pipe Another large pipe Still another large pipe
This morning, the pipe segments began to arrive for the section of Bay Division Pipeline No. 5 nearest us. The pipes are a distinct dark blue-green, with the ends capped with paper.

Tagged: »

Comments Off  ::  Share or discuss  ::  2010-05-17  ::  Stephen

Bookmarks for 3 May through 13 May

Thursday 13 May 2010 - Filed under Bookmarks

Comments Off  ::  Share or discuss  ::  2010-05-13  ::  Stephen

Large machine, trench work

Saturday 8 May 2010 - Filed under Observations + Peninsula + Photo

Other side

They assembled this machine this week, but haven’t fired it up. The pipeline runs right behind our property, so I expect we’ll hear the crunching of treads and a large motor (or more than one) fire up early in the morning soon.

Lined up Preparing

When you’re digging a miles-long trench, there’s a clear axis of work. And it looks like they’ll be removing the road surface soon.

Comments Off  ::  Share or discuss  ::  2010-05-08  ::  Stephen

Reasonable photos from 2009

Wednesday 5 May 2010 - Filed under General + Observations + Photo

I’ve started uploading a subset of my photos to Flickr.  Here are four from 2009 I’m reasonably happy with, with some commentary.

Scrub jay
We took a short trip north to the coast in the spring, camping a few nights at the Bodega Dunes site in Sonoma Coast SP. This bird was interested in our activities, although not as aggressive as some distant cousins at San Simeon.

Model Saturn V
NASA Ames had an event last year, with much model rocketry on the airfield. This model Saturn V was one of the most impressive rockets fired, and I was happy to catch it as it took off.

Dock
The dock at the cottage. Weathered, if not gnarly.

Sign
I was amused by the modification to this sign. (O.P.P. is short for Ontario Provincial Police; C.P.O. for Community Policing Office; C-3PO for a particular protocol droid.)

Comments Off  ::  Share or discuss  ::  2010-05-05  ::  Stephen

Tent interior at Peter Pan 360

Wednesday 5 May 2010 - Filed under City + Observations + Photo


We went up to San Francisco to see the Peter Pan show at Ferry Park today. The kids were very excited by the human-powered ticking crocodile. There’s a large concession tent for use before the show and at intermission; this shot is of one of its peaks.

Comments Off  ::  Share or discuss  ::  2010-05-05  ::  Stephen

2010-05-05 :: Stephen // Observations + Peninsula + Photo
El Camino beautification scoop
Comments Off
2010-04-22 :: Stephen // Bookmarks
Bookmarks for 17 March through 22 April
Comments Off
2010-03-04 :: Stephen // Bookmarks
Bookmarks for 8 February through 4 March
Comments Off
2010-01-25 :: Stephen // Bookmarks
Bookmarks for 8 January through 25 January
Comments Off
2010-01-05 :: Stephen // Bookmarks
Bookmarks for 29 November through 2 January
Comments Off
2009-11-10 :: Stephen // Bookmarks
Bookmarks for 26 October through 10 November
Comments Off
2009-10-24 :: Stephen // Bookmarks
Bookmarks for 21 October through 24 October
Comments Off
2009-10-20 :: Stephen // Bookmarks
Bookmarks for 11 October through 20 October
Comments Off
2009-10-05 :: Stephen // Bookmarks
Bookmarks for 2 October through 5 October
Comments Off
2009-09-26 :: Stephen // Bookmarks
Bookmarks for 25 September
Comments Off
2009-05-30 :: Stephen // Family + Observations + Peninsula
A few hours at Maker Faire 2009
Comments Off
Tagged: »