Skip to content

Commit

Permalink
CA-389840: Bug in parsing output of 'xen-livepatch list'
Browse files Browse the repository at this point in the history
The bug is in parsing the output of 'xen-livepatch list'. The following
output can't be parsed correctly:
lp_4.17.3-3.11.gf717213.xs8-4.17.3-3.12.xs8| CHECKED

The regex pattern doesn't exclude the '|' after 'xs8'. As a result, the
vertical line '|' is parsed as part of '3.12.xs8|'. The correct one should be
'3.12.xs8'.

Signed-off-by: Ming Lu <[email protected]>
  • Loading branch information
minglumlu committed Mar 14, 2024
1 parent 4e0d37c commit 969b7e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
32 changes: 32 additions & 0 deletions ocaml/tests/test_livepatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ lp_4.13.4-10.22.xs8-4.13.4-10.23.xs8 | CHECKED
|}
, None
)
; ( {|
ID | status
----------------------------------------+------------
lp_4.13.4-10.22.xs8-4.13.4-10.23.xs8| CHECKED
lp_4.13.4-10.22.xs8-4.13.4-10.23.xs8| APPLIED
|}
, Some ("4.13.4", "10.22.xs8", "4.13.4", "10.23.xs8")
)
; ( {|
ID | status
----------------------------------------+------------
lp_4.13.4-10.22.xs8-4.13.4-10.23.xs8|CHECKED
lp_4.13.4-10.22.xs8-4.13.4-10.23.xs8|APPLIED
|}
, Some ("4.13.4", "10.22.xs8", "4.13.4", "10.23.xs8")
)
; ( {|
ID | status
----------------------------------------+------------
p_4.13.4-10.22.xs8-4.13.4-10.23.xs8 | CHECKED
p_4.13.4-10.22.xs8-4.13.4-10.23.xs8 | APPLIED
|}
, None
)
; ( {|
ID | status | metadata
----------------------------------------+------------+---------------
lp_4.17.3-3.11.gf717213.xs8-4.17.3-3.12.xs8| CHECKED |
lp_4.17.3-3.11.gf717213.xs8-4.17.3-3.13.xs8| APPLIED |
|}
, Some ("4.17.3", "3.11.gf717213.xs8", "4.17.3", "3.13.xs8")
)
]
end)

Expand Down
43 changes: 22 additions & 21 deletions ocaml/xapi/livepatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,43 +187,44 @@ module KernelLivePatch = struct
end

module XenLivePatch = struct
let get_regexp status =
Re.Posix.compile_pat
(Printf.sprintf {|^[ ]*lp_([^- ]+)-([^- ]+)-([^- ]+)-([^- ]+).+%s.*$|}
status
)
let drop x = Astring.Char.Ascii.(is_control x || is_white x)

let get_livepatches pattern s =
let get_livepatches state s =
let pattern =
Re.Posix.compile_pat {|^lp_([^- ]+)-([^- ]+)-([^- ]+)-([^- ]+)$|}
in
Astring.String.cuts ~sep:"\n" s
|> List.filter_map (fun line ->
match Re.exec_opt pattern line with
| Some groups ->
let base_version = Re.Group.get groups 1 in
let base_release = Re.Group.get groups 2 in
let to_version = Re.Group.get groups 3 in
let to_release = Re.Group.get groups 4 in
Some (base_version, base_release, to_version, to_release)
| None ->
Astring.String.cuts ~sep:"|" line
|> List.map (Astring.String.trim ~drop)
|> function
| name :: state' :: _ when state' = state -> (
match Re.exec_opt pattern name with
| Some groups ->
let base_version = Re.Group.get groups 1 in
let base_release = Re.Group.get groups 2 in
let to_version = Re.Group.get groups 3 in
let to_release = Re.Group.get groups 4 in
Some (base_version, base_release, to_version, to_release)
| None ->
None
)
| _ ->
None
)

let get_running_livepatch' s =
let r = get_regexp "APPLIED" in
get_livepatches r s |> get_latest_livepatch
get_livepatches "APPLIED" s |> get_latest_livepatch

let get_running_livepatch () =
Helpers.call_script !Xapi_globs.xen_livepatch_cmd ["list"]
|> get_running_livepatch'

let get_checked_livepatches () =
Helpers.call_script !Xapi_globs.xen_livepatch_cmd ["list"]
|> get_livepatches (get_regexp "CHECKED")
|> get_livepatches "CHECKED"

let get_base_build_id () =
let drop x =
let open Astring.Char.Ascii in
is_control x || is_blank x || is_white x
in
Helpers.call_script !Xapi_globs.xl_cmd ["info"; "build_id"]
|> Astring.String.trim ~drop
|> function
Expand Down

0 comments on commit 969b7e7

Please sign in to comment.