[gnu.emacs.bug] Some 4.2-ish kernel bug workarounds

hakanson@CSE.OGC.EDU (Marion Hakanson) (10/27/89)

Below are a set of patches to GNU Emacs 18.54 which work around one
fairly common bug in 4.2bsd-based systems (and another which is
probably a bit rarer).  Both problems occur in Sequent DYNIX-3.0.12
(Symmetry for sure, Balance probably), so I have included the
s-dynix.h which I built for use here, since DYNIX isn't as close to
4.2bsd as it used to be.  Note that if you don't run emacs in its
X-Windows display mode, you will likely not be exercising the bugs.

First, the common bug.  Here's my "rlog" entry describing the patch,
which affects sysdep.c, x11term.c, and xterm.c.  Note that the bug's
presence can be signified by defining F_SETOWN_SOCK_NEG in the s-*.h
configuration file for the system.

===================================================================
date: 89/10/20 17:57:20;  author: hakanson;  state: Exp;  lines added/del: ...
Install patch to work around kernel bug in interpretation of
the F_SETOWN value.  Turns out that DYNIX-3.0.12, along with
Ultrix-2.0 and SunOS-3.3 (maybe up to 3.5) gets the sign wrong
when the filedescriptor is for a socket.  Otherwise things
work as documented in fcntl(2).  The workaround is to negate
the argument if we know the fd is a socket, so the SIGIO/SIGURG
signals get sent to their proper PID or PGRP.
===================================================================

The slightly less ubiquitous bug is that DYNIX-3.0.12 (and earlier),
and conceivably other 4.2bsd-based systems, for some reason only
supports FASYNC mode (and SIGIO) on ttys -- notably ommitted is
sockets, on which this feature appear to be supported by most other
such systems.  This means that interrupt_input mode works fine from a
tty, but fails under X-Windows.  A fairly non-intrusive patch to
keyboard.c is described below:

===================================================================
date: 89/10/23 15:16:48;  author: hakanson;  state: Exp;  lines added/del: ...
Install NO_SOCK_SIGIO flag to point out when (as in DYNIX)
SIGIO doesn't work on sockets.  If this is the case, and you
are running under X-Windows, for example, then Emacs won't
get SIGIO's like it expects.  So we turn off interrupt_input
mode when this flag is set and we're reading from a socket.
===================================================================

Note that the first problem described above is likely to be fairly
common among folks stuck with old OS software, so the patches for it
may prove to be of fairly general use.  It also explains the behavior
someone described on bug-gnu-emacs awhile back, where all of their
emacs subprocesses were getting unsolicited SIGIO's (but only when run
under X-Windows, if I recall correctly).

Here are the patches, and below them is an s-dynix.h file.

===================================================================
*** /tmp/,RCSt1020764	Thu Oct 26 16:19:28 1989
--- keyboard.c	Mon Oct 23 15:19:40 1989
***************
*** 1,5 ****
--- 1,6 ----
  /* Keyboard input; editor command loop.
     Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
+  $Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/keyboard.c,v 1.2 89/10/23 15:16:48 hakanson Exp $
  
  This file is part of GNU Emacs.
  
***************
*** 1827,1832 ****
--- 1828,1838 ----
    reset_sys_modes ();
  #ifdef SIGIO
  /* Note SIGIO has been undef'd if FIONREAD is missing.  */
+ #ifdef NO_SOCK_SIGIO
+   if (read_socket_hook)
+     interrupt_input = 0;	/* disable if reading from a socket */
+   else
+ #endif /* NO_SOCK_SIGIO */
    interrupt_input = !NULL (interrupt);
  #else /* not SIGIO */
    interrupt_input = 0;
===================================================================
*** /tmp/,RCSt1020764	Thu Oct 26 16:19:37 1989
--- sysdep.c	Fri Oct 20 17:57:30 1989
***************
*** 834,839 ****
--- 834,842 ----
        if (interrupt_input)
  	{
  	  old_fcntl_owner = fcntl (0, F_GETOWN, 0);
+ 	  /* F_SETOWN_SOCK_NEG not needed here;  This is not a socket,
+ 	       since we wouldn't be here if read_socket_hook was set.
+ 	  */
  	  fcntl (0, F_SETOWN, getpid ());
  	  init_sigio ();
  	}
