Skip to contentSkip to conversation

New command to check NT's hibernation state

DISCUSSION Peter Lustig 2 participants 3 messages Dec 10, 2011 – Dec 16, 2011 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 31 lines · Vladimir Serbinenko
Quoted 31 lines

/* nthibr.c - tests whether an MS Windows system partition is hibernated */ /*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2011 Free Software Foundation, Inc. *

   *  GRUB is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  GRUB is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.

*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see<http://www.gnu.org/licenses/>. */

#include<grub/types.h>
#include<grub/mm.h>
#include<grub/disk.h>
#include<grub/file.h>
#include<grub/misc.h>
#include<grub/dl.h>
#include<grub/extcmd.h>
#include<grub/lib/arg.h>
#include<grub/err.h>
#include<grub/i18n.h>

Quoted 8 lines
Quoted 7 lines

GRUB_MOD_LICENSE("GPLv3+");

/* Define this 'empty' array to let the '-h' and '-u' switches be processed */

static const struct grub_arg_option options[] = {
    {0, 0, 0, 0, 0, 0}
};

No need for empty array. passing 0 instead of this array works.

I have tried it both ways. For some reason, passing the empty array works, but passing a null pointer doesn't. That is, when I try the latter, the options '-u' and '-h' are treated as regular arguments to the command.

Quoted 108 lines
Quoted 28 lines

static grub_err_t
grub_cmd_nthibr (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc, char **args)

{
    grub_err_t status = GRUB_ERR_NONE;
    char *partition_name, *hibr_file_path = 0, hibr_file_magic[5];
    grub_size_t name_length;
    grub_ssize_t magic_size;
    grub_disk_t partition;
    grub_file_t hibr_file = 0;

    /* Check argument count */
    if (!argc)
      {
        status = grub_error (GRUB_ERR_BAD_ARGUMENT,
                             N_("too few arguments specified"));
        goto exit;
      }
    else if (argc>   1)
      {
        status = grub_error (GRUB_ERR_BAD_ARGUMENT,
                             N_("too many arguments specified"));
        goto exit;
      }

    partition_name = args[0];
    name_length = grub_strlen (partition_name);
Quoted 8 lines
    /* Check if partition specifier 'looks right' */
    if (partition_name[0] != '(' || partition_name[name_length - 1] != ')')
      {
        status = grub_error (GRUB_ERR_BAD_FILENAME,
                             N_("invalid partition specifier"));
        goto exit;
      }

I would recommend adding '(' and ')' if none found rather than giving error.

Quoted 7 lines
    /* Check if partition actually exists */
    partition_name[name_length - 1] = '\0';
    partition = grub_disk_open (partition_name + 1);
    if (!partition)
      {
        status = grub_error (GRUB_ERR_UNKNOWN_DEVICE,
                             N_("partition not found"));

No need to run grub_error here. grub_disk_open already sets grub_errno and grub_errmsg. Just status = grub_errno is enough.

Quoted 8 lines
        goto exit;
      }
    else
      {
        grub_disk_close (partition);
        partition_name[name_length - 1] = ')';
      }

No need for 'else' clause (goto already takes care of it)

Quoted 6 lines
    /* Build path to 'hiberfil.sys' */
    hibr_file_path = grub_malloc (grub_strlen (partition_name)
                                  + grub_strlen ("/hiberfil.sys") + 1);
    if (!hibr_file_path)
      {
        status = grub_error (GRUB_ERR_OUT_OF_MEMORY,
Quoted 9 lines
                             N_("out of memory"));
        goto exit;
      }
    else
      {
        grub_strcpy (hibr_file_path, partition_name);
        grub_strcat (hibr_file_path, "/hiberfil.sys");
      }

Same here (no need for grub_error and else)

Quoted 6 lines
    /* Try to open 'hiberfil.sys' */
    hibr_file = grub_file_open (hibr_file_path);
    if (!hibr_file)
      {
        status = grub_error (GRUB_ERR_FILE_NOT_FOUND,
                             N_("'hiberfil.sys' not found"));

ditto

Quoted 10 lines
        goto exit;
      }

    /* Try to read magic number of 'hiberfil.sys' */
    magic_size = sizeof (hibr_file_magic) - 1;
    grub_memset (hibr_file_magic, 0, sizeof (hibr_file_magic));
    if (grub_file_read (hibr_file, hibr_file_magic, magic_size)<   magic_size)
      {
        status = grub_error (GRUB_ERR_BAD_FILE_TYPE,
                             N_("'hiberfil.sys' too small"));

you need to conditionalize call to grub_error. if (!grub_errno) ... since incomplete read may indicate an FS problem in which case you want the original error message to remain.

Quoted 12 lines
        goto exit;
      }

    /* Return SUCCESS if magic indicates file is active; else return FAILURE */
    if (!grub_strncasecmp ("hibr", hibr_file_magic, magic_size))
      grub_puts (N_("The system is hibernated."));
    else
      {
        grub_puts (N_("The system is NOT hibernated."));
        status = GRUB_ERR_TEST_FAILURE;
      }

We also do grub_error (GRUB_ERR_TEST_FAILURE, "false");

Quoted 4 lines

exit:

    /* Ensure unmanaged resources are cleaned up */
    if (hibr_file_path)
      grub_free (hibr_file_path);
Quoted 7 lines

grub_free already checks for not-NULL pointers.

Quoted 4 lines
    if (hibr_file)
      grub_file_close (hibr_file);

    return status;

You can eliminate status and the need of keeping one by using return grub_errno;

I was unaware 'grub_errno' was used so extensively. Thanks for the tip: it definitely makes the logic cleaner.

Quoted 2 lines
Quoted 1 line

}

