Browse Source

control-datatypes: refactor control protocl replies and information messages as enum class

helican 6 months ago
parent
commit
4e21d13c14

+ 74 - 74
src/control.cc

@@ -45,8 +45,8 @@ bool control_conn_t::process_packet()
     cp_cmd pktType = (cp_cmd)rbuf[0];
     if (pktType == cp_cmd::QUERYVERSION) {
         // Responds with:
-        // DINIT_RP_CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
-        char replyBuf[] = { DINIT_RP_CPVERSION, 0, 0, 0, 0 };
+        // cp_rply::CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
+        char replyBuf[] = { (char)cp_rply::CPVERSION, 0, 0, 0, 0 };
         memcpy(replyBuf + 1, &min_compat_version, 2);
         memcpy(replyBuf + 3, &cp_version, 2);
         if (!queue_packet(replyBuf, sizeof(replyBuf))) return false;
@@ -81,7 +81,7 @@ bool control_conn_t::process_packet()
             auto sd_type = static_cast<shutdown_type_t>(rbuf[1]);
 
             services->stop_all_services(sd_type);
-            char ackBuf[] = { DINIT_RP_ACK };
+            char ackBuf[] = { (char)cp_rply::ACK };
             if (! queue_packet(ackBuf, 1)) return false;
 
             // Clear the packet from the buffer
@@ -130,7 +130,7 @@ bool control_conn_t::process_packet()
     }
 
     // Unrecognized: give error response
-    char outbuf[] = { DINIT_RP_BADREQ };
+    char outbuf[] = { (char)cp_rply::BADREQ };
     if (!queue_packet(outbuf, 1)) return false;
     bad_conn_close = true;
     return true;
@@ -151,7 +151,7 @@ bool control_conn_t::process_find_load(cp_cmd pktType)
     rbuf.extract((char *)&svcSize, 1, 2);
     if (svcSize <= 0 || svcSize > (1024 - 3)) {
         // Queue error response / mark connection bad
-        char badreqRep[] = { DINIT_RP_BADREQ };
+        char badreqRep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreqRep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -171,7 +171,7 @@ bool control_conn_t::process_find_load(cp_cmd pktType)
     rbuf.consume(chklen);
     chklen = 0;
     
-    char fail_code = DINIT_RP_NOSERVICE;
+    cp_rply fail_code = cp_rply::NOSERVICE;
 
     if (pktType == cp_cmd::LOADSERVICE) {
         // LOADSERVICE
@@ -180,17 +180,17 @@ bool control_conn_t::process_find_load(cp_cmd pktType)
         }
         catch (service_description_exc &sdexc) {
             log_service_load_failure(sdexc);
-            fail_code = DINIT_RP_SERVICE_DESC_ERR;
+            fail_code = cp_rply::SERVICE_DESC_ERR;
         }
         catch (service_not_found &snf) {
             log(loglevel_t::ERROR, "Could not load service ", snf.service_name, ": ",
                     snf.exc_description);
-            // fail_code = DINIT_RP_NOSERVICE;   (already set)
+            // fail_code = cp_rply::NOSERVICE;   (already set)
         }
         catch (service_load_exc &slexc) {
             log(loglevel_t::ERROR, "Could not load service ", slexc.service_name, ": ",
                     slexc.exc_description);
-            fail_code = DINIT_RP_SERVICE_LOAD_ERR;
+            fail_code = cp_rply::SERVICE_LOAD_ERR;
         }
     }
     else {
@@ -199,7 +199,7 @@ bool control_conn_t::process_find_load(cp_cmd pktType)
     }
     
     if (record == nullptr) {
-        std::vector<char> rp_buf = { fail_code };
+        std::vector<char> rp_buf = { (char)fail_code };
         if (! queue_packet(std::move(rp_buf))) return false;
         return true;
     }
@@ -208,7 +208,7 @@ bool control_conn_t::process_find_load(cp_cmd pktType)
     handle_t handle = allocate_service_handle(record);
     std::vector<char> rp_buf;
     rp_buf.reserve(7);
