Skip to content

Commit fbaad2b

Browse files
authored
CA-403620: Drop the usage of fuser in stunnel client proxy (#6197)
Two changes in the PR: **Drop the usage of fuser in stunnel client proxy** The drawback of fuser is that it gets too many things involved. E.g. it is observed that it got stuck on cifs kernel module. This change uses a cleaner way to remember the stunnel client proxy. Even when the xapi restarted unexpectedly, it can stop the remnant stunnel proxy and start a new one. **Make the stunnel proxy local port configurable** Making it configurable can avoid the situation when the port conflicts with others, e.g. an external program from users.
2 parents f9b5e52 + e7f2b70 commit fbaad2b

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

ocaml/libs/stunnel/stunnel.ml

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -448,44 +448,30 @@ let with_connect ?unique_id ?use_fork_exec_helper ?write_to_log ~verify_cert
448448
)
449449
5
450450
451-
let with_client_proxy ~verify_cert ~remote_host ~remote_port ~local_host
452-
~local_port f =
453-
( try
454-
D.debug "Clean up running stunnel client proxy if there is any ..." ;
455-
let out, _ =
456-
Forkhelpers.execute_command_get_output "/usr/sbin/fuser"
457-
["-4k"; string_of_int local_port ^ "/tcp"]
458-
in
459-
D.debug "Killed running stunnel client proxy:%s" out
460-
with
461-
| Forkhelpers.Spawn_internal_error (stderr, stdout, process_status) -> (
462-
match process_status with
463-
| Unix.WEXITED 1 ->
464-
D.debug "No running stunnel client proxy"
465-
| _ ->
466-
D.warn
467-
"Cleaning up running stunnel client proxy returned unexpectedly: \
468-
stdout=(%s); stderr=(%s)"
469-
stdout stderr
470-
)
471-
) ;
472-
473-
retry
451+
let with_client_proxy_systemd_service ~verify_cert ~remote_host ~remote_port
452+
~local_host ~local_port ~service f =
453+
let cmd_path = stunnel_path () in
454+
let config =
455+
config_file
456+
~accept:(Some (local_host, local_port))
457+
verify_cert remote_host remote_port
458+
in
459+
let stop () = ignore (Fe_systemctl.stop ~service) in
460+
(* Try stopping anyway before starting it. *)
461+
ignore_exn stop () ;
462+
let conf_path, out = Filename.open_temp_file service ".conf" in
463+
let finally = Xapi_stdext_pervasives.Pervasiveext.finally in
464+
finally
474465
(fun () ->
475-
let pid, _ =
476-
attempt_one_connect
477-
(`Local_host_port (local_host, local_port))
478-
verify_cert remote_host remote_port
479-
in
480-
D.debug "Started a client proxy (pid:%s): %s:%s -> %s:%s"
481-
(string_of_int (getpid pid))
482-
local_host (string_of_int local_port) remote_host
483-
(string_of_int remote_port) ;
484-
Xapi_stdext_pervasives.Pervasiveext.finally
485-
(fun () -> f ())
486-
(fun () -> disconnect_with_pid ~wait:false ~force:true pid)
466+
finally (fun () -> output_string out config) (fun () -> close_out out) ;
467+
finally
468+
(fun () ->
469+
Fe_systemctl.start_transient ~service cmd_path [conf_path] ;
470+
f ()
471+
)
472+
(fun () -> ignore_exn stop ())
487473
)
488-
5
474+
(fun () -> Unixext.unlink_safe conf_path)
489475
490476
let check_verify_error line =
491477
let sub_after i s =

ocaml/libs/stunnel/stunnel.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ val with_moved_exn : t -> (t -> 'd) -> 'd
8888

8989
val safe_release : t -> unit
9090

91-
val with_client_proxy :
91+
val with_client_proxy_systemd_service :
9292
verify_cert:verification_config option
9393
-> remote_host:string
9494
-> remote_port:int
9595
-> local_host:string
9696
-> local_port:int
97+
-> service:string
9798
-> (unit -> 'a)
9899
-> 'a

ocaml/xapi/repository_helpers.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ let with_local_repositories ~__context f =
398398
with Pool_role.This_host_is_a_master ->
399399
Option.get (Helpers.get_management_ip_addr ~__context)
400400
in
401-
Stunnel.with_client_proxy ~verify_cert:(Stunnel_client.pool ())
402-
~remote_host:master_addr ~remote_port:Constants.default_ssl_port
403-
~local_host:"127.0.0.1"
401+
Stunnel.with_client_proxy_systemd_service
402+
~verify_cert:(Stunnel_client.pool ()) ~remote_host:master_addr
403+
~remote_port:Constants.default_ssl_port ~local_host:"127.0.0.1"
404404
~local_port:!Xapi_globs.local_yum_repo_port
405+
~service:"stunnel_proxy_for_update_client"
405406
@@ fun () ->
406407
let enabled = get_enabled_repositories ~__context in
407408
Xapi_stdext_pervasives.Pervasiveext.finally

ocaml/xapi/xapi_globs.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ let xapi_globs_spec =
11431143
; ("max_traces", Int max_traces)
11441144
; ("max_observer_file_size", Int max_observer_file_size)
11451145
; ("test-open", Int test_open) (* for consistency with xenopsd *)
1146+
; ("local_yum_repo_port", Int local_yum_repo_port)
11461147
]
11471148

11481149
let xapi_globs_spec_with_descriptions = []

0 commit comments

Comments
 (0)