From owner-freebsd-hackers Fri Feb 15 5:56:32 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from snipe.prod.itd.earthlink.net (snipe.mail.pas.earthlink.net [207.217.120.62]) by hub.freebsd.org (Postfix) with ESMTP id 72F3C37B402 for ; Fri, 15 Feb 2002 05:56:28 -0800 (PST) Received: from pool0097.cvx22-bradley.dialup.earthlink.net ([209.179.198.97] helo=mindspring.com) by snipe.prod.itd.earthlink.net with esmtp (Exim 3.33 #1) id 16biqf-0002BC-00; Fri, 15 Feb 2002 05:56:25 -0800 Message-ID: <3C6D137F.87FB3DC5@mindspring.com> Date: Fri, 15 Feb 2002 05:56:15 -0800 From: Terry Lambert X-Mailer: Mozilla 4.7 [en]C-CCK-MCD {Sony} (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Sansonetti Laurent Cc: freebsd-hackers@freebsd.org Subject: Re: Reading userland environnement from the kernel References: <1013782534.2860.5.camel@freebsd.datatest.test.datarescue.be> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Sansonetti Laurent wrote: > Hi hackers, > > Is there a way to read user-land environ(7) table from the kernel for a > given process ? Yes and no, or we'd already have implemented variant symbolic links. The problem is manifold: 1) The environment is pointed to by the environ ** pointer in the user process. The location of the environ ** pointer is not well known. 2) The environ ** value may be overridden by the user program entirely, so the pages where the data lives aren't where the are expected, so a saved pointer to envp *[] at execve time is not a workaround 3) The envrion ** is require by POSUCKS (sometimes spelled "POSIX"), so getting rid of it and making the getenv/setenv/putenv/unsetenv functions use a multiplex system call is not an option that maintains POSIX compliance. 4) It's hard to satisfy #2 and #3 and maintain binary compatability; the gross way you could do this is to save two copies of environ **, the real one at startup, and the shadow one called "environ **", and then if the shadow does not match the real, fall back to the historical behaviour. Synchornizing means that you would need to know when the change happens (not possible, unless you catch a write fault and implicitly fix it up, like SVR4 does with page zero pointer dereferences, unless you specifically tune the kernel to fault fatally on them), or you would have to reflect all kernel level changes into the user space area shadow (expensive, but doable). 5) The execve() envp *[] passing is tricky, at best, for a modified implementation, since you have to read it back to pass it down. One option, which fails POSIX again, is to pass the default in if there is a NULL passed here, for an in kernel implementation (actually, you don't have to pass anything for the user environment, if the system and group contain everything you care about). 6) You can also put the environ ** into user pages (read only) that are also mapped into a pointer off the proc structure (read/write), so that the kernel changes are visible to user space. This makes it so that environ ** is not writable, but it is OK to read it, so a minimum number of changes are required for system/group/user logical names. I run with a variant of #6 on one of my machines; I use the same page I use for the environ ** for the pid, gid, and other data to make them zero kernel overhead for getpid, getppid, getgid, etc. -- basically, any system call that only reads a small fixed sized data value. This still means that the environment is stored in the user space process, but the current environ ** is always known to the kernel, and if it needs to be modified, it takes a system call. It's pretty cool: it lets me set the environment variables for processes from other processes, and everyone inherits from init's environment (system logical name table), the process group leader's (if they aren't the leader themselves: group logical name table) and then themselves, in increasing priority, on getenv(). But of course, it violates the writability of **environ, which POSUCKS wants, but I don't care (on that machine, signals default to restarting system calls so that my user space threads library is incredibly light weight, and getting the one-close-destroys-all-locks-even-for-other-opens behaviour is non-default, too... you have to fcntl(F_POSIX)). -- Terry To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message