Large refactoring of dinitctl.
[oweals/dinit.git] / src / control.cc
index 825e8f386fbc6456d59c7f837a91a2c6e7eb1d2a..5cc8798caea449421fa299bd0463a4514a49da9f 100644 (file)
@@ -51,7 +51,7 @@ bool ControlConn::processPacket()
         char outbuf[] = { DINIT_RP_BADREQ };
         if (! queuePacket(outbuf, 1)) return false;
         bad_conn_close = true;
-        iob.setWatchFlags(out_events);
+        iob.setWatches(OUT_EVENTS);
     }
     return true;
 }
@@ -75,7 +75,7 @@ bool ControlConn::processFindLoad(int pktType)
         char badreqRep[] = { DINIT_RP_BADREQ };
         if (! queuePacket(badreqRep, 1)) return false;
         bad_conn_close = true;
-        iob.setWatchFlags(out_events);
+        iob.setWatches(OUT_EVENTS);
         return true;
     }
     
@@ -151,7 +151,7 @@ bool ControlConn::processStartStop(int pktType)
         char badreqRep[] = { DINIT_RP_BADREQ };
         if (! queuePacket(badreqRep, 1)) return false;
         bad_conn_close = true;
-        iob.setWatchFlags(out_events);
+        iob.setWatches(OUT_EVENTS);
         return true;
     }
     else {
@@ -225,7 +225,7 @@ bool ControlConn::processUnpinService()
         char badreqRep[] = { DINIT_RP_BADREQ };
         if (! queuePacket(badreqRep, 1)) return false;
         bad_conn_close = true;
-        iob.setWatchFlags(out_events);
+        iob.setWatches(OUT_EVENTS);
         return true;
     }
     else {
@@ -268,7 +268,7 @@ ControlConn::handle_t ControlConn::allocateServiceHandle(ServiceRecord *record)
 
 bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
 {
-    int in_flag = bad_conn_close ? 0 : in_events;
+    int in_flag = bad_conn_close ? 0 : IN_EVENTS;
     bool was_empty = outbuf.empty();
 
     // If the queue is empty, we can try to write the packet out now rather than queueing it.
@@ -277,12 +277,10 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
         int wr = write(iob.fd, pkt, size);
         if (wr == -1) {
             if (errno == EPIPE) {
-                delete this;
                 return false;
             }
             if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
                 // TODO log error
-                delete this;
                 return false;
             }
             // EAGAIN etc: fall through to below
@@ -290,7 +288,7 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
         else {
             if ((unsigned)wr == size) {
                 // Ok, all written.
-                iob.setWatchFlags(in_flag);
+                iob.setWatches(in_flag);
                 return true;
             }
             pkt += wr;
@@ -301,7 +299,7 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
     // Create a vector out of the (remaining part of the) packet:
     try {
         outbuf.emplace_back(pkt, pkt + size);
-        iob.setWatchFlags(in_flag | out_events);
+        iob.setWatches(in_flag | OUT_EVENTS);
         return true;
     }
     catch (std::bad_alloc &baexc) {
@@ -312,11 +310,10 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
             // We can't send out-of-memory response as we already wrote as much as we
             // could above. Neither can we later send the response since we have currently
             // sent an incomplete packet. All we can do is close the connection.
-            delete this;
             return false;
         }
         else {
-            iob.setWatchFlags(out_events);
+            iob.setWatches(OUT_EVENTS);
             return true;
         }
     }
@@ -326,7 +323,7 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept
 // make them extraordinary difficult to combine into a single method.
 bool ControlConn::queuePacket(std::vector<char> &&pkt) noexcept
 {
-    int in_flag = bad_conn_close ? 0 : in_events;
+    int in_flag = bad_conn_close ? 0 : IN_EVENTS;
     bool was_empty = outbuf.empty();
     
     if (was_empty) {
@@ -335,12 +332,10 @@ bool ControlConn::queuePacket(std::vector<char> &&pkt) noexcept
         int wr = write(iob.fd, pkt.data(), pkt.size());
         if (wr == -1) {
             if (errno == EPIPE) {
-                delete this;
                 return false;
             }
             if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
                 // TODO log error
-                delete this;
                 return false;
             }
             // EAGAIN etc: fall through to below
@@ -348,7 +343,7 @@ bool ControlConn::queuePacket(std::vector<char> &&pkt) noexcept
         else {
             if ((unsigned)wr == pkt.size()) {
                 // Ok, all written.
-                iob.setWatchFlags(in_flag);
+                iob.setWatches(in_flag);
                 return true;
             }
             outpkt_index = wr;
@@ -357,7 +352,7 @@ bool ControlConn::queuePacket(std::vector<char> &&pkt) noexcept
     
     try {
         outbuf.emplace_back(pkt);
-        iob.setWatchFlags(in_flag | out_events);
+        iob.setWatches(in_flag | OUT_EVENTS);
         return true;
     }
     catch (std::bad_alloc &baexc) {
@@ -368,11 +363,10 @@ bool ControlConn::queuePacket(std::vector<char> &&pkt) noexcept
             // We can't send out-of-memory response as we already wrote as much as we
             // could above. Neither can we later send the response since we have currently
             // sent an incomplete packet. All we can do is close the connection.
-            delete this;
             return false;
         }
         else {
-            iob.setWatchFlags(out_events);
+            iob.setWatches(OUT_EVENTS);
             return true;
         }
     }
@@ -394,21 +388,19 @@ bool ControlConn::dataReady() noexcept
     if (r == -1) {
         if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
             // TODO log error
-            delete this;
             return true;
         }
         return false;
     }
     
     if (r == 0) {
-        delete this;
         return true;
     }
     
     // complete packet?
     if (rbuf.get_length() >= chklen) {
         try {
-            return processPacket();
+            return !processPacket();
         }
         catch (std::bad_alloc &baexc) {
             doOomClose();
@@ -420,11 +412,11 @@ bool ControlConn::dataReady() noexcept
         // TODO log error?
         // TODO error response?
         bad_conn_close = true;
-        iob.setWatchFlags(out_events);
+        iob.setWatches(OUT_EVENTS);
     }
     else {
-        int out_flags = (bad_conn_close || !outbuf.empty()) ? out_events : 0;
-        iob.setWatchFlags(in_events | out_flags);
+        int out_flags = (bad_conn_close || !outbuf.empty()) ? OUT_EVENTS : 0;
+        iob.setWatches(IN_EVENTS | out_flags);
     }
     
     return false;
@@ -438,7 +430,6 @@ bool ControlConn::sendData() noexcept
             char oomBuf[] = { DINIT_RP_OOM };
             write(iob.fd, oomBuf, 1);
         }
-        delete this;
         return true;
     }
     
@@ -448,7 +439,6 @@ bool ControlConn::sendData() noexcept
     if (written == -1) {
         if (errno == EPIPE) {
             // read end closed
-            delete this;
             return true;
         }
         else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
@@ -456,7 +446,6 @@ bool ControlConn::sendData() noexcept
         }
         else {
             log(LogLevel::ERROR, "Error writing to control connection: ", strerror(errno));
-            delete this;
             return true;
         }
         return false;
@@ -469,10 +458,9 @@ bool ControlConn::sendData() noexcept
         outpkt_index = 0;
         if (outbuf.empty() && ! oom_close) {
             if (! bad_conn_close) {
-                iob.setWatchFlags(in_events);
+                iob.setWatches(IN_EVENTS);
             }
             else {
-                delete this;
                 return true;
             }
         }
@@ -484,7 +472,7 @@ bool ControlConn::sendData() noexcept
 ControlConn::~ControlConn() noexcept
 {
     close(iob.fd);
-    iob.deregisterWatch(loop);
+    iob.deregister(*loop);
     
     // Clear service listeners
     for (auto p : serviceKeyMap) {