Blame SOURCES/0156-Use-Distribution-Package-Sort-for-grub2-mkconfig-112.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Robert Marshall <rmarshall@redhat.com>
f731ee
Date: Mon, 16 Mar 2015 14:14:19 -0400
f731ee
Subject: [PATCH] Use Distribution Package Sort for grub2-mkconfig (#1124074)
f731ee
f731ee
Users reported that newly installed kernels on their systems installed
f731ee
with grub-mkconfig would not appear on the grub boot list in order
f731ee
starting with the most recent. Added an option for rpm-based systems to
f731ee
use the rpm-sort library to sort kernels instead.
f731ee
f731ee
Resolves rhbz#1124074
f731ee
---
f731ee
 configure.ac              |  29 +++++
f731ee
 Makefile.util.def         |  16 +++
f731ee
 util/grub-rpm-sort.c      | 281 ++++++++++++++++++++++++++++++++++++++++++++++
f731ee
 .gitignore                |   1 +
f731ee
 util/grub-mkconfig_lib.in |   8 +-
f731ee
 util/grub-rpm-sort.8      |  12 ++
f731ee
 6 files changed, 346 insertions(+), 1 deletion(-)
f731ee
 create mode 100644 util/grub-rpm-sort.c
f731ee
 create mode 100644 util/grub-rpm-sort.8
f731ee
f731ee
diff --git a/configure.ac b/configure.ac
f731ee
index 048ce7c89fa..de17e65c4b7 100644
f731ee
--- a/configure.ac
f731ee
+++ b/configure.ac
f731ee
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
f731ee
 grub_TRANSFORM([grub-mkrescue])
f731ee
 grub_TRANSFORM([grub-probe])
f731ee
 grub_TRANSFORM([grub-reboot])
f731ee
+grub_TRANSFORM([grub-rpm-sort])
f731ee
 grub_TRANSFORM([grub-script-check])
f731ee
 grub_TRANSFORM([grub-set-default])
f731ee
 grub_TRANSFORM([grub-sparc64-setup])
f731ee
@@ -89,6 +90,7 @@ grub_TRANSFORM([grub-mkrescue.1])
f731ee
 grub_TRANSFORM([grub-mkstandalone.3])
f731ee
 grub_TRANSFORM([grub-ofpathname.3])
f731ee
 grub_TRANSFORM([grub-probe.3])
f731ee
+grub_TRANSFORM([grub-rpm-sort.8])
f731ee
 grub_TRANSFORM([grub-reboot.3])
f731ee
 grub_TRANSFORM([grub-render-label.3])
f731ee
 grub_TRANSFORM([grub-script-check.3])
f731ee
@@ -1543,6 +1545,33 @@ fi
f731ee
 
f731ee
 AC_SUBST([LIBDEVMAPPER])
f731ee
 
f731ee
+AC_ARG_ENABLE([rpm-sort],
f731ee
+              [AS_HELP_STRING([--enable-rpm-sort],
f731ee
+                              [enable native rpm sorting of kernels in grub (default=guessed)])])
f731ee
+if test x"$enable_rpm-sort" = xno ; then
f731ee
+  rpm_sort_excuse="explicitly disabled"
f731ee
+fi
f731ee
+
f731ee
+if test x"$rpm_sort_excuse" = x ; then
f731ee
+  # Check for rpmlib header.
f731ee
+  AC_CHECK_HEADER([rpm/rpmlib.h], [],
f731ee
+               [rpm_sort_excuse="need rpm/rpmlib header"])
f731ee
+fi
f731ee
+
f731ee
+if test x"$rpm_sort_excuse" = x ; then
f731ee
+  # Check for rpm library.
f731ee
+  AC_CHECK_LIB([rpm], [rpmvercmp], [],
f731ee
+               [rpm_sort_excuse="rpmlib missing rpmvercmp"])
f731ee
+fi
f731ee
+
f731ee
+if test x"$rpm_sort_excuse" = x ; then
f731ee
+   LIBRPM="-lrpm";
f731ee
+   AC_DEFINE([HAVE_RPM], [1],
f731ee
+             [Define to 1 if you have the rpm library.])
f731ee
+fi
f731ee
+
f731ee
+AC_SUBST([LIBRPM])
f731ee
+
f731ee
 LIBGEOM=
f731ee
 if test x$host_kernel = xkfreebsd; then
f731ee
   AC_CHECK_LIB([geom], [geom_gettree], [],
f731ee
diff --git a/Makefile.util.def b/Makefile.util.def
f731ee
index 87029a10dfb..3ac75720466 100644
f731ee
--- a/Makefile.util.def
f731ee
+++ b/Makefile.util.def
f731ee
@@ -675,6 +675,22 @@ program = {
f731ee
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
f731ee
 };
f731ee
 
f731ee
+program = {
f731ee
+  name = grub-rpm-sort;
f731ee
+  mansection = 8;
f731ee
+  installdir = sbin;
f731ee
+
f731ee
+  common = grub-core/kern/emu/misc.c;
f731ee
+  common = grub-core/kern/emu/argp_common.c;
f731ee
+  common = grub-core/osdep/init.c;
f731ee
+  common = util/misc.c;
f731ee
+  common = util/grub-rpm-sort.c;
f731ee
+
f731ee
+  ldadd = grub-core/gnulib/libgnu.a;
f731ee
+  ldadd = libgrubkern.a;
f731ee
+  ldadd = '$(LIBDEVMAPPER) $(LIBRPM)';
f731ee
+};
f731ee
+
f731ee
 script = {
f731ee
   name = grub-mkconfig;
f731ee
   common = util/grub-mkconfig.in;
f731ee
diff --git a/util/grub-rpm-sort.c b/util/grub-rpm-sort.c
f731ee
new file mode 100644
f731ee
index 00000000000..f33bd1ed568
f731ee
--- /dev/null
f731ee
+++ b/util/grub-rpm-sort.c
f731ee
@@ -0,0 +1,281 @@
f731ee
+#include <config.h>
f731ee
+#include <grub/types.h>
f731ee
+#include <grub/util/misc.h>
f731ee
+#include <stdio.h>
f731ee
+#include <stdlib.h>
f731ee
+#include <unistd.h>
f731ee
+#include <errno.h>
f731ee
+#include <assert.h>
f731ee
+#include <argp.h>
f731ee
+#include <rpm/rpmlib.h>
f731ee
+
f731ee
+static size_t
f731ee
+read_file (const char *input, char **ret)
f731ee
+{
f731ee
+  FILE *in;
f731ee
+  size_t s;
f731ee
+  size_t sz = 2048;
f731ee
+  size_t offset = 0;
f731ee
+  char *text;
f731ee
+
f731ee
+  if (!strcmp(input, "-"))
f731ee
+    in = stdin;
f731ee
+  else
f731ee
+    in = grub_util_fopen(input, "r");
f731ee
+
f731ee
+  text = xmalloc (sz);
f731ee
+
f731ee
+  if (!in)
f731ee
+    grub_util_error (_("cannot open `%s': %s"), input, strerror (errno));
f731ee
+
f731ee
+  while ((s = fread (text + offset, 1, sz - offset, in)) != 0)
f731ee
+    {
f731ee
+      offset += s;
f731ee
+      if (sz - offset == 0)
f731ee
+	{
f731ee
+	  sz += 2048;
f731ee
+	  text = xrealloc (text, sz);
f731ee
+	}
f731ee
+    }
f731ee
+
f731ee
+  text[offset] = '\0';
f731ee
+  *ret = text;
f731ee
+
f731ee
+  if (in != stdin)
f731ee
+    fclose(in);
f731ee
+
f731ee
+  return offset + 1;
f731ee
+}
f731ee
+
f731ee
+/* returns name/version/release */
f731ee
+/* NULL string pointer returned if nothing found */
f731ee
+static void
f731ee
+split_package_string (char *package_string, char **name,
f731ee
+                     char **version, char **release)
f731ee
+{
f731ee
+  char *package_version, *package_release;
f731ee
+
f731ee
+  /* Release */
f731ee
+  package_release = strrchr (package_string, '-');
f731ee
+
f731ee
+  if (package_release != NULL)
f731ee
+      *package_release++ = '\0';
f731ee
+
f731ee
+  *release = package_release;
f731ee
+
f731ee
+  /* Version */
f731ee
+  package_version = strrchr(package_string, '-');
f731ee
+
f731ee
+  if (package_version != NULL)
f731ee
+      *package_version++ = '\0';
f731ee
+
f731ee
+  *version = package_version;
f731ee
+  /* Name */
f731ee
+  *name = package_string;
f731ee
+
f731ee
+  /* Bubble up non-null values from release to name */
f731ee
+  if (*name == NULL)
f731ee
+    {
f731ee
+      *name = (*version == NULL ? *release : *version);
f731ee
+      *version = *release;
f731ee
+      *release = NULL;
f731ee
+    }
f731ee
+  if (*version == NULL)
f731ee
+    {
f731ee
+      *version = *release;
f731ee
+      *release = NULL;
f731ee
+    }
f731ee
+}
f731ee
+
f731ee
+/*
f731ee
+ * package name-version-release comparator for qsort
f731ee
+ * expects p, q which are pointers to character strings (char *)
f731ee
+ * which will not be altered in this function
f731ee
+ */
f731ee
+static int
f731ee
+package_version_compare (const void *p, const void *q)
f731ee
+{
f731ee
+  char *local_p, *local_q;
f731ee
+  char *lhs_name, *lhs_version, *lhs_release;
f731ee
+  char *rhs_name, *rhs_version, *rhs_release;
f731ee
+  int vercmpflag = 0;
f731ee
+
f731ee
+  local_p = alloca (strlen (*(char * const *)p) + 1);
f731ee
+  local_q = alloca (strlen (*(char * const *)q) + 1);
f731ee
+
f731ee
+  /* make sure these allocated */
f731ee
+  assert (local_p);
f731ee
+  assert (local_q);
f731ee
+
f731ee
+  strcpy (local_p, *(char * const *)p);
f731ee
+  strcpy (local_q, *(char * const *)q);
f731ee
+
f731ee
+  split_package_string (local_p, &lhs_name, &lhs_version, &lhs_release);
f731ee
+  split_package_string (local_q, &rhs_name, &rhs_version, &rhs_release);
f731ee
+
f731ee
+  /* Check Name and return if unequal */
f731ee
+  vercmpflag = rpmvercmp ((lhs_name == NULL ? "" : lhs_name),
f731ee
+                          (rhs_name == NULL ? "" : rhs_name));
f731ee
+  if (vercmpflag != 0)
f731ee
+    return vercmpflag;
f731ee
+
f731ee
+  /* Check version and return if unequal */
f731ee
+  vercmpflag = rpmvercmp ((lhs_version == NULL ? "" : lhs_version),
f731ee
+                          (rhs_version == NULL ? "" : rhs_version));
f731ee
+  if (vercmpflag != 0)
f731ee
+    return vercmpflag;
f731ee
+
f731ee
+  /* Check release and return the version compare value */
f731ee
+  vercmpflag = rpmvercmp ((lhs_release == NULL ? "" : lhs_release),
f731ee
+                          (rhs_release == NULL ? "" : rhs_release));
f731ee
+
f731ee
+  return vercmpflag;
f731ee
+}
f731ee
+
f731ee
+static void
f731ee
+add_input (const char *filename, char ***package_names, size_t *n_package_names)
f731ee
+{
f731ee
+  char *orig_input_buffer = NULL;
f731ee
+  char *input_buffer;
f731ee
+  char *position_of_newline;
f731ee
+  char **names = *package_names;
f731ee
+  char **new_names = NULL;
f731ee
+  size_t n_names = *n_package_names;
f731ee
+
f731ee
+  if (!*package_names)
f731ee
+    new_names = names = xmalloc (sizeof (char *) * 2);
f731ee
+
f731ee
+  if (read_file (filename, &orig_input_buffer) < 2)
f731ee
+    {
f731ee
+      if (new_names)
f731ee
+	free (new_names);
f731ee
+      if (orig_input_buffer)
f731ee
+	free (orig_input_buffer);
f731ee
+      return;
f731ee
+    }
f731ee
+
f731ee
+  input_buffer = orig_input_buffer;
f731ee
+  while (input_buffer && *input_buffer &&
f731ee
+	 (position_of_newline = strchrnul (input_buffer, '\n')))
f731ee
+    {
f731ee
+      size_t sz = position_of_newline - input_buffer;
f731ee
+      char *new;
f731ee
+
f731ee
+      if (sz == 0)
f731ee
+	{
f731ee
+	  input_buffer = position_of_newline + 1;
f731ee
+	  continue;
f731ee
+	}
f731ee
+
f731ee
+      new = xmalloc (sz+1);
f731ee
+      strncpy (new, input_buffer, sz);
f731ee
+      new[sz] = '\0';
f731ee
+
f731ee
+      names = xrealloc (names, sizeof (char *) * (n_names + 1));
f731ee
+      names[n_names] = new;
f731ee
+      n_names++;
f731ee
+
f731ee
+      /* move buffer ahead to next line */
f731ee
+      input_buffer = position_of_newline + 1;
f731ee
+      if (*position_of_newline == '\0')
f731ee
+	input_buffer = NULL;
f731ee
+    }
f731ee
+
f731ee
+  free (orig_input_buffer);
f731ee
+
f731ee
+  *package_names = names;
f731ee
+  *n_package_names = n_names;
f731ee
+}
f731ee
+
f731ee
+static char *
f731ee
+help_filter (int key, const char *text, void *input __attribute__ ((unused)))
f731ee
+{
f731ee
+  return (char *)text;
f731ee
+}
f731ee
+
f731ee
+static struct argp_option options[] = {
f731ee
+  { 0, }
f731ee
+};
f731ee
+
f731ee
+struct arguments
f731ee
+{
f731ee
+  size_t ninputs;
f731ee
+  size_t input_max;
f731ee
+  char **inputs;
f731ee
+};
f731ee
+
f731ee
+static error_t
f731ee
+argp_parser (int key, char *arg, struct argp_state *state)
f731ee
+{
f731ee
+  struct arguments *arguments = state->input;
f731ee
+  switch (key)
f731ee
+    {
f731ee
+    case ARGP_KEY_ARG:
f731ee
+      assert (arguments->ninputs < arguments->input_max);
f731ee
+      arguments->inputs[arguments->ninputs++] = xstrdup (arg);
f731ee
+      break;
f731ee
+    default:
f731ee
+      return ARGP_ERR_UNKNOWN;
f731ee
+    }
f731ee
+  return 0;
f731ee
+}
f731ee
+
f731ee
+static struct argp argp = {
f731ee
+  options, argp_parser, N_("[INPUT_FILES]"),
f731ee
+  N_("Sort a list of strings in RPM version sort order."),
f731ee
+  NULL, help_filter, NULL
f731ee
+};
f731ee
+
f731ee
+int
f731ee
+main (int argc, char *argv[])
f731ee
+{
f731ee
+  struct arguments arguments;
f731ee
+  char **package_names = NULL;
f731ee
+  size_t n_package_names = 0;
f731ee
+  int i;
f731ee
+
f731ee
+  grub_util_host_init (&argc, &argv);
f731ee
+
f731ee
+  memset (&arguments, 0, sizeof (struct arguments));
f731ee
+  arguments.input_max = argc+1;
f731ee
+  arguments.inputs = xmalloc ((arguments.input_max + 1)
f731ee
+			      * sizeof (arguments.inputs[0]));
f731ee
+  memset (arguments.inputs, 0, (arguments.input_max + 1)
f731ee
+	  * sizeof (arguments.inputs[0]));
f731ee
+
f731ee
+  /* Parse our arguments */
f731ee
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
f731ee
+    grub_util_error ("%s", _("Error in parsing command line arguments\n"));
f731ee
+
f731ee
+  /* If there's no inputs in argv, add one for stdin */
f731ee
+  if (!arguments.ninputs)
f731ee
+    {
f731ee
+      arguments.ninputs = 1;
f731ee
+      arguments.inputs[0] = xmalloc (2);
f731ee
+      strcpy(arguments.inputs[0], "-");
f731ee
+    }
f731ee
+
f731ee
+  for (i = 0; i < arguments.ninputs; i++)
f731ee
+    add_input(arguments.inputs[i], &package_names, &n_package_names);
f731ee
+
f731ee
+  if (package_names == NULL || n_package_names < 1)
f731ee
+    grub_util_error ("%s", _("Invalid input\n"));
f731ee
+
f731ee
+  qsort (package_names, n_package_names, sizeof (char *),
f731ee
+	 package_version_compare);
f731ee
+
f731ee
+  /* send sorted list to stdout */
f731ee
+  for (i = 0; i < n_package_names; i++)
f731ee
+    {
f731ee
+      fprintf (stdout, "%s\n", package_names[i]);
f731ee
+      free (package_names[i]);
f731ee
+    }
f731ee
+
f731ee
+  free (package_names);
f731ee
+  for (i = 0; i < arguments.ninputs; i++)
f731ee
+    free (arguments.inputs[i]);
f731ee
+
f731ee
+  free (arguments.inputs);
f731ee
+
f731ee
+  return 0;
f731ee
+}
f731ee
diff --git a/.gitignore b/.gitignore
f731ee
index 18ab8e81278..b6c45776a4d 100644
f731ee
--- a/.gitignore
f731ee
+++ b/.gitignore
f731ee
@@ -237,6 +237,7 @@ po/POTFILES.in
f731ee
 po/POTFILES-shell.in
f731ee
 /grub-glue-efi
f731ee
 /grub-render-label
f731ee
+grub-rpm-sort
f731ee
 /grub-glue-efi.exe
f731ee
 /grub-render-label.exe
f731ee
 grub-core/gnulib/locale.h
f731ee
diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
f731ee
index 5c9ed84a5e7..e85b60c322a 100644
f731ee
--- a/util/grub-mkconfig_lib.in
f731ee
+++ b/util/grub-mkconfig_lib.in
f731ee
@@ -213,6 +213,12 @@ version_sort ()
f731ee
    esac
f731ee
 }
f731ee
 
f731ee
+if [ "x$RPMLIB" = x ]; then
f731ee
+  kernel_sort=version_sort
f731ee
+else
f731ee
+  kernel_sort="${sbindir}/grub-rpm-sort"
f731ee
+fi
f731ee
+
f731ee
 version_test_numeric ()
f731ee
 {
f731ee
   version_test_numeric_a="$1"
f731ee
@@ -229,7 +235,7 @@ version_test_numeric ()
f731ee
     version_test_numeric_a="$version_test_numeric_b"
f731ee
     version_test_numeric_b="$version_test_numeric_c"
f731ee
   fi
f731ee
-  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
f731ee
+  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | "$kernel_sort" | head -n 1 | grep -qx "$version_test_numeric_b" ; then
f731ee
     return 0
f731ee
   else
f731ee
     return 1
f731ee
diff --git a/util/grub-rpm-sort.8 b/util/grub-rpm-sort.8
f731ee
new file mode 100644
f731ee
index 00000000000..8ce21488448
f731ee
--- /dev/null
f731ee
+++ b/util/grub-rpm-sort.8
f731ee
@@ -0,0 +1,12 @@
f731ee
+.TH GRUB-RPM-SORT 8 "Wed Feb 26 2014"
f731ee
+.SH NAME
f731ee
+\fBgrub-rpm-sort\fR \(em Sort input according to RPM version compare.
f731ee
+
f731ee
+.SH SYNOPSIS
f731ee
+\fBgrub-rpm-sort\fR [OPTIONS].
f731ee
+
f731ee
+.SH DESCRIPTION
f731ee
+You should not normally run this program directly.  Use grub-mkconfig instead.
f731ee
+
f731ee
+.SH SEE ALSO
f731ee
+.BR "info grub"