You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.5KB

  1. /*===========================================================================
  2. Copyright (c) 1998-2000, The Santa Cruz Operation
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. *Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. *Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. *Neither name of The Santa Cruz Operation nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  15. IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGE.
  26. =========================================================================*/
  27. #include <stdio.h>
  28. #include <signal.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include "global.h" /* pid_t, shell, and basename() */
  33. #define tst(a, b) (*mode == 'r' ? (b) : (a))
  34. #define RDR 0
  35. #define WTR 1
  36. /* HBB 20010312: make this a bit safer --- don't blindly assume it's 1 */
  37. #ifdef FD_CLOEXEC
  38. # define CLOSE_ON_EXEC FD_CLOEXEC
  39. #else
  40. # define CLOSE_ON_EXEC 1
  41. #endif
  42. #ifdef HAVE_IO_H
  43. # include <io.h> /* for setmode() */
  44. #endif
  45. static pid_t popen_pid[20];
  46. static void (*tstat)(int);
  47. int myopen(char *path, int flag, int mode) {
  48. /* opens a file descriptor and then sets close-on-exec for the file */
  49. int fd;
  50. /* If file is not explicitly in Binary mode, make
  51. * sure we override silly Cygwin behaviour of automatic binary
  52. * mode for files in "binary mounted" paths */
  53. #if O_BINARY != O_TEXT
  54. if(!(flag | O_BINARY)) flag |= O_TEXT;
  55. #endif
  56. if(mode)
  57. fd = open(path, flag, mode);
  58. else
  59. fd = open(path, flag);
  60. if(fd != -1 && (fcntl(fd, F_SETFD, CLOSE_ON_EXEC) != -1))
  61. return (fd);
  62. else {
  63. /* Ensure that if the fcntl fails and fd is valid, then
  64. the file is closed properly. In general this should
  65. not happen. */
  66. if(fd != -1) { close(fd); }
  67. return (-1);
  68. }
  69. }
  70. FILE *myfopen(char *path, char *mode) {
  71. /* opens a file pointer and then sets close-on-exec for the file */
  72. FILE *fp;
  73. fp = fopen(path, mode);
  74. #ifdef SETMODE
  75. if(fp && !strchr(mode, 'b')) { SETMODE(fileno(fp), O_TEXT); }
  76. #endif /* SETMODE */
  77. if(fp && (fcntl(fileno(fp), F_SETFD, CLOSE_ON_EXEC) != -1)) {
  78. return (fp);
  79. }
  80. if(fp) {
  81. fclose(fp);
  82. }
  83. return (NULL);
  84. }
  85. FILE *mypopen(char *cmd, char *mode) {
  86. int p[2];
  87. pid_t *poptr;
  88. int myside, yourside;
  89. pid_t pid;
  90. if(pipe(p) < 0) return (NULL);
  91. myside = tst(p[WTR], p[RDR]);
  92. yourside = tst(p[RDR], p[WTR]);
  93. if((pid = fork()) == 0) {
  94. /* myside and yourside reverse roles in child */
  95. int stdio;
  96. /* close all pipes from other popen's */
  97. for(poptr = popen_pid; poptr < popen_pid + 20; poptr++) {
  98. if(*poptr) (void)close(poptr - popen_pid);
  99. }
  100. stdio = tst(0, 1);
  101. close(myside);
  102. close(stdio);
  103. fcntl(yourside, F_DUPFD, stdio);
  104. close(yourside);
  105. execlp(shell, basename(shell), "-c", cmd, (void *)0);
  106. _exit(1);
  107. } else if(pid > 0) {
  108. tstat = signal(SIGTSTP, SIG_DFL);
  109. }
  110. if(pid == -1) {
  111. return (NULL);
  112. }
  113. popen_pid[myside] = pid;
  114. (void)close(yourside);
  115. return (fdopen(myside, mode));
  116. }
  117. int mypclose(FILE *ptr) {
  118. int f;
  119. pid_t r;
  120. int status = -1;
  121. sighandler_t hstat, istat, qstat;
  122. f = fileno(ptr);
  123. UNUSED(fclose(ptr));
  124. istat = signal(SIGINT, SIG_IGN);
  125. qstat = signal(SIGQUIT, SIG_IGN);
  126. hstat = signal(SIGHUP, SIG_IGN);
  127. while((r = wait(&status)) != popen_pid[f] && r != -1)
  128. ; /* nothing */
  129. if(r == -1) status = -1;
  130. UNUSED(signal(SIGINT, istat));
  131. UNUSED(signal(SIGQUIT, qstat));
  132. UNUSED(signal(SIGHUP, hstat));
  133. UNUSED(signal(SIGTSTP, tstat));
  134. /* mark this pipe closed */
  135. popen_pid[f] = 0;
  136. return (status);
  137. }