$NetBSD: patch-am,v 1.2 2005/05/29 14:38:33 wiz Exp $

--- glunix/src/rexec/drexec.cc.orig	1997-10-02 21:34:05.000000000 +0200
+++ glunix/src/rexec/drexec.cc
@@ -306,7 +306,14 @@
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+
+#ifdef __NetBSD__
+#include <sys/ptrace.h>
+#include <sys/ioctl.h>
+#else
 #include <stropts.h>
+#endif
+
 #include <grp.h>
 #include <pwd.h>
 #include <bsd.h>
@@ -353,9 +360,11 @@ static Bool FinalizeParentPtyPiping(int 
 static Bool ChildPtyPiping(char *ptyName, char *ptyErrorName, int ptyMasterFd,
 			   int ptyErrorMasterFd, MsgRexec_ExecCommand *msg);
 static int PtyMasterOpen(char *ptyName);
-/*static int PtySlaveOpen(char *ptyName, int uid, int gid);*/
+#ifdef __NetBSD__
+static int PtySlaveOpen(char *ptyName, int uid, int gid);
+#else
 static int PtySlaveOpen(char *ptyName, int ptyMasterFd);
-
+#endif
 /*****************************************************************************/
 
 // All of the routines on this list are called before this module
@@ -1141,72 +1150,82 @@ ChildPtyPiping(char *ptyName, char *ptyE
     windowSize = msg->windowSize;
 
     // Open the stderr pty
+#ifdef __NetBSD__
+    if((ptyErrorSlaveFd = PtySlaveOpen(ptyErrorName, msg->uid, msg->gid)) == -1) {
+#else
     if((ptyErrorSlaveFd = PtySlaveOpen(ptyErrorName, ptyErrorMasterFd))
        == -1) {
+#endif
 	DE("PtySlaveOpen() failed\n");
 	return False;
     }
     close(ptyErrorMasterFd);
 
-/*    if(setsid() == -1) {
-	Debug_Print(MODULE_REXEC, DEBUG_ERROR, "(rexec)ChildPtyPiping: "
-		    "setsid() failed\n");
-	return False;
-    }*/
+#ifdef __NetBSD__
+    if(setsid() == -1) {
+      DE("(rexec)ChildPtyPiping: setsid() failed\n");
+      return False;
+    }
+#endif
 
-    setpgrp();
+    setpgrp(0,0);
 
     // Open the stdin/stdout pty
+#ifdef __NetBSD__
+    if((ptySlaveFd = PtySlaveOpen(ptyName, msg->uid, msg->gid)) == -1) {
+#else
     if((ptySlaveFd = PtySlaveOpen(ptyName, ptyMasterFd)) == -1) {
-/*    if((ptySlaveFd = PtySlaveOpen(ptyName, msg->uid, msg->gid)) == -1) {*/
+#endif
 	DE("PtySlaveOpen() failed\n");
 	return False;
     }
     close(ptyMasterFd);
 
-/* This should work on BSD, but doesn't on SysV
-#if defined(TIOCSCTTY) && !defined(CIBAUD)
+#ifdef __NetBSD__
+    /* This should work on BSD, but doesn't on SysV*/
     if(ioctl(ptySlaveFd, TIOCSCTTY, (char *) 0) == -1) {
-	DE("cannot set controlling tty: %s\n", strerror(errno));
-	return False;
+      DE("cannot set controlling tty: %s\n", strerror(errno));
+      return False;
     }
-#else
-#error Nope
-#endif */
-
+#else 
     (void) ioctl(ptySlaveFd, TCSETSF, (char *)&startupTermios);
     (void) ioctl(ptySlaveFd, TIOCSWINSZ, (char *)&windowSize);
+#endif
 
-/*    while(tcsetattr(ptySlaveFd, TCSANOW, &startupTermios) == -1) {
-	if(errno == EINTR) {
-	    continue;
-	} else {
-	    * BUGBUG maybe should return failure here? *
-	    DE("ioctl: %s\n", strerror(errno));
-	}
+#ifdef __NetBSD__
+    while(tcsetattr(ptySlaveFd, TCSANOW, &startupTermios) == -1) {
+      if(errno == EINTR) {
+	continue;
+      } else {
+	/* BUGBUG maybe should return failure here? */
+	DE("ioctl: %s\n", strerror(errno));
+      }
     }
     
     if(ioctl(ptySlaveFd, TIOCSWINSZ, (char *) &windowSize) == -1) {
-	* BUGBUG maybe should return failure here? *
-	DE("ioctl: %s\n", strerror(errno));
-    }	*/
-    
+      /* BUGBUG maybe should return failure here? */
+	    DE("ioctl: %s\n", strerror(errno));
+    }  
+#else
     (void) ioctl(ptyErrorSlaveFd, TCSETSF, (char *)&startupTermios);
     (void) ioctl(ptyErrorSlaveFd, TIOCSWINSZ, (char *)&windowSize);
+#endif
 
-/*    while(tcsetattr(ptyErrorSlaveFd, TCSANOW, &startupTermios) == -1) {
-	if(errno == EINTR) {
-	    continue;
-	} else {
-	    * BUGBUG maybe should return failure here? *
-	    DE("ioctl: %s\n", strerror(errno));
-	}
+#ifdef __NetBSD__
+    while(tcsetattr(ptyErrorSlaveFd, TCSANOW, &startupTermios) == -1) {
+      if(errno == EINTR) {
+	continue;
+      } else {
+	/* BUGBUG maybe should return failure here? */
+	DE("ioctl: %s\n", strerror(errno));
+      }
     }
     
     if(ioctl(ptyErrorSlaveFd, TIOCSWINSZ, (char *) &windowSize) == -1) {
-	* BUGBUG maybe should return failure here? *
-	DE("ioctl: %s\n", strerror(errno));
-    }	*/
+      /* BUGBUG maybe should return failure here? */
+      DE("ioctl: %s\n", strerror(errno));
+    }
+#endif
 
     if(msg->validIoFds[IO_INPUT]) {
 	while(dup2(ptySlaveFd, STDIN_FILENO) == -1) {
@@ -1251,6 +1270,7 @@ ChildPtyPiping(char *ptyName, char *ptyE
     return True;
 }
 
+#ifndef __NetBSD__
 int
 PtyMasterOpen(char *ptyName)
 {
@@ -1271,7 +1291,7 @@ PtyMasterOpen(char *ptyName)
 
     return ptyMasterFd;
 }
-
+#else
 /******************************************************************************
  * PtyMasterOpen --
  *     This is the actual procedure that opens up a master pty. (BSD STYLE)
@@ -1284,40 +1304,43 @@ PtyMasterOpen(char *ptyName)
  *
  * Side effects:
  ****************************************************************************/
-/*int
+
+int
 PtyMasterOpen(char *ptyName)
 {
-    int ptyMasterFd;
-    char *ptr1, *ptr2;
-
-    strcpy(ptyName, "/dev/ptyXY");
-
-    ASSERT(strlen(ptyName) < PTY_PATH_LENGTH);
-
-    // array index: 0123456789 (for references in following code)
-    for(ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
-	ptyName[8] = *ptr1;
-	for(ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
-	    ptyName[9] = *ptr2;
-
-	    if((ptyMasterFd = open(ptyName, O_RDWR)) == -1) {
-		if(errno == ENOENT) {
-		    return -1;
-		} else {
-		    continue;
-		}
-	    }
-
-	    ptyName[5] = 't'; // need to change string to "tty" so that this
-                              // string can be used later to open the slave
-                              // end of the pty by PtyChildOpen
-	    return ptyMasterFd;
+  int ptyMasterFd;
+  char *ptr1, *ptr2;
+  
+  strcpy(ptyName, "/dev/ptyXY");
+
+  ASSERT(strlen(ptyName) < PTY_PATH_LENGTH);
+
+  // array index: 0123456789 (for references in following code)
+  for(ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
+    ptyName[8] = *ptr1;
+    for(ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
+      ptyName[9] = *ptr2;
+
+      if((ptyMasterFd = open(ptyName, O_RDWR)) == -1) {
+	if(errno == ENOENT) {
+	  return -1;
+	} else {
+	  continue;
 	}
+      }
+
+      ptyName[5] = 't'; // need to change string to "tty" so that this
+      // string can be used later to open the slave
+      // end of the pty by PtyChildOpen
+      return ptyMasterFd;
     }
+  }
 
-    return -1;
-}*/
+  return -1;
+}
+#endif
 
+#ifndef __NetBSD__
 int
 PtySlaveOpen(char *ptyName, int ptyMasterFd)
 {
@@ -1364,7 +1387,7 @@ PtySlaveOpen(char *ptyName, int ptyMaste
 
     return ptySlaveFd;
 }
-
+#else
 /******************************************************************************
  * PtySlaveOpen --
  *     This is the actual procedure that opens up a slave pty. (BSD STYLE)
@@ -1397,7 +1420,7 @@ PtySlaveOpen(char *ptyName, int uid, int
 
     return ptySlaveFd;
 }
-
+#endif
 /******************************************************************************
  * Rexec_AbortProgramCallback --
  *
@@ -1800,7 +1823,11 @@ ChildDeathCallback(int sig, int arg) {
 
 	/* If it didn't exit, but something else happened
 	   (received the SIG_STOP or SIG_CONT possibly) */
+#ifdef __NetBSD__
+	if ((WIFSTOPPED(procStat)) || (WIFSIGNALED(procStat))) {
+#else
 	if ((WIFSTOPPED(procStat)) || (WIFCONTINUED(procStat))) {
+#endif
 	    // BUGBUG Shouldn't we signal the startup here????
 	    D5("process status says it didn't exit!\n");
 	    continue;
