mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [musl] fopen with "e" mode to close file descriptor in exec... functions
Date: Fri, 7 Feb 2020 16:03:31 -0500	[thread overview]
Message-ID: <20200207210331.GK1663@brightrain.aerifal.cx> (raw)
In-Reply-To: <CA+dhEd5BMvfw9k89HyOR6hwObSunTZrFZut1uA1bVdF6PY7_RA@mail.gmail.com>

On Fri, Feb 07, 2020 at 09:22:01PM +0300, Alexander Scherbatiy wrote:
> Hello,
> 
> Below is a simple program without any errors handling that uses
> fopen with "ae" mode to open a "test.log" file with append access
> and call posix_spawn to create a child process and list all open files for
> the child process.
> 
> The program output shows that the child process has "test.log" file as open.
> I used docker with Alpine Linux 3.11.3 (musl libc x86_64 1.1.24).
> Is it an expected behaviour for Alpine Linux?
> 
> The same program on my Ubuntu 19.10 (with "ash" changed to "bash") shows
> that
> the child process doesn't have  "test.log" file as open.
> 
> Thanks,
> Alexander.
> 
> ----------  posix_spawn_sample.c ----------
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> 
> #include <unistd.h>
> #include <spawn.h>
> #include <sys/wait.h>
> 
> extern char **environ;
> 
> int main()
> {
>     FILE* file = fopen("test.log", "ae");
>     fprintf(file, "test line\n");
> 
>     pid_t pid;
>     char *argv[] = {"bash", "-c", "echo PID=$$ && lsof -p $$", NULL};
>     int status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
>     printf("Child pid: %i\n", pid);
>     waitpid(pid, &status, 0);
> 
>     fclose(file);
>     return 0;
> }
> --------------------------

I can't reproduce this. I had to change /bin/sh to /bin/bash to be
able to run the test at all, and the lsof I have (busybox) doesn't
support -p, but I can't see any indication that the child has the log
file open. I also tried changing it to run "ls -l /proc/$$/fd" in the
child, and a fd 3 shows up there but prints an error because fd 3 is
the directory fd for listing /proc/$$/fd and it's closed before trying
to stat it -- apparently ash execs ls rather than running it as a
child. Indeed adding "; echo" makes that go away.

If you're still having the problem, could you attach a log of running
your test under "strace -f"?

Rich

  reply	other threads:[~2020-02-07 21:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-07 18:22 Alexander Scherbatiy
2020-02-07 21:03 ` Rich Felker [this message]
2020-02-08  7:45   ` Alexander Scherbatiy
2020-02-08 16:19     ` Rich Felker
2020-02-10  9:57       ` Alexander Scherbatiy
2020-02-10 14:56         ` Rich Felker

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200207210331.GK1663@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.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).