Back to index

glibc  2.9
openchild.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 
00030 /*
00031  * Copyright (c) 1988 by Sun Microsystems, Inc.
00032  */
00033 
00034 /*
00035  * Open two pipes to a child process, one for reading, one for writing.
00036  * The pipes are accessed by FILE pointers. This is NOT a public
00037  * interface, but for internal use only!
00038  */
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <unistd.h>
00042 #include <string.h>
00043 #include <rpc/rpc.h>
00044 #include <rpc/clnt.h>
00045 
00046 #ifdef USE_IN_LIBIO
00047 # include <libio/iolibio.h>
00048 # define fflush(s) INTUSE(_IO_fflush) (s)
00049 # define __fdopen(fd,m) INTUSE(_IO_fdopen) (fd,m)
00050 #endif
00051 
00052 /*
00053  * returns pid, or -1 for failure
00054  */
00055 int
00056 _openchild (const char *command, FILE ** fto, FILE ** ffrom)
00057 {
00058   int i;
00059   int pid;
00060   int pdto[2];
00061   int pdfrom[2];
00062 
00063   if (__pipe (pdto) < 0)
00064     goto error1;
00065   if (__pipe (pdfrom) < 0)
00066     goto error2;
00067   switch (pid = __fork ())
00068     {
00069     case -1:
00070       goto error3;
00071 
00072     case 0:
00073       /*
00074        * child: read from pdto[0], write into pdfrom[1]
00075        */
00076       __close (0);
00077       __dup (pdto[0]);
00078       __close (1);
00079       __dup (pdfrom[1]);
00080       fflush (stderr);
00081       for (i = _rpc_dtablesize () - 1; i >= 3; i--)
00082        __close (i);
00083       fflush (stderr);
00084       execlp (command, command, NULL);
00085       perror ("exec");
00086       _exit (~0);
00087 
00088     default:
00089       /*
00090        * parent: write into pdto[1], read from pdfrom[0]
00091        */
00092       *fto = __fdopen (pdto[1], "w");
00093       __close (pdto[0]);
00094       *ffrom = __fdopen (pdfrom[0], "r");
00095       __close (pdfrom[1]);
00096       break;
00097     }
00098   return pid;
00099 
00100   /*
00101    * error cleanup and return
00102    */
00103 error3:
00104   __close (pdfrom[0]);
00105   __close (pdfrom[1]);
00106 error2:
00107   __close (pdto[0]);
00108   __close (pdto[1]);
00109 error1:
00110   return -1;
00111 }