mirror of
https://github.com/GnoConsortium/gno.git
synced 2024-09-29 09:56:26 +00:00
reimplement and fix popen/pclose routines
This commit is contained in:
parent
1554cedb32
commit
b94726dc62
@ -1,90 +1,167 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1988 The Regents of the University of California.
|
* This is a complete rewrite of the popen/pclose routines.
|
||||||
* All rights reserved.
|
* Devin Reade, 1997.
|
||||||
*
|
*
|
||||||
* This code is derived from software written by Ken Arnold and
|
* $Id: popen.c,v 1.2 1997/09/21 06:04:01 gdr Exp $
|
||||||
* published in UNIX Review, Vol. 6, No. 8.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* This file is formatted for tab stops every 8 columns.
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. All advertising materials mentioning features or use of this software
|
|
||||||
* must display the following acknowledgement:
|
|
||||||
* This product includes software developed by the University of
|
|
||||||
* California, Berkeley and its contributors.
|
|
||||||
* 4. Neither the name of the University nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef __ORCAC__
|
#ifdef __ORCAC__
|
||||||
segment "libc_gen__";
|
segment "libc_gen__";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma optimize 0
|
|
||||||
#pragma debug 0
|
|
||||||
#pragma memorymodel 0
|
|
||||||
|
|
||||||
#if defined(LIBC_SCCS) && !defined(lint)
|
|
||||||
static char *sccsid = "from: @(#)popen.c 5.15 (Berkeley) 2/23/91";
|
|
||||||
static char *rcsid = "popen.c,v 1.5 1993/08/26 00:44:55 jtc Exp";
|
|
||||||
#endif /* LIBC_SCCS and not lint */
|
|
||||||
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <assert.h>
|
#include <sys/queue.h>
|
||||||
|
#ifdef __GNO__
|
||||||
|
#include <gno/gno.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static pid_t *pids;
|
#include <assert.h> /* shouldn't be necessary in final code */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This #define makes us create a temporary file containing the
|
||||||
|
* commands to execute rather than giving them on the gsh command
|
||||||
|
* line. Gsh has a problem with accepting command line arguments ...
|
||||||
|
*/
|
||||||
|
#define USE_SCRIPT 1
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We maintain state information by having a linked list of these
|
||||||
|
* structs.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static SLIST_HEAD(popenStateHead, popenEntry_t) popenHead;
|
||||||
|
static struct popenStateHead *popenHeadPtr = NULL;
|
||||||
|
|
||||||
|
typedef struct popenEntry_t {
|
||||||
|
SLIST_ENTRY(popenEntry_t) pr_link; /* link */
|
||||||
|
FILE *pr_fp; /* the FILE pointer */
|
||||||
|
pid_t pr_pid; /* pid of the child */
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
char *pr_name; /* name of the "script" file */
|
||||||
|
#endif
|
||||||
|
} popenEntry_t;
|
||||||
|
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
|
||||||
|
/*
|
||||||
|
* _popen_script -- create a temporary file of type EXEC that contains
|
||||||
|
* the set of commands that we will execute. This
|
||||||
|
* is a Real Kludge to get around a problem with gsh
|
||||||
|
* ignoring it's command line flags (-c in particular).
|
||||||
|
*/
|
||||||
|
static char *
|
||||||
|
_popen_script (const char *command) {
|
||||||
|
/* the number of chars in prefix comes from "popen<pid>":
|
||||||
|
* "popen" 5
|
||||||
|
* <pid> up to 6 (for a 16-bit pid)
|
||||||
|
* null 1
|
||||||
|
*/
|
||||||
|
char prefix[12];
|
||||||
|
char *result;
|
||||||
|
FILE *fp;
|
||||||
|
int i;
|
||||||
|
#ifdef __GNO__
|
||||||
|
int oldEmulate;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
sprintf(prefix, "popen%u", getpid());
|
||||||
|
if ((result = tempnam(NULL, prefix)) == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if ((fp = fopen(result, "w")) == NULL) {
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
fprintf(fp, "%s\n", command);
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
/* change file/aux type to SRC/EXEC */
|
||||||
|
#ifdef __GNO__
|
||||||
|
oldEmulate = _setModeEmulation (1);
|
||||||
|
#endif
|
||||||
|
i = chmod(result, S_IRWXU);
|
||||||
|
#ifdef __GNO__
|
||||||
|
_setModeEmulation(oldEmulate);
|
||||||
|
#endif
|
||||||
|
if (i == -1) {
|
||||||
|
i = errno;
|
||||||
|
free(result);
|
||||||
|
errno = i;
|
||||||
|
result = NULL;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* USE_SCRIPT */
|
||||||
|
|
||||||
|
#pragma optimize 78 /* bits 3 and 6, minimum */
|
||||||
|
#pragma debug 0
|
||||||
|
|
||||||
|
/* change this when we have an sh(1) for GNO */
|
||||||
|
#ifdef __GNO__
|
||||||
|
#define SHELL_PATH _PATH_GSHELL
|
||||||
|
#define SHELL_NAME "gsh"
|
||||||
|
#else
|
||||||
|
#define SHELL_PATH _PATH_BSHELL
|
||||||
|
#define SHELL_NAME "sh"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
#define SH_CFLAG
|
||||||
|
#else
|
||||||
|
#define SH_CFLAG "-c",
|
||||||
|
#error this is broken
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __GNO__
|
#ifdef __GNO__
|
||||||
#pragma databank 1
|
#pragma databank 1
|
||||||
static void
|
static void
|
||||||
_popen_child(int *pdes, char type, char *command)
|
_popen_child(int *pdes, char *type, char *command)
|
||||||
{
|
{
|
||||||
if (type == 'r') {
|
char buffer[128];
|
||||||
|
strcpy(buffer, "/bin/gsh ");
|
||||||
|
strcat(buffer, command);
|
||||||
|
|
||||||
|
if (*type == 'r') {
|
||||||
if (pdes[1] != STDOUT_FILENO) {
|
if (pdes[1] != STDOUT_FILENO) {
|
||||||
(void) dup2(pdes[1], STDOUT_FILENO);
|
if (dup2(pdes[1], STDOUT_FILENO) == -1) {
|
||||||
(void) close(pdes[1]);
|
_exit(126);
|
||||||
}
|
}
|
||||||
(void) close(pdes[0]);
|
close(pdes[1]);
|
||||||
|
}
|
||||||
|
close(pdes[0]);
|
||||||
} else {
|
} else {
|
||||||
if (pdes[0] != STDIN_FILENO) {
|
if (pdes[0] != STDIN_FILENO) {
|
||||||
(void) dup2(pdes[0], STDIN_FILENO);
|
if (dup2(pdes[0], STDIN_FILENO) == -1 ) {
|
||||||
(void) close(pdes[0]);
|
_exit(126);
|
||||||
}
|
}
|
||||||
(void) close(pdes[1]);
|
close(pdes[0]);
|
||||||
}
|
}
|
||||||
|
close(pdes[1]);
|
||||||
/* change this when we have an sh(1) for GNO */
|
}
|
||||||
#if 1
|
#if 0
|
||||||
execl(_PATH_GSHELL, "gsh", "-c", command, (char *) 0);
|
execl(SHELL_PATH, SHELL_NAME, SH_CFLAG command, (char *) 0);
|
||||||
#else
|
#else
|
||||||
execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0);
|
_execve("/bin/gsh", buffer);
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
p = strerror(errno);
|
||||||
|
write(STDERR_FILENO, p, strlen(p));
|
||||||
|
write(STDERR_FILENO, "\r", 1);
|
||||||
|
}
|
||||||
_exit(127);
|
_exit(127);
|
||||||
}
|
}
|
||||||
#pragma databank 0
|
#pragma databank 0
|
||||||
@ -94,108 +171,181 @@ _popen_child(int *pdes, char type, char *command)
|
|||||||
FILE *
|
FILE *
|
||||||
popen(const char *command, const char *type)
|
popen(const char *command, const char *type)
|
||||||
{
|
{
|
||||||
FILE *iop;
|
static int pdes[2];
|
||||||
int fds, pid;
|
popenEntry_t *entry = NULL;
|
||||||
int pdes[2];
|
int err;
|
||||||
|
|
||||||
if (*type != 'r' && *type != 'w' || type[1]) {
|
/* initialize the status pointer if necessary */
|
||||||
|
if (popenHeadPtr == NULL) {
|
||||||
|
popenHeadPtr = &popenHead;
|
||||||
|
SLIST_INIT(popenHeadPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* verify legality of access mode */
|
||||||
|
if ((*type != 'r' && *type != 'w') || type[1]) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return (NULL);
|
goto fail;
|
||||||
}
|
|
||||||
if (pids == NULL) {
|
|
||||||
if ((fds = getdtablesize()) <= 0) {
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
if ((pids = malloc(fds * sizeof(int))) == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
bzero((char *) pids, fds * sizeof(pid_t));
|
|
||||||
}
|
|
||||||
if (pipe(pdes) < 0) {
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __GNO__
|
/* get a new status element */
|
||||||
pid = fork2(_popen_child, 1024, 0, "forked child of popen", 5,
|
if ((entry = malloc(sizeof(popenEntry_t))) == NULL) {
|
||||||
pdes, *type, command);
|
goto fail;
|
||||||
switch (pid) {
|
}
|
||||||
#else
|
entry->pr_fp = NULL;
|
||||||
switch (pid = vfork()) {
|
entry->pr_pid = 0;
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
entry->pr_name = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* create the pipes */
|
||||||
|
if (pipe(pdes) < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
if ((command = _popen_script(command)) == NULL) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
entry->pr_name = (char *) command;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __GNO__
|
||||||
|
entry->pr_pid = fork2(_popen_child, 4096, 0,
|
||||||
|
"forked child of popen", 6, pdes, type,
|
||||||
|
command);
|
||||||
|
switch (entry->pr_pid) {
|
||||||
|
#else
|
||||||
|
switch (entry->pr_pid = vfork()) {
|
||||||
|
#endif
|
||||||
case -1: /* error */
|
case -1: /* error */
|
||||||
(void) close(pdes[0]);
|
err = errno;
|
||||||
(void) close(pdes[1]);
|
close(pdes[0]);
|
||||||
return (NULL);
|
close(pdes[1]);
|
||||||
|
errno = err;
|
||||||
|
goto fail;
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
|
|
||||||
#ifndef __GNO__
|
#ifndef __GNO__
|
||||||
case 0: /* child */
|
case 0: /* child */
|
||||||
if (*type == 'r') {
|
if (*type == 'r') {
|
||||||
if (pdes[1] != STDOUT_FILENO) {
|
if (pdes[1] != STDOUT_FILENO) {
|
||||||
(void) dup2(pdes[1], STDOUT_FILENO);
|
if (dup2(pdes[1], STDOUT_FILENO) == -1) {
|
||||||
(void) close(pdes[1]);
|
_exit(126);
|
||||||
}
|
}
|
||||||
(void) close(pdes[0]);
|
close(pdes[1]);
|
||||||
|
}
|
||||||
|
close(pdes[0]);
|
||||||
} else {
|
} else {
|
||||||
if (pdes[0] != STDIN_FILENO) {
|
if (pdes[0] != STDIN_FILENO) {
|
||||||
(void) dup2(pdes[0], STDIN_FILENO);
|
if (dup2(pdes[0], STDIN_FILENO) == -1 ) {
|
||||||
(void) close(pdes[0]);
|
_exit(126);
|
||||||
}
|
}
|
||||||
(void) close(pdes[1]);
|
close(pdes[0]);
|
||||||
}
|
}
|
||||||
execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0);
|
close(pdes[1]);
|
||||||
|
}
|
||||||
|
execl(SHELL_PATH, SHELL_NAME, SH_CFLAG command, (char *) 0);
|
||||||
_exit(127);
|
_exit(127);
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
#endif /* ! __GNO__ */
|
#endif /* ! __GNO__ */
|
||||||
}
|
}
|
||||||
/* parent; assume fdopen can't fail... */
|
|
||||||
|
/* parent */
|
||||||
if (*type == 'r') {
|
if (*type == 'r') {
|
||||||
iop = fdopen(pdes[0], type);
|
if ((entry->pr_fp = fdopen(pdes[0], type)) == NULL) {
|
||||||
(void) close(pdes[1]);
|
close(pdes[0]);
|
||||||
|
}
|
||||||
|
close(pdes[1]);
|
||||||
} else {
|
} else {
|
||||||
iop = fdopen(pdes[1], type);
|
if ((entry->pr_fp = fdopen(pdes[1], type)) == NULL) {
|
||||||
(void) close(pdes[0]);
|
close(pdes[1]);
|
||||||
|
}
|
||||||
|
close(pdes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (entry->pr_fp != NULL) {
|
||||||
* this assert may be removed when getdtablesize is replaced by
|
SLIST_INSERT_HEAD(popenHeadPtr, entry, pr_link);
|
||||||
* a kernel system call rather than just a stub returning OPEN_MAX
|
return entry->pr_fp;
|
||||||
*/
|
}
|
||||||
assert(fileno(iop) < getdtablesize());
|
/*FALLTHROUGH*/
|
||||||
|
|
||||||
pids[fileno(iop)] = pid;
|
fail:
|
||||||
return (iop);
|
/*
|
||||||
|
* OK, so goto's are passe' -- but it reduces the size of the
|
||||||
|
* generated code.
|
||||||
|
*/
|
||||||
|
if (entry) {
|
||||||
|
err = errno;
|
||||||
|
if (entry->pr_fp == NULL) {
|
||||||
|
if (entry->pr_pid > 0) {
|
||||||
|
kill(entry->pr_pid, SIGKILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (entry->pr_name) {
|
||||||
|
unlink(entry->pr_name);
|
||||||
|
free(entry->pr_name);
|
||||||
|
}
|
||||||
|
free(entry);
|
||||||
|
errno = err;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
#ifdef __GNO__
|
||||||
pclose(FILE * iop)
|
#define WAITSTAT_TYPE union wait *
|
||||||
{
|
#else
|
||||||
register int fdes;
|
#define WAITSTAT_TYPE int *
|
||||||
int omask;
|
#endif
|
||||||
union wait pstat;
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
|
/*
|
||||||
/*
|
|
||||||
* this assert may be removed when getdtablesize is replaced by
|
|
||||||
* a kernel system call rather than just a stub returning OPEN_MAX
|
|
||||||
*/
|
|
||||||
assert(fileno(iop) < getdtablesize());
|
|
||||||
|
|
||||||
/*
|
|
||||||
* pclose returns -1 if stream is not associated with a
|
* pclose returns -1 if stream is not associated with a
|
||||||
* `popened' command, if already `pclosed', or waitpid
|
* `popened' command, if already `pclosed', or waitpid
|
||||||
* returns an error.
|
* returns an error.
|
||||||
*/
|
*/
|
||||||
fdes = fileno(iop);
|
|
||||||
if (pids == NULL || pids[fdes] == 0) {
|
int
|
||||||
return (-1);
|
pclose(FILE * iop)
|
||||||
|
{
|
||||||
|
union wait pstat;
|
||||||
|
pid_t pid;
|
||||||
|
popenEntry_t *np;
|
||||||
|
|
||||||
|
if (iop == NULL || popenHeadPtr == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
(void) fclose(iop);
|
|
||||||
|
for (np = popenHeadPtr->slh_first; np != NULL;
|
||||||
|
np = np->pr_link.sle_next) {
|
||||||
|
if (np->pr_fp == iop) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (np == NULL) {
|
||||||
|
/* iop not opened by popen(3) */
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
assert(np->pr_name != NULL);
|
||||||
|
assert(np->pr_name[0] != '\0');
|
||||||
|
SLIST_REMOVE(popenHeadPtr, np, popenEntry_t, pr_link);
|
||||||
|
|
||||||
|
/* Close it, and wait for the child to exit. */
|
||||||
|
fclose(iop);
|
||||||
|
#if 1
|
||||||
|
pid = wait((WAITSTAT_TYPE) &pstat);
|
||||||
|
#else
|
||||||
do {
|
do {
|
||||||
pid = waitpid(pids[fdes], &pstat, 0);
|
pid = waitpid(np->pr_pid, (WAITSTAT_TYPE) &pstat, 0);
|
||||||
} while (pid == -1 && errno == EINTR);
|
} while (pid == -1 && errno == EINTR);
|
||||||
pids[fdes] = 0;
|
#endif
|
||||||
|
|
||||||
|
/* clean up the structures */
|
||||||
|
#ifdef USE_SCRIPT
|
||||||
|
unlink(np->pr_name);
|
||||||
|
free(np->pr_name);
|
||||||
|
#endif
|
||||||
|
free(np);
|
||||||
return (pid == -1 ? -1 : pstat.w_retcode);
|
return (pid == -1 ? -1 : pstat.w_retcode);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user