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

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