tail -f -pid data race

Version:

coreutils-7.5 (fixed in 7.6)

How it is diagnosed (reproduced or source analysis)?

We couldn’t reproduce this bug, so we just analyzed the source code.

Symptom:

Data written just before the writer process dies might not have been output by tail

Root cause:

Data race.

Writer process might write something and die in-between of tail read some contents and calls sleep.

Comments written by us is in blue color.

static void tail_forever (... …) {

     while (1) {

     /* read file and flush any newly arrived content to stdout. */

     if (!any_input)

       {

         if (writer_is_dead)

           break;

       /* In the buggy version, the writer process might write something and

         die here (before the content was read above), thus the sleep of tail

         would miss the content. */

-       if (xnanosleep (sleep_interval))

-          error (EXIT_FAILURE, errno, _("cannot read realtime clock"));

       /* Once the writer is dead, read the files once more to

          avoid a race condition.  */

      writer_is_dead = (pid != 0 && kill (pid, 0) != 0

                    /* Handle the case in which you cannot send a

                       signal to the writer, so kill fails and sets

                       errno to EPERM.  */

                         && errno != EPERM);

+         if (!writer_is_dead && xnanosleep (sleep_interval))

+            error (EXIT_FAILURE, errno, _("cannot read realtime clock"));

       }

   }

}

Is there Error Message?

No

Can Errlog anticipate an error msg?

Yes. Log the error return value of system call ‘kill’.

This log msg will be quite helpful as in this bug, the key to diagnose is to know the writer process died, which is manifested by ‘kill’ returned non-zero. So we should log it!