I've followed all your recommendations (as far as possible). Here's the code again.

~Peter Lustig

/* nthibr.c - tests whether an MS Windows system partition is hibernated */ /*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2011 Free Software Foundation, Inc. *

 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/

#include <grub/types.h>
#include <grub/mm.h>
#include <grub/disk.h>
#include <grub/file.h>
#include <grub/misc.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/lib/arg.h>
#include <grub/err.h>
#include <grub/i18n.h>

GRUB_MOD_LICENSE("GPLv3+");

/* Define this 'empty' array to let the '-h' and '-u' switches be processed */ static const struct grub_arg_option options[] = {
{0, 0, 0, 0, 0, 0}
};

static grub_err_t
grub_cmd_nthibr (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc, char **args)
{
char *partition_name = 0, *hibr_file_path = 0, hibr_file_magic[5]; grub_size_t name_length;
grub_ssize_t magic_size;
grub_disk_t partition;
grub_file_t hibr_file = 0;

  /* Check argument count */
  if (!argc)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("too few arguments"));
  else if (argc > 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("too many arguments"));

  /* Make copy of partition specifier, so it can be modified (if needed) */
  name_length = grub_strlen (args[0]);
  partition_name = grub_zalloc (name_length + 3);
  if (!partition_name)
    goto exit;
  grub_strcpy (partition_name, args[0]);

  /* Ensure partition specifier start with a '(' */
  if (partition_name[0] != '(')
    {
      grub_memmove (partition_name + 1, partition_name, name_length++);
      partition_name[0] = '(';
    }

/* Ensure partition specifier ends with a ')' */ if (partition_name[name_length - 1] != ')')

    partition_name[(++name_length) - 1] = ')';
  /* Check if partition actually exists */
  partition_name[name_length - 1] = '\0';
  partition = grub_disk_open (partition_name + 1);
  if (!partition)
    goto exit;
  grub_disk_close (partition);
  partition_name[name_length - 1] = ')';

  /* Build path to 'hiberfil.sys' */
  hibr_file_path = grub_malloc ((grub_strlen (partition_name) 
                                 + grub_strlen ("/hiberfil.sys") + 1));
  if (!hibr_file_path)
    goto exit;
  grub_strcpy (hibr_file_path, partition_name);
  grub_strcat (hibr_file_path, "/hiberfil.sys");

  /* Try to open 'hiberfil.sys' */
  hibr_file = grub_file_open (hibr_file_path);
  if (!hibr_file)
    {
      if (grub_errno == GRUB_ERR_FILE_NOT_FOUND)
        grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("'hiberfil.sys' not found"));
      goto exit;
    }

  /* Try to read magic number of 'hiberfil.sys' */
  magic_size = sizeof (hibr_file_magic) - 1;
  grub_memset (hibr_file_magic, 0, sizeof (hibr_file_magic));
  if (grub_file_read (hibr_file, hibr_file_magic, magic_size) < magic_size)
    {
      if (!grub_errno)
        grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("'hiberfil.sys' too small"));
      goto exit;
    }

  /* Return SUCCESS if magic indicates file is active; else return FAILURE */
  if (!grub_strncasecmp ("hibr", hibr_file_magic, magic_size))
    grub_error (GRUB_ERR_NONE, "true");
  else 
    grub_error (GRUB_ERR_TEST_FAILURE, "false");

exit:
/* Ensure unmanaged resources are cleaned up */
if (partition_name)

    grub_free (partition_name);
  if (hibr_file_path)
    grub_free (hibr_file_path);
  if (hibr_file)
    grub_file_close (hibr_file);

return grub_errno;
}

static grub_extcmd_t cmd;

GRUB_MOD_INIT (nthibr)
{
cmd = grub_register_extcmd ("nthibr", grub_cmd_nthibr, 0,

                              N_("DEVICE"),
                              N_("Test whether an NT system partition "
                                 "is hibernated."), 
                              options);
}

GRUB_MOD_FINI (nthibr)
{
grub_unregister_extcmd (cmd);
}

#2 lore raw
Quoted 1 line · Peter Lustig

I've followed all your recommendations (as far as possible). Here's the code again.