===================================================================
*** /tmp/,RCSt1021755	Thu Oct 26 16:43:59 1989
--- /tmp/,RCSt2021755	Thu Oct 26 16:44:00 1989
***************
*** 24,36 ****
  
  /*
   *	$Source: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v $
!  *	$Author: rfrench $
   *	$Locker:  $
!  *	$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v 1.12 88/02/29 14:11:07 rfrench Exp $
   */
  
  #ifndef lint
! static char *rcsid_xterm_c = "$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v 1.12 88/02/29 14:11:07 rfrench Exp $";
  #endif	lint
  
  /* On 4.3 this loses if it comes after x11term.h.
--- 24,36 ----
  
  /*
   *	$Source: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v $
!  *	$Author: hakanson $
   *	$Locker:  $
!  *	$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v 1.13 89/10/20 17:57:30 hakanson Exp $
   */
  
  #ifndef lint
! static char *rcsid_xterm_c = "$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/x11term.c,v 1.13 89/10/20 17:57:30 hakanson Exp $";
  #endif	lint
  
  /* On 4.3 this loses if it comes after x11term.h.
***************
*** 1896,1903 ****
  	
  #ifdef F_SETOWN
  	old_fcntl_owner = fcntl (0, F_GETOWN, 0);
  	fcntl (0, F_SETOWN, getpid ());
! #endif
  
  	/* Enable interrupt_input because otherwise we cannot asynchronously
  	   detect C-g sent as a keystroke event from the X server.  */
--- 1896,1907 ----
  	
  #ifdef F_SETOWN
  	old_fcntl_owner = fcntl (0, F_GETOWN, 0);
+ #ifdef F_SETOWN_SOCK_NEG
+ 	fcntl (0, F_SETOWN, -getpid ());	/* stdin is a socket here */
+ #else
  	fcntl (0, F_SETOWN, getpid ());
! #endif /* F_SETOWN_SOCK_NEG */
! #endif /* F_SETOWN */
  
  	/* Enable interrupt_input because otherwise we cannot asynchronously
  	   detect C-g sent as a keystroke event from the X server.  */
===================================================================
*** /tmp/,RCSt1021762	Thu Oct 26 16:44:18 1989
--- /tmp/,RCSt2021762	Thu Oct 26 16:44:19 1989
***************
*** 22,34 ****
  
  /*
   *	$Source: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v $
!  *	$Author: rlk $
   *	$Locker:  $
!  *	$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v 1.28 86/08/27 13:30:57 rlk Exp $
   */
  
  #ifndef lint
! static char *rcsid_TrmXTERM_c = "$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v 1.28 86/08/27 13:30:57 rlk Exp $";
  #endif	lint
  
  /* On 4.3 this loses if it comes after xterm.h.  */
--- 22,34 ----
  
  /*
   *	$Source: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v $
!  *	$Author: hakanson $
   *	$Locker:  $
!  *	$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v 1.29 89/10/20 17:57:47 hakanson Exp $
   */
  
  #ifndef lint
! static char *rcsid_TrmXTERM_c = "$Header: /usr/src/local/gnuemacs/dist-18.54/src/RCS/xterm.c,v 1.29 89/10/20 17:57:47 hakanson Exp $";
  #endif	lint
  
  /* On 4.3 this loses if it comes after xterm.h.  */
***************
*** 1656,1663 ****
  #endif /* USG */
  #ifdef F_SETOWN
    old_fcntl_owner = fcntl (0, F_GETOWN, 0);
    fcntl (0, F_SETOWN, getpid ());
! #endif
  #ifndef USG
    if (unrequest) unrequest_sigio ();
  #endif
--- 1656,1667 ----
  #endif /* USG */
  #ifdef F_SETOWN
    old_fcntl_owner = fcntl (0, F_GETOWN, 0);
+ #ifdef F_SETOWN_SOCK_NEG
+   fcntl (0, F_SETOWN, -getpid ());	/* stdin is a socket here */
+ #else
    fcntl (0, F_SETOWN, getpid ());
! #endif /* F_SETOWN_SOCK_NEG */
! #endif /* F_SETOWN */
  #ifndef USG
    if (unrequest) unrequest_sigio ();
  #endif
