popen.c revision 657b1f3d64bcf8eaa2385dba72a6047f089433b2
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include "synonyms.h"
#include "mtlib.h"
#include "file64.h"
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <thread.h>
#include <pthread.h>
#include <synch.h>
#include <spawn.h>
#include "stdiom.h"
#include "mse.h"
#include "libc.h"
#define RDR 0
#define WTR 1
extern const char **environ;
typedef struct node {
int fd;
} node_t;
/*
* Cancellation cleanup handler.
* If we were cancelled in waitpid(), create a daemon thread to
* reap our abandoned child. No other thread can do this for us.
*/
static void
{
}
FILE *
{
int p[2];
int myside;
int yourside;
int fd;
const char *shpath;
int stdio;
char *argvec[4];
int error;
static const char *shell = "sh";
static const char *sh_flg = "-c";
return (NULL);
return (NULL);
}
(void) posix_spawnattr_destroy(&attr);
return (NULL);
}
if (pipe(p) < 0) {
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
return (NULL);
}
/* myside and yourside reverse roles in child */
/* This will fail more quickly if we run out of fds */
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
return (NULL);
}
/* in the child, close all pipes from other popen's */
/*
* These conditions may apply if a previous iob returned
* by popen() was closed with fclose() rather than pclose(),
* or if close(fileno(iob)) was called.
* Accommodate these programming error.
*/
}
if (error == 0)
if (error == 0)
if (error == 0)
yourside);
}
if (error == 0)
if (error) {
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
return (NULL);
}
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
if (error) {
return (NULL);
}
return (iop);
}
int
{
int status;
/* mark this pipe closed */
if (pid <= 0) {
return (-1);
}
/*
* pclose() is a cancellation point.
* Call waitpid_cancel() rather than _waitpid() to make
* sure that we actually perform the cancellation logic.
*
* If we have already been cancelled (pclose() was called from
* a cancellation cleanup handler), attempt to reap the process
* w/o waiting, and if that fails just call cleanup(pid).
*/
if (_thrp_cancelled()) {
return (status);
return (-1);
}
status = -1;
break;
}
}
return (status);
}
static void
{
/*
* curr->fd can equal fd if a previous iob returned by
* popen() was closed with fclose() rather than pclose(),
* or if close(fileno(iob)) was called.
* Accommodate this programming error.
*/
return;
}
}
else
}
/*
* _insert() and _delete() are used by p2open() in libgen.
*/
int
{
return (-1);
return (0);
}
{
else
return (pid);
}
}
return (-1);
}