Blame login/tst-pututxline-lockfail.c

Packit Service b302fa
/* Test the lock upgrade path in tst-pututxline.
Packit Service b302fa
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service b302fa
   This file is part of the GNU C Library.
Packit Service b302fa
Packit Service b302fa
   The GNU C Library is free software; you can redistribute it and/or
Packit Service b302fa
   modify it under the terms of the GNU Lesser General Public License as
Packit Service b302fa
   published by the Free Software Foundation; either version 2.1 of the
Packit Service b302fa
   License, or (at your option) any later version.
Packit Service b302fa
Packit Service b302fa
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service b302fa
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b302fa
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b302fa
   Lesser General Public License for more details.
Packit Service b302fa
Packit Service b302fa
   You should have received a copy of the GNU Lesser General Public
Packit Service b302fa
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit Service b302fa
   not, see <http://www.gnu.org/licenses/>.  */
Packit Service b302fa
Packit Service b302fa
/* pututxline upgrades the read lock on the file to a write lock.
Packit Service b302fa
   This test verifies that if the lock upgrade fails, the utmp
Packit Service b302fa
   subsystem remains in a consistent state, so that pututxline can be
Packit Service b302fa
   called again.  */
Packit Service b302fa
Packit Service b302fa
#include <errno.h>
Packit Service b302fa
#include <fcntl.h>
Packit Service b302fa
#include <stdlib.h>
Packit Service b302fa
#include <support/check.h>
Packit Service b302fa
#include <support/namespace.h>
Packit Service b302fa
#include <support/support.h>
Packit Service b302fa
#include <support/temp_file.h>
Packit Service b302fa
#include <support/xthread.h>
Packit Service b302fa
#include <support/xunistd.h>
Packit Service b302fa
#include <unistd.h>
Packit Service b302fa
#include <utmp.h>
Packit Service b302fa
#include <utmpx.h>
Packit Service b302fa
Packit Service b302fa
/* Path to the temporary utmp file.   */
Packit Service b302fa
static char *path;
Packit Service b302fa
Packit Service b302fa
/* Used to synchronize the subprocesses.  The barrier itself is
Packit Service b302fa
   allocated in shared memory.  */
Packit Service b302fa
static pthread_barrier_t *barrier;
Packit Service b302fa
Packit Service b302fa
/* Use pututxline to write an entry for PID.  */
Packit Service b302fa
static struct utmpx *
Packit Service b302fa
write_entry (pid_t pid)
Packit Service b302fa
{
Packit Service b302fa
  struct utmpx ut =
Packit Service b302fa
    {
Packit Service b302fa
     .ut_type = LOGIN_PROCESS,
Packit Service b302fa
     .ut_id = "1",
Packit Service b302fa
     .ut_user = "root",
Packit Service b302fa
     .ut_pid = pid,
Packit Service b302fa
     .ut_line = "entry",
Packit Service b302fa
     .ut_host = "localhost",
Packit Service b302fa
    };
Packit Service b302fa
  return pututxline (&ut);
Packit Service b302fa
}
Packit Service b302fa
Packit Service b302fa
/* Create the initial entry in a subprocess, so that the utmp
Packit Service b302fa
   subsystem in the original process is not disturbed.  */
