+/* Try several methods to determine the hostname from a Linux or
+ * FreeBSD guest. Note that type and distro have been set, so we can
+ * use that information to direct the search.
+ */
+static int
+check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
+{
+ switch (fs->type) {
+ case OS_TYPE_LINUX:
+ /* Red Hat-derived would be in /etc/sysconfig/network, and
+ * Debian-derived in the file /etc/hostname. Very old Debian and
+ * SUSE use /etc/HOSTNAME. It's best to just look for each of
+ * these files in turn, rather than try anything clever based on
+ * distro.
+ */
+ if (guestfs_is_file (g, "/etc/HOSTNAME")) {
+ fs->hostname = first_line_of_file (g, "/etc/HOSTNAME");
+ if (fs->hostname == NULL)
+ return -1;
+ }
+ else if (guestfs_is_file (g, "/etc/hostname")) {
+ fs->hostname = first_line_of_file (g, "/etc/hostname");
+ if (fs->hostname == NULL)
+ return -1;
+ }
+ else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
+ if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
+ check_hostname_redhat) == -1)
+ return -1;
+ }
+ break;
+
+ case OS_TYPE_FREEBSD:
+ /* /etc/rc.conf contains the hostname, but there is no Augeas lens
+ * for this file.
+ */
+ if (guestfs_is_file (g, "/etc/rc.conf")) {
+ if (check_hostname_freebsd (g, fs) == -1)
+ return -1;
+ }
+ break;
+
+ case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
+ case OS_TYPE_UNKNOWN:
+ default:
+ /* nothing, keep GCC warnings happy */;
+ }
+
+ return 0;
+}
+
+/* Parse the hostname from /etc/sysconfig/network. This must be called
+ * from the inspect_with_augeas wrapper.
+ */
+static int
+check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
+{
+ char *hostname;
+
+ hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
+ if (!hostname)
+ return -1;
+
+ fs->hostname = hostname; /* freed by guestfs___free_inspect_info */
+ return 0;
+}
+
+/* Parse the hostname from /etc/rc.conf. On FreeBSD this file
+ * contains comments, blank lines and:
+ * hostname="freebsd8.example.com"
+ * ifconfig_re0="DHCP"
+ * keymap="uk.iso"
+ * sshd_enable="YES"
+ */
+static int
+check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
+{
+ const char *filename = "/etc/rc.conf";
+ int64_t size;
+ char **lines;
+ size_t i;
+
+ /* Don't trust guestfs_read_lines not to break with very large files.
+ * Check the file size is something reasonable first.
+ */
+ size = guestfs_filesize (g, filename);
+ if (size == -1)
+ /* guestfs_filesize failed and has already set error in handle */
+ return -1;
+ if (size > 1000000) {
+ error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+ filename, size);
+ return -1;
+ }
+
+ lines = guestfs_read_lines (g, filename);
+ if (lines == NULL)
+ return -1;
+
+ for (i = 0; lines[i] != NULL; ++i) {
+ if (STRPREFIX (lines[i], "hostname=\"") ||
+ STRPREFIX (lines[i], "hostname='")) {
+ size_t len = strlen (lines[i]) - 10 - 1;
+ fs->hostname = safe_strndup (g, &lines[i][10], len);
+ break;
+ } else if (STRPREFIX (lines[i], "hostname=")) {
+ size_t len = strlen (lines[i]) - 9;
+ fs->hostname = safe_strndup (g, &lines[i][9], len);
+ break;
+ }
+ }
+
+ guestfs___free_string_list (lines);
+ return 0;
+}
+