[comp.unix.misc] mail notification at login

allen@nntp-server.caltech.edu (Scott R. Allen) (02/07/91)

Hello -

I am experiencing an anomoly with mail that I don't quite
understand and I would greatly appreciate it if someone could
explain this to me.  Here is the scenario:

Sun workstation --SunOS version 4.1

1.  Root sends mail to user1.
2.  User1 logs in and gets the 'you have new mail' message.
3.  User1 logs out.
4.  User1 logs in again and gets the same 'you have new mail' message.
5.  User1 logs out.
6.  Root executes the command 'cat /var/spool/mail/user1' and the
    all of the mail in user1's folder scrolls on the screen, the
    message that root sent being the last.
7.  User1 logs in again and this time gets the message 'you
    have mail' rather than 'you have new mail'.

The only thing that has happened between step 4 and step 7 is 
that root has done a cat on user1's mail folder.  My question is:
WHY does this happen?  How does it now that the file has been
read by someone and why does it change the message from 'you have
new mail' to 'you have mail'?  

It is my understanding that this test for mail is internal to
the 'login' program.

Note:  A probably unrelated detail -- If this is the first time
that user1 has ever gotton mail (when root sends it in step 1)
and therefore user1 doesn't have a /var/spool/mail/user1 file
then it is created at step 1.  Oddly enough, if this is the case
then in step 2 when user1 logs in the message only says 'you
have mail' rather than 'you have new mail'.  I assume that this
is a bug.  To get around this, root sends user1 two mail messages
or someone else has previously sent user1 mail.

If someone can explain how 'login' decides between 'you have mail'
and 'you have new mail' I would really appreciate it.

thanks

Scott Allen
allen@tybalt.caltech.edu

allen@nntp-server.caltech.edu (Scott R. Allen) (02/07/91)

Thanks for the replies -- I understand now.

Scott

jik@athena.mit.edu (Jonathan I. Kamens) (02/07/91)

  /bin/login (or whatever checks for new mail when the user logs in)
uses the following algorithm to figure out what message to print:

1. Stat() the user's mail file.

2. If the stat() fails, or if the st_size member of the stat structure
   is equal to 0, then exit the algorithm (i.e. don't print anything).

3. If the st_mtime (the *modification* time of the file) is later than
   the st_atime (the *access* time of the file), then print "You have
   new mail." and exit the algorithm.

4. Print "You have mail." and exit the algorithm.

  When root cat's the user's mail file, the access time is set to the
current time, which is later than the last modification time, so login
assumes that the user has read the mail and put it back into his
mailbox.

  When the first mail message is sent to the user and the file is
created, then the access time is equal to the last modification time,
so login assumes once again that the user has read the mail and put it
back into his mailbox.

  However, when a message is sent to the user and then nothing else
happens before the user logs in, then the modification time is later
than the access time, so login prints the "new mail" message.

  I hope this clarifies things.

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710