There are some linux systems in the wild where the /etc/hostname file
does not contain the configured hostname as sole line in that file. This
file actually allows for comments [1] starting with "#" and the original
logic would extract the comment line instead of the hostname on such
systems. This patch fixes this by filtering out the comment lines and
any empty lines.
[1]
https://www.freedesktop.org/software/systemd/man/hostname.html
---
Please keep in mind that this is my first attempt at OCaml after taking
a quick crash-course to get familiar with the language to produce this
patch. Therefore, the patch may be very suboptimal and break guidelines,
so please be forgiving :-)
daemon/inspect_fs_unix.ml | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/daemon/inspect_fs_unix.ml b/daemon/inspect_fs_unix.ml
index 63cb279d0..533ef71b2 100644
--- a/daemon/inspect_fs_unix.ml
+++ b/daemon/inspect_fs_unix.ml
@@ -556,11 +556,23 @@ and check_hostname_from_file filename =
let chroot =
let name = sprintf "check_hostname_from_file: %s" filename in
Chroot.create ~name () in
-
- let hostname = Chroot.f chroot read_small_file filename in
- match hostname with
- | None | Some [] | Some [""] -> None
- | Some (hostname :: _) -> Some hostname
+ try
+ let lines = Chroot.f chroot read_small_file filename in
+ let lines = match lines with
+ | None -> raise Not_found
+ | Some lines -> List.map String.trim lines in
+ let rec loop = function
+ | [] -> raise Not_found
+ | line :: _ when String.length line > 0 &&
+ not (String.is_prefix line "#") ->
+ line
+ | _ :: lines ->
+ loop lines
+ in
+ let hostname = loop lines in
+ Some hostname
+ with
+ Not_found -> None
(* Parse the hostname from /etc/sysconfig/network. This must be
* called from the 'with_augeas' wrapper. Note that F18+ and
--
2.36.1