deadlocks due to setenv in lookup_program.c
Subsystems (2)
Hi,
When using the lookup_program module, we've seen an issue where the main automount process waits forever for the forked child to produce output (and quit). This causes whatever processes that require the automount to wait on autofs_wait.
Coredump of the forked child showed that it was stuck waiting on a lock when calling setenv (it never went so far as doing the execl):
#0 __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63 #1 0x00007ffff60505ce in __add_to_environ (name=0x55555558b2e0 "autodir", value=0x55555558b300 "/a",
combined=0x0, replace=1) at setenv.c:133
This seems to be caused by the macro_setenv call in lookup_one in modules/lookup_program.c -- that calls into glibc's setenv, and depending on when exactly the process was forked, it could be looking at a locked envlock.
I guess there are two approaches which to fixing the issue and I'd like to get some thoughts before sending a patch:
1. Not calling out to any environment-mutating functions (clearenv, putenv, setenv) which can hold the envlock after the initial setup. In our deployment, the only place this happens is macro_setenv and the sd_notify call in daemon/automount.c.
2. Avoiding the use of setenv in the forked child (it is MT-Unsafe anyway). We can copy environ and use something like execle.
I tested both patches about a week ago which seemed to fix the problem; there are no more deadlocks of this shape.
Ian Kent
maintainerQuoted 22 lines · Jun Eeo
Hi,
When using the
lookup_programmodule, we've seen an issue where the main automount process waits forever for the forked child to produce output (and quit). This causes whatever processes that require the automount to wait onautofs_wait.Coredump of the forked child showed that it was stuck waiting on a lock when calling setenv (it never went so far as doing the execl):
#0
__lll_lock_wait_private() at../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63#10x00007ffff60505cein__add_to_environ(name=0x55555558b2e0 "autodir", value=0x55555558b300 "/a",combined=0x0, replace=1) at setenv.c:133This seems to be caused by the
macro_setenvcall inlookup_oneinmodules/lookup_program.c-- that calls into glibc's setenv, and depending on when exactly the process was forked, it could be looking at a locked envlock.I guess there are two approaches which to fixing the issue and I'd like to get some thoughts before sending a patch:
Quoted 7 lines
1. Not calling out to any environment-mutating functions (clearenv, putenv, setenv) which can hold the envlock after the initial setup. In our deployment, the only place this happens is
macro_setenvand thesd_notifycall indaemon/automount.c.2. Avoiding the use of setenv in the forked child (it is MT-Unsafe anyway). We can copy environ and use something like execle.
Certain macros are required to implement the Sun map functionality
so not using environment setting functions isn't really ok I think.
Sounds like not using env modifying calls in the forked child is
preferable although the child environment is independent in this
case. From what I've seen MT-unsafe problems occur when you do
something in the parent MT environment and don't undo it in both
the parent and forked child.
Quoted 3 lines
I tested both patches about a week ago which seemed to fix the problem; there are no more deadlocks of this shape.
Post patches and we can discuss the implications.
I'm happy to change things if needed. But it sounds a little bit
like there's a glibc bug at play (unlikely) too, we'll need to
understand what's going on there too.
Ian
PATCH 1/1 autofs-5.1.9 - don't call unsetenv in sd_notify
Avoid a stray unsetenv after the main thread starts servicing automounts. One potential issue this may cause is:
main thread
main()
-> sd_notify(1, ...)
-> unsetenv()
worker thread:
lookup_program.c:lookup_one()
-> fork() # if the fork happened just before unsetenv in sd_notify,
# the envlock in glibc's setenv.c will be in a locked state.
-> macro_setenv() # in the child
-> setenv()
---
daemon/automount.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)daemon/automount.c +1 -1
@@ -2739,7 +2739,7 @@ int main(int argc, char *argv[])#ifdef WITH_SYSTEMD if (flags &DAEMON_FLAGS_SYSTEMD_SERVICE ) -sd_notify (1 ,"READY=1" ); +sd_notify (0 ,"READY=1" );#endif signal_handler_thid =pthread_self ();
Signature · 1 line
2.43.7
PATCH 1/1 autofs-5.1.9 - use execle instead of calling out to setenv.
In reply to Ian Kent--- include/macros.h | 4 +- lib/macros.c | 91 ++++++++++++++++++++++++++++++++++++++-- modules/lookup_program.c | 14 +++++-- 3 files changed, 101 insertions(+), 8 deletions(-)
include/macros.h +3 -1
@@ -40,6 +40,8 @@ void macro_free_global_table(void);void macro_free_table (struct substvar *table);const struct substvar *macro_findvar (const struct substvar *table,const char *str,int len); -void macro_setenv (struct substvar *table); + +int clone_environ (char ** envp,char *** envp_ptr); +int macro_setenv (char *** envp,struct substvar *table);#endif
lib/macros.c +87 -4
114 lines not on this page · raw message
modules/lookup_program.c +11 -3
@@ -247,14 +247,22 @@ static char *lookup_one(struct autofs_point *ap,* MAPFMT_DEFAULT must be "sun" for ->parse_init() to have setup * the macro table. */ +char ** envp; +if (clone_environ (environ, &envp)) { +fprintf (stderr,"clone_environ failed.\n" ); +_exit (255 ); + }if (ctxt->mapfmt && !strcmp (ctxt->mapfmt,MAPFMT_DEFAULT )) {struct parse_context *pctxt = (struct parse_context *) ctxt->parse->context;/* Add standard environment as seen by sun map parser */ pctxt->subst =addstdenv (pctxt->subst, prefix); -macro_setenv (pctxt->subst); +if (macro_setenv (&envp, pctxt->subst)) { +fprintf (stderr,"macro_setenv failed.\n" ); +_exit (255 ); + } } -execl (ctxt->mapname, ctxt->mapname, name,NULL ); -_exit (255 );/* execl() failed */ +execle (ctxt->mapname, ctxt->mapname, name,NULL , envp); +_exit (255 );/* execle() failed */ }close (pipefd[1 ]);close (epipefd[1 ]);
Signature · 1 line
2.43.7
Helper for reproducing
In reply to Ian Kent(Apologies for the spam!)
One way to easily reproduce the issue is to run the autofs service as a systemd unit (i.e. with the --systemd-service flag set), compile this and add to LD_PRELOAD in the unit:
----8<----
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
intsd_notify(int unset_environment, const char* state)
{
fprintf(stderr, "sd_notify(%d, %s)\n", unset_environment, state);
int (*fn)(int, const char*) = dlsym(RTLD_NEXT, "sd_notify");
int rv = (*fn)(0, state);
fprintf(stderr, "doing the unsetenv loop\n");
if (unset_environment)
while (1)
unsetenv("NOTIFY_SOCKET");
return rv;
}---->8----
That helper just makes the unsetenv in sd_notify a bit more apparent:
----8<----
_public_ int sd_pid_notify_with_fds(
pid_t pid,
int unset_environment,
const char *state,
const int *fds,
unsigned n_fds) {
// bunch of work
finish:
if (unset_environment)
unsetenv("NOTIFY_SOCKET");
return r;
}// ...
_public_ int sd_notify(int unset_environment, const char *state) {
return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0);
}---->8----
At least in my version of glibc -- this produced the same coredump deadlocks we were seeing in production. If you trigger an automount, the systemd service should then look like:
$ systemctl status autofs
● autofs.service - Automount daemon
Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2025-09-15 13:07:30 BST; 31min ago
Main PID: 3115468 (automount)
Tasks: 15 (limit: 615964)
Memory: 4.5M
CGroup: /system.slice/autofs.service
├─3115468 /usr/sbin/automount --systemd-service --dont-check-daemon -n 1 -t 0 /tmp/my-config
└─3115724 /usr/sbin/automount --systemd-service --dont-check-daemon -n 1 -t 0 /tmp/my-config(i.e. the child process remains stuck forever). The stack trace in the child process should then be something like:
(gdb) bt
#0 __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63 #1 0x00007f110f6505ce in __add_to_environ (name=0x55f89b8643c0 "autodir", value=0x55f89b8643e0 "/a",
combined=0x0, replace=1) at setenv.c:133 #2 0x0000002600000025 in ?? () #3 0x00007f10df7fb4f0 in ?? () #4 0x00007f1110e10540 in ?? () at dl-open.c:200 from /lib64/ld-linux-x86-64.so.2 #5 0x00007f10df7fb460 in ?? () #6 0x0000000000000000 in ?? ()
Ian Kent
maintainerRe: Helper for reproducing
Quoted 5 lines · Jun Eeo
(Apologies for the spam!)
One way to easily reproduce the issue is to run the autofs service as a systemd unit (i.e. with the --systemd-service flag set), compile this and add to
LD_PRELOADin the unit:
Thanks for your effort and the problem descriptions.
It would be best if the second patch also had a description.
I try hard to ensure every patch has a description and I also always
add development attribution (Signed-off-by) to patches. Patches can
hang around for ages of before being committed or be implicated
in problems in some way long after they are committed and having at
least something to help work out what was happening at the time is
useful.
Anyway, I'll have a look at these soon as I can get time.
Thanks,
Ian
Quoted 72 lines
----8<----
#define_GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>int
sd_notify(int unset_environment, const char* state){ fprintf(stderr, "sd_notify(%d, %s)\n", unset_environment, state); int (*fn)(int, const char*) = dlsym(RTLD_NEXT, "sd_notify"); int rv = (*fn)(0, state); fprintf(stderr, "doing the unsetenv loop\n"); if (unset_environment) while (1) unsetenv("NOTIFY_SOCKET"); return rv; }---->8----
That helper just makes the unsetenv in
sd_notifya bit more apparent:----8<----
_public_ int sd_pid_notify_with_fds(
pid_tpid,
intunset_environment,
const char *state,
const int *fds,
unsignedn_fds) {
// bunch of work
finish:if (unset_environment) unsetenv("NOTIFY_SOCKET"); return r; }// ...
_public_ int sd_notify(int unset_environment, const char *state) { return sd_pid_notify_with_fds(0, unset_environment, state, NULL, 0); }---->8----
At least in my version of glibc -- this produced the same coredump deadlocks we were seeing in production. If you trigger an automount, the systemd service should then look like:
$ systemctl status autofs ● autofs.service - Automount daemon Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2025-09-15 13:07:30 BST; 31min ago Main PID: 3115468 (automount) Tasks: 15 (limit: 615964) Memory: 4.5M CGroup: /system.slice/autofs.service ├─3115468 /usr/sbin/automount --systemd-service --dont-check-daemon -n 1 -t 0 /tmp/my-config └─3115724 /usr/sbin/automount --systemd-service --dont-check-daemon -n 1 -t 0 /tmp/my-config(i.e. the child process remains stuck forever). The stack trace in the child process should then be something like:
(gdb) bt
#0__lll_lock_wait_private() at../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63#10x00007f110f6505cein__add_to_environ(name=0x55f89b8643c0 "autodir", value=0x55f89b8643e0 "/a",combined=0x0, replace=1) at setenv.c:133 #2 0x0000002600000025 in ?? () #3 0x00007f10df7fb4f0 in ?? () #4 0x00007f1110e10540 in ?? () at dl-open.c:200 from /lib64/ld-linux-x86-64.so.2 #5 0x00007f10df7fb460 in ?? () #6 0x0000000000000000 in ?? ()