Sunday, 14 January, 2001

Always Check the Return Code!

Here's an interesting lesson.  Today I was working on an example of using the exec family of functions (from Linux glibc).  The six functions in this family all do the same thing—launch another program—but each does it subtly differently.  Which one you call depends on whether you want to locate the program in the PATH, and how you want to specify the command line arguments and environment strings.  I got my little test program working fine fairly quickly, and then I ran across a seventh function, called fexecve.  This function works just like execve, except that you specify a file descriptor rather than a path name.  "Simple enough," I thought, and went to work implementing that call.  Except that it didn't work.  No matter what I did, the program would fail.

I finally decided to treat my example hack like a production program and added some error checking.  Sure enough, a call to perror after the call to fexecve failed resulted in this message:  "Function not implemented."  Sure enough, fexecve isn't implemented in any version of glibc.  I don't know why it's not implemented, and in my opinion it shouldn't even be in the library if it doesn't work.  I could probably get some mileage out of a major rant on this one, but I'll restrain myself.

The lesson, of course, and one that I insist on learning over and over, is always check the return code.  Sheesh.  You'd think I'd figure that out after 20 years.