From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2961 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: proposed popen replacement using posix_spawn Date: Sat, 23 Mar 2013 21:40:57 -0400 Message-ID: <20130324014057.GX20323@brightrain.aerifal.cx> References: <20130324014017.GW20323@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="LTeJQqWS0MN7I/qa" X-Trace: ger.gmane.org 1364089266 27485 80.91.229.3 (24 Mar 2013 01:41:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 24 Mar 2013 01:41:06 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-2962-gllmg-musl=m.gmane.org@lists.openwall.com Sun Mar 24 02:41:33 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1UJZwD-0001Dj-8D for gllmg-musl@plane.gmane.org; Sun, 24 Mar 2013 02:41:33 +0100 Original-Received: (qmail 8043 invoked by uid 550); 24 Mar 2013 01:41:09 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 8031 invoked from network); 24 Mar 2013 01:41:09 -0000 Content-Disposition: inline In-Reply-To: <20130324014017.GW20323@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:2961 Archived-At: --LTeJQqWS0MN7I/qa Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Mar 23, 2013 at 09:40:17PM -0400, Rich Felker wrote: > Hi all, > > I've rewritten popen using posix_spawn and, based on minimal testing, > it seems to be working. Please take a look and let me know if you see And here's the forgotten attachment. :-) Rich --LTeJQqWS0MN7I/qa Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="popen.c" #include #include #include #include #include "stdio_impl.h" #include "syscall.h" extern char **__environ; FILE *popen(const char *cmd, const char *mode) { int p[2], op, e; pid_t pid; FILE *f; posix_spawn_file_actions_t fa; if (*mode == 'r') { op = 0; } else if (*mode == 'w') { op = 1; } else { errno = EINVAL; return 0; } if (pipe2(p, O_CLOEXEC)) return NULL; f = fdopen(p[op], mode); if (!f) { __syscall(SYS_close, p[0]); __syscall(SYS_close, p[1]); return NULL; } FLOCK(f); /* Remove close-on-exec flag if dup2 will be a no-op in the child */ if (p[1-op] == 1-op) fcntl(1-op, F_SETFD, 0); e = ENOMEM; if (!posix_spawn_file_actions_init(&fa)) { if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) { if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0, (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) { posix_spawn_file_actions_destroy(&fa); f->pipe_pid = pid; fcntl(p[op], F_SETFD, 0); __syscall(SYS_close, p[1-op]); FUNLOCK(f); return f; } } posix_spawn_file_actions_destroy(&fa); } fclose(f); __syscall(SYS_close, p[1-op]); errno = e; return 0; } --LTeJQqWS0MN7I/qa--