===================================================================
And here's the new s-dynix.h file....
===================================================================
/* Definitions file for GNU Emacs running on bsd 4.2
   Copyright (C) 1985, 1986 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the GNU Emacs General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
GNU Emacs, but only under the conditions described in the
GNU Emacs General Public License.   A copy of this license is
supposed to have been given to you along with GNU Emacs so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */


/*
 *	System-specific information for Sequent's DYNIX-3.0
 *	This is mostly early 4.2bsd, but with some 4.3 added,
 *	and some 4.2 features not supported.
 *	This file should also work for DYNIX-2.1, except for
 *	the definition of MAIL_USE_FLOCK.
 */

/*
 *	Define symbols to identify the version of Unix this is.
 *	Define all the symbols that apply correctly.
 */

#ifndef BSD4_2
#define BSD4_2
#endif /* BSD4_2 */

#ifndef BSD
#define BSD
#endif /* BSD */

/* SYSTEM_TYPE should indicate the kind of system you are using.
 It sets the Lisp variable system-type.  */

#define SYSTEM_TYPE "berkeley-unix"

/* nomultiplejobs should be defined if your system's shell
 does not have "job control" (the ability to stop a program,
 run some other program, then continue the first one).  */

/* #define NOMULTIPLEJOBS */

/* Default is to set interrupt_input to 1: do input buffering within Emacs */

#define INTERRUPT_INPUT

/* First pty name is /dev/ptyp0.  */

#define FIRST_PTY_LETTER 'p'
 
/*
 *	Define HAVE_TIMEVAL if the system supports the BSD style clock values.
 *	Look in <sys/time.h> for a timeval structure.
 */

#define HAVE_TIMEVAL
 
/*
 *	Define HAVE_SELECT if the system supports the `select' system call.
 */

#define HAVE_SELECT

/*
 *	Define HAVE_PTYS if the system supports pty devices.
 */

#define HAVE_PTYS

/* Define HAVE_SOCKETS if system supports 4.2-compatible sockets.  */

#define HAVE_SOCKETS

/* On many 4.2-based systems, there's a rather tricky bug
 * with the interpretation of the pid/pgrp value given to
 * the F_SETOWN fcntl() call.  It works as documented EXCEPT
 * when applied to filedescriptors for sockets, in which case
 * the sign must be reversed.  If your emacs subprocesses get
 * SIGIO's when they shouldn't, while running on a socket
 * (e.g. under X windows), you should probably define this.
 */

#define F_SETOWN_SOCK_NEG

/* Some really obscure 4.2-based systems (like Sequent DYNIX)
 * do not support asynchronous I/O (using SIGIO) on sockets,
 * even though it works fine on tty's.  If you have one of
 * these systems, define the following, and then use it in
 * config.h (or elsewhere) to decide when (not) to use SIGIO.
 */

#define NO_SOCK_SIGIO

/*
 *	Define NONSYSTEM_DIR_LIBRARY to make Emacs emulate
 *      The 4.2 opendir, etc., library functions.
 */

/* #define NONSYSTEM_DIR_LIBRARY */

/* Define this symbol if your system has the functions bcopy, etc. */

#define BSTRING

/* subprocesses should be defined if you want to
   have code for asynchronous subprocesses
   (as used in M-x compile and M-x shell).
   This is generally OS dependent, and not supported
   under most USG systems. */

#define subprocesses

/* If your system uses COFF (Common Object File Format) then define the
   preprocessor symbol "COFF". */

/* #define COFF */

/* define MAIL_USE_FLOCK if the mailer uses flock
   to interlock access to /usr/spool/mail/$USER.
   The alternative is that a lock file named
   /usr/spool/mail/$USER.lock.  */

#define MAIL_USE_FLOCK

/* Define CLASH_DETECTION if you want lock files to be written
   so that Emacs can tell instantly when you try to modify
   a file that someone else has modified in his Emacs.  */

#define CLASH_DETECTION

/* We use the Berkeley (and usg5.2.2) interface to nlist.  */

#define NLIST_STRUCT

/* The file containing the kernel's symbol table is called /vmunix.  */

#define KERNEL_FILE "/dynix"

/* The symbol in the kernel where the load average is found
   is named _avenrun.  */

#define LDAV_SYMBOL "_avenrun"
===================================================================

-- 
Marion Hakanson         Domain: hakanson@cse.ogc.edu
                        UUCP  : {hp-pcd,tektronix}!ogccse!hakanson