Packit Service b302fa
static void
Packit Service b302fa
subprocess_create_entry (void *closure)
Packit Service b302fa
{
Packit Service b302fa
  TEST_COMPARE (utmpname (path), 0);
Packit Service b302fa
  TEST_VERIFY (write_entry (101) != NULL);
Packit Service b302fa
}
Packit Service b302fa
Packit Service b302fa
/* Acquire an advisory read lock on PATH.  */
Packit Service b302fa
__attribute__ ((noreturn)) static void
Packit Service b302fa
subprocess_lock_file (void)
Packit Service b302fa
{
Packit Service b302fa
  int fd = xopen (path, O_RDONLY, 0);
Packit Service b302fa
Packit Service b302fa
  struct flock64 fl =
Packit Service b302fa
    {
Packit Service b302fa
     .l_type = F_RDLCK,
Packit Service b302fa
     fl.l_whence = SEEK_SET,
Packit Service b302fa
    };
Packit Service b302fa
  TEST_COMPARE (fcntl64 (fd, F_SETLKW, &fl), 0);
Packit Service b302fa
Packit Service b302fa
  /* Signal to the main process that the lock has been acquired.  */
Packit Service b302fa
  xpthread_barrier_wait (barrier);
Packit Service b302fa
Packit Service b302fa
  /* Wait for the unlock request from the main process.  */
Packit Service b302fa
  xpthread_barrier_wait (barrier);
Packit Service b302fa
Packit Service b302fa
  /* Implicitly unlock the file.  */
Packit Service b302fa
  xclose (fd);
Packit Service b302fa
Packit Service b302fa
  /* Overwrite the existing entry.  */
Packit Service b302fa
  TEST_COMPARE (utmpname (path), 0);
Packit Service b302fa
  errno = 0;
Packit Service b302fa
  setutxent ();
Packit Service b302fa
  TEST_COMPARE (errno, 0);
Packit Service b302fa
  TEST_VERIFY (write_entry (102) != NULL);
Packit Service b302fa
  errno = 0;
Packit Service b302fa
  endutxent ();
Packit Service b302fa
  TEST_COMPARE (errno, 0);
Packit Service b302fa
Packit Service b302fa
  _exit (0);
Packit Service b302fa
}
Packit Service b302fa
Packit Service b302fa
static int
Packit Service b302fa
do_test (void)
Packit Service b302fa
{
Packit Service b302fa
  xclose (create_temp_file ("tst-pututxline-lockfail-", &path));
Packit Service b302fa
Packit Service b302fa
  {
Packit Service b302fa
    pthread_barrierattr_t attr;
Packit Service b302fa
    xpthread_barrierattr_init (&attr);
Packit Service b302fa
    xpthread_barrierattr_setpshared (&attr, PTHREAD_SCOPE_PROCESS);
Packit Service b302fa
    barrier = support_shared_allocate (sizeof (*barrier));
Packit Service b302fa
    xpthread_barrier_init (barrier, &attr, 2);
Packit Service b302fa
    xpthread_barrierattr_destroy (&attr);
Packit Service b302fa
  }
Packit Service b302fa
Packit Service b302fa
  /* Write the initial entry.  */
Packit Service b302fa
  support_isolate_in_subprocess (subprocess_create_entry, NULL);
Packit Service b302fa
Packit Service b302fa
  pid_t locker_pid = xfork ();
Packit Service b302fa
  if (locker_pid == 0)
Packit Service b302fa
    subprocess_lock_file ();
Packit Service b302fa
Packit Service b302fa
  /* Wait for the file locking to complete.  */
Packit Service b302fa
  xpthread_barrier_wait (barrier);
Packit Service b302fa
Packit Service b302fa
  /* Try to add another entry.  This attempt will fail, with EINTR or
Packit Service b302fa
     EAGAIN.  */
Packit Service b302fa
  TEST_COMPARE (utmpname (path), 0);
Packit Service b302fa
  TEST_VERIFY (write_entry (102) == NULL);
Packit Service b302fa
  if (errno != EINTR)
Packit Service b302fa
    TEST_COMPARE (errno, EAGAIN);
Packit Service b302fa
Packit Service b302fa
  /* Signal the subprocess to overwrite the entry.  */
Packit Service b302fa
  xpthread_barrier_wait (barrier);
Packit Service b302fa
Packit Service b302fa
  /* Wait for write and unlock to complete.  */
Packit Service b302fa
  {
Packit Service b302fa
    int status;
Packit Service b302fa
    xwaitpid (locker_pid, &status, 0);
Packit Service b302fa
    TEST_COMPARE (status, 0);
Packit Service b302fa
  }
Packit Service b302fa
Packit Service b302fa
  /* The file is no longer locked, so this operation will succeed.  */
Packit Service b302fa
  TEST_VERIFY (write_entry (103) != NULL);
Packit Service b302fa
  errno = 0;
Packit Service b302fa
  endutxent ();
Packit Service b302fa
  TEST_COMPARE (errno, 0);
Packit Service b302fa
Packit Service b302fa
  /* Check that there is just one entry with the expected contents.
Packit Service b302fa
     If pututxline becomes desynchronized internally, the entry is not
Packit Service b302fa
     overwritten (bug 24902).  */
Packit Service b302fa
  errno = 0;
Packit Service b302fa
  setutxent ();
Packit Service b302fa
  TEST_COMPARE (errno, 0);
Packit Service b302fa
  struct utmpx *ut = getutxent ();
Packit Service b302fa
  TEST_VERIFY_EXIT (ut != NULL);
Packit Service b302fa
  TEST_COMPARE (ut->ut_type, LOGIN_PROCESS);
Packit Service b302fa
  TEST_COMPARE_STRING (ut->ut_id, "1");
Packit Service b302fa
  TEST_COMPARE_STRING (ut->ut_user, "root");
Packit Service b302fa
  TEST_COMPARE (ut->ut_pid, 103);
Packit Service b302fa
  TEST_COMPARE_STRING (ut->ut_line, "entry");
Packit Service b302fa
  TEST_COMPARE_STRING (ut->ut_host, "localhost");
Packit Service b302fa
  TEST_VERIFY (getutxent () == NULL);
Packit Service b302fa
  errno = 0;
Packit Service b302fa
  endutxent ();
Packit Service b302fa
  TEST_COMPARE (errno, 0);
Packit Service b302fa
Packit Service b302fa
  xpthread_barrier_destroy (barrier);
Packit Service b302fa
  support_shared_free (barrier);
Packit Service b302fa
  free (path);
Packit Service b302fa
  return 0;
Packit Service b302fa
}
Packit Service b302fa
Packit Service b302fa
#include <support/test-driver.c>