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

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