[net.unix-wizards] Help!

paws@sphinx.UChicago.UUCP (Randy Smith) (06/11/85)

    I'm having problems with using the ioctl system call.  I eventually
  got a sample of the problem I'm having down to the following C program.

-----------------------------------------------------

#include <sys/ioctl.h>
#include <sgtty.h>
#include <stdio.h>
#include <sys/wait.h>

FILE *debug;

main()
{
	long me, mepg;
	int imepg;
	union wait status;

	debug = fopen("debug.file","w");
	if (!fork()) {
	   me = getpid();
	   imepg = (int) mepg = getpgrp((int)me);
	   fprintf(debug,"Me: %d, Pg: %d\n",me,mepg);
	   fflush(debug);

	   fprintf(debug,"Return from setpgrp: %d\n",setpgrp(me,me));
	   fflush(debug);

	   fprintf(debug,"Return from ioctl: %d\n",ioctl(1,TIOCSPGRP,&imepg));
	   fflush(debug);

	   fprintf(debug,"I made it (thought you'd like to know)\n");
	   fflush(debug);
	   exit(0);
	}
	fprintf(debug,"Pid from wait: %d\n",wait(&status));
	fprintf(debug,"Status code returned: %d\n",status);
}

   ------------------------------------------------------

    (forgive the sloppy last printf).

    When I copile and run this code, it hangs in the ioctl.  My debug
  file shows up (after I ^C) looking like:

--------------------------------------------------------

Me: 5567, Pg: 5566
Return from setpgrp: 0

--------------------------------------

    If I do a ps (from another terminal) while the thing is hanging, I get:

      F UID   PID  PPID CP PRI NI     ADDR  SZ  RSS WCHAN STAT TTY  TIME COMMAND
   8201 134  4098     1  0  15  0 ff8b8800  58   14     0 I    i05  0:13 -tcsh 
 408021 134  5217  4098  0  25  0 ff8c4800 202    8       T    i05  0:04 emacs 
   8001 134  5354  4098  0   5  0 ff918000  28    6 b698c I    i05  0:00 testio
   8001 134  5355  5354  0   4  0 ff954000  36    6       T    i05  0:00 testio
   8201 134  5373     1  0  15  0 ff8ed800  56   50     0 S    i13  0:11 -tcsh 
   8001 134  5462  5373 34  33  0 ff926800  70   38       R    i13  0:00 ps glx
   8001 134  5463  5373  4   1  0 ff93c800  30   10 3f944 S    i13  0:00 tee ex

 
  where testio is the name of the program, and tee and emacs should be ignored.

    Why is this call hanging on me?  I'm on a Pyramid 90x, but I got the same
  (ie. confusing enough that I couldn't tell the difference) results on a 
  VAX 750.  Suggestions from the net?

                                     -- Randy Smith

    P.S.  If the parent process doesn't wait for the child, the child EXITS
           during the ioctl.


    UUcp:  . . . ihnp4!gargoyle!sphinx!paws
    Arpa:  dvlp.dilvish%uchicago.mailnet@mit-multics.arpa
    CSnet: randy@uchicago.csnet

paws@sphinx.UChicago.UUCP (06/12/85)

    Not a day after I posted my message I figured out what was going on.
  (Oops)  The ioctl is done when the process is no longer in the process
  group of the terminal (after the setpgrp) and unix apparently doesn't 
  like ioctl's preformed from such processes.  Switching the order of the
  ioctl and the setpgrp got rid of the hang.

                              -- Randy Smith

-- 

    UUcp:  . . . ihnp4!gargoyle!sphinx!paws
    Arpa:  dvlp.dilvish%uchicago.mailnet@mit-multics.arpa
    CSnet: randy@uchicago.csnet

guy@sun.uucp (Guy Harris) (06/13/85)

>     I'm having problems with using the ioctl system call.

The problem is not with the "ioctl" system call but with the specific
"ioctl" call you're using, namely TIOCSPGRP.  If a process attempts to do a
TRIOCSPGRP on its control terminal and it isn't in the same process group as
the terminal, the process gets a SIGTTOU signal and stops (which is why the
state field of the "ps" reports T).

Moral: do the TIOCSPGRP first and then do the "setpgrp" on yourself second.

	Guy Harris

chris@umcp-cs.UUCP (Chris Torek) (06/16/85)

> Moral: do the TIOCSPGRP first and then do the "setpgrp" on yourself second.

And make darned sure you've held SIGINT and the like!
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland