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

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