Blame elf/tst-pldd.c

Packit Bot 63fc95
/* Basic tests for pldd program.
Packit Bot 63fc95
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Bot 63fc95
   This file is part of the GNU C Library.
Packit Bot 63fc95
Packit Bot 63fc95
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot 63fc95
   modify it under the terms of the GNU Lesser General Public
Packit Bot 63fc95
   License as published by the Free Software Foundation; either
Packit Bot 63fc95
   version 2.1 of the License, or (at your option) any later version.
Packit Bot 63fc95
Packit Bot 63fc95
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot 63fc95
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 63fc95
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot 63fc95
   Lesser General Public License for more details.
Packit Bot 63fc95
Packit Bot 63fc95
   You should have received a copy of the GNU Lesser General Public
Packit Bot 63fc95
   License along with the GNU C Library; if not, see
Packit Bot 63fc95
   <http://www.gnu.org/licenses/>.  */
Packit Bot 63fc95
Packit Bot 63fc95
#include <stdio.h>
Packit Bot 63fc95
#include <string.h>
Packit Bot 63fc95
#include <unistd.h>
Packit Bot 63fc95
#include <stdint.h>
Packit Bot 63fc95
#include <libgen.h>
Packit Bot 63fc95
#include <stdbool.h>
Packit Bot 63fc95
Packit Bot 63fc95
#include <array_length.h>
Packit Bot 63fc95
#include <gnu/lib-names.h>
Packit Bot 63fc95
Packit Bot 63fc95
#include <support/subprocess.h>
Packit Bot 63fc95
#include <support/capture_subprocess.h>
Packit Bot 63fc95
#include <support/check.h>
Packit Bot 63fc95
Packit Bot 63fc95
static void
Packit Bot 63fc95
target_process (void *arg)
Packit Bot 63fc95
{
Packit Bot 63fc95
  pause ();
Packit Bot 63fc95
}
Packit Bot 63fc95
Packit Bot 63fc95
/* The test runs in a container because pldd does not support tracing
Packit Bot 63fc95
   a binary started by the loader iself (as with testrun.sh).  */
Packit Bot 63fc95
Packit Bot 63fc95
static int
Packit Bot 63fc95
do_test (void)
Packit Bot 63fc95
{
Packit Bot 63fc95
  /* Create a copy of current test to check with pldd.  */
Packit Bot 63fc95
  struct support_subprocess target = support_subprocess (target_process, NULL);
Packit Bot 63fc95
Packit Bot 63fc95
  /* Run 'pldd' on test subprocess.  */
Packit Bot 63fc95
  struct support_capture_subprocess pldd;
Packit Bot 63fc95
  {
Packit Bot 63fc95
    /* Three digits per byte plus null terminator.  */
Packit Bot 63fc95
    char pid[3 * sizeof (uint32_t) + 1];
Packit Bot 63fc95
    snprintf (pid, array_length (pid), "%d", target.pid);
Packit Bot 63fc95
Packit Bot 63fc95
    const char prog[] = "/usr/bin/pldd";
Packit Bot 63fc95
Packit Bot 63fc95
    pldd = support_capture_subprogram (prog,
Packit Bot 63fc95
      (char *const []) { (char *) prog, pid, NULL });
Packit Bot 63fc95
Packit Bot 63fc95
    support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
Packit Bot 63fc95
  }
Packit Bot 63fc95
Packit Bot 63fc95
  /* Check 'pldd' output.  The test is expected to be linked against only
Packit Bot 63fc95
     loader and libc.  */
Packit Bot 63fc95
  {
Packit Bot 63fc95
    pid_t pid;
Packit Bot 63fc95
    char buffer[512];
Packit Bot 63fc95
#define STRINPUT(size) "%" # size "s"
Packit Bot 63fc95
Packit Bot 63fc95
    FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
Packit Bot 63fc95
    TEST_VERIFY (out != NULL);
Packit Bot 63fc95
Packit Bot 63fc95
    /* First line is in the form of <pid>: <full path of executable>  */
Packit Bot 63fc95
    TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
Packit Bot 63fc95
Packit Bot 63fc95
    TEST_COMPARE (pid, target.pid);
Packit Bot 63fc95
    TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
Packit Bot 63fc95
Packit Bot 63fc95
    /* It expects only one loader and libc loaded by the program.  */
Packit Bot 63fc95
    bool interpreter_found = false, libc_found = false;
Packit Bot 63fc95
    while (fgets (buffer, array_length (buffer), out) != NULL)
Packit Bot 63fc95
      {
Packit Bot 63fc95
       /* Ignore vDSO.  */
Packit Bot 63fc95
        if (buffer[0] != '/')
Packit Bot 63fc95
         continue;
Packit Bot 63fc95
Packit Bot 63fc95
       /* Remove newline so baseline (buffer) can compare against the
Packit Bot 63fc95
          LD_SO and LIBC_SO macros unmodified.  */
Packit Bot 63fc95
       if (buffer[strlen(buffer)-1] == '\n')
Packit Bot 63fc95
         buffer[strlen(buffer)-1] = '\0';
Packit Bot 63fc95
Packit Bot 63fc95
       if (strcmp (basename (buffer), LD_SO) == 0)
Packit Bot 63fc95
         {
Packit Bot 63fc95
           TEST_COMPARE (interpreter_found, false);
Packit Bot 63fc95
           interpreter_found = true;
Packit Bot 63fc95
           continue;
Packit Bot 63fc95
         }
Packit Bot 63fc95
Packit Bot 63fc95
       if (strcmp (basename (buffer), LIBC_SO) == 0)
Packit Bot 63fc95
         {
Packit Bot 63fc95
           TEST_COMPARE (libc_found, false);
Packit Bot 63fc95
           libc_found = true;
Packit Bot 63fc95
           continue;
Packit Bot 63fc95
         }
Packit Bot 63fc95
      }
Packit Bot 63fc95
    TEST_COMPARE (interpreter_found, true);
Packit Bot 63fc95
    TEST_COMPARE (libc_found, true);
Packit Bot 63fc95
Packit Bot 63fc95
    fclose (out);
Packit Bot 63fc95
  }
Packit Bot 63fc95
Packit Bot 63fc95
  support_capture_subprocess_free (&pldd);
Packit Bot 63fc95
  support_process_terminate (&target);
Packit Bot 63fc95
Packit Bot 63fc95
  return 0;
Packit Bot 63fc95
}
Packit Bot 63fc95
Packit Bot 63fc95
#include <support/test-driver.c>