[comp.unix.questions] how to determine if a process has access to tty ports

coleman@cam.nist.gov (Sean Sheridan Coleman X5672) (01/23/91)

I want to be able to determine if another process has control
of an RS232 port on a Alm Board on a Sun4. I am writing a 
program to gather data from a device that is connected via
RS-232. I want to deny access to the program if another program
is accessing the port currently. 

I want to know (1) how to tell if a port is being used
currently for a login port. ie.. some has log in using the port
(2) how to tell if a process has control of a port for out going
communication such as kermit?

I use a simplistic tty driver for comunicating to the  ports.
Is there something I should add to the driver code to allow
other programs to do what I have asked about above?

Thanks

Sean Coleman
NIST
coleman@bldrdoc.gov

mike (Michael Stefanik) (01/23/91)

In article <6763@fs1.cam.nist.gov> cam.nist.gov!coleman (Sean Sheridan Coleman X5672) writes:
:I want to be able to determine if another process has control
:of an RS232 port on a Alm Board on a Sun4. I am writing a 
:program to gather data from a device that is connected via
:RS-232. I want to deny access to the program if another program
:is accessing the port currently. 

If you're worried about login processes, you could read /etc/utmp (but if
this is a special device, I doubt that's the case); the slicker solution
would be to read the namelist in /unix, and look through the proc table.
A similar (but considerably slower) solution would be to popen() ps -t ttyxx
and see if there is anything attached to it.  If you're looking for any
process that has simply done an open() on the port, though, that's another
story.
-- 
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."

jfh@rpp386.cactus.org (John F Haugh II) (01/25/91)

In article <385@bria> bria!mike (Michael Stefanik) writes:
>If you're worried about login processes, you could read /etc/utmp (but if
>this is a special device, I doubt that's the case); the slicker solution
>would be to read the namelist in /unix, and look through the proc table.
>A similar (but considerably slower) solution would be to popen() ps -t ttyxx
>and see if there is anything attached to it.  If you're looking for any
>process that has simply done an open() on the port, though, that's another
>story.

There are other ways - for example, BSD seems to come with an "ofiles"
command, and USG seems to come with a "fuser" command.

For example -

% fuser /dev/tty03
/dev/tty03:  2793 2794 2809 48 2404
% ofiles /dev/tty03
/dev/tty03	
User    	PID	Type	Command
jfh     	2793	f	sh            
jfh     	2794	f	sh            
jfh     	2809	f	vi            
jfh     	48	f	ksh           
jfh     	2404	f	rn            

take your pick.

It is possible under System V to "disconnect" yourself from the controlling
terminal in such a way that ps(1) does not report you as having the port
open, or you could close all your open files, but still be associated with
the port.  Here is an example of each.

% ofiles /dev/console
/dev/console	
User    	PID	Type	Command
sysinfo 	40	f	lload         
% ps -ft console
    UID   PID  PPID  C    STIME TTY  TIME COMMAND
   root    31     1  0 19:30:02 con  0:02 /usr/local/bin/utmpd 
% ps -fp 40
    UID   PID  PPID  C    STIME TTY  TIME COMMAND
   root    40     1  8 19:30:14   ?  0:03 /etc/lload 
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org
"13 of 17 valedictorians in Boston High Schools last spring were immigrants
 or children of immigrants"   -- US News and World Report, May 15, 1990