The empty options are still there. Your arguments for having it aren't command-specific so it's a wrong to put such workarounds here. Also please user command and not extcmd since you have no arguments.

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#3 lore raw
In reply to Peter Lustig
Quoted 30 lines · Peter Lustig

/* nthibr.c - tests whether an MS Windows system partition is hibernated */ /*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2011 Free Software Foundation, Inc. *

  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  GRUB is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.

*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see<http://www.gnu.org/licenses/>. */

#include<grub/types.h>
#include<grub/mm.h>
#include<grub/disk.h>
#include<grub/file.h>
#include<grub/misc.h>
#include<grub/dl.h>
#include<grub/extcmd.h>
#include<grub/lib/arg.h>
#include<grub/err.h>
#include<grub/i18n.h>

Quoted 6 lines

GRUB_MOD_LICENSE("GPLv3+");

/* Define this 'empty' array to let the '-h' and '-u' switches be processed */

static const struct grub_arg_option options[] = {
   {0, 0, 0, 0, 0, 0}
};

Please remove this and use grub_command_t

Quoted 16 lines

static grub_err_t
grub_cmd_nthibr (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc, char **args)

{
   char *partition_name = 0, *hibr_file_path = 0, hibr_file_magic[5];
   grub_size_t name_length;
   grub_ssize_t magic_size;
   grub_disk_t partition;
   grub_file_t hibr_file = 0;

   /* Check argument count */
   if (!argc)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("too few arguments"));
   else if (argc>  1)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("too many arguments"));

Either ignore trailing argument or put both in one check != 1

Quoted 26 lines
   /* Make copy of partition specifier, so it can be modified (if needed) */
   name_length = grub_strlen (args[0]);
   partition_name = grub_zalloc (name_length + 3);
   if (!partition_name)
     goto exit;
   grub_strcpy (partition_name, args[0]);

   /* Ensure partition specifier start with a '(' */
   if (partition_name[0] != '(')
     {
       grub_memmove (partition_name + 1, partition_name, name_length++);
       partition_name[0] = '(';
     }
   /* Ensure partition specifier ends with a ')' */
   if (partition_name[name_length - 1] != ')')
     partition_name[(++name_length) - 1] = ')';

   /* Check if partition actually exists */
   partition_name[name_length - 1] = '\0';
   partition = grub_disk_open (partition_name + 1);
   if (!partition)
     goto exit;
   grub_disk_close (partition);
   partition_name[name_length - 1] = ')';
Quoted 8 lines
   /* Build path to 'hiberfil.sys' */
   hibr_file_path = grub_malloc ((grub_strlen (partition_name)
                                  + grub_strlen ("/hiberfil.sys") + 1));
   if (!hibr_file_path)
     goto exit;
   grub_strcpy (hibr_file_path, partition_name);
   grub_strcat (hibr_file_path, "/hiberfil.sys");

It would be more efficient if you just try to open file and see what error you get. Actually you don't even need to distinguish between error cases here since you return the error you encountered. This will also save one useless alloc.

Quoted 6 lines
   /* Try to open 'hiberfil.sys' */
   hibr_file = grub_file_open (hibr_file_path);
   if (!hibr_file)
     {
       if (grub_errno == GRUB_ERR_FILE_NOT_FOUND)
         grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("'hiberfil.sys' not found"));

This overriding is unnecessary. FS code already provides this message

Quoted 19 lines
       goto exit;
     }
   /* Try to read magic number of 'hiberfil.sys' */
   magic_size = sizeof (hibr_file_magic) - 1;
   grub_memset (hibr_file_magic, 0, sizeof (hibr_file_magic));
   if (grub_file_read (hibr_file, hibr_file_magic, magic_size)<  magic_size)
     {
       if (!grub_errno)
         grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("'hiberfil.sys' too small"));
       goto exit;
     }

   /* Return SUCCESS if magic indicates file is active; else return FAILURE */
   if (!grub_strncasecmp ("hibr", hibr_file_magic, magic_size))
     grub_error (GRUB_ERR_NONE, "true");
   else
     grub_error (GRUB_ERR_TEST_FAILURE, "false");
Quoted 6 lines

exit:

   /* Ensure unmanaged resources are cleaned up */
   if (partition_name)
     grub_free (partition_name);
   if (hibr_file_path)
     grub_free (hibr_file_path);

No need to check that argument to free is non-NULL.

Quoted 22 lines
   if (hibr_file)
     grub_file_close (hibr_file);
   return grub_errno;
}

static grub_extcmd_t cmd;

GRUB_MOD_INIT (nthibr)

{
   cmd = grub_register_extcmd ("nthibr", grub_cmd_nthibr, 0,
                               N_("DEVICE"),
                               N_("Test whether an NT system partition "
                                  "is hibernated."),
                               options);
}

GRUB_MOD_FINI (nthibr)

{
   grub_unregister_extcmd (cmd);
}
Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko