Fixup assert definitions.
[sxemacs] / src / callproc.c
index 32fe641..c652f17 100644 (file)
@@ -47,7 +47,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #include "sysproc.h"
 #include "sysfile.h"           /* Always include after sysproc.h */
 #include "syssignal.h"         /* Always include before systty.h */
-#include "ui/systty.h"
+#include "ui/TTY/systty.h"
 
 
 
@@ -121,17 +121,6 @@ static Lisp_Object call_process_cleanup(Lisp_Object fdpid)
 }
 
 static Lisp_Object fork_error;
-#if 0                          /* UNUSED */
-static void report_fork_error(char *string, Lisp_Object data)
-{
-       Lisp_Object errstring = lisp_strerror(errno);
-
-       fork_error = Fcons(build_string(string), Fcons(errstring, data));
-
-       /* terminate this branch of the fork, without closing stdin/out/etc. */
-       _exit(1);
-}
-#endif                         /* unused */
 
 DEFUN("old-call-process-internal", Fold_call_process_internal, 1, MANY, 0, /*
 Call PROGRAM synchronously in separate process, with coding-system specified.
@@ -293,7 +282,10 @@ quit again.
                fd[1] = open(NULL_DEVICE, O_WRONLY | OPEN_BINARY, 0);
                fd[0] = -1;
        } else {
-               pipe(fd);
+               if( pipe(fd) < 0 )
+                       report_file_error("Opening process input file pipe",
+                                         Fcons(infile, Qnil));
+
 #if 0
                /* Replaced by close_process_descs */
                set_exclusive_use(fd[0]);
@@ -481,20 +473,61 @@ quit again.
        }
 }
 \f
+static int max_filedesc(void)
+{
+       /* Cache it to avoid calling getrlimit all the time.
+          It won't really change over time 
+       */
+       static int maxfd = -1;
+
+       if (maxfd >= 0)
+               return maxfd;
+       maxfd = MAXDESC;
+
+#  ifdef HAVE_GETRLIMIT64
+       struct rlimit64 rlim;
+       (void)getrlimit64(RLIMIT_NOFILE, &rlim);
+       maxfd = rlim.rlim_cur;
+#  elif  HAVE_GETRLIMIT
+       struct rlimit rlim;
+       (void)getrlimit(RLIMIT_NOFILE, &rlim);
+       maxfd = rlim.rlim_cur;
+#  endif
+
+       return maxfd;
+}
+
+
 /* Move the file descriptor FD so that its number is not less than MIN. *
    The original file descriptor remains open.  */
-static int relocate_fd(int fd, int min)
+static int relocate_fd(int fd, int minfd)
 {
-       if (fd >= min)
+       int newfd = -1;
+
+       if (minfd < 0) {
+               stderr_out("Bad relocated_fd minimum file descriptor: %d\n", 
+                          minfd);
+               _exit(1);
+       }
+       if (fd >= minfd)
                return fd;
+       
+       newfd = dup(fd);
+
+       if (newfd == -1) {
+               stderr_out("Error while setting up child: %s\n",
+                          strerror(errno));
+               _exit(1);
+       }
+       if (newfd >= minfd )
+               return newfd;
        else {
-               int newfd = dup(fd);
-               if (newfd == -1) {
-                       stderr_out("Error while setting up child: %s\n",
-                                  strerror(errno));
-                       _exit(1);
-               }
-               return relocate_fd(newfd, min);
+               int recurse_fd = relocate_fd(newfd, minfd);
+               /* Close all the previously recursivelly dup'ed
+                  file descriptors 
+               */
+               close(newfd);
+               return recurse_fd;
        }
 }
 
@@ -545,7 +578,7 @@ child_setup(int in, int out, int err, char **new_argv, const char *current_dir)
           event-tty.c) to allow for stream devices to be handled correctly.
           There isn't much to do, in fact, and I'll fix it shortly.  That
           way, the Lisp definition can be used non-interactively too. */
-#if !defined (NO_SUBPROCESSES) 
+#if !defined (NO_SUBPROCESSES)
        /* Close Emacs's descriptors that this process should not have.  */
        close_process_descs();
 #endif                         /* not NO_SUBPROCESSES */
@@ -643,14 +676,26 @@ child_setup(int in, int out, int err, char **new_argv, const char *current_dir)
        out = relocate_fd(out, 3);
        err = relocate_fd(err, 3);
 
+
+#ifdef HAVE_DUP2
+       /* dup2 will automatically close STD* handles before duping
+          them 
+       */
+       dup2(in, STDIN_FILENO);
+       dup2(out, STDOUT_FILENO);
+       dup2(err, STDERR_FILENO);
+#else
        /* Set the standard input/output channels of the new process.  */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
 
-       dup2(in, STDIN_FILENO);
-       dup2(out, STDOUT_FILENO);
-       dup2(err, STDERR_FILENO);
+       /* Sub-optimally hoping that dup will use the just closed STD*
+          handles */
+       dup(in);
+       dup(out);
+       dup(err);
+#endif
 
        close(in);
        close(out);
@@ -663,7 +708,7 @@ child_setup(int in, int out, int err, char **new_argv, const char *current_dir)
        {
                int fd;
 
-               for (fd = 3; fd < MAXDESC; fd++)
+               for (fd = 3; fd < max_filedesc(); fd++)
                        close(fd);
        }