Received: from list by lists.gnu.org with archive (Exim 4.71) id 1W1NXg-0005M2-0A for mharc-grub-devel@gnu.org; Thu, 09 Jan 2014 16:53:32 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44958) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W1NXZ-0005HX-Es for grub-devel@gnu.org; Thu, 09 Jan 2014 16:53:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W1NXS-0004C3-IS for grub-devel@gnu.org; Thu, 09 Jan 2014 16:53:25 -0500 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::10]:27198) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W1NXS-0004Bq-7w for grub-devel@gnu.org; Thu, 09 Jan 2014 16:53:18 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; t=1389304397; l=6888; s=domk; d=bubecks.de; h=Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH; bh=8H0uS0+uBZdgjq8REZ/bRegfG2I=; b=smv1J1XDqF+rLX/Yl36Z9ifwFb3Q97pHRyD8JxOdjjlsAlsjYxO80tH8WvwLRAG1Mgu 5Ti2Z5Y5kP+ZRu86EMByYoRC1sLZSkzjAt8Cur70/YWTjip9bcvJvw5nyZsgeYVEnhFTt l15LpSgtKvsd9vi8MD3mUO6rquUHFqwXg4Q= X-RZG-AUTH: :OGUIeEGmdd9LocaRWNUrTCqIQctGnGgg+eszxi8Zzh68aZhci43fYAKnuM1wed0CSQ== X-RZG-CLASS-ID: mo00 Received: from brain.bubecks.de (p5B2CECB3.dip0.t-ipconnect.de [91.44.236.179]) by smtp.strato.de (RZmta 32.17 DYNA|AUTH) with (TLSv1:DHE-RSA-AES256-SHA encrypted) ESMTPSA id C03d59q09L7xxa1 ; Thu, 9 Jan 2014 22:07:59 +0100 (CET) Received: from thorin.localdomain (fw.wid.reinform.de [10.2.1.254]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by brain.bubecks.de (Postfix) with ESMTPSA id 001D6884EE; Thu, 9 Jan 2014 22:07:59 +0100 (CET) Received: by thorin.localdomain (Postfix, from userid 5033) id E649DC4787; Thu, 9 Jan 2014 22:07:58 +0100 (CET) From: "Dr. Tilmann Bubeck" To: grub-devel@gnu.org Subject: [PATCH] Refactor grub_read_mountinfo() out of grub_find_root_devices_from_mountinfo() Date: Thu, 9 Jan 2014 22:07:48 +0100 Message-Id: <1389301668-8283-1-git-send-email-tilmann@bubecks.de> X-Mailer: git-send-email 1.8.4.2 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:238:20a:202:5300::10 Cc: "Dr. Tilmann Bubeck" X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jan 2014 21:53:30 -0000 The existing function grub_find_root_devices_from_mountinfo() reads /proc/self/mountinfo AND searches for a given device. This patch moves the code to read /proc/self/mountinfo out of this function into a new function called grub_read_mountinfo(). This new function can then be used to read /proc/self/mountinfo and do the filtering on its own. The existing function grub_find_root_devices_from_mountinfo() is changed, so that it now uses the new function grub_read_mountinfo() to do its work. This reduces the code to basically filtering for the wanted device. This refactoring is necessary for ext2 embedding to work but it is a atomic change on its own and therefore put into a seperate changeset. Signed-off-by: Dr. Tilmann Bubeck --- grub-core/osdep/linux/getroot.c | 106 +++++++++++++++++++++++++++------------- include/grub/emu/getroot.h | 16 ++++++ 2 files changed, 88 insertions(+), 34 deletions(-) diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c index 772de0a..58c8500 100644 --- a/grub-core/osdep/linux/getroot.c +++ b/grub-core/osdep/linux/getroot.c @@ -119,16 +119,6 @@ struct btrfs_ioctl_search_args { static int grub_util_is_imsm (const char *os_dev); - -#define ESCAPED_PATH_MAX (4 * PATH_MAX) -struct mountinfo_entry -{ - int id; - int major, minor; - char enc_root[ESCAPED_PATH_MAX + 1], enc_path[ESCAPED_PATH_MAX + 1]; - char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1]; -}; - static char ** grub_util_raid_getmembers (const char *name, int bootable) { @@ -365,21 +355,18 @@ get_btrfs_fs_prefix (const char *mount_path) } -char ** -grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) +/* Read all entries from /proc/self/mountinfo and return them in a xmalloc'ed + memory which has to be freed be the caller after use. + The last entry is marked with mountinfo_entry.enc_root[0] = 0. + In case of any errors NULL is returned. */ +struct mountinfo_entry * +grub_read_mountinfo(void) { FILE *fp; char *buf = NULL; size_t len = 0; grub_size_t entry_len = 0, entry_max = 4; struct mountinfo_entry *entries; - struct mountinfo_entry parent_entry = { 0, 0, 0, "", "", "", "" }; - int i; - - if (! *dir) - dir = "/"; - if (relroot) - *relroot = NULL; fp = grub_util_fopen ("/proc/self/mountinfo", "r"); if (! fp) @@ -387,31 +374,30 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) entries = xmalloc (entry_max * sizeof (*entries)); - /* First, build a list of relevant visible mounts. */ - while (getline (&buf, &len, fp) > 0) + /* First, build a list of visible mounts. */ + while (1) { struct mountinfo_entry entry; int count; - size_t enc_path_len; const char *sep; + if (entry_len >= entry_max) + { + entry_max <<= 1; + entries = xrealloc (entries, entry_max * sizeof (*entries)); + } + + if (getline (&buf, &len, fp) <= 0) + break; + if (sscanf (buf, "%d %d %u:%u %s %s%n", - &entry.id, &parent_entry.id, &entry.major, &entry.minor, + &entry.id, &entry.parent_id, &entry.major, &entry.minor, entry.enc_root, entry.enc_path, &count) < 6) continue; unescape (entry.enc_root); unescape (entry.enc_path); - enc_path_len = strlen (entry.enc_path); - /* Check that enc_path is a prefix of dir. The prefix must either be - the entire string, or end with a slash, or be immediately followed - by a slash. */ - if (strncmp (dir, entry.enc_path, enc_path_len) != 0 || - (enc_path_len && dir[enc_path_len - 1] != '/' && - dir[enc_path_len] && dir[enc_path_len] != '/')) - continue; - sep = strstr (buf + count, " - "); if (!sep) continue; @@ -422,6 +408,58 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) unescape (entry.device); + entries[entry_len] = entry; + entry_len++; + } + + fclose (fp); + + /* Mark the end of the list: */ + entries[entry_len].enc_root[0] = 0; + + return entries; +} + +char ** +grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) +{ + char *buf = NULL; + grub_size_t entry_len = 0, entry_max = 4; + struct mountinfo_entry *entries_all; + struct mountinfo_entry *entries; + struct mountinfo_entry parent_entry = { 0, 0, 0, 0, "", "", "", "" }; + int i; + int entry_no; + + if (! *dir) + dir = "/"; + if (relroot) + *relroot = NULL; + + entries_all = grub_read_mountinfo(); + if (! entries_all) + return NULL; /* fall through to other methods */ + + entries = xmalloc (entry_max * sizeof (*entries)); + + /* First, build a list of relevant visible mounts. */ + for (entry_no = 0; entries_all[entry_no].enc_root[0] != 0; entry_no++) + { + struct mountinfo_entry entry; + size_t enc_path_len; + + entry = entries_all[entry_no]; + parent_entry.id = entry.parent_id; + + enc_path_len = strlen (entry.enc_path); + /* Check that enc_path is a prefix of dir. The prefix must either be + the entire string, or end with a slash, or be immediately followed + by a slash. */ + if (strncmp (dir, entries[i].enc_path, enc_path_len) != 0 || + (enc_path_len && dir[enc_path_len - 1] != '/' && + dir[enc_path_len] && dir[enc_path_len] != '/')) + continue; + /* Using the mount IDs, find out where this fits in the list of visible mount entries we've seen so far. There are three interesting cases. Firstly, it may be inserted at the end: this is @@ -469,6 +507,8 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) } } + free (entries_all); + /* Now scan visible mounts for the ones we're interested in. */ for (i = entry_len - 1; i >= 0; i--) { @@ -534,13 +574,11 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) free (fs_prefix); free (buf); free (entries); - fclose (fp); return ret; } free (buf); free (entries); - fclose (fp); return NULL; } diff --git a/include/grub/emu/getroot.h b/include/grub/emu/getroot.h index 73fa2d3..0c4ef6d 100644 --- a/include/grub/emu/getroot.h +++ b/include/grub/emu/getroot.h @@ -25,6 +25,10 @@ #include #include +#ifdef HAVE_LIMITS_H +#include +#endif + enum grub_dev_abstraction_types { GRUB_DEV_ABSTRACTION_NONE, GRUB_DEV_ABSTRACTION_LVM, @@ -49,6 +53,18 @@ void grub_util_follow_gpart_up (const char *name, grub_disk_addr_t *off_out, #include #ifdef __linux__ + +#define ESCAPED_PATH_MAX (4 * PATH_MAX) +struct mountinfo_entry +{ + int id, parent_id; + int major, minor; + char enc_root[ESCAPED_PATH_MAX + 1], enc_path[ESCAPED_PATH_MAX + 1]; + char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1]; +}; + +struct mountinfo_entry * +grub_read_mountinfo(void); char ** grub_find_root_devices_from_mountinfo (const char *dir, char **relroot); #endif -- 1.8.1.4