[net.unix] How to get login shell pid's

dave@andromeda.UUCP (Dave Bloom) (09/26/85)

Here's the story:

I'm writing my own version of "idledaemon" but I'd like a few suggestions from
the net. Namely, what's the best way to get the pid of a users login shell? The
SystemV command 'who -u' supplies the number, but I can't find anything in the
4.2 BSD universe that does the same. Any suggestions? Any idea how AT&T does
it in 'who', and can it be done similarly in 4.2 BSD?

Thanx.
-- 
-------------------------------------------------------------------------------
      allegra\					       Dave Bloom
      harvard \ pyramid\
       seismo  \  pyrnj >!andromeda!dave         HOME: (201) 868-1764
     ut-sally   >!topaz/			 WORK: (201) 648-5083
       sri-iu  /
ihnp4!packard /		           "You're never alone with a schizophrenic...."

pds@mit-vax.UUCP (Philip Steen) (09/29/85)

In article <4@andromeda.UUCP> version B 2.10.2 9/18/84; site mit-vax.UUCP version B 2.10.3 4.3bsd-beta 6/6/85; site andromeda.UUCP mit-vax!mit-eddie!think!harvard!seismo!columbia!topaz!andromeda!dave dave@andromeda.UUCP (Dave Bloom) writes:
>... what's the best way to get the pid of a users login shell?
>... can it be done similarly in 4.2 BSD?

Sure it can.  The shell variable $$ gets the pid for you, use it thusly in
the C-Shell:

	set pid=$$
	echo "the pid of this shell is $$"

One can do something similar in the Bourne Shell.

itkin@luke.UUCP (Steven List) (10/01/85)

In article <4@andromeda.UUCP> dave@andromeda.UUCP (Dave Bloom) writes:
>I'm writing my own version of "idledaemon" but I'd like a few suggestions from
>the net. Namely, what's the best way to get the pid of a users login shell? The
>SystemV command 'who -u' supplies the number, but I can't find anything in the
>4.2 BSD universe that does the same. Any suggestions? Any idea how AT&T does
>it in 'who', and can it be done similarly in 4.2 BSD?

System V (and III) use /etc/utmp to store information about logged in users.
There is an include file <utmp.h> described in section 4 of the Programmer
Reference Manual that describes the structure of each /etc/utmp entry.

As I recall, in S5 there is also a set of functions that will read and
write /etc/utmp for you.
-- 
***
*  Steven List @ Benetics Corporation, Mt. View, CA
*  Just part of the stock at "Uncle Bene's Farm"
*  {cdp,greipa,idi,oliveb,sun,tolerant}!bene!luke!itkin
***

mikel@codas.UUCP (Mikel Manitius) (10/06/85)

> Here's the story:
> 
> I'm writing my own version of "idledaemon" but I'd like a few suggestions from
> the net. Namely, what's the best way to get the pid of a users login shell? The
> SystemV command 'who -u' supplies the number, but I can't find anything in the
> 4.2 BSD universe that does the same. Any suggestions? Any idea how AT&T does
> it in 'who', and can it be done similarly in 4.2 BSD?
> 
> Thanx.
> -- 
> -------------------------------------------------------------------------------
>       allegra\					       Dave Bloom
>       harvard \ pyramid\
>        seismo  \  pyrnj >!andromeda!dave         HOME: (201) 868-1764
>      ut-sally   >!topaz/			 WORK: (201) 648-5083
>        sri-iu  /
> ihnp4!packard /		           "You're never alone with a schizophrenic...."

Under System V, the login shell's pid is stored in /etc/utmp, the follwing
is the contents of /usr/include/utmp.h
-------
/*	@(#)utmp.h	1.2	*/
/*	@(#)utmp.h	3.2	*/

/*	<sys/types.h> must be included.					*/

#define	UTMP_FILE	"/etc/utmp"
#define	WTMP_FILE	"/etc/wtmp"
#define	ut_name	ut_user

struct utmp
  {
	char ut_user[8] ;		/* User login name */
	char ut_id[4] ; 		/* /etc/lines id(usually line #) */
	char ut_line[12] ;		/* device name (console, lnxx) */
	short ut_pid ;			/* process id */
	short ut_type ; 		/* type of entry */
	struct exit_status
	  {
	    short e_termination ;	/* Process termination status */
	    short e_exit ;		/* Process exit status */
	  }
	ut_exit ;			/* The exit status of a process
					 * marked as DEAD_PROCESS.
					 */
	time_t ut_time ;		/* time entry was made */
  } ;

/*	Definitions for ut_type						*/

#define	EMPTY		0
#define	RUN_LVL		1
#define	BOOT_TIME	2
#define	OLD_TIME	3
#define	NEW_TIME	4
#define	INIT_PROCESS	5	/* Process spawned by "init" */
#define	LOGIN_PROCESS	6	/* A "getty" process waiting for login */
#define	USER_PROCESS	7	/* A user process */
#define	DEAD_PROCESS	8
#define	ACCOUNTING	9

#define	UTMAXTYPE	ACCOUNTING	/* Largest legal value of ut_type */

/*	Special strings or formats used in the "ut_line" field when	*/
/*	accounting for something other than a process.			*/
/*	No string for the ut_line field can be more than 11 chars +	*/
/*	a NULL in length.						*/

#define	RUNLVL_MSG	"run-level %c"
#define	BOOT_MSG	"system boot"
#define	OTIME_MSG	"old time"
#define	NTIME_MSG	"new time"
-------------

	And you just read all the entries that have a ut_type equal to
USER_PROCESS. This does not exist on BSD systems, however, I once wrote
a kill idle program on a V8 System, you don't really need to know the
person's root pid, just read /etc/utmp and get his ut_line, check the
access times to determine if he is idle, and if so, set sg.sg_ispeed
and sg.sg_ospeed to 0 (from sgtty.h, or, I beleive stty.h on BSD), then
stty(3) or ioctl(2) his terminal with this (as long as you have modem
control) and he will be logged off. Also, it's nice to warn the person
that he will be nuked if he remains idle for another few minutes, then
nuke him.
-- 
                                        =======
     Mikel Manitius                   ==----=====    AT&T
     (305) 869-2462 RNX: 755         ==------=====   Information Systems 
     ...{akguc|ihnp4}!codas!mikel    ===----======   SDSS Regional Support
     ...attmail!mmanitius             ===========    Altamonte Springs, FL
     My opinions are my own.            =======