Blame elf/tst-ldconfig-bad-aux-cache.c

Packit Service 276103
/* Test ldconfig does not segfault when aux-cache is corrupted (Bug 18093).
Packit Service 276103
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service 276103
   This file is part of the GNU C Library.
Packit Service 276103
Packit Service 276103
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 276103
   modify it under the terms of the GNU Lesser General Public License as
Packit Service 276103
   published by the Free Software Foundation; either version 2.1 of the
Packit Service 276103
   License, or (at your option) any later version.
Packit Service 276103
Packit Service 276103
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 276103
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 276103
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 276103
   Lesser General Public License for more details.
Packit Service 276103
Packit Service 276103
   You should have received a copy of the GNU Lesser General Public
Packit Service 276103
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit Service 276103
   not, see <http://www.gnu.org/licenses/>.  */
Packit Service 276103
Packit Service 276103
/* This test does the following:
Packit Service 276103
   Run ldconfig to create the caches.
Packit Service 276103
   Corrupt the caches.
Packit Service 276103
   Run ldconfig again.
Packit Service 276103
   At each step we verify that ldconfig does not crash.  */
Packit Service 276103
Packit Service 276103
#include <stdio.h>
Packit Service 276103
#include <stdlib.h>
Packit Service 276103
#include <string.h>
Packit Service 276103
#include <unistd.h>
Packit Service 276103
#include <errno.h>
Packit Service 276103
#include <sys/wait.h>
Packit Service 276103
#include <ftw.h>
Packit Service 276103
#include <stdint.h>
Packit Service 276103
Packit Service be2034
#include <support/capture_subprocess.h>
Packit Service 276103
#include <support/check.h>
Packit Service 276103
#include <support/support.h>
Packit Service 276103
#include <support/xunistd.h>
Packit Service 276103
Packit Service 276103
#include <dirent.h>
Packit Service 276103
Packit Service 276103
static int
Packit Service 276103
display_info (const char *fpath, const struct stat *sb,
Packit Service 276103
              int tflag, struct FTW *ftwbuf)
Packit Service 276103
{
Packit Service 276103
  printf ("info: %-3s %2d %7jd   %-40s %d %s\n",
Packit Service 276103
          (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
Packit Service 276103
          (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
Packit Service 276103
          (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
Packit Service 276103
          (tflag == FTW_SLN) ? "sln" : "???",
Packit Service 276103
          ftwbuf->level, (intmax_t) sb->st_size,
Packit Service 276103
          fpath, ftwbuf->base, fpath + ftwbuf->base);
Packit Service 276103
  /* To tell nftw to continue.  */
Packit Service 276103
  return 0;
Packit Service 276103
}
Packit Service 276103
Packit Service be2034
static void
Packit Service be2034
execv_wrapper (void *args)
Packit Service be2034
{
Packit Service be2034
  char **argv = args;
Packit Service be2034
Packit Service be2034
  execv (argv[0], argv);
Packit Service be2034
  FAIL_EXIT1 ("execv: %m");
Packit Service be2034
}
Packit Service be2034
Packit Service 276103
/* Run ldconfig with a corrupt aux-cache, in particular we test for size
Packit Service 276103
   truncation that might happen if a previous ldconfig run failed or if
Packit Service 276103
   there were storage or power issues while we were writing the file.
Packit Service 276103
   We want ldconfig not to crash, and it should be able to do so by
Packit Service 276103
   computing the expected size of the file (bug 18093).  */
Packit Service 276103
static int
Packit Service 276103
do_test (void)
Packit Service 276103
{
Packit Service 276103
  char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
Packit Service be2034
  char *args[] = { prog, NULL };
Packit Service 276103
  const char *path = "/var/cache/ldconfig/aux-cache";
Packit Service 276103
  struct stat64 fs;
Packit Service 276103
  long int size, new_size, i;
Packit Service 276103
Packit Service 276103
  /* Create the needed directories. */
Packit Service 276103
  xmkdirp ("/var/cache/ldconfig", 0777);
Packit Service 276103
Packit Service be2034
  /* Run ldconfig first to generate the aux-cache.  */
Packit Service be2034
  struct support_capture_subprocess result;
Packit Service be2034
  result = support_capture_subprocess (execv_wrapper, args);
Packit Service be2034
  support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
Packit Service be2034
  support_capture_subprocess_free (&result);
Packit Service be2034
Packit Service be2034
  xstat (path, &fs);
Packit Service be2034
Packit Service be2034
  size = fs.st_size;
Packit Service be2034
  /* Run 3 tests, each truncating aux-cache shorter and shorter.  */
Packit Service be2034
  for (i = 3; i > 0; i--)
Packit Service 276103
    {
Packit Service be2034
      new_size = size * i / 4;
Packit Service be2034
      if (truncate (path, new_size))
Packit Service be2034
        FAIL_EXIT1 ("truncation failed: %m");
Packit Service be2034
      if (nftw (path, display_info, 1000, 0) == -1)
Packit Service be2034
        FAIL_EXIT1 ("nftw failed.");
Packit Service be2034
Packit Service be2034
      /* Verify that ldconfig can run with a truncated
Packit Service be2034
         aux-cache and doesn't crash.  */
Packit Service be2034
      struct support_capture_subprocess result;
Packit Service be2034
      result = support_capture_subprocess (execv_wrapper, args);
Packit Service be2034
      support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
Packit Service be2034
      support_capture_subprocess_free (&result);
Packit Service 276103
    }
Packit Service 276103
Packit Service 276103
  free (prog);
Packit Service 276103
  return 0;
Packit Service 276103
}
Packit Service 276103
Packit Service 276103
#include <support/test-driver.c>