[comp.unix.questions] Logging the ^d logoff

pjh@mccc.UUCP (Peter J. Holsberg) (02/12/88)

A while ago, I expressed interest in a method of creating a file of 
user logins/outs.  Many people made many helpful suggestions, but
I find myself without a solution.

One suggestion, which came very close, was to trap 0 1 3 with a line in
/etc/profile so that on ^d logout (or hangup, etc.) a line with "who am
i" information would be written to /usr/.logins.  I found that this
worked quite well except for one thing:  despite the fact that the login
time is accurate, the logout time is always about one second after the
login time, regardless of the actual logout!

So, how can I make "who am i", imbedded in a 'trap' statement execute at
the instant of the trap, rather than when /etc/profile is first
executed?  Many thanks for your help.

-- 
Peter Holsberg                  UUCP: {rutgers!}princeton!mccc!pjh
Technology Division             CompuServe: 70240,334
Mercer College                  GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/13/88)

In article <209@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
> One suggestion, which came very close, was to trap 0 1 3 with a line in
> /etc/profile so that on ^d logout (or hangup, etc.) a line with "who am
> i" information would be written to /usr/.logins.  I found that this
> worked quite well except for one thing:  despite the fact that the login
> time is accurate, the logout time is always about one second after the
> login time, regardless of the actual logout!

I suspect the only problem is that you forgot that the arguments to the
"trap" built-in would be evaluated twice, once at the time of executing
the "trap" built-in, and once (with whatever the result of the first
evaluation was) when the trap fires at logout time.  Once you realize
this, the solution is obvious: quote the "who am i" so that it will not
be evaluated when "trap" is set.

pjh@mccc.UUCP (Peter J. Holsberg) (02/14/88)

In article <7255@brl-smoke.ARPA> gwyn@brl-smoke.ARPA (Doug Gwyn ) writes:
|In article <209@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
|> One suggestion, which came very close, was to trap 0 1 3 with a line in
|> /etc/profile so that on ^d logout (or hangup, etc.) a line with "who am
|> i" information would be written to /usr/.logins.  I found that this
|> worked quite well except for one thing:  despite the fact that the login
|> time is accurate, the logout time is always about one second after the
|> login time, regardless of the actual logout!
|
|I suspect the only problem is that you forgot that the arguments to the
|"trap" built-in would be evaluated twice, once at the time of executing
|the "trap" built-in, and once (with whatever the result of the first
|evaluation was) when the trap fires at logout time.  Once you realize
|this, the solution is obvious: quote the "who am i" so that it will not
|be evaluated when "trap" is set.

Here's what's in my /etc/profile now:

echo IN: "\t" $LOGNAME "\t" `tty` "\t" `date` >> /usr/pjh/.logins

trap 'echo OUT: "\t" $LOGNAME " \t\c" >>/usr/.logins; `tty` >>/usr/.logins  `date`  >> /usr/.logins' 0 1 3


The 'trap' doesnm't work.  Please suggest modifications.  (I've tried
many but clearly, I've overlooked the one that will work.)  Thx.

-- 
Peter Holsberg                  UUCP: {rutgers!}princeton!mccc!pjh
Technology Division             CompuServe: 70240,334
Mercer College                  GEnie: PJHOLSBERG
Trenton, NJ 08690               Voice: 1-609-586-4800

wnp@dcs.UUCP (Wolf N. Paul) (02/16/88)

In article <213@mccc.UUCP> pjh@mccc.UUCP (Peter J. Holsberg) writes:
>Here's what's in my /etc/profile now:
>
>echo IN: "\t" $LOGNAME "\t" `tty` "\t" `date` >> /usr/pjh/.logins
>
>trap 'echo OUT: "\t" $LOGNAME " \t\c" >>/usr/.logins; `tty` >>/usr/.logins  `date`  >> /usr/.logins' 0 1 3
>
>
>The 'trap' doesnm't work.  Please suggest modifications.  (I've tried

I tried something yesterday related to a discussion of the MKS shell (MS DOS),
where someone wanted a ".logout" equivalent for the Korn shell.

In /etc/profile I put the following line:

	trap ". $HOME/.logout" 0 1 3

and in $HOME/.logout any commands that I want executed every time I log out.

This works on my Microport System V machine.

Actually, the trap line in /etc/profile is within a case statement which 
checks for a $0 of "-sh" - since we don't want this to execute whenever we
leave ANY old shell, but only when we leave the login shell.

Putting whatever you want to happen on logout into a separate shell script
removes all of the ambiguities and pitfalls of when it will be interpreted
by the shell - it won't, until the trap is sprung and the separate shell
script is read.

-------
-- 
-------------------
Wolf N. Paul                  Phone: (214) 306-9101 (h)   (214) 404-8077 (w)
3387 Sam Rayburn Run          UUCP: ihnp4!killer!{dcs, doulos}!wnp
Carrollton, TX 75007          INTERNET: wnp@dcs.UUCP       ESL:  62832882

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/17/88)

Ok, here's a working version.  I chose to evaluate $LOGNAME when the trap
is set, to forestall any later forgery, but you can change this if you
wish, once you figure out the general principle by studying this example:

echo "IN:\t$LOGNAME\t`tty`\t`date`" >> /usr/.logins
trap "echo \"OUT:\t$LOGNAME\t\`tty\`\t\`date\`\" >> /usr/.logins" 0 1 15

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (02/17/88)

I tried "who am i" on Ultrix and Xenix, and looked in a real SysV
manual. All seem to list the login time of the user, rether than the
current time. May I comment to your attention the "date" command.

How about:
trap "echo `who am i` `date`" 0 1 3

this gives you the one line output you might want.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

levy@ttrdc.UUCP (Daniel R. Levy) (02/17/88)

In article <7255@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes:
# In article <209@mccc.UUCP>, pjh@mccc.UUCP (Peter J. Holsberg) writes:
# > One suggestion, which came very close, was to trap 0 1 3 with a line in
# > /etc/profile so that on ^d logout (or hangup, etc.) a line with "who am
# > i" information would be written to /usr/.logins.  I found that this
# > worked quite well except for one thing:  despite the fact that the login
# > time is accurate, the logout time is always about one second after the
# > login time, regardless of the actual logout!
# I suspect the only problem is that you forgot that the arguments to the
# "trap" built-in would be evaluated twice, once at the time of executing
# the "trap" built-in, and once (with whatever the result of the first
# evaluation was) when the trap fires at logout time.  Once you realize
# this, the solution is obvious: quote the "who am i" so that it will not
# be evaluated when "trap" is set.

A few notes:
1) "who am i" will print out the login time, not the present time.
   [I thought you knew better than this, Doug!]
2) There is no need to trap anything other than 0.  Trapping 3 will
   definitely give misleading results if the user hits the QUIT character.

trap '(/usr/bin/id; /bin/date) >> /usr/.logins' 0

3) Anything which is user-executable like this can be forged.  A malicious
   user could clear out or alter /usr/.logins (which must be world writeable).
   The user can 'exec true' or 'kill -9 $$' or something like that to get
   out without leaving a record, or he can reset the trap manually.

4) There's no clear way out of the box without getting accounting software.
-- 
|------------Dan Levy------------|  Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
|         an Engihacker @        |  	<most AT&T machines>}!ttrdc!ttrda!levy
| AT&T Computer Systems Division |  Disclaimer?  Huh?  What disclaimer???
|--------Skokie, Illinois--------|