-    rp_buf.push_back(DINIT_RP_SERVICERECORD);
+    rp_buf.push_back((char)cp_rply::SERVICERECORD);
     rp_buf.push_back(static_cast<char>(record->get_state()));
     for (int i = 0; i < (int) sizeof(handle); i++) {
         rp_buf.push_back(*(((char *) &handle) + i));
@@ -233,7 +233,7 @@ bool control_conn_t::check_dependents(service_record *service, bool &had_depende
                 // packet type, size
                 reply_pkt.reserve(1 + sizeof(size_t) + sizeof(handle_t));
                 reply_pkt.resize(1 + sizeof(size_t));
-                reply_pkt[0] = DINIT_RP_DEPENDENTS;
+                reply_pkt[0] = (char)cp_rply::DEPENDENTS;
             }
             auto old_size = reply_pkt.size();
             reply_pkt.resize(old_size + sizeof(handle_t));
@@ -274,31 +274,31 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
     service_record *service = find_service_for_key(handle);
     if (service == nullptr) {
         // Service handle is bad
-        char badreqRep[] = { DINIT_RP_BADREQ };
+        char badreqRep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreqRep, 1)) return false;
         bad_conn_close = true;
         return true;
     }
     else {
-        char ack_buf[1] = { DINIT_RP_ACK };
+        char ack_buf[1] = { (char)cp_rply::ACK };
         
         switch (pktType) {
         case cp_cmd::STARTSERVICE:
             // start service, mark as required
             if (services->is_shutting_down()) {
-                ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
+                ack_buf[0] = (char)cp_rply::SHUTTINGDOWN;
                 break;
             }
             if ((service->get_state() == service_state_t::STOPPED
                     || service->get_state() == service_state_t::STOPPING)
                     && service->is_stop_pinned()) {
-                ack_buf[0] = DINIT_RP_PINNEDSTOPPED;
+                ack_buf[0] = (char)cp_rply::PINNEDSTOPPED;
                 break;
             }
             if (do_pin) service->pin_start();
             service->start();
             services->process_queues();
-            if (service->get_state() == service_state_t::STARTED) ack_buf[0] = DINIT_RP_ALREADYSS;
+            if (service->get_state() == service_state_t::STARTED) ack_buf[0] = (char)cp_rply::ALREADYSS;
             break;
         case cp_cmd::STOPSERVICE:
         {
@@ -306,13 +306,13 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
             bool do_restart = ((rbuf[1] & 4) == 4);
             bool gentle = ((rbuf[1] & 2) == 2);
             if (do_restart && services->is_shutting_down()) {
-                ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
+                ack_buf[0] = (char)cp_rply::SHUTTINGDOWN;
                 break;
             }
             if ((service->get_state() == service_state_t::STARTED
                     || service->get_state() == service_state_t::STARTING)
                     && service->is_start_pinned()) {
-                ack_buf[0] = DINIT_RP_PINNEDSTARTED;
+                ack_buf[0] = (char)cp_rply::PINNEDSTARTED;
                 break;
             }
             if (gentle) {
@@ -329,7 +329,7 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
             service_state_t wanted_state;
             if (do_restart) {
                 if (! service->restart()) {
-                    ack_buf[0] = DINIT_RP_NAK;
+                    ack_buf[0] = (char)cp_rply::NAK;
                     break;
                 }
                 wanted_state = service_state_t::STARTED;
@@ -341,20 +341,20 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
                 wanted_state = service_state_t::STOPPED;
             }
             services->process_queues();
-            if (service->get_state() == wanted_state && !do_restart) ack_buf[0] = DINIT_RP_ALREADYSS;
+            if (service->get_state() == wanted_state && !do_restart) ack_buf[0] = (char)cp_rply::ALREADYSS;
             break;
         }
         case cp_cmd::WAKESERVICE:
         {
             // re-attach a service to its (started) dependents, causing it to start.
             if (services->is_shutting_down()) {
-                ack_buf[0] = DINIT_RP_SHUTTINGDOWN;
+                ack_buf[0] = (char)cp_rply::SHUTTINGDOWN;
                 break;
             }
             if ((service->get_state() == service_state_t::STOPPED
                     || service->get_state() == service_state_t::STOPPING)
                     && service->is_stop_pinned()) {
-                ack_buf[0] = DINIT_RP_PINNEDSTOPPED;
+                ack_buf[0] = (char)cp_rply::PINNEDSTOPPED;
                 break;
             }
             bool found_dpt = false;
@@ -370,12 +370,12 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
                 }
             }
             if (!found_dpt) {
-                ack_buf[0] = DINIT_RP_NAK;
+                ack_buf[0] = (char)cp_rply::NAK;
             }
 
             if (do_pin) service->pin_start();
             services->process_queues();
-            if (service->get_state() == service_state_t::STARTED) ack_buf[0] = DINIT_RP_ALREADYSS;
+            if (service->get_state() == service_state_t::STARTED) ack_buf[0] = (char)cp_rply::ALREADYSS;
             break;
         }
         case cp_cmd::RELEASESERVICE:
@@ -383,7 +383,7 @@ bool control_conn_t::process_start_stop(cp_cmd pktType)
             if (do_pin) service->pin_stop();
             service->stop(false);
             services->process_queues();
-            if (service->get_state() == service_state_t::STOPPED) ack_buf[0] = DINIT_RP_ALREADYSS;
+            if (service->get_state() == service_state_t::STOPPED) ack_buf[0] = (char)cp_rply::ALREADYSS;
             break;
         default:
             // avoid warning for unhandled switch/case values
@@ -420,7 +420,7 @@ bool control_conn_t::process_unpin_service()
     service_record *service = find_service_for_key(handle);
     if (service == nullptr) {
         // Service handle is bad
-        char badreqRep[] = { DINIT_RP_BADREQ };
+        char badreqRep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreqRep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -428,7 +428,7 @@ bool control_conn_t::process_unpin_service()
 
     service->unpin();
     services->process_queues();
-    char ack_buf[] = { (char) DINIT_RP_ACK };
+    char ack_buf[] = { (char) cp_rply::ACK };
     if (! queue_packet(ack_buf, 1)) return false;
     
     // Clear the packet from the buffer
@@ -457,7 +457,7 @@ bool control_conn_t::process_unload_service()
     service_record *service = find_service_for_key(handle);
     if (service == nullptr) {
         // Service handle is bad
-        char badreq_rep[] = { DINIT_RP_BADREQ };
+        char badreq_rep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreq_rep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -465,7 +465,7 @@ bool control_conn_t::process_unload_service()
 
     if (!service->has_lone_ref() || service->get_state() != service_state_t::STOPPED) {
         // Cannot unload: has other references
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         if (!queue_packet(nak_rep, 1)) return false;
     }
     else {
@@ -477,7 +477,7 @@ bool control_conn_t::process_unload_service()
         key_service_map.erase(handle);
 
         // send ack
-        char ack_buf[] = { (char) DINIT_RP_ACK };
+        char ack_buf[] = { (char) cp_rply::ACK };
         if (!queue_packet(ack_buf, 1)) return false;
     }
 
@@ -507,7 +507,7 @@ bool control_conn_t::process_reload_service()
     service_record *service = find_service_for_key(handle);
     if (service == nullptr) {
         // Service handle is bad
-        char badreq_rep[] = { DINIT_RP_BADREQ };
+        char badreq_rep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreq_rep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -515,7 +515,7 @@ bool control_conn_t::process_reload_service()
 
     if (!service->has_lone_ref(false)) {
         // Cannot unload: has other references
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         if (! queue_packet(nak_rep, 1)) return false;
     }
     else {
@@ -530,13 +530,13 @@ bool control_conn_t::process_reload_service()
             services->process_queues();
 
             // send ack
-            char ack_buf[] = { (char) DINIT_RP_ACK };
+            char ack_buf[] = { (char) cp_rply::ACK };
             if (! queue_packet(ack_buf, 1)) return false;
         }
         catch (service_load_exc &slexc) {
             log(loglevel_t::ERROR, "Could not reload service ", slexc.service_name, ": ",
                     slexc.exc_description);
-            char nak_rep[] = { DINIT_RP_NAK };
+            char nak_rep[] = { (char)cp_rply::NAK };
             if (! queue_packet(nak_rep, 1)) return false;
         }
     }
@@ -604,7 +604,7 @@ bool control_conn_t::list_services()
             int nameLen = std::min((size_t)256, name.length());
             pkt_buf.resize(hdrsize + nameLen);
             
-            pkt_buf[0] = DINIT_RP_SVCINFO;
+            pkt_buf[0] = (char)cp_rply::SVCINFO;
             pkt_buf[1] = nameLen;
             
             fill_status_buffer(&pkt_buf[2], sptr);
@@ -616,7 +616,7 @@ bool control_conn_t::list_services()
             if (!queue_packet(std::move(pkt_buf))) return false;
         }
         
-        char ack_buf[] = { (char) DINIT_RP_LISTDONE };
+        char ack_buf[] = { (char) cp_rply::LISTDONE };
         if (! queue_packet(ack_buf, 1)) return false;
         
         return true;
@@ -643,17 +643,17 @@ bool control_conn_t::process_service_status()
 
     service_record *service = find_service_for_key(handle);
     if (service == nullptr || service->get_name().length() > std::numeric_limits<uint16_t>::max()) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
     // Reply:
-    // 1 byte packet type = DINIT_RP_SERVICESTATUS
+    // 1 byte packet type = cp_rply::SERVICESTATUS
     // 1 byte reserved ( = 0)
     // STATUS_BUFFER_SIZE bytes status
 
     std::vector<char> pkt_buf(2 + STATUS_BUFFER_SIZE);
-    pkt_buf[0] = DINIT_RP_SERVICESTATUS;
+    pkt_buf[0] = (char)cp_rply::SERVICESTATUS;
     pkt_buf[1] = 0;
     fill_status_buffer(pkt_buf.data() + 2, service);
 
@@ -683,7 +683,7 @@ bool control_conn_t::add_service_dep(bool do_enable)
     service_record *to_service = find_service_for_key(to_handle);
     if (from_service == nullptr || to_service == nullptr || from_service == to_service) {
         // Service handle is bad
-        char badreq_rep[] = { DINIT_RP_BADREQ };
+        char badreq_rep[] = { (char)cp_rply::BADREQ };
         if (!queue_packet(badreq_rep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -693,7 +693,7 @@ bool control_conn_t::add_service_dep(bool do_enable)
     int dep_type_int = rbuf[1];
     if (!contains({dependency_type::MILESTONE, dependency_type::REGULAR,
             dependency_type::WAITS_FOR}, dep_type_int)) {
-        char badreqRep[] = { DINIT_RP_BADREQ };
+        char badreqRep[] = { (char)cp_rply::BADREQ };
         if (!queue_packet(badreqRep, 1)) return false;
         bad_conn_close = true;
     }
@@ -704,7 +704,7 @@ bool control_conn_t::add_service_dep(bool do_enable)
         if (from_service->get_state() != service_state_t::STOPPED &&
                 to_service->get_state() != service_state_t::STARTED) {
             // Cannot create dependency now since it would be contradicted:
-            char nak_rep[] = { DINIT_RP_NAK };
+            char nak_rep[] = { (char)cp_rply::NAK };
             if (! queue_packet(nak_rep, 1)) return false;
             rbuf.consume(pkt_size);
             chklen = 0;
@@ -725,7 +725,7 @@ bool control_conn_t::add_service_dep(bool do_enable)
             service_record * dep_to = dep.get_to();
             if (dep_to == from_service) {
                 // fail, circular dependency!
-                char nak_rep[] = { DINIT_RP_NAK };
+                char nak_rep[] = { (char)cp_rply::NAK };
                 if (! queue_packet(nak_rep, 1)) return false;
                 rbuf.consume(pkt_size);
                 chklen = 0;
@@ -769,7 +769,7 @@ bool control_conn_t::add_service_dep(bool do_enable)
         }
     }
 
-    char ack_rep[] = { DINIT_RP_ACK };
+    char ack_rep[] = { (char)cp_rply::ACK };
     if (! queue_packet(ack_rep, 1)) return false;
     rbuf.consume(pkt_size);
     chklen = 0;
@@ -799,7 +799,7 @@ bool control_conn_t::rm_service_dep()
     service_record *to_service = find_service_for_key(to_handle);
     if (from_service == nullptr || to_service == nullptr || from_service == to_service) {
         // Service handle is bad
-        char badreq_rep[] = { DINIT_RP_BADREQ };
+        char badreq_rep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreq_rep, 1)) return false;
         bad_conn_close = true;
         return true;
@@ -809,7 +809,7 @@ bool control_conn_t::rm_service_dep()
     int dep_type_int = rbuf[1];
     if (! contains({dependency_type::MILESTONE, dependency_type::REGULAR,
             dependency_type::WAITS_FOR}, dep_type_int)) {
-        char badreqRep[] = { DINIT_RP_BADREQ };
+        char badreqRep[] = { (char)cp_rply::BADREQ };
         if (! queue_packet(badreqRep, 1)) return false;
         bad_conn_close = true;
     }
@@ -819,7 +819,7 @@ bool control_conn_t::rm_service_dep()
     bool did_remove = from_service->rm_dep(to_service, dep_type);
     services->process_queues();
 
-    char ack_rep[] = { did_remove ? (char)DINIT_RP_ACK : (char)DINIT_RP_NAK };
+    char ack_rep[] = { did_remove ? (char)cp_rply::ACK : (char)cp_rply::NAK };
     if (! queue_packet(ack_rep, 1)) return false;
     rbuf.consume(pkt_size);
     chklen = 0;
@@ -845,12 +845,12 @@ bool control_conn_t::process_query_name()
 
     service_record *service = find_service_for_key(handle);
     if (service == nullptr || service->get_name().length() > std::numeric_limits<uint16_t>::max()) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
     // Reply:
-    // 1 byte packet type = DINIT_RP_SERVICENAME
+    // 1 byte packet type = cp_rply::SERVICENAME
     // 1 byte reserved
     // uint16_t length
     // N bytes name
@@ -859,7 +859,7 @@ bool control_conn_t::process_query_name()
     const std::string &name = service->get_name();
     uint16_t name_length = name.length();
     reply.resize(2 + sizeof(uint16_t) + name_length);
-    reply[0] = DINIT_RP_SERVICENAME;
+    reply[0] = (char)cp_rply::SERVICENAME;
     memcpy(reply.data() + 2, &name_length, sizeof(name_length));
     memcpy(reply.data() + 2 + sizeof(uint16_t), name.c_str(), name_length);
 
@@ -874,8 +874,8 @@ bool control_conn_t::process_setenv()
     typename string::size_type eq;
 
     constexpr int pkt_size = 4;
-    char badreqRep[] = { DINIT_RP_BADREQ };
-    char okRep[] = { DINIT_RP_ACK };
+    char badreqRep[] = { (char)cp_rply::BADREQ };
+    char okRep[] = { (char)cp_rply::ACK };
 
     if (rbuf.get_length() < pkt_size) {
         chklen = pkt_size;
@@ -941,7 +941,7 @@ bool control_conn_t::process_set_trigger()
 
     service_record *service = find_service_for_key(handle);
     if (service == nullptr || service->get_type() != service_type_t::TRIGGERED) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
@@ -949,7 +949,7 @@ bool control_conn_t::process_set_trigger()
     tservice->set_trigger(trigger_val != 0);
     services->process_queues();
 
-    char ack_rep[] = { DINIT_RP_ACK };
+    char ack_rep[] = { (char)cp_rply::ACK };
     return queue_packet(ack_rep, 1);
 }
 
@@ -976,13 +976,13 @@ bool control_conn_t::process_catlog()
     if (service == nullptr || (service->get_type() != service_type_t::PROCESS
             && service->get_type() != service_type_t::BGPROCESS
             && service->get_type() != service_type_t::SCRIPTED)) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
     base_process_service *bps = static_cast<base_process_service *>(service);
     if (bps->get_log_mode() != log_type_id::BUFFER) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
@@ -990,7 +990,7 @@ bool control_conn_t::process_catlog()
     const char *bufaddr = buffer_details.first;
     unsigned buflen = buffer_details.second;
 
-    std::vector<char> pkt = { (char)DINIT_RP_SERVICE_LOG, 0 /* flags; reserved for future */ };
+    std::vector<char> pkt = { (char)cp_rply::SERVICE_LOG, 0 /* flags; reserved for future */ };
     pkt.insert(pkt.end(), (char *)(&buflen), (char *)(&buflen + 1));
     pkt.insert(pkt.end(), bufaddr, bufaddr + buflen);
     if ((flags & 1) != 0) {
@@ -1017,33 +1017,33 @@ bool control_conn_t::process_signal()
 
     service_record *service = find_service_for_key(handle);
     if (service == nullptr) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
     // Reply:
-    // 1 byte packet type = DINIT_RP_*
+    // 1 byte packet type = cp_rply::*
 
     pid_t spid = service->get_pid();
     // we probably don't want to kill/signal every process (in the current group),
     // but get_pid() sometimes returns -1 if e.g. service is not 'started'
     if (spid == -1 || spid == 0) {
-        char nak_rep[] = { DINIT_RP_SIGNAL_NOPID };
+        char nak_rep[] = { (char)cp_rply::SIGNAL_NOPID };
         return queue_packet(nak_rep, 1);
     }
     else {
         if (bp_sys::kill(spid, sig_num) != 0) {
             if (errno == EINVAL) {
                 log(loglevel_t::ERROR, "Requested signal not in valid signal range.");
-                char nak_rep[] = { DINIT_RP_SIGNAL_BADSIG };
+                char nak_rep[] = { (char)cp_rply::SIGNAL_BADSIG };
                 return queue_packet(nak_rep, 1);
             }
             log(loglevel_t::ERROR, "Error sending signal to process: ", strerror(errno));
-            char nak_rep[] = { DINIT_RP_SIGNAL_KILLERR };
+            char nak_rep[] = { (char)cp_rply::SIGNAL_KILLERR };
             return queue_packet(nak_rep, 1);
         }
     }
-    char ack_rep[] = { DINIT_RP_ACK };
+    char ack_rep[] = { (char)cp_rply::ACK };
     return queue_packet(ack_rep, 1);
 }
 
@@ -1065,18 +1065,18 @@ bool control_conn_t::process_query_dsc_dir()
 
     service_record *service = find_service_for_key(handle);
     if (service == nullptr || !spare_ok) {
-        char nak_rep[] = { DINIT_RP_NAK };
+        char nak_rep[] = { (char)cp_rply::NAK };
         return queue_packet(nak_rep, 1);
     }
 
     // Reply:
-    // 1 byte packet type = DINIT_RP_SVCDSCDIR
+    // 1 byte packet type = cp_rply::SVCDSCDIR
     // 4 bytes (uint32_t) = directory length (no nul terminator)
     // N bytes            = directory (no nul)
     std::vector<char> reppkt;
     size_t sdir_len = strlen(service->get_service_dsc_dir());
     reppkt.resize(1 + sizeof(uint32_t) + sdir_len);  // packet type, dir length, dir
-    reppkt[0] = DINIT_RP_SVCDSCDIR;
+    reppkt[0] = (char)cp_rply::SVCDSCDIR;
     std::memcpy(&reppkt[1], &sdir_len, sizeof(sdir_len));
     std::memcpy(&reppkt[1 + sizeof(uint32_t)], service->get_service_dsc_dir(), sdir_len);
 
@@ -1093,7 +1093,7 @@ bool control_conn_t::query_load_mech()
         dirload_service_set *dss = static_cast<dirload_service_set *>(services);
         std::vector<char> reppkt;
         reppkt.resize(2 + sizeof(uint32_t) * 2);  // packet type, loader type, packet size, # dirs
-        reppkt[0] = DINIT_RP_LOADER_MECH;
+        reppkt[0] = (char)cp_rply::LOADER_MECH;
         reppkt[1] = SSET_TYPE_DIRLOAD;
 
         // Number of directories in load path:
@@ -1114,7 +1114,7 @@ bool control_conn_t::query_load_mech()
             if (total_size < curpos) {
                 // Overflow. In theory we could now limit to size_t max, but the size must already
                 // be crazy long; let's abort.
-                char ack_rep[] = { DINIT_RP_NAK };
+                char ack_rep[] = { (char)cp_rply::NAK };
                 if (! queue_packet(ack_rep, 1)) return false;
                 return true;
             }
@@ -1126,7 +1126,7 @@ bool control_conn_t::query_load_mech()
             uint32_t new_try_path_size = try_path_size * uint32_t(2u);
             if (new_try_path_size < try_path_size) {
                 // Overflow.
-                char ack_rep[] = { DINIT_RP_NAK };
+                char ack_rep[] = { (char)cp_rply::NAK };
                 return queue_packet(ack_rep, 1);
             }
             try_path_size = new_try_path_size;
@@ -1155,7 +1155,7 @@ bool control_conn_t::query_load_mech()
     }
     else {
         // If we don't know how to deal with the service set type, send a NAK reply:
-        char ack_rep[] = { DINIT_RP_NAK };
+        char ack_rep[] = { (char)cp_rply::NAK };
         return queue_packet(ack_rep, 1);
     }
 }
@@ -1205,7 +1205,7 @@ void control_conn_t::service_event(service_record *service, service_event_t even
             std::vector<char> pkt;
             constexpr int pktsize = 3 + sizeof(key) + STATUS_BUFFER_SIZE;
             pkt.reserve(pktsize);
-            pkt.push_back(DINIT_IP_SERVICEEVENT);
+            pkt.push_back((char)cp_info::SERVICEEVENT);
             pkt.push_back(pktsize);
             char *p = (char *)&key;
             for (unsigned j = 0; j < sizeof(key); j++) {
@@ -1368,7 +1368,7 @@ bool control_conn_t::send_data() noexcept
     if (outbuf.empty() && bad_conn_close) {
         if (oom_close) {
             // Send oom response
-            char oomBuf[] = { DINIT_RP_OOM };
+            char oomBuf[] = { (char)cp_rply::OOM };
             bp_sys::write(iob.get_watched_fd(), oomBuf, 1);
         }
         return true;
@@ -1399,7 +1399,7 @@ bool control_conn_t::send_data() noexcept
         outbuf.pop_front();
         outpkt_index = 0;
         if (oom_close) {
-            // remain active, try to send DINIT_RP_OOM shortly
+            // remain active, try to send cp_rply::OOM shortly
             return false;
         }
         if (outbuf.empty() && bad_conn_close) {

+ 4 - 3
src/dinit-monitor.cc

@@ -170,7 +170,7 @@ int dinit_monitor_main(int argc, char **argv)
                 int pktlen = (unsigned char) rbuffer[1];
                 fill_buffer_to(rbuffer, socknum, pktlen);
 
-                if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
+                if (rbuffer[0] == (char)cp_info::SERVICEEVENT) {
                     handle_t ev_handle;
                     rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
                     service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
@@ -383,7 +383,8 @@ static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p
 {
     using namespace std;
 
-    if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
+    cp_rply reply_pkt_h = (cp_rply)rbuffer[0];
+    if (reply_pkt_h == cp_rply::SERVICERECORD) {
         fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
         rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
         if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
@@ -391,7 +392,7 @@ static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p
         rbuffer.consume(3 + sizeof(*handle_p));
         return 0;
     }
-    else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
+    else if (reply_pkt_h == cp_rply::NOSERVICE) {
         return 1;
     }
     else {

+ 47 - 45
src/dinitctl.cc

@@ -732,7 +732,7 @@ static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t h
 
     wait_for_reply(rbuffer, socknum);
 
-    if (rbuffer[0] != DINIT_RP_SERVICENAME) {
+    if (rbuffer[0] != (char)cp_rply::SERVICENAME) {
         throw cp_read_exception{0};
     }
 
@@ -814,7 +814,7 @@ static int wait_service_state(int socknum, cpbuffer_t &rbuffer, handle_t handle,
             unsigned pktlen = (unsigned char) rbuffer[1];
             fill_buffer_to(rbuffer, socknum, pktlen);
 
-            if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
+            if (rbuffer[0] == (char)cp_info::SERVICEEVENT) {
                 // earlier versions do not include status info, the size in that case is base_pkt_size:
                 constexpr unsigned base_pkt_size = 2 + sizeof(handle_t) + 1;
                 if (pktlen < base_pkt_size) {
@@ -951,9 +951,9 @@ static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *serv
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        auto reply_pkt_h = rbuffer[0];
+        cp_rply reply_pkt_h = (cp_rply)rbuffer[0];
         rbuffer.consume(1); // consume header
-        if (reply_pkt_h == DINIT_RP_ALREADYSS) {
+        if (reply_pkt_h == cp_rply::ALREADYSS) {
             bool already = (state == wanted_state);
             if (verbose) {
                 cout << "Service " << (already ? "(already) " : "")
@@ -961,15 +961,15 @@ static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *serv
             }
             return 0; // success!
         }
-        if (reply_pkt_h == DINIT_RP_PINNEDSTARTED) {
+        if (reply_pkt_h == cp_rply::PINNEDSTARTED) {
             cerr << "dinitctl: cannot stop service '" << service_name << "' as it is pinned started\n";
             return 1;
         }
-        if (reply_pkt_h == DINIT_RP_PINNEDSTOPPED) {
+        if (reply_pkt_h == cp_rply::PINNEDSTOPPED) {
             cerr << "dinitctl: cannot start service '" << service_name << "' as it is pinned stopped\n";
             return 1;
         }
-        if (reply_pkt_h == DINIT_RP_DEPENDENTS && pcommand == cp_cmd::STOPSERVICE) {
+        if (reply_pkt_h == cp_rply::DEPENDENTS && pcommand == cp_cmd::STOPSERVICE) {
             cerr << "dinitctl: cannot stop service '" << service_name << "' due to the following dependents:\n";
             if (command != ctl_cmd::RESTART_SERVICE) {
                 cerr << "(only direct dependents are listed. Exercise caution before using '--force' !!)\n";
@@ -996,7 +996,7 @@ static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *serv
             cerr << "\n";
             return 1;
         }
-        if (reply_pkt_h == DINIT_RP_NAK && command == ctl_cmd::RESTART_SERVICE) {
+        if (reply_pkt_h == cp_rply::NAK && command == ctl_cmd::RESTART_SERVICE) {
             if (ignore_unstarted) {
                 if (verbose) {
                     cout << "Service '" << service_name << "' is not currently started.\n";
@@ -1006,15 +1006,15 @@ static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *serv
             cerr << "dinitctl: cannot restart service; service not started.\n";
             return 1;
         }
-        if (reply_pkt_h == DINIT_RP_NAK && command == ctl_cmd::WAKE_SERVICE) {
+        if (reply_pkt_h == cp_rply::NAK && command == ctl_cmd::WAKE_SERVICE) {
             cerr << "dinitctl: service has no active dependents, cannot wake.\n";
             return 1;
         }
-        if (reply_pkt_h == DINIT_RP_SHUTTINGDOWN) {
+        if (reply_pkt_h == cp_rply::SHUTTINGDOWN) {
             cerr << "dinitctl: cannot start/restart/wake service, shutdown is in progress.\n";
             return 1;
         }
-        if (reply_pkt_h != DINIT_RP_ACK && reply_pkt_h != DINIT_RP_ALREADYSS) {
+        if (reply_pkt_h != cp_rply::ACK && reply_pkt_h != cp_rply::ALREADYSS) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -1057,7 +1057,8 @@ static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p
 {
     using namespace std;
     
-    if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
+    cp_rply reply_pkt_h = (cp_rply)rbuffer[0];
+    if (reply_pkt_h == cp_rply::SERVICERECORD) {
         fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
         rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
         if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
@@ -1065,21 +1066,21 @@ static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p
         rbuffer.consume(3 + sizeof(*handle_p));
         return 0;
     }
-    else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
+    else if (reply_pkt_h == cp_rply::NOSERVICE) {
         if (write_error) {
             cerr << "dinitctl: failed to find service description.\n";
             cerr << "dinitctl: check service description file exists / service name spelling.\n";
         }
         return 1;
     }
-    else if (rbuffer[0] == DINIT_RP_SERVICE_DESC_ERR) {
+    else if (reply_pkt_h == cp_rply::SERVICE_DESC_ERR) {
         if (write_error) {
             cerr << "dinitctl: error in service description.\n";
             cerr << "dinitctl: try 'dinitcheck <service-name>' or check log for more information.\n";
         }
         return 1;
     }
-    else if (rbuffer[0] == DINIT_RP_SERVICE_LOAD_ERR) {
+    else if (reply_pkt_h == cp_rply::SERVICE_LOAD_ERR) {
         if (write_error) {
             cerr << "dinitctl: error loading service (or dependency of service).\n";
             cerr << "dinitctl: try 'dinitcheck <service-name>' or check log for more information.\n";
@@ -1110,7 +1111,7 @@ static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_n
         write_all_x(socknum, m);
         
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (char)cp_rply::ACK) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -1135,7 +1136,7 @@ static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_
 
     handle_t handle;
 
-    if (rbuffer[0] == DINIT_RP_NOSERVICE) {
+    if (rbuffer[0] == (char)cp_rply::NOSERVICE) {
         cerr << "dinitctl: service not loaded." << endl;
         return 1;
     }
@@ -1152,12 +1153,12 @@ static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] == DINIT_RP_NAK) {
+        if (rbuffer[0] == (char)cp_rply::NAK) {
             cerr << "dinitctl: could not unload service; service not stopped, or is a dependency of "
                     "other service." << endl;
             return 1;
         }
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (char)cp_rply::ACK) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -1182,7 +1183,7 @@ static int reload_service(int socknum, cpbuffer_t &rbuffer, const char *service_
 
     handle_t handle;
 
-    if (rbuffer[0] == DINIT_RP_NOSERVICE) {
+    if (rbuffer[0] == (char)cp_rply::NOSERVICE) {
         rbuffer.consume(1);
         // If the service isn't loaded yet at all, just do a basic load:
         if (issue_load_service(socknum, service_name, false) == 1) {
@@ -1213,12 +1214,12 @@ static int reload_service(int socknum, cpbuffer_t &rbuffer, const char *service_
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] == DINIT_RP_NAK) {
+        if (rbuffer[0] == (char)cp_rply::NAK) {
             cerr << "dinitctl: could not reload service; service in wrong state, incompatible change, "
                     "or bad service description." << endl;
             return 1;
         }
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (char)cp_rply::ACK) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -1239,7 +1240,7 @@ static int list_services(int socknum, cpbuffer_t &rbuffer)
     write_all_x(socknum, cmdbuf, 1);
 
     wait_for_reply(rbuffer, socknum);
-    while (rbuffer[0] == DINIT_RP_SVCINFO) {
+    while (rbuffer[0] == (char)cp_rply::SVCINFO) {
         int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
         fill_buffer_to(rbuffer, socknum, hdrsize);
         unsigned name_len = (unsigned char)rbuffer[1];
@@ -1342,7 +1343,7 @@ static int list_services(int socknum, cpbuffer_t &rbuffer)
         wait_for_reply(rbuffer, socknum);
     }
 
-    if (rbuffer[0] != DINIT_RP_LISTDONE) {
+    if (rbuffer[0] != (char)cp_rply::LISTDONE) {
         cerr << "dinitctl: control socket protocol error" << endl;
         return 1;
     }
@@ -1364,7 +1365,7 @@ static int service_status(int socknum, cpbuffer_t &rbuffer, const char *service_
 
     handle_t handle;
 
-    if (rbuffer[0] == DINIT_RP_NOSERVICE) {
+    if (rbuffer[0] == (char)cp_rply::NOSERVICE) {
         if (is_status) {
             cerr << "dinitctl: service not loaded." << endl;
         }
@@ -1383,7 +1384,7 @@ static int service_status(int socknum, cpbuffer_t &rbuffer, const char *service_
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] != DINIT_RP_SERVICESTATUS) {
+        if (rbuffer[0] != (char)cp_rply::SERVICESTATUS) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -1574,7 +1575,7 @@ static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
     wait_for_reply(rbuffer, socknum);
 
     // check reply
-    if (rbuffer[0] == DINIT_RP_NAK) {
+    if (rbuffer[0] == (char)cp_rply::NAK) {
         if (add) {
             cerr << "dinitctl: could not add dependency: circular dependency or wrong state" << endl;
         }
@@ -1583,7 +1584,7 @@ static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
         }
         return 1;
     }
-    if (rbuffer[0] != DINIT_RP_ACK) {
+    if (rbuffer[0] != (char)cp_rply::ACK) {
         cerr << "dinitctl: control socket protocol error" << endl;
         return 1;
     }
@@ -1607,7 +1608,7 @@ static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer, bool verbose)
 
     wait_for_reply(rbuffer, socknum);
 
-    if (rbuffer[0] != DINIT_RP_ACK) {
+    if (rbuffer[0] != (char)cp_rply::ACK) {
         cerr << "dinitctl: control socket protocol error" << endl;
         return 1;
     }
@@ -1643,7 +1644,7 @@ static std::vector<std::string> get_service_description_dirs(int socknum, cpbuff
 
     wait_for_reply(rbuffer, socknum);
 
-    if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
+    if (rbuffer[0] != (char)cp_rply::LOADER_MECH) {
         cerr << "dinitctl: control socket protocol error" << endl;
         return {};
     }
@@ -1700,7 +1701,7 @@ static std::string get_service_description_dir(int socknum, cpbuffer_t &rbuffer,
     write_all_x(socknum, m);
     wait_for_reply(rbuffer, socknum);
 
-    if (rbuffer[0] != DINIT_RP_SVCDSCDIR) {
+    if (rbuffer[0] != (char)cp_rply::SVCDSCDIR) {
         throw dinit_protocol_error();
     }
     rbuffer.consume(1);
@@ -1907,7 +1908,7 @@ static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, service_dir_
         wait_for_reply(rbuffer, socknum);
 
         // check reply
-        if (rbuffer[0] == DINIT_RP_NAK) {
+        if (rbuffer[0] == (char)cp_rply::NAK) {
             if (enable) {
                 cerr << "dinitctl: could not enable service: possible circular dependency" << endl;
             }
@@ -1916,7 +1917,7 @@ static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, service_dir_
             }
             return 1;
         }
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (char)cp_rply::ACK) {
             cerr << "dinitctl: control socket protocol error" << endl;
             return 1;
         }
@@ -1978,7 +1979,7 @@ static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, service_dir_
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] != DINIT_RP_SERVICESTATUS) {
+        if (rbuffer[0] != (char)cp_rply::SERVICESTATUS) {
             cerr << "dinitctl: protocol error." << endl;
             return 1;
         }
@@ -2052,10 +2053,10 @@ static int do_setenv(int socknum, cpbuffer_t &rbuffer, std::vector<const char *>
         // send
         write_all_x(socknum, buf.data(), buf.size());
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] == DINIT_RP_BADREQ) {
+        if (rbuffer[0] == (char)cp_rply::BADREQ) {
             cerr << "dinitctl: failed to export environment." << endl;
             return 1;
-        } else if (rbuffer[0] != DINIT_RP_ACK) {
+        } else if (rbuffer[0] != (char)cp_rply::ACK) {
             throw dinit_protocol_error();
         }
         rbuffer.consume(1);
@@ -2082,11 +2083,11 @@ static int trigger_service(int socknum, cpbuffer_t &rbuffer, const char *service
         write_all_x(socknum, m);
 
         wait_for_reply(rbuffer, socknum);
-        if (rbuffer[0] == DINIT_RP_NAK) {
+        if (rbuffer[0] == (char)cp_rply::NAK) {
             cerr << "dinitctl: cannot trigger a service that is not of 'triggered' type.\n";
             return 1;
         }
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (char)cp_rply::ACK) {
             cerr << "dinitctl: protocol error.\n";
             return 1;
         }
@@ -2114,24 +2115,25 @@ static int signal_send(int socknum, cpbuffer_t &rbuffer, const char *service_nam
     write_all_x(socknum, m);
 
     wait_for_reply(rbuffer, socknum);
-    if (rbuffer[0] == DINIT_RP_NAK) {
+    cp_rply reply_pkt_h = (cp_rply)rbuffer[0];
+    if (reply_pkt_h == cp_rply::NAK) {
         cerr << "dinitctl: cannot send signal to service.\n";
         return 1;
     }
-    if (rbuffer[0] == DINIT_RP_SIGNAL_NOPID) {
+    if (reply_pkt_h == cp_rply::SIGNAL_NOPID) {
         cerr << "dinitctl: could not get vaild PID of service; service is not process, "
         "service in wrong state." << endl;
         return 1;
     }
-    if (rbuffer[0] == DINIT_RP_SIGNAL_BADSIG) {
+    if (reply_pkt_h == cp_rply::SIGNAL_BADSIG) {
         cerr << "dinitctl: provided signal was invalid.\n";
         return 1;
     }
-    if (rbuffer[0] == DINIT_RP_SIGNAL_KILLERR) {
+    if (reply_pkt_h == cp_rply::SIGNAL_KILLERR) {
         cerr << "dinitctl: failed sending signal to service.\n";
         return 1;
     }
-    if (rbuffer[0] != DINIT_RP_ACK) {
+    if (reply_pkt_h != cp_rply::ACK) {
         cerr << "dinitctl: protocol error.\n";
         return 1;
     }
@@ -2179,11 +2181,11 @@ static int cat_service_log(int socknum, cpbuffer_t &rbuffer, const char *service
     write_all_x(socknum, m);
 
     wait_for_reply(rbuffer, socknum);
-    if (rbuffer[0] == DINIT_RP_NAK) {
+    if (rbuffer[0] == (char)cp_rply::NAK) {
         cerr << "dinitctl: cannot cat log for service not configured to buffer output.\n";
         return 1;
     }
-    if (rbuffer[0] != DINIT_RP_SERVICE_LOG) {
+    if (rbuffer[0] != (char)cp_rply::SERVICE_LOG) {
         cerr << "dinitctl: protocol error.\n";
         return 1;
     }

+ 56 - 57
src/includes/control-cmds.h

@@ -6,8 +6,6 @@
 // Dinit control command packet types
 
 // Requests:
-
-
 enum class cp_cmd :dinit_cptypes::cp_cmd_t {
     // Query protocol version:
     QUERYVERSION = 0,
@@ -73,81 +71,82 @@ enum class cp_cmd :dinit_cptypes::cp_cmd_t {
 };
 
 // Replies:
+enum class cp_rply :dinit_cptypes::cp_rply_t {
+    // Reply: ACK/NAK to request
+    ACK = 50,
+    NAK = 51,
 
-// Reply: ACK/NAK to request
-constexpr static int DINIT_RP_ACK = 50;
-constexpr static int DINIT_RP_NAK = 51;
-
-// Request was bad (connection will be closed)
-constexpr static int DINIT_RP_BADREQ = 52;
-
-// Connection being closed due to out-of-memory condition
-constexpr static int DINIT_RP_OOM = 53;
+    // Request was bad (connection will be closed)
+    BADREQ = 52,
 
-// Start service replies:
-constexpr static int DINIT_RP_SERVICELOADERR = 54;
-constexpr static int DINIT_RP_SERVICEOOM = 55; // couldn't start due to out-of-memory
+    // Connection being closed due to out-of-memory condition
+    OOM = 53,
 
-// Not (any longer?) used
-//constexpr static int DINIT_RP_SSISSUED = 56;  // service start/stop was issued (includes 4-byte service handle)
-//constexpr static int DINIT_RP_SSREDUNDANT = 57;  // service was already started/stopped (or for stop, not loaded)
+    // Start service replies:
+    SERVICELOADERR = 54,
+    SERVICEOOM = 55, // couldn't start due to out-of-memory
 
-// Query version response:
-constexpr static int DINIT_RP_CPVERSION = 58;
+    // Not (any longer?) used
+    //SSISSUED = 56,  // service start/stop was issued (includes 4-byte service handle)
+    //SSREDUNDANT = 57,  // service was already started/stopped (or for stop, not loaded)
 
-// Service record loaded/found
-constexpr static int DINIT_RP_SERVICERECORD = 59;
-//     followed by 4-byte service handle, 1-byte service state
+    // Query version response:
+    CPVERSION = 58,
 
-// Couldn't find/load service
-constexpr static int DINIT_RP_NOSERVICE = 60;
+    // Service record loaded/found
+    SERVICERECORD = 59,
+    //     followed by 4-byte service handle, 1-byte service state
 
-// Service is already started/stopped
-constexpr static int DINIT_RP_ALREADYSS = 61;
+    // Couldn't find/load service
+    NOSERVICE = 60,
 
-// Information on a service / list complete:
-constexpr static int DINIT_RP_SVCINFO = 62;
-constexpr static int DINIT_RP_LISTDONE = 63;
+    // Service is already started/stopped
+    ALREADYSS = 61,
 
-// Service loader information:
-constexpr static int DINIT_RP_LOADER_MECH = 64;
+    // Information on a service / list complete:
+    SVCINFO = 62,
+    LISTDONE = 63,
 
-// Dependent services prevent stopping/restarting. Includes size_t count, handle_t * N handles.
-constexpr static int DINIT_RP_DEPENDENTS = 65;
+    // Service loader information:
+    LOADER_MECH = 64,
 
-// Service name:
-constexpr static int DINIT_RP_SERVICENAME = 66;
+    // Dependent services prevent stopping/restarting. Includes size_t count, handle_t * N handles.
+    DEPENDENTS = 65,
 
-// Service is pinned stopped/started:
-constexpr static int DINIT_RP_PINNEDSTOPPED = 67;
-constexpr static int DINIT_RP_PINNEDSTARTED = 68;
+    // Service name:
+    SERVICENAME = 66,
 
-// Shutdown is in progress, can't start/restart/wake service:
-constexpr static int DINIT_RP_SHUTTINGDOWN = 69;
+    // Service is pinned stopped/started:
+    PINNEDSTOPPED = 67,
+    PINNEDSTARTED = 68,
 
-// Service status:
-constexpr static int DINIT_RP_SERVICESTATUS = 70;
+    // Shutdown is in progress, can't start/restart/wake service:
+    SHUTTINGDOWN = 69,
 
-// Service description error:
-constexpr static int DINIT_RP_SERVICE_DESC_ERR = 71;
-// Service load error (general):
-constexpr static int DINIT_RP_SERVICE_LOAD_ERR = 72;
+    // Service status:
+    SERVICESTATUS = 70,
 
-// Service log:
-constexpr static int DINIT_RP_SERVICE_LOG = 73;
+    // Service description error:
+    SERVICE_DESC_ERR = 71,
+    // Service load error (general):
+    SERVICE_LOAD_ERR = 72,
 
-// Signal replies:
-constexpr static int DINIT_RP_SIGNAL_NOPID = 74;
-constexpr static int DINIT_RP_SIGNAL_BADSIG = 75;
-constexpr static int DINIT_RP_SIGNAL_KILLERR = 76;
+    // Service log:
+    SERVICE_LOG = 73,
 
-// Service description directory:
-constexpr static int DINIT_RP_SVCDSCDIR = 77;
+    // Signal replies:
+    SIGNAL_NOPID = 74,
+    SIGNAL_BADSIG = 75,
+    SIGNAL_KILLERR = 76,
 
+    // Service description directory:
+    SVCDSCDIR = 77,
+};
 
 // Information (out-of-band):
-
-// Service event occurred (4-byte service handle, 1 byte event code)
-constexpr static int DINIT_IP_SERVICEEVENT = 100;
+enum class cp_info :dinit_cptypes::cp_info_t {
+    // Service event occurred (4-byte service handle, 1 byte event code)
+    SERVICEEVENT = 100,
+};
 
 #endif

+ 2 - 0
src/includes/control-datatypes.h

@@ -9,6 +9,8 @@ namespace dinit_cptypes {
     using handle_t = uint32_t;
     using trigger_val_t = uint8_t;
     using cp_cmd_t = uint8_t;
+    using cp_rply_t = uint8_t;
+    using cp_info_t = uint8_t;
 } // namespace dinit_cptypes
 
 #endif

+ 3 - 2
src/includes/dinit-client.h

@@ -237,16 +237,17 @@ template <typename Buf> inline void write_all_x(int fd, const Buf &b)
 // throws an exception on protocol mismatch or error.
 inline uint16_t check_protocol_version(int minversion, int version, cpbuffer_t &rbuffer, int fd)
 {
+    using namespace dinit_cptypes;
     constexpr int bufsize = 1;
     char buf[bufsize] = { (char)cp_cmd::QUERYVERSION };
     write_all_x(fd, buf, bufsize);
 
     wait_for_reply(rbuffer, fd);
-    if (rbuffer[0] != DINIT_RP_CPVERSION) {
+    if (rbuffer[0] != (char)cp_rply::CPVERSION) {
         throw cp_read_exception{0};
     }
 
-    // DINIT_RP_CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
+    // cp_rply::CVERSION, (2 byte) minimum compatible version, (2 byte) actual version
     constexpr int rbufsize = 1 + 2 * sizeof(uint16_t);
     fill_buffer_to(rbuffer, fd, rbufsize);
     uint16_t rminversion;

+ 1 - 1
src/shutdown.cc

@@ -361,7 +361,7 @@ int main(int argc, char **argv)
     
         wait_for_reply(rbuffer, socknum);
         
-        if (rbuffer[0] != DINIT_RP_ACK) {
+        if (rbuffer[0] != (dinit_cptypes::cp_rply_t)cp_rply::ACK) {
             cerr << "shutdown: control socket protocol error" << endl;
             return 1;
         }

+ 40 - 40
src/tests/cptests/cptests.cc

@@ -52,7 +52,7 @@ void cptest_queryver()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 5);
-    assert(wdata[0] == DINIT_RP_CPVERSION);
+    assert(wdata[0] == (char)cp_rply::CPVERSION);
 
     delete cc;
 }
@@ -79,7 +79,7 @@ void cptest_listservices()
     //event_loop.regd_bidi_watchers[fd]->write_ready(event_loop, fd);
 
     // We expect, for each service:
-    // (1 byte)   DINIT_RP_SVCINFO
+    // (1 byte)   cp_rply::SVCINFO
     // (1 byte)   service name length
     // (1 byte)   state
     // (1 byte)   target state
@@ -96,7 +96,7 @@ void cptest_listservices()
 
     int pos = 0;
     for (int i = 0; i < 3; i++) {
-        assert(wdata[pos++] == DINIT_RP_SVCINFO);
+        assert(wdata[pos++] == (char)cp_rply::SVCINFO);
         unsigned char name_len_c = wdata[pos++];
         pos += 6;
 
@@ -133,13 +133,13 @@ static handle_t  find_service(int fd, const char *service_name,
     bp_sys::extract_written_data(fd, wdata);
 
     // We expect:
-    // (1 byte)   DINIT_RP_SERVICERECORD
+    // (1 byte)   cp_rply::SERVICERECORD
     // (1 byte)   state
     // (handle_t) handle
     // (1 byte)   target state
 
     assert(wdata.size() == 3 + sizeof(handle_t));
-    assert(wdata[0] == DINIT_RP_SERVICERECORD);
+    assert(wdata[0] == (char)cp_rply::SERVICERECORD);
     service_state_t s = static_cast<service_state_t>(wdata[1]);
     assert(s == expected_state);
     service_state_t ts = static_cast<service_state_t>(wdata[6]);
@@ -227,13 +227,13 @@ void cptest_findservice3()
     event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd);
 
     // We expect:
-    // (1 byte)   DINIT_RP_NOSERVICE
+    // (1 byte)   cp_rply::NOSERVICE
 
     std::vector<char> wdata;
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_NOSERVICE);
+    assert(wdata[0] == (char)cp_rply::NOSERVICE);
 
     delete cc;
 }
@@ -286,7 +286,7 @@ void cptest_loadservice()
     event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd);
 
     // We expect:
-    // (1 byte)   DINIT_RP_SERVICERECORD
+    // (1 byte)   cp_rply::SERVICERECORD
     // (1 byte)   state
     // (handle_t) handle
     // (1 byte)   target state
@@ -295,7 +295,7 @@ void cptest_loadservice()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 3 + sizeof(handle_t));
-    assert(wdata[0] == DINIT_RP_SERVICERECORD);
+    assert(wdata[0] == (char)cp_rply::SERVICERECORD);
     service_state_t s = static_cast<service_state_t>(wdata[1]);
     assert(s == service_state_t::STOPPED);
     service_state_t ts = static_cast<service_state_t>(wdata[6]);
@@ -313,7 +313,7 @@ void cptest_loadservice()
     event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd);
 
     // We expect:
-    // (1 byte)   DINIT_RP_SERVICERECORD
+    // (1 byte)   cp_rply::SERVICERECORD
     // (1 byte)   state
     // (handle_t) handle
     // (1 byte)   target state
@@ -321,7 +321,7 @@ void cptest_loadservice()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 3 + sizeof(handle_t));
-    assert(wdata[0] == DINIT_RP_SERVICERECORD);
+    assert(wdata[0] == (char)cp_rply::SERVICERECORD);
     s = static_cast<service_state_t>(wdata[1]);
     assert(s == service_state_t::STOPPED);
     ts = static_cast<service_state_t>(wdata[6]);
@@ -361,7 +361,7 @@ void cptest_startstop()
     std::vector<char> wdata;
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1 + 7 + STATUS_BUFFER_SIZE /* ACK reply + info packet */);
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     // packetsize, key (handle), event
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     handle_t ip_h;
@@ -371,7 +371,7 @@ void cptest_startstop()
 
     constexpr unsigned reply_start = 7 + STATUS_BUFFER_SIZE;
     // we get ALREADYSS since it started immediately:
-    assert(wdata[reply_start] == DINIT_RP_ALREADYSS);
+    assert(wdata[reply_start] == (char)cp_rply::ALREADYSS);
     assert(s1->get_state() == service_state_t::STARTED);
 
     // Issue stop:
@@ -384,14 +384,14 @@ void cptest_startstop()
 
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1 + 7 + STATUS_BUFFER_SIZE);
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     // packetsize, key (handle), event
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(ip_h), reinterpret_cast<char *>(&ip_h));
     assert(ip_h == h);
     assert(wdata[6] == static_cast<int>(service_event_t::STOPPED));
     // we get ALREADYSS since it stopped immediately:
-    assert(wdata[reply_start] == DINIT_RP_ALREADYSS);
+    assert(wdata[reply_start] == (char)cp_rply::ALREADYSS);
     assert(s1->get_state() == service_state_t::STOPPED);
 
     delete cc;
@@ -426,8 +426,8 @@ void cptest_start_pinned()
 
     std::vector<char> wdata;
     bp_sys::extract_written_data(fd, wdata);
-    assert(wdata.size() == 1 /* DINIT_RP_PINNEDSTOPPED */);
-    assert(wdata[0] == DINIT_RP_PINNEDSTOPPED);
+    assert(wdata.size() == 1 /* cp_rply::PINNEDSTOPPED */);
+    assert(wdata[0] == (char)cp_rply::PINNEDSTOPPED);
 
     delete cc;
 }
@@ -470,12 +470,12 @@ void cptest_gentlestop()
     bp_sys::extract_written_data(fd, wdata);
 
     // We expect:
-    // 1 byte: DINIT_RP_DEPENDENTS
+    // 1 byte: cp_rply::DEPENDENTS
     // size_t: number of handles (N)
     // N * handle_t: handles for dependents that would be stopped
 
     assert(wdata.size() == (1 + sizeof(size_t) + sizeof(handle_t)));
-    assert(wdata[0] == DINIT_RP_DEPENDENTS);
+    assert(wdata[0] == (char)cp_rply::DEPENDENTS);
 
     size_t nhandles;
     memcpy(&nhandles, wdata.data() + 1, sizeof(nhandles));
@@ -520,13 +520,13 @@ void cptest_queryname()
     bp_sys::extract_written_data(fd, wdata);
 
     // We expect:
-    // 1 byte packet type = DINIT_RP_SERVICENAME
+    // 1 byte packet type = cp_rply::SERVICENAME
     // 1 byte reserved
     // uint16_t length
     // N bytes name
 
     assert(wdata.size() == (2 + sizeof(uint16_t) + strlen(test1_name)));
-    assert(wdata[0] == DINIT_RP_SERVICENAME);
+    assert(wdata[0] == (char)cp_rply::SERVICENAME);
     assert(wdata[1] == 0);
     uint16_t len;
     memcpy(&len, wdata.data() + 2, sizeof(uint16_t));
@@ -569,7 +569,7 @@ void cptest_unload()
     std::vector<char> wdata;
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_NAK);
+    assert(wdata[0] == (char)cp_rply::NAK);
 
 
     handle_t h2 = find_service(fd, service_name2, service_state_t::STOPPED,
@@ -588,7 +588,7 @@ void cptest_unload()
     // We should receive ACK:
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_ACK);
+    assert(wdata[0] == (char)cp_rply::ACK);
 
     // Now try to unload s1 again:
 
@@ -603,7 +603,7 @@ void cptest_unload()
     // We should receive ACK:
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_ACK);
+    assert(wdata[0] == (char)cp_rply::ACK);
 
     // If we try to FIND service 1 now, it should not be there:
     cmd = { (char)cp_cmd::FINDSERVICE };
@@ -618,7 +618,7 @@ void cptest_unload()
 
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_NOSERVICE);
+    assert(wdata[0] == (char)cp_rply::NOSERVICE);
 
     delete cc;
 }
@@ -656,7 +656,7 @@ void cptest_addrmdeps()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_ACK);
+    assert(wdata[0] == (char)cp_rply::ACK);
 
     // Issue start for S1. S2 should also start:
     cmd = { (char)cp_cmd::STARTSERVICE, 0 /* don't pin */ };
@@ -720,7 +720,7 @@ void cptest_enableservice()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1 + 7 + STATUS_BUFFER_SIZE /* ACK reply + info packet */);
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     // packetsize, key (handle), event
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     handle_t ip_h;
@@ -729,7 +729,7 @@ void cptest_enableservice()
     assert(wdata[6] == static_cast<int>(service_event_t::STARTED));
 
     // and then the ack:
-    assert(wdata[7 + STATUS_BUFFER_SIZE] == DINIT_RP_ACK);
+    assert(wdata[7 + STATUS_BUFFER_SIZE] == (char)cp_rply::ACK);
 
     sset.process_queues();
 
@@ -778,7 +778,7 @@ void cptest_restart()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1 /* NAK reply, wrong state */);
-    assert(wdata[0] == DINIT_RP_NAK);
+    assert(wdata[0] == (char)cp_rply::NAK);
 
     // Start the service now:
     s1->start();
@@ -794,13 +794,13 @@ void cptest_restart()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 7 + STATUS_BUFFER_SIZE + 1);  // info packet (service stopped) + ACK
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     handle_t ip_h;
     std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(ip_h), reinterpret_cast<char *>(&ip_h));
     assert(ip_h == h);
     assert(wdata[6] == static_cast<int>(service_event_t::STOPPED));
-    assert(wdata[7 + STATUS_BUFFER_SIZE] == DINIT_RP_ACK);
+    assert(wdata[7 + STATUS_BUFFER_SIZE] == (char)cp_rply::ACK);
 
     sset.process_queues();
     assert(s1->get_state() == service_state_t::STARTING);
@@ -812,7 +812,7 @@ void cptest_restart()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 7 + STATUS_BUFFER_SIZE);  /* info packet */
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     // packetsize, key (handle), event
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(ip_h), reinterpret_cast<char *>(&ip_h));
@@ -861,7 +861,7 @@ void cptest_wake()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1 + 7 + STATUS_BUFFER_SIZE /* ACK reply + info packet */);
-    assert(wdata[0] == DINIT_IP_SERVICEEVENT);
+    assert(wdata[0] == (char)cp_info::SERVICEEVENT);
     // packetsize, key (handle), event
     assert(wdata[1] == 7 + STATUS_BUFFER_SIZE);
     handle_t ip_h;
@@ -870,7 +870,7 @@ void cptest_wake()
     assert(wdata[6] == static_cast<int>(service_event_t::STARTED));
 
     // and then the ack (already started):
-    assert(wdata[7 + STATUS_BUFFER_SIZE] == DINIT_RP_ALREADYSS);
+    assert(wdata[7 + STATUS_BUFFER_SIZE] == (char)cp_rply::ALREADYSS);
 
     // now stop s2 (and therefore s1):
     s2->stop(true);
@@ -890,7 +890,7 @@ void cptest_wake()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_NAK);
+    assert(wdata[0] == (char)cp_rply::NAK);
 
     delete cc;
 }
@@ -930,11 +930,11 @@ void cptest_servicestatus()
 
     constexpr static int STATUS_BUFFER_SIZE = 6 + ((sizeof(pid_t) > sizeof(int)) ? sizeof(pid_t) : sizeof(int));
 
-    // 1 byte: DINIT_RP_SERVICESTATUS
+    // 1 byte: cp_rply::SERVICESTATUS
     // 1 byte: reserved
     // STATUS_BUFFER_SIZE bytes: status
     assert(wdata.size() == (2 + STATUS_BUFFER_SIZE));
-    assert(wdata[0] == DINIT_RP_SERVICESTATUS);
+    assert(wdata[0] == (char)cp_rply::SERVICESTATUS);
     assert(wdata[2] == (int)service_state_t::STOPPED); // state
     assert(wdata[3] == (int)service_state_t::STOPPED); // target state
     assert(wdata[4] == 0); // various flags
@@ -950,7 +950,7 @@ void cptest_servicestatus()
     bp_sys::extract_written_data(fd, wdata);
 
     assert(wdata.size() == (2 + STATUS_BUFFER_SIZE));
-    assert(wdata[0] == DINIT_RP_SERVICESTATUS);
+    assert(wdata[0] == (char)cp_rply::SERVICESTATUS);
     assert(wdata[2] == (int)service_state_t::STARTED); // state
     assert(wdata[3] == (int)service_state_t::STARTED); // target state
     assert(wdata[4] == 8); // various flags; 8 = marked active
@@ -1002,7 +1002,7 @@ void cptest_sendsignal()
     std::vector<char> wdata;
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_ACK);
+    assert(wdata[0] == (char)cp_rply::ACK);
 
     assert(bp_sys::last_sig_sent == SIGHUP);
 
@@ -1021,7 +1021,7 @@ void cptest_sendsignal()
     wdata.clear();
     bp_sys::extract_written_data(fd, wdata);
     assert(wdata.size() == 1);
-    assert(wdata[0] == DINIT_RP_ACK);
+    assert(wdata[0] == (char)cp_rply::ACK);
 
     assert(bp_sys::last_sig_sent == SIGILL);