lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010, Oracle America, Inc. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials |
| 13 | * provided with the distribution. |
| 14 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Open two pipes to a child process, one for reading, one for writing. |
| 34 | * The pipes are accessed by FILE pointers. This is NOT a public |
| 35 | * interface, but for internal use only! |
| 36 | */ |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
| 41 | #include <rpc/rpc.h> |
| 42 | #include <rpc/clnt.h> |
| 43 | |
| 44 | #include <libio/iolibio.h> |
| 45 | #define fflush(s) _IO_fflush (s) |
| 46 | #define __fdopen(fd,m) _IO_fdopen (fd,m) |
| 47 | |
| 48 | /* |
| 49 | * returns pid, or -1 for failure |
| 50 | */ |
| 51 | int |
| 52 | _openchild (const char *command, FILE ** fto, FILE ** ffrom) |
| 53 | { |
| 54 | int i; |
| 55 | int pid; |
| 56 | int pdto[2]; |
| 57 | int pdfrom[2]; |
| 58 | |
| 59 | if (__pipe (pdto) < 0) |
| 60 | goto error1; |
| 61 | if (__pipe (pdfrom) < 0) |
| 62 | goto error2; |
| 63 | switch (pid = __fork ()) |
| 64 | { |
| 65 | case -1: |
| 66 | goto error3; |
| 67 | |
| 68 | case 0: |
| 69 | /* |
| 70 | * child: read from pdto[0], write into pdfrom[1] |
| 71 | */ |
| 72 | __close (0); |
| 73 | __dup (pdto[0]); |
| 74 | __close (1); |
| 75 | __dup (pdfrom[1]); |
| 76 | fflush (stderr); |
| 77 | for (i = _rpc_dtablesize () - 1; i >= 3; i--) |
| 78 | __close (i); |
| 79 | fflush (stderr); |
| 80 | execlp (command, command, NULL); |
| 81 | perror ("exec"); |
| 82 | _exit (~0); |
| 83 | |
| 84 | default: |
| 85 | /* |
| 86 | * parent: write into pdto[1], read from pdfrom[0] |
| 87 | */ |
| 88 | *fto = __fdopen (pdto[1], "w"); |
| 89 | __close (pdto[0]); |
| 90 | *ffrom = __fdopen (pdfrom[0], "r"); |
| 91 | __close (pdfrom[1]); |
| 92 | break; |
| 93 | } |
| 94 | return pid; |
| 95 | |
| 96 | /* |
| 97 | * error cleanup and return |
| 98 | */ |
| 99 | error3: |
| 100 | __close (pdfrom[0]); |
| 101 | __close (pdfrom[1]); |
| 102 | error2: |
| 103 | __close (pdto[0]); |
| 104 | __close (pdto[1]); |
| 105 | error1: |
| 106 | return -1; |
| 107 | } |