summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-05-24 15:40:36 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-06-01 19:31:07 +0100
commit5d5e79d37414aabb13ff905ee82bc7501d656b41 (patch)
treed1c1863fa78ff8bc05de0a52a85810efd23e1d90
parentecf4a68da0baab2509c7cb02a24b73a4eb01f7c1 (diff)
downloadlibguestfs-5d5e79d37414aabb13ff905ee82bc7501d656b41.tar.gz
libguestfs-5d5e79d37414aabb13ff905ee82bc7501d656b41.tar.xz
libguestfs-5d5e79d37414aabb13ff905ee82bc7501d656b41.zip
inspection: Don't fail if /etc/HOSTNAME or /etc/hostname are empty files (RHBZ#823821).
Change guestfs___first_line_of_file so that if the file is empty this returns an empty string instead of an error. This is consistent with the behaviour of this function in the case where the file starts with a \n character, where it would previously have returned an empty string. Change all callers so that they handle this case. Then change the hostname parsing code so that it doesn't give up when /etc/HOSTNAME is empty, but falls through to the next alternative, and similarly for /etc/hostname. Thanks Todd Mummert for finding and diagnosing this bug. (cherry picked from commit f00066d22b11bf40d0272f68565a2a27fea15627)
-rw-r--r--src/inspect_fs.c8
-rw-r--r--src/inspect_fs_unix.c20
2 files changed, 24 insertions, 4 deletions
diff --git a/src/inspect_fs.c b/src/inspect_fs.c
index fbbdda41..9268f168 100644
--- a/src/inspect_fs.c
+++ b/src/inspect_fs.c
@@ -487,6 +487,10 @@ check_package_management (guestfs_h *g, struct inspect_fs *fs)
/* Get the first line of a small file, without any trailing newline
* character.
+ *
+ * NOTE: If the file is completely empty or begins with a '\n'
+ * character, this returns an empty string (not NULL). The caller
+ * will usually need to check for this case.
*/
char *
guestfs___first_line_of_file (guestfs_h *g, const char *filename)
@@ -512,9 +516,9 @@ guestfs___first_line_of_file (guestfs_h *g, const char *filename)
if (lines == NULL)
return NULL;
if (lines[0] == NULL) {
- error (g, _("%s: file is empty"), filename);
guestfs___free_string_list (lines);
- return NULL;
+ /* Empty file: Return an empty string as explained above. */
+ return safe_strdup (g, "");
}
/* lines[1] should be NULL because of '1' argument above ... */
diff --git a/src/inspect_fs_unix.c b/src/inspect_fs_unix.c
index 85928a2b..ae76c6a6 100644
--- a/src/inspect_fs_unix.c
+++ b/src/inspect_fs_unix.c
@@ -181,6 +181,12 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs,
fs->product_name = guestfs___first_line_of_file (g, release_filename);
if (fs->product_name == NULL)
return -1;
+ if (STREQ (fs->product_name, "")) {
+ free (fs->product_name);
+ fs->product_name = NULL;
+ error (g, _("release file %s is empty or malformed"), release_filename);
+ return -1;
+ }
return 0;
}
@@ -644,13 +650,23 @@ check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
if (fs->hostname == NULL)
return -1;
+ if (STREQ (fs->hostname, "")) {
+ free (fs->hostname);
+ fs->hostname = NULL;
+ }
}
- else if (guestfs_is_file (g, "/etc/hostname")) {
+
+ if (!fs->hostname && guestfs_is_file (g, "/etc/hostname")) {
fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
if (fs->hostname == NULL)
return -1;
+ if (STREQ (fs->hostname, "")) {
+ free (fs->hostname);
+ fs->hostname = NULL;
+ }
}
- else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
+
+ if (!fs->hostname && guestfs_is_file (g, "/etc/sysconfig/network")) {
const char *configfiles[] = { "/etc/sysconfig/network", NULL };
if (inspect_with_augeas (g, fs, configfiles,
check_hostname_redhat) == -1)