openchild.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * Open two pipes to a child process, one for reading, one for writing.
  33. * The pipes are accessed by FILE pointers. This is NOT a public
  34. * interface, but for internal use only!
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <rpc/rpc.h>
  41. #include <rpc/clnt.h>
  42. #include <libio/iolibio.h>
  43. #define fflush(s) _IO_fflush (s)
  44. #define __fdopen(fd,m) _IO_fdopen (fd,m)
  45. /*
  46. * returns pid, or -1 for failure
  47. */
  48. int
  49. _openchild (const char *command, FILE ** fto, FILE ** ffrom)
  50. {
  51. int i;
  52. int pid;
  53. int pdto[2];
  54. int pdfrom[2];
  55. if (__pipe (pdto) < 0)
  56. goto error1;
  57. if (__pipe (pdfrom) < 0)
  58. goto error2;
  59. switch (pid = __fork ())
  60. {
  61. case -1:
  62. goto error3;
  63. case 0:
  64. /*
  65. * child: read from pdto[0], write into pdfrom[1]
  66. */
  67. __close (0);
  68. __dup (pdto[0]);
  69. __close (1);
  70. __dup (pdfrom[1]);
  71. fflush (stderr);
  72. for (i = _rpc_dtablesize () - 1; i >= 3; i--)
  73. __close (i);
  74. fflush (stderr);
  75. execlp (command, command, NULL);
  76. perror ("exec");
  77. _exit (~0);
  78. default:
  79. /*
  80. * parent: write into pdto[1], read from pdfrom[0]
  81. */
  82. *fto = __fdopen (pdto[1], "w");
  83. __close (pdto[0]);
  84. *ffrom = __fdopen (pdfrom[0], "r");
  85. __close (pdfrom[1]);
  86. break;
  87. }
  88. return pid;
  89. /*
  90. * error cleanup and return
  91. */
  92. error3:
  93. __close (pdfrom[0]);
  94. __close (pdfrom[1]);
  95. error2:
  96. __close (pdto[0]);
  97. __close (pdto[1]);
  98. error1:
  99. return -1;
  100. }