Skip to content

Commit

Permalink
HACK! Add a 2s delay before changing user to wait for the cohttp serv…
Browse files Browse the repository at this point in the history
…er to be initialized

Cohttp does not return after listening
This fixes: was not possible to run the server on port 80/443 when using <user> in config file
(Regression during switch to cohttp)
  • Loading branch information
balat committed Sep 30, 2022
1 parent 852f72a commit c7ef012
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ocsigenserver.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
opam-version: "2.0"
name: "ocsigenserver"
version: "5.0.1"
version: "5.0.2"
maintainer: "dev@ocsigen.org"
synopsis: "A full-featured and extensible Web server"
description: "Ocsigen Server implements most features of the HTTP protocol, and has a very powerful extension mechanism that makes it very easy to plug your own OCaml modules for generating pages. Many extensions are already implemented, like a reverse proxy, content compression, access control, authentication, etc."
Expand Down
32 changes: 20 additions & 12 deletions src/server/ocsigen_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,21 @@ let start ?config () =
with e ->
Lwt_log.ign_error ~section ~exn:e "Cannot create the command pipe"));
(* I change the user for the process *)
(try
(if current_uid = 0
then
match user with None -> () | Some user -> Unix.initgroups user gid);
Unix.setgid gid; Unix.setuid uid
with (Unix.Unix_error _ | Failure _) as e ->
Lwt_log.ign_error ~section "Error: Wrong user or group";
raise e);
Lwt.async (fun () ->
Lwt_unix.sleep 2. >>= fun () ->
(*HACK! This is to wait for the server to be initialized by cohttp.
How to do that in a cleaner way? *)
(try
(if current_uid = 0
then
match user with
| None -> ()
| Some user -> Unix.initgroups user gid);
Unix.setgid gid; Unix.setuid uid
with (Unix.Unix_error _ | Failure _) as e ->
Lwt_log.ign_error ~section "Error: Wrong user or group";
raise e);
Lwt.return ());
let minthreads = Ocsigen_config.get_minthreads ()
and maxthreads = Ocsigen_config.get_maxthreads () in
if minthreads > maxthreads
Expand Down Expand Up @@ -445,10 +452,11 @@ let start ?config () =
Ocsigen_cohttp.service ~address ~port
~connector:extensions_connector ())
connection
@ (List.map (fun (address, port, (crt, key)) ->
Ocsigen_cohttp.service
~ssl:(crt, key, Some (ask_for_passwd [address, port]))
~address ~port ~connector:extensions_connector ()))
@ List.map
(fun (address, port, (crt, key)) ->
Ocsigen_cohttp.service
~ssl:(crt, key, Some (ask_for_passwd [address, port]))
~address ~port ~connector:extensions_connector ())
ssl_connection)
(*
Ocsigen_messages.warning "Ocsigen has been launched (initialisations ok)";
Expand Down

0 comments on commit c7ef012

Please sign in to comment.