From b91682f88cb5456776ba9acdb09e967473f7e823 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Thu, 12 Jul 2018 18:47:39 +0100 Subject: [PATCH] control: ensure shutdown type is valid. --- src/control.cc | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/control.cc b/src/control.cc index b8aad43..6a0fe36 100644 --- a/src/control.cc +++ b/src/control.cc @@ -14,6 +14,14 @@ namespace { // Control protocol minimum compatible version and current version: constexpr uint16_t min_compat_version = 1; constexpr uint16_t cp_version = 1; + + // check for value in a set + template + inline bool contains(const T (&v)[N], int i) + { + return std::find_if(std::begin(v), std::end(v), + [=](T p){ return i == static_cast(p); }) != std::end(v); + } } bool control_conn_t::process_packet() @@ -55,16 +63,21 @@ bool control_conn_t::process_packet() return true; } - auto sd_type = static_cast(rbuf[1]); - - services->stop_all_services(sd_type); - char ackBuf[] = { DINIT_RP_ACK }; - if (! queue_packet(ackBuf, 1)) return false; - - // Clear the packet from the buffer - rbuf.consume(2); - chklen = 0; - return true; + if (contains({shutdown_type_t::CONTINUE, shutdown_type_t::HALT, + shutdown_type_t::POWEROFF, shutdown_type_t::REBOOT}, rbuf[1])) { + auto sd_type = static_cast(rbuf[1]); + + services->stop_all_services(sd_type); + char ackBuf[] = { DINIT_RP_ACK }; + if (! queue_packet(ackBuf, 1)) return false; + + // Clear the packet from the buffer + rbuf.consume(2); + chklen = 0; + return true; + } + + // (otherwise fall through to below). } if (pktType == DINIT_CP_LISTSERVICES) { return list_services(); @@ -378,12 +391,6 @@ bool control_conn_t::list_services() } } -// check for value in a set -static inline bool contains(const int (&v)[3], int i) -{ - return std::find(std::begin(v), std::end(v), i) != std::end(v); -} - bool control_conn_t::add_service_dep() { // 1 byte packet type -- 2.25.1