|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* Copyright (c) 2010, Oracle America, Inc.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
6c4009 |
* modification, are permitted provided that the following conditions are
|
|
Packit |
6c4009 |
* met:
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
6c4009 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
6c4009 |
* * Redistributions in binary form must reproduce the above
|
|
Packit |
6c4009 |
* copyright notice, this list of conditions and the following
|
|
Packit |
6c4009 |
* disclaimer in the documentation and/or other materials
|
|
Packit |
6c4009 |
* provided with the distribution.
|
|
Packit |
6c4009 |
* * Neither the name of the "Oracle America, Inc." nor the names of its
|
|
Packit |
6c4009 |
* contributors may be used to endorse or promote products derived
|
|
Packit |
6c4009 |
* from this software without specific prior written permission.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
6c4009 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
6c4009 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit |
6c4009 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit |
6c4009 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
Packit |
6c4009 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
6c4009 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
Packit |
6c4009 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
6c4009 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
Packit |
6c4009 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
Packit |
6c4009 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
6c4009 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* Open two pipes to a child process, one for reading, one for writing.
|
|
Packit |
6c4009 |
* The pipes are accessed by FILE pointers. This is NOT a public
|
|
Packit |
6c4009 |
* interface, but for internal use only!
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <rpc/rpc.h>
|
|
Packit |
6c4009 |
#include <rpc/clnt.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <libio/iolibio.h>
|
|
Packit |
6c4009 |
#define fflush(s) _IO_fflush (s)
|
|
Packit |
6c4009 |
#define __fdopen(fd,m) _IO_fdopen (fd,m)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* returns pid, or -1 for failure
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
_openchild (const char *command, FILE ** fto, FILE ** ffrom)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
int pid;
|
|
Packit |
6c4009 |
int pdto[2];
|
|
Packit |
6c4009 |
int pdfrom[2];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__pipe (pdto) < 0)
|
|
Packit |
6c4009 |
goto error1;
|
|
Packit |
6c4009 |
if (__pipe (pdfrom) < 0)
|
|
Packit |
6c4009 |
goto error2;
|
|
Packit |
6c4009 |
switch (pid = __fork ())
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case -1:
|
|
Packit |
6c4009 |
goto error3;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* child: read from pdto[0], write into pdfrom[1]
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
__close (0);
|
|
Packit |
6c4009 |
__dup (pdto[0]);
|
|
Packit |
6c4009 |
__close (1);
|
|
Packit |
6c4009 |
__dup (pdfrom[1]);
|
|
Packit |
6c4009 |
fflush (stderr);
|
|
Packit |
6c4009 |
for (i = _rpc_dtablesize () - 1; i >= 3; i--)
|
|
Packit |
6c4009 |
__close (i);
|
|
Packit |
6c4009 |
fflush (stderr);
|
|
Packit |
6c4009 |
execlp (command, command, NULL);
|
|
Packit |
6c4009 |
perror ("exec");
|
|
Packit |
6c4009 |
_exit (~0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* parent: write into pdto[1], read from pdfrom[0]
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
*fto = __fdopen (pdto[1], "w");
|
|
Packit |
6c4009 |
__close (pdto[0]);
|
|
Packit |
6c4009 |
*ffrom = __fdopen (pdfrom[0], "r");
|
|
Packit |
6c4009 |
__close (pdfrom[1]);
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return pid;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* error cleanup and return
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
error3:
|
|
Packit |
6c4009 |
__close (pdfrom[0]);
|
|
Packit |
6c4009 |
__close (pdfrom[1]);
|
|
Packit |
6c4009 |
error2:
|
|
Packit |
6c4009 |
__close (pdto[0]);
|
|
Packit |
6c4009 |
__close (pdto[1]);
|
|
Packit |
6c4009 |
error1:
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|