stat(1) returns outdated info

Asked by Cyrille Bollu

Hi,

I've written a little script that gives me a message upon login in case of pending commit in my local git repository.

It worked OK until I upgraded to 14.04.1: Then, it started to show false positive.

In the end, I tracked the problem to be related with stat(1) returning outdated information (See hereunder for more details).

Does anyone has a clue of why stat(1) returns outdated info?

Best regards,

Cyrille

=======================================

Technical details:
==============

The script is placed in the /etc/update-mot-d.d/ folder and does the following:

if [ -d /etc/.git ];then
  cd /etc
  diff=$(sudo git status --porcelain);
  if [ -n "$diff" ];then
    echo
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
    echo
    echo 'WARNING: There are uncommited changes in the /etc directory!'
    echo
    echo ' Please run "cd /etc;git status" to figure out what is going on.'
    echo
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
  fi
fi

Now, as I said, it started to return false positive after the upgrade to Ubuntu 14.04.1. For example, yesterday, I've created a file /etc/test during a few hours and then deleted it and today I've had a false positive against this file (It's quite easily reproductible).

Hopefully, I'd changed my script to give me more information:

if [ -d /etc/.git ];then
  cd /etc
  diff=$(sudo git status --porcelain);
  if [ -n "$diff" ];then
    echo
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
    echo
    echo 'WARNING: There are uncommited changes in the /etc directory!'
    echo
    echo ' Please run "cd /etc;git status" to figure out what is going on.'

echo "$diff"
f=$(echo $diff | cut -f 2 -d " " -)
git ls-files --debug $f
stat --format "%n %Y %Z %s" $f
    echo
    echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
  fi
fi

And, the result was this:

login as: support
support@xxx's password:
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-37-generic x86_64)

 * Documentation: https://help.ubuntu.com/

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

WARNING: There are uncommited changes in the /etc directory!

    Please run "cd /etc;git status" to figure out what is going on.
?? test
test 1414588482 1414588482 0

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  System information as of Wed Oct 29 14:54:19 CET 2014

  System load: 0.28 Processes: 153
  Usage of /: 4.6% of 45.71GB Users logged in: 1
  Memory usage: 1% IP address for p1p1: a.b.c.d
  Swap usage: 0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

16 packages can be updated.
16 updates are security updates.

Last login: Wed Oct 29 14:54:19 2014 from 2001::
support@xxx:~$ stat /etc/test
stat: cannot stat ‘/etc/test’: No such file or directory
support@xxx:~$

See how stat(1) returns outdated information during the motd?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

Putting sudo in scripts isn't needed. remove the sudo command then run the script with sudo. The whole script will run as root, so is not needed. Does this make a difference?

Revision history for this message
Cyrille Bollu (cyrille-bollu) said :
#2

Hi,

Thanks for the reply.

The problem never occurs when I run the script interactively; It only occurs when it is launched upon login as part of the update-motd utility.

....

Ah ok got it:

pam.d/login changed between 12.04 and 14.04: They have added a "noupdate" option:

session optional pam_motd.so motd=/run/motd.dynamic noupdate

That's most probably the problem.

Now, the question is: how is this /run/motd.dynamic file updated at all?

Thanks anyway!

Cyrille

Revision history for this message
Best Manfred Hampl (m-hampl) said :
#3

In a quick search I see two bugs about (not correctly) updating motd, maybe one of these has some relationship to your problem:

https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1169558
and
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=757059

Related to the Launchpad/Ubuntu bug:
Is /etc/motd a symlink to /var/run/motd or a real file?

The debian bug has an patch attached that changes the contents of /etc/pam.d/sshd into
...
session optional pam_motd.so motd=/run/motd.dynamic
session optional pam_motd.so noupdate
...
Maybe trying a similar approach in /etc/pam.d/logon could be worth trying

Revision history for this message
Cyrille Bollu (cyrille-bollu) said :
#4

Hi,

thanks for the reply.

I think you've found the root cause:

Ubuntu solved the bug 1169558 about the double update. But, they fixed it wrongly, generating the bug 757059 in Debian's BTS?

Thus, to solve the issue, the fix in bug 757059 should be applied.

Cheers,

Cyrille

Revision history for this message
Cyrille Bollu (cyrille-bollu) said :
#5

Thanks Manfred Hampl, that solved my question.