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