home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / openchild.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  3.0 KB  |  134 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char sccsid[] =     "@(#)openchild.c    2.3 88/08/15 4.0 RPCSRC; from 1.7 88/02/08 SMI";
  3. #endif
  4. /*
  5.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  6.  * unrestricted use provided that this legend is included on all tape
  7.  * media and as a part of the software program in whole or part.  Users
  8.  * may copy or modify Sun RPC without charge, but are not authorized
  9.  * to license or distribute it to anyone else except as part of a product or
  10.  * program developed by the user.
  11.  * 
  12.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  13.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  14.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  15.  * 
  16.  * Sun RPC is provided with no support and without any obligation on the
  17.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  18.  * modification or enhancement.
  19.  * 
  20.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  21.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  22.  * OR ANY PART THEREOF.
  23.  * 
  24.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  25.  * or profits or other special, indirect and consequential damages, even if
  26.  * Sun has been advised of the possibility of such damages.
  27.  * 
  28.  * Sun Microsystems, Inc.
  29.  * 2550 Garcia Avenue
  30.  * Mountain View, California  94043
  31.  */
  32.  
  33. /*
  34.  * Copyright (c) 1988 by Sun Microsystems, Inc.
  35.  */
  36.  
  37. /*
  38.  * Open two pipes to a child process, one for reading, one for writing.
  39.  * The pipes are accessed by FILE pointers. This is NOT a public
  40.  * interface, but for internal use only!
  41.  */
  42. #include <stdio.h>
  43.  
  44. extern char *malloc();
  45. extern char *rindex();
  46. extern int sprintf();
  47.  
  48. static char *basename();
  49. static char SHELL[] = "/bin/sh";
  50.  
  51.  
  52. /*
  53.  * returns pid, or -1 for failure
  54.  */
  55. _openchild(command, fto, ffrom)
  56.     char *command;
  57.     FILE **fto;
  58.     FILE **ffrom;
  59. {
  60.     int i;
  61.     int pid;
  62.     int pdto[2];
  63.     int pdfrom[2];
  64.     char *com;
  65.  
  66.         
  67.     if (pipe(pdto) < 0) {
  68.         goto error1;
  69.     }
  70.     if (pipe(pdfrom) < 0) {
  71.         goto error2;
  72.     }
  73.     switch (pid = vfork()) {
  74.     case -1:
  75.         goto error3;
  76.  
  77.     case 0:
  78.         /* 
  79.          * child: read from pdto[0], write into pdfrom[1]
  80.          */
  81.         (void) close(0); 
  82.         (void) dup(pdto[0]);
  83.         (void) close(1); 
  84.         (void) dup(pdfrom[1]);
  85.         for (i = _rpc_dtablesize() - 1; i >= 3; i--) {
  86.             (void) close(i);
  87.         }
  88.         com = malloc((unsigned) strlen(command) + 6);
  89.         if (com == NULL) {
  90.             _exit(~0);
  91.         }    
  92.         (void) sprintf(com, "exec %s", command);
  93.         execl(SHELL, basename(SHELL), "-c", com, NULL);
  94.         _exit(~0);
  95.  
  96.     default:
  97.         /*
  98.          * parent: write into pdto[1], read from pdfrom[0]
  99.          */
  100.         *fto = fdopen(pdto[1], "w");
  101.         (void) close(pdto[0]);
  102.         *ffrom = fdopen(pdfrom[0], "r");
  103.         (void) close(pdfrom[1]);
  104.         break;
  105.     }
  106.     return (pid);
  107.  
  108.     /*
  109.      * error cleanup and return
  110.      */
  111. error3:
  112.     (void) close(pdfrom[0]);
  113.     (void) close(pdfrom[1]);
  114. error2:
  115.     (void) close(pdto[0]);
  116.     (void) close(pdto[1]);
  117. error1:
  118.     return (-1);
  119. }
  120.  
  121. static char *
  122. basename(path)
  123.     char *path;
  124. {
  125.     char *p;
  126.  
  127.     p = rindex(path, '/');
  128.     if (p == NULL) {
  129.         return (path);
  130.     } else {
  131.         return (p + 1);
  132.     }
  133. }
  134.