mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] vfork()-based posix_spawn() has more failure modes than fork()-based one
@ 2022-05-02 19:26 Alexey Izbyshev
  2022-05-02 20:49 ` Carlos O'Donell
  2022-05-02 21:18 ` Rich Felker
  0 siblings, 2 replies; 9+ messages in thread
From: Alexey Izbyshev @ 2022-05-02 19:26 UTC (permalink / raw)
  To: musl

Hi,

I was recently made aware via [1] that vfork() can have more failure 
modes than fork() on Linux. The only case I know about is due to Linux 
not allowing processes in different time namespaces to share address 
space, but probably there are or will be more. An example is below 
(requires Linux >= 5.6).

$ cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char *argv[], char *envp[]) {
   if (getenv("TEST_FORK")) {
     pid_t pid = fork();
     if (pid < 0) {
         perror("fork");
         return 127;
     }
     if (pid == 0) {
         execve(argv[1], argv + 1, envp);
         _exit(127);
     }
   } else {
       int err = posix_spawn(0, argv[1], 0, 0, argv + 1, envp);
       if (err) {
         printf("posix_spawn: %s\n", strerror(err));
         return 127;
       }
   }
   wait(NULL);
   return 0;
}

$ musl-gcc test.c
$ unshare -UrT ./a.out /bin/echo OK
posix_spawn: Invalid argument
$ TEST_FORK=1 unshare -UrT ./a.out /bin/echo OK
OK

A common expectation from applications is that they can use 
posix_spawn() as a drop-in replacement for fork()/exec() (when its 
child-tweaking features are sufficient), but this case breaks the 
expectation. Do you think it would make sense for musl to fallback to 
fork() in case vfork() fails in posix_spawn()?

I've also opened a bug about this in glibc[2]. Maybe libcs could 
coordinate in how they handle this case.

Alexey

[1] https://github.com/python/cpython/issues/91307
[2] https://sourceware.org/bugzilla/show_bug.cgi?id=29115

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-02-22 22:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02 19:26 [musl] vfork()-based posix_spawn() has more failure modes than fork()-based one Alexey Izbyshev
2022-05-02 20:49 ` Carlos O'Donell
2022-05-02 21:18 ` Rich Felker
2022-05-02 21:25   ` Florian Weimer
2022-05-02 21:31     ` Rich Felker
2023-02-22 22:04       ` Alexey Izbyshev
2022-05-02 21:31     ` Carlos O'Donell
2022-05-02 21:49       ` Alexey Izbyshev
2022-05-02 21:56         ` Alexey Izbyshev

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).