[net.unix] which is my login terminal

nwh@gec-rl-hrc.co.uk (Nigel Holder Marconi) (06/24/86)

   I have an application that needs to know whether I am logged
on at the console to decide whether I can paint all over the
sun bit-mapped screen.  I could be in suntools, a pty (either via
suntools, remote login or any windowing system), the normal console,
device, or an external port (eg. ttya).  What I really need to know
is whether I actually logged in at the console for this session.
As other people might want to run the application the solution must
be general.  If I'm not on a pty then it is possible to use ttyname().
Otherwise, how is it possible ? (I've thought about finding
out somehow whether my pty and its associated winxx device
are installed in the suntools environment, but haven't pursued it yet).


Nigel Holder			UK JANET:       yf21@uk.co.gec-mrc.u
Marconi Research,		ARPA:           yf21%u.gec-mrc.co.uk@ucl-cs
Chelmsford,
Essex. CM2 8HN.

+44 245 73331   ext. 3219 / 3214

jay@isis.UUCP (Jay Batson) (06/26/86)

In article <34@gec-rl-hrc.co.uk> nwh@gec-rl-hrc.co.uk (Nigel Holder Marconi) writes:
>
>   I have an application that needs to know whether I am logged
>on at the console to decide whether I can paint all over the
>sun bit-mapped screen.

This is a simplistic solution, and probably does more work than necessary.
Somebody will probably enlighten us as to another way, but this _should_
also work.  And I've been flamed about using my _V7_ info (since that's
what I'm running) to answer net questions, but I thought I'd
chime in anyway....

The function below will simply check if the minor device numbers of
the console and the tty device associated with the current process.
The minor device number should probably be the same if they are the
same terminal.  One _IMPORTANT_ caveat - I don't know enough about a Sun to
say FOR SURE that this will work on that machine.  If windows are
implemented as separate logical devices, I'm certain it won't.
To be honest, I'm not sure that the minor numbers would be the same on
all machines - I know it is on mine.  I've never looked into the
matter extensively.

Look at stat(2), ttyname/slot(2), and types(5), and try this:
(for you purists, I know I'm not doing error checking...)
(I'm not certain that /etc/ttys is used/present in SV, so this probably
will only work on 4.2 and similar.)

___cut here___
/* returns 0 if on console, -1 if not. */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
on_console()
{
	struct stat somevar1, somevar2;
	char ttydata[16], ttyline[16]; /* size is somewhat arbitrary here... */
	int slotnum; /* for the line number in /etc/ttys indicating the
			tty line our process is associated with */
	FILE *ttydescr; /* to read /etc/ttys */
	
		/* get the /etc/ttys line we are associated with */
	slotnum = ttyslot();
		/* actually get the /etc/ttys entry - this seems redundant -
			there ought to be another way... */
	ttydescr = fopen("/etc/ttys", "r");
	for ( ; slotnum > 0 ; slotnum--) fgets(ttydata, 16, ttydescr);
	fclose(ttydescr);
		/* zap nl */
	ttydata[(strlen(ttydata) - 1)] = '\0';
		/* need "/dev/", and need to zap 2 char ttys line hdr */
	sprintf(ttyline, "/dev/%s", &ttydata[2]);
		/* we want the minor device numbers of the console, and
			of our device */
	stat ("/dev/console", &somevar1);
	stat (ttyline, &somevar2);
		/* test if they are the same and return appropriate values */
	if (minor(somevar1.st_rdev) == minor(somevar2.st_rdev))
		return(0);
	else
		return(-1);
}

Well, now that I've said that, it seems like a lot of trouble, and I'm
not sure of it's portability, although lint only objects to my lack of
error checking.

Oh well, it was an interesting 10 minutes.  LET THE FLAMES BEGIN!!!
--------

"Stop it!! Stop it now.  This is getting silly again, and this silliness
has _got_ to stop.  Go on to the next sketch.  Go on.  Turn this camera o    "

Jay Batson
{seismo,hplabs}!hao!isis!jay

guido@mcvax.uucp (Guido van Rossum) (06/30/86)

In shell scripts, do the following:

	case `tty` in
	/dev/console)	CONSOLE=YES;;
	*)		CONSOLE=NO;;
	esac

The equivalent 'C' code is:

bool on_console() {
	int i;
	/* Search file descriptors 0, 1, 2 for a device name */
	for (i= 0; i <= 2; ++i) {
		char *p= ttyname(i);
		if (p != 0 && strcmp(p, "/dev/console") == 0)
			return YES;
	}
	return NO;
}

Of course this returns NO in a window -- there you can't 'scribble all
over the display' as the original poster asked.

Guido van Rossum, CWI, Amsterdam <guido@mcvax.uucp>

mark@umcp-cs.UUCP (Mark Weiser) (07/01/86)

In article <34@gec-rl-hrc.co.uk> nwh@gec-rl-hrc.co.uk (Nigel Holder Marconi) writes:
>
>   I have an application that needs to know whether I am logged
>on at the console to decide whether I can paint all over the
>sun bit-mapped screen.  I could be in suntools, a pty (either via
>suntools, remote login or any windowing system), the normal console,
>device, or an external port (eg. ttya).  What I really need to know
>is whether I actually logged in at the console for this session.
>As other people might want to run the application the solution must
>be general.  If I'm not on a pty then it is possible to use ttyname().

There is no sure fire method, but one I use is: check ttyname(0)
(i.e. of the file descripter underlying stdin).  If it is /dev/console,
assume you DO have screen access.  If that is not
/dev/console, then check for the existence of the WINDOW_GFX environment
variable.  If it is not set, assume you do not have screen access.
-- 
Spoken: Mark Weiser 	ARPA:	mark@maryland	Phone: +1-301-454-7817
CSNet:	mark@umcp-cs 	UUCP:	{seismo,allegra}!umcp-cs!mark
USPS: Computer Science Dept., University of Maryland, College Park, MD 20742