From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by inbox.vuxu.org (OpenSMTPD) with SMTP id 1c223e02 for ; Fri, 7 Feb 2020 21:03:46 +0000 (UTC) Received: (qmail 11499 invoked by uid 550); 7 Feb 2020 21:03:44 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 11475 invoked from network); 7 Feb 2020 21:03:44 -0000 Date: Fri, 7 Feb 2020 16:03:31 -0500 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20200207210331.GK1663@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: Rich Felker Subject: Re: [musl] fopen with "e" mode to close file descriptor in exec... functions 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 > #include > #include > > #include > #include > #include > > 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