From: Eli Zaretskii <eliz@gnu.org>
To: Spencer Baugh <sbaugh@janestreet.com>
Cc: emacs-devel@gnu.org
Subject: Re: Environment variables in dynamic modules
Date: Thu, 11 Jan 2024 19:02:22 +0200 [thread overview]
Message-ID: <835xzzls69.fsf@gnu.org> (raw)
In-Reply-To: <ierwmsf5036.fsf@janestreet.com> (message from Spencer Baugh on Thu, 11 Jan 2024 11:03:25 -0500)
> From: Spencer Baugh <sbaugh@janestreet.com>
> Date: Thu, 11 Jan 2024 11:03:25 -0500
>
>
> In Emacs, process-environment (read by Lisp getenv) is distinct from C
> environment variables (read by C getenv).
>
> This means that a dynamic module which links against a library which
> reads environment variables will not be affected by changes to
> process-environment.
>
> For example, if a user calls (setenv "VAR" "value") or binds
> process-environment to (cons (cons "VAR" "value" process-environment)),
> a getenv("VAR") in the dynamic module library won't return "value".
> Likewise, if a dynamic module spawns subprocesses, they will inherit the
> environment that the Emacs process started with, not the current
> environment in process-environment.
>
> This is usually unexpected, and causes difficult-to-track-down bugs,
> especially for dynamic modules that spawn subprocesses or for large
> dynamic modules with lots of functionality.
>
> There are a number of possible ways to solve it:
>
> A. Carefully track down every place that a library reads environment
> variables or spawns subprocesses, and pass in the Emacs environment
> instead.
> (but this is intractable in modules which call other libraries)
>
> B. Advise Elisp setenv to also change the C environment
> (but this doesn't work with let-bindings of process-environment)
>
> C. Set all variables in the C environment to match process-environment
> every time we call into the dynamic module
> (but this is slow and hurts performance)
>
> D. Use linker tricks to replace C getenv with a version which calls back
> into Emacs.
> (but this doesn't work on other threads, since we can only call into
> Emacs from the main thread)
>
> None of these are particularly satisfying. I have implemented D, but
> since my module uses multiple threads, it doesn't really solve the
> problem for me.
>
> Any suggestions?
The only correct solution is C, and you should only do that in the
module when the module calls something that really needs the
environment. D is not portable, since a library that was linked to
use getenv from libc will not necessarily call getenv that you
provided, at least not in all systems. I don't know why you say C is
slow; did you time it? You could, of course, set only some of the
environment variables, those that matter, instead of setting all of
them.
Libraries that want to allow their callers to manipulate the
environment should do that themselves where needed. Only the library
and its callers know when it's TRT to modify the environment and when
it is not, so no 3rd-part code (such as something in Emacs) can do
that for them. For example, how Emacs know that some library called
by some other library that your module calls needs to see the modified
environment that Emacs uses?
It makes little sense to me to do this in Emacs, since most modules
don't care about the changes to process-environment done by Emacs.
next prev parent reply other threads:[~2024-01-11 17:02 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-11 16:03 Environment variables in dynamic modules Spencer Baugh
2024-01-11 17:02 ` Eli Zaretskii [this message]
2024-01-11 17:53 ` Spencer Baugh
2024-01-11 17:18 ` Andreas Schwab
-- strict thread matches above, loose matches on Subject: below --
2024-01-12 0:29 Psionic K
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=835xzzls69.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=sbaugh@janestreet.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).