From e2906025c86aeaed9ef02c3c54a56f2bb209711f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 31 Jul 2016 20:35:10 +0900 Subject: [PATCH] nghttpx: Don't exit from save_pid and set_alpn_prefs --- src/shrpx.cc | 16 ++++++++++------ src/shrpx_ssl.cc | 14 ++++++-------- src/shrpx_ssl.h | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 4bfe35ce..65cd6f84 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -294,7 +294,7 @@ int chown_to_running_user(const char *path) { } // namespace namespace { -void save_pid() { +int save_pid() { constexpr auto SUFFIX = StringRef::from_lit(".XXXXXX"); auto &pid_file = get_config()->pid_file; @@ -313,7 +313,7 @@ void save_pid() { auto error = errno; LOG(ERROR) << "Could not save PID to file " << pid_file << ": " << strerror(error); - exit(EXIT_FAILURE); + return -1; } auto content = util::utos(get_config()->pid) + '\n'; @@ -322,14 +322,14 @@ void save_pid() { auto error = errno; LOG(ERROR) << "Could not save PID to file " << pid_file << ": " << strerror(error); - exit(EXIT_FAILURE); + return -1; } if (fsync(fd) == -1) { auto error = errno; LOG(ERROR) << "Could not save PID to file " << pid_file << ": " << strerror(error); - exit(EXIT_FAILURE); + return -1; } close(fd); @@ -341,7 +341,7 @@ void save_pid() { unlink(temp_path); - exit(EXIT_FAILURE); + return -1; } if (get_config()->uid != 0) { @@ -351,6 +351,8 @@ void save_pid() { << " failed: " << strerror(error); } } + + return 0; } } // namespace @@ -2379,7 +2381,9 @@ int process_options(Config *config, tlsconf.tls_proto_mask = ssl::create_tls_proto_mask(tlsconf.tls_proto_list); - tlsconf.alpn_prefs = ssl::set_alpn_prefs(tlsconf.npn_list); + if (ssl::set_alpn_prefs(tlsconf.alpn_prefs, tlsconf.npn_list) != 0) { + return -1; + } tlsconf.bio_method = create_bio_method(); diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index fa09e137..469338a3 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -94,16 +94,14 @@ int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) { } } // namespace -// This function is meant be called from master process, hence the -// call exit(3). -std::vector -set_alpn_prefs(const std::vector &protos) { +int set_alpn_prefs(std::vector &out, + const std::vector &protos) { size_t len = 0; for (const auto &proto : protos) { if (proto.size() > 255) { LOG(FATAL) << "Too long ALPN identifier: " << proto.size(); - exit(EXIT_FAILURE); + return -1; } len += 1 + proto.size(); @@ -111,10 +109,10 @@ set_alpn_prefs(const std::vector &protos) { if (len > (1 << 16) - 1) { LOG(FATAL) << "Too long ALPN identifier list: " << len; - exit(EXIT_FAILURE); + return -1; } - auto out = std::vector(len); + out.resize(len); auto ptr = out.data(); for (const auto &proto : protos) { @@ -123,7 +121,7 @@ set_alpn_prefs(const std::vector &protos) { ptr += proto.size(); } - return out; + return 0; } namespace { diff --git a/src/shrpx_ssl.h b/src/shrpx_ssl.h index 5f31c7a7..5138fff5 100644 --- a/src/shrpx_ssl.h +++ b/src/shrpx_ssl.h @@ -181,8 +181,8 @@ bool check_http2_requirement(SSL *ssl); // passed to SSL_CTX_set_options(). long int create_tls_proto_mask(const std::vector &tls_proto_list); -std::vector -set_alpn_prefs(const std::vector &protos); +int set_alpn_prefs(std::vector &out, + const std::vector &protos); // Setups server side SSL_CTX. This function inspects get_config() // and if upstream_no_tls is true, returns nullptr. Otherwise