Proper way to access executable's "environment"?

David Wolfskill david at catwhisker.org
Sun May 26 17:20:26 UTC 2013


A while back, I volunteered to maintain x11-wm/piewm, as it had been
assigned to "ports" and I'm one of the very few folks I know who uses
it.

The vast bulk of this code dates back to tvtwm and twm before that;
the primary source file is still called "twm.c" and had last been
updated 12 June 1998.

While I have been generally using piewm successfully (modulo a
couple of fixes I sent upstream a few years back) in a FreeBSD/i386
environment, I recently decided to experiment with FreeBSD/amd64
on my laptop.

While the experiment worked OK for the most part, I was not at all
happy to find that piewm survived but briefly on invocation in an
amd64 environment.  Further, I found that tvtwm (my fallback WM)
did not exhibit such a failure.  (This is not the first time such
a difference in behavior has been observed.)

After poking around (more like "thrashing") for a bit, I managed
-- somehow -- to determine that piewm was getting a SIGSEGV very
early on, when it attempted to access the execution "environment."
(It turns out that tvtwm doesn't do this.  There is virtue in
simplicity. :-})

I was puzzled by this, as it does the same thing for i386, and didn't
choke & die.

So I started looking at code that I had reason to expect would do this
correctly -- src/usr.bin/env/env.c, for starters -- and saw that while
piewm's twm.c declares main as:

int
main(int argc, char **argv, char **environ)
{
...

and proceeds to use environ as a pointer to a NULL-terminated list
of strings, env.c declares:

extern char **environ;

and the function main is declared:

int
main(int argc, char **argv)
{
...


And that difference struck me as possibly of significance.

So I hacked twm.c, per:

--- twm.c	1998-06-12 13:28:07.000000000 -0700
+++ twm.c	2013-05-10 21:02:32.000000000 -0700
@@ -93,6 +93,7 @@
 #endif
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
 #include <fcntl.h>
 #include "twm.h"
@@ -177,6 +178,8 @@
 
 unsigned long black, white;
 
+extern char **environ;
+
 /***********************************************************************
  *
  *  Procedure:
@@ -186,7 +189,7 @@
  */
 
 int
-main(int argc, char **argv, char **environ)
+main(int argc, char **argv)
 {
     Window root, parent, *children;
     unsigned int nchildren;


rebuilt piewm, et voilà:  no crash.  :-)

I thought this was encouraging, and sent a note to Russ Nelson at
crynwr.com -- the upstream site.  That was on 10 May; I've had no
response.

I subsequently rebuild piewm with the patch applied for my usual
FreeBSD/i386 environment, and it's (also) been trouble-free.  (That
said, I don't actually do anything with the environment for piewm.
Indeed, in nearly all respects, I could use tvtwm without any difference
-- and those that would show up are too arcane to describe here.)

So I have a couple of questions related to the above:
* Is the patch correct?  I don't do much with C, and even less with C
  code that accesses the environment.  (When I write code, it's normally
  scripts, mostly in /bin/sh.)  getenv(3) mentioned the "#include
  <stdlib.h>", so I did that based on theat man page.  I didn't see
  anything about declaring environ as an "extern char **", but wasn't
  keen on making more changes than necessary to the code.  (Mind, I
  fully support making "necessary" -- or even "strongly advised" --
  changes.)

* I also prefer getting the changes pushed upstream -- I believe I was
  reponsible for the previous couple of changes to piewm, because of bugs
  I found.  But so far, "upstream" hasn't responded, and the code (as it
  presently stands) is demonstrably broken for amd64.

  All that said, it's fair to note that (at best), piewm is a
  vanishingly small niche.  However, other than tvtwm, I haven't
  found a WM with which I am anywhere near as comfortable; thus, I
  have a rather vested interest in ensuring that it continues to
  work.

  But if Russ Nelson is no longer in a position to be both willing &
  able to host the upstream site, I'm willing to do it myself.

  What's a reasonable way for me to proceed?

(I'm subscribed to ports@; no need to Cc: me.)

Thanks!

Peace,
david
-- 
David H. Wolfskill				david at catwhisker.org
Taliban: Evil men with guns afraid of truth from a 14-year old girl.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-ports/attachments/20130526/cc34b77a/attachment.sig>


More information about the freebsd-ports mailing list