You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by sh...@apache.org on 2021/01/05 15:57:40 UTC

[trafficserver] branch master updated: traffic_ctl - plugin msg now require only the tag as mandatory field data field is now optional. (#7364)

This is an automated email from the ASF dual-hosted git repository.

shinrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new b5f8232  traffic_ctl - plugin msg  now require only the tag as mandatory field data field is now optional. (#7364)
b5f8232 is described below

commit b5f82321a59154a91b6cac5de0488851fc1974e8
Author: Damian Meden <da...@gmail.com>
AuthorDate: Tue Jan 5 15:57:32 2021 +0000

    traffic_ctl - plugin msg  now require only the tag as mandatory field data field is now optional. (#7364)
    
    * traffic_ctl - plugin msg  now require only the tag as mandatory field. data field is now optional.
    * traffic_ctl - Protect TS_EVENT_LIFECYCLE_MSG handler in case of empty data.
---
 doc/appendices/command-line/traffic_ctl.en.rst     |  4 ++-
 .../c-api/lifecycle_plugin/lifecycle_plugin.c      |  4 +++
 plugins/experimental/header_freq/header_freq.cc    |  2 ++
 .../experimental/memory_profile/memory_profile.cc  | 40 ++++++++++++----------
 plugins/experimental/traffic_dump/traffic_dump.cc  |  4 +--
 src/traffic_ctl/plugin.cc                          |  7 +++-
 src/traffic_ctl/traffic_ctl.cc                     |  4 ++-
 7 files changed, 41 insertions(+), 24 deletions(-)

diff --git a/doc/appendices/command-line/traffic_ctl.en.rst b/doc/appendices/command-line/traffic_ctl.en.rst
index 7489b83..644ffcb 100644
--- a/doc/appendices/command-line/traffic_ctl.en.rst
+++ b/doc/appendices/command-line/traffic_ctl.en.rst
@@ -235,7 +235,9 @@ traffic_ctl plugin
    :cpp:enumerator:`TSLifecycleHookID::TS_LIFECYCLE_MSG_HOOK` will receive a callback for that hook.
    The :arg:`TAG` and :arg:`DATA` will be available to the plugin hook processing. It is expected
    that plugins will use :arg:`TAG` to select relevant messages and determine the format of the
-   :arg:`DATA`.
+   :arg:`DATA`. The :arg:`DATA` is optional and may not be available to consume, if not available then size will be 0
+   and the data will be NULL. Any extra passed value beside the tag and the optional data will be ignored.
+   Check :c:type:`TSPluginMsg` for more info.
 
 traffic_ctl host
 ----------------
diff --git a/example/plugins/c-api/lifecycle_plugin/lifecycle_plugin.c b/example/plugins/c-api/lifecycle_plugin/lifecycle_plugin.c
index 19879c8..41d8d56 100644
--- a/example/plugins/c-api/lifecycle_plugin/lifecycle_plugin.c
+++ b/example/plugins/c-api/lifecycle_plugin/lifecycle_plugin.c
@@ -49,6 +49,10 @@ CallbackHandler(TSCont this, TSEvent id, void *data)
   case TS_EVENT_LIFECYCLE_MSG: {
     TSPluginMsg *msg = (TSPluginMsg *)data;
     TSDebug(PLUGIN_NAME, "Message to '%s' - %zu bytes of data", msg->tag, msg->data_size);
+    if (msg->data_size == 0) {
+      TSDebug(PLUGIN_NAME, "Message data is not available");
+    }
+
     break;
   }
   default:
diff --git a/plugins/experimental/header_freq/header_freq.cc b/plugins/experimental/header_freq/header_freq.cc
index d696a1d..e498b37 100644
--- a/plugins/experimental/header_freq/header_freq.cc
+++ b/plugins/experimental/header_freq/header_freq.cc
@@ -195,6 +195,8 @@ handle_hook(TSCont contp, TSEvent event, void *edata)
         TSCont c = TSContCreate(CB_Command_Log, TSMutexCreate());
         TSContDataSet(c, new std::string(static_cast<const char *>(msgp->data), msgp->data_size));
         TSContScheduleOnPool(c, 0, TS_THREAD_POOL_TASK);
+      } else if (msgp->data_size == 0) {
+        TSError("[%s] No command provided.", PLUGIN_NAME);
       } else {
         TSError("[%s] Unknown command '%.*s'", PLUGIN_NAME, static_cast<int>(msgp->data_size),
                 static_cast<const char *>(msgp->data));
diff --git a/plugins/experimental/memory_profile/memory_profile.cc b/plugins/experimental/memory_profile/memory_profile.cc
index 163f3dd..ee20b95 100644
--- a/plugins/experimental/memory_profile/memory_profile.cc
+++ b/plugins/experimental/memory_profile/memory_profile.cc
@@ -49,26 +49,28 @@ CallbackHandler(TSCont cont, TSEvent id, void *data)
     TSDebug(PLUGIN_NAME, "Message to '%s' - %zu bytes of data", msg->tag, msg->data_size);
     if (strcmp(PLUGIN_NAME, msg->tag) == 0) { // Message is for us
 #if TS_HAS_JEMALLOC
-      int retval = 0;
-      if (strncmp((char *)msg->data, "dump", msg->data_size) == 0) {
-        if ((retval = mallctl("prof.dump", nullptr, nullptr, nullptr, 0)) != 0) {
-          TSError("mallct(prof.dump) failed retval=%d errno=%d", retval, errno);
+      if (msg->data_size) {
+        int retval = 0;
+        if (strncmp((char *)msg->data, "dump", msg->data_size) == 0) {
+          if ((retval = mallctl("prof.dump", nullptr, nullptr, nullptr, 0)) != 0) {
+            TSError("mallct(prof.dump) failed retval=%d errno=%d", retval, errno);
+          }
+        } else if (strncmp((char *)msg->data, "activate", msg->data_size) == 0) {
+          bool active = true;
+
+          if ((retval = mallctl("prof.active", nullptr, nullptr, &active, sizeof(active))) != 0) {
+            TSError("mallct(prof.activate) on failed retval=%d errno=%d", retval, errno);
+          }
+        } else if (strncmp((char *)msg->data, "deactivate", msg->data_size) == 0) {
+          bool active = false;
+          if ((retval = mallctl("prof.active", nullptr, nullptr, &active, sizeof(active))) != 0) {
+            TSError("mallct(prof.activate) off failed retval=%d errno=%d", retval, errno);
+          }
+        } else if (strncmp((char *)msg->data, "stats", msg->data_size) == 0) {
+          malloc_stats_print(nullptr, nullptr, nullptr);
+        } else {
+          TSError("Unexpected msg %*.s", (int)msg->data_size, (char *)msg->data);
         }
-      } else if (strncmp((char *)msg->data, "activate", msg->data_size) == 0) {
-        bool active = true;
-
-        if ((retval = mallctl("prof.active", nullptr, nullptr, &active, sizeof(active))) != 0) {
-          TSError("mallct(prof.activate) on failed retval=%d errno=%d", retval, errno);
-        }
-      } else if (strncmp((char *)msg->data, "deactivate", msg->data_size) == 0) {
-        bool active = false;
-        if ((retval = mallctl("prof.active", nullptr, nullptr, &active, sizeof(active))) != 0) {
-          TSError("mallct(prof.activate) off failed retval=%d errno=%d", retval, errno);
-        }
-      } else if (strncmp((char *)msg->data, "stats", msg->data_size) == 0) {
-        malloc_stats_print(nullptr, nullptr, nullptr);
-      } else {
-        TSError("Unexpected msg %*.s", (int)msg->data_size, (char *)msg->data);
       }
 #else
       TSError("Not built with jemalloc");
diff --git a/plugins/experimental/traffic_dump/traffic_dump.cc b/plugins/experimental/traffic_dump/traffic_dump.cc
index 38ae1f1..20c2959 100644
--- a/plugins/experimental/traffic_dump/traffic_dump.cc
+++ b/plugins/experimental/traffic_dump/traffic_dump.cc
@@ -43,14 +43,14 @@ global_message_handler(TSCont contp, TSEvent event, void *edata)
     std::string_view tag(msg->tag, strlen(msg->tag));
     if (tag.substr(0, PLUGIN_PREFIX.size()) == PLUGIN_PREFIX) {
       tag.remove_prefix(PLUGIN_PREFIX.size());
-      if (tag == "sample") {
+      if (tag == "sample" && msg->data_size) {
         const auto new_sample_size = static_cast<int64_t>(strtol(static_cast<char const *>(msg->data), nullptr, 0));
         TSDebug(debug_tag, "TS_EVENT_LIFECYCLE_MSG: Received Msg to change sample size to %" PRId64 "bytes", new_sample_size);
         SessionData::set_sample_pool_size(new_sample_size);
       } else if (tag == "reset") {
         TSDebug(debug_tag, "TS_EVENT_LIFECYCLE_MSG: Received Msg to reset disk usage counter");
         SessionData::reset_disk_usage();
-      } else if (tag == "limit") {
+      } else if (tag == "limit" && msg->data_size) {
         const auto new_max_disk_usage = static_cast<int64_t>(strtol(static_cast<char const *>(msg->data), nullptr, 0));
         TSDebug(debug_tag, "TS_EVENT_LIFECYCLE_MSG: Received Msg to change max disk usage to %" PRId64 "bytes", new_max_disk_usage);
         SessionData::set_max_disk_usage(new_max_disk_usage);
diff --git a/src/traffic_ctl/plugin.cc b/src/traffic_ctl/plugin.cc
index 8935ee7..a9eb08b 100644
--- a/src/traffic_ctl/plugin.cc
+++ b/src/traffic_ctl/plugin.cc
@@ -29,7 +29,12 @@ CtrlEngine::plugin_msg()
   TSMgmtError error;
   auto msg_data = arguments.get("msg");
 
-  error = TSLifecycleMessage(msg_data[0].c_str(), msg_data[1].c_str(), msg_data[1].size() + 1);
+  if (msg_data.size() == 1) { // no data provided, just the tag
+    error = TSLifecycleMessage(msg_data[0].c_str(), nullptr, 0);
+  } else {
+    error = TSLifecycleMessage(msg_data[0].c_str(), msg_data[1].c_str(), msg_data[1].size() + 1);
+  }
+
   if (error != TS_ERR_OKAY) {
     CtrlMgmtError(error, "message '%s' not sent", msg_data[0].c_str());
     status_code = CTRL_EX_ERROR;
diff --git a/src/traffic_ctl/traffic_ctl.cc b/src/traffic_ctl/traffic_ctl.cc
index 33b363e..f71cd7b 100644
--- a/src/traffic_ctl/traffic_ctl.cc
+++ b/src/traffic_ctl/traffic_ctl.cc
@@ -229,7 +229,9 @@ main(int argc, const char **argv)
   metric_command.add_command("zero", "Clear one or more metric values", "", MORE_THAN_ONE_ARG_N, [&]() { engine.metric_zero(); });
 
   // plugin command
-  plugin_command.add_command("msg", "Send message to plugins - a TAG and the message DATA", "", 2, [&]() { engine.plugin_msg(); })
+  plugin_command
+    .add_command("msg", "Send message to plugins - a TAG and the message DATA(optional)", "", MORE_THAN_ONE_ARG_N,
+                 [&]() { engine.plugin_msg(); })
     .add_example_usage("traffic_ctl plugin msg TAG DATA");
 
